示例#1
0
        /// <summary>
        /// Removes the specified user names from the specified roles for the configured applicationName.
        /// </summary>
        /// <remarks>Inherited from RoleProvider ==> Forwarded to previous provider if this provider hasn't been initialized</remarks>
        /// <param name="usernames">A string array of user names to be removed from the specified roles.</param>
        /// <param name="roleNames">A string array of role names to remove the specified user names from.</param>
        /// <exception cref="System.InvalidOperationException">
        /// </exception>
        /// <exception cref="System.Configuration.Provider.ProviderException"></exception>
        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            if (!InitializeCalled)
            {
                PreviousProvider.RemoveUsersFromRoles(usernames, roleNames);
            }
            else
            {
                foreach (string rolename in roleNames)
                {
                    if (!RoleExists(rolename))
                    {
                        throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resources.SimpleRoleProvider_NoRoleFound, rolename));
                    }
                }

                foreach (string username in usernames)
                {
                    foreach (string rolename in roleNames)
                    {
                        if (!IsUserInRole(username, rolename))
                        {
                            throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resources.SimpleRoleProvder_UserNotInRole, username, rolename));
                        }
                    }
                }

                using (var db = NewMySqlSecurityDbContext)
                {
                    List <int> userIds      = GetUserIdsFromNames(db, usernames);
                    List <int> roleIds      = GetRoleIdsFromNames(db, roleNames);
                    var        affectedRows = 0;

                    foreach (int userId in userIds)
                    {
                        foreach (int roleId in roleIds)
                        {
                            // Review: Is there a way to do these all in one query?
                            var usersInRole = db.UsersInRoles.SingleOrDefault(x => x.UserId == userId && x.RoleId == roleId);

                            if (usersInRole != null)
                            {
                                db.UsersInRoles.Remove(usersInRole);
                                affectedRows++;
                            }
                        }
                    }

                    if (db.SaveChanges() != affectedRows)
                    {
                        throw new ProviderException(Resources.Security_DbFailure);
                    }
                }
            }
        }
        // Inherited from RoleProvider ==> Forwarded to previous provider if this provider hasn't been initialized
        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            if (!InitializeCalled)
            {
                PreviousProvider.RemoveUsersFromRoles(usernames, roleNames);
            }
            else
            {
                foreach (string rolename in roleNames)
                {
                    if (!RoleExists(rolename))
                    {
                        throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, WebDataResources.SimpleRoleProvider_NoRoleFound, rolename));
                    }
                }

                foreach (string username in usernames)
                {
                    foreach (string rolename in roleNames)
                    {
                        if (!IsUserInRole(username, rolename))
                        {
                            throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, WebDataResources.SimpleRoleProvder_UserNotInRole, username, rolename));
                        }
                    }
                }

                using (var db = ConnectToDatabase())
                {
                    List <int> userIds = GetUserIdsFromNames(db, usernames);
                    List <int> roleIds = GetRoleIdsFromNames(db, roleNames);

                    foreach (int userId in userIds)
                    {
                        foreach (int roleId in roleIds)
                        {
                            // Review: Is there a way to do these all in one query?
                            int rows = db.Execute("DELETE FROM " + UsersInRoleTableName + " WHERE UserId = " + userId + " and RoleId = " + roleId);
                            if (rows != 1)
                            {
                                throw new ProviderException(WebDataResources.Security_DbFailure);
                            }
                        }
                    }
                }
            }
        }