Esempio n. 1
0
        public ActionResult EditUser(User updatedUser, FormCollection formCollection)
        {
            try
            {
                _membershipProviderApplicationService.UpdateUser(updatedUser, false);

                var updatedRoles = formCollection["Roles"].Replace("false", "").Split(new [] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var usersRoles   = _roleProviderApplicationService.GetRolesForUser(updatedUser.UserName);

                var rolesToRemove = usersRoles.Except(updatedRoles);
                foreach (var roleToRemove in rolesToRemove)
                {
                    _roleProviderApplicationService.DeleteUserFromRole(updatedUser.UserName, roleToRemove);
                }

                var rolesToAdd = updatedRoles.Except(usersRoles);
                foreach (var roleToAdd in rolesToAdd)
                {
                    _roleProviderApplicationService.AddUserToRole(updatedUser.UserName, roleToAdd);
                }

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(RedirectToAction("EditUser", new { id = updatedUser.Id }));
            }
        }
Esempio n. 2
0
        public override void RemoveUsersFromRoles(string[] userNames, string[] roleNames)
        {
            foreach (string rolename in roleNames)
            {
                if (!RoleExists(rolename))
                {
                    throw new ProviderException("Role name not found.");
                }
            }

            foreach (string username in userNames)
            {
                if (username.IndexOf(',') > 0)
                {
                    throw new ArgumentException("User names cannot contain commas.");
                }

                foreach (string rolename in roleNames)
                {
                    if (!IsUserInRole(username, rolename))
                    {
                        throw new ProviderException("User is not in role.");
                    }
                }
            }

            try
            {
                foreach (string userName in userNames)
                {
                    foreach (string roleName in roleNames)
                    {
                        _roleProviderApplicationService.DeleteUserFromRole(userName, roleName);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }