Beispiel #1
0
        public string AddRoles(string userId, string loginId, string[] roles)
        {
            ResponseModel resp = new ResponseModel();
            UserRole      userrole;
            string        sResult = "ok|Roles Adicionados| ROLES";

            foreach (string roleId in roles)
            {
                userrole = new UserRole()
                {
                    UserId = userId, RoleId = roleId
                };
                resp = _userservices.AddRole(userrole);
                if (!resp.response)
                {
                    sResult += "wn|Problemas parciales en Roles|ROLES";
                }
            }
            if (resp.response)
            {
                List <RoleForm> uroles = new List <RoleForm>();
                uroles  = _userservices.GetRolesForm(userId);
                sResult = Objeto.SerializarLista(uroles, '|', '~', false);
            }
            else
            {
                sResult = resp.message + "wn|Problemas al adicionar Roles|ROLES";
            }

            return(sResult);
        }
        public async Task <IActionResult> ManageRoles(UserRoleModel model, string addRoles, string removeRoles)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            foreach (var id in model.RoleModel.UserIds)
            {
                var user = await _userManager.FindByIdAsync(id);

                if (!string.IsNullOrEmpty(addRoles))
                {
                    if (model.RoleModel.SubmitterChecked)
                    {
                        await _userServices.AddRole(user, "Submitter");
                    }
                    if (model.RoleModel.DeveloperChecked)
                    {
                        await _userServices.AddRole(user, "Developer");
                    }
                    if (model.RoleModel.ManagerChecked)
                    {
                        await _userServices.AddRole(user, "Manager");
                    }
                    if (model.RoleModel.AdminChecked)
                    {
                        await _userServices.AddRole(user, "Admin");
                    }
                }
                if (!string.IsNullOrEmpty(removeRoles))
                {
                    if (model.RoleModel.SubmitterChecked)
                    {
                        await _userServices.RemoveRole(user, "Submitter");
                    }
                    if (model.RoleModel.DeveloperChecked)
                    {
                        await _userServices.RemoveRole(user, "Developer");
                    }
                    if (model.RoleModel.ManagerChecked)
                    {
                        await _userServices.RemoveRole(user, "Manager");
                    }
                    if (model.RoleModel.AdminChecked)
                    {
                        await _userServices.RemoveRole(user, "Admin");
                    }
                }
                //Get Index of matching User in UserModel
                int            index = model.UserModel.Users.IndexOf(model.UserModel.Users.Where(u => u.Id == id).FirstOrDefault());
                IList <string> roles = await _userManager.GetRolesAsync(user);

                string joined = string.Join(", ", roles);
                model.UserModel.Users[index].Roles = joined;
            }

            ViewBag.ChangeConfirmation = "Changes have been saved.";
            model.RoleModel.Reset();
            return(View(model));
        }