コード例 #1
0
        /// <summary>
        /// Removes the specified user names from the specified roles for the configured applicationName.
        /// </summary>
        /// <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>
        public override void RemoveUsersFromRoles(string[] userNames, string[] roleNames)
        {
            using (InventoryManagementEntities context = new InventoryManagementEntities())
            {
                IQueryable <aspnet_Roles> roles = context.aspnet_Roles.Where(MatchRoleApplication()).Where(ProviderUtils.BuildContainsExpression <aspnet_Roles, string>(r => r.RoleName, roleNames));
                if (roles.Count() != roleNames.Length)
                {
                    throw new ProviderException("Role not found.");
                }

                IQueryable <aspnet_Membership> users = context.aspnet_Membership.Include("Role").Where(MatchUserApplication()).Where(ProviderUtils.BuildContainsExpression <aspnet_Membership, string>(u => u.aspnet_Users.UserName, userNames));
                if (users.Count() != userNames.Length)
                {
                    throw new ProviderException("User not found.");
                }

                try
                {
                    foreach (aspnet_Membership user in users)
                    {
                        foreach (aspnet_Roles role in roles)
                        {
                            /*if (!user.Role.IsLoaded)
                             * {
                             *  user.Role.Load();
                             * }*/

                            if (user.aspnet_Users.aspnet_Roles.Contains(role))
                            {
                                user.aspnet_Users.aspnet_Roles.Remove(role);
                            }
                        }
                    }

                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    if (WriteExceptionsToEventLog)
                    {
                        WriteToEventLog(ex, "RemoveUsersFromRoles");
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Adds the specified user names to the specified roles for the configured applicationName.
        /// </summary>
        /// <param name="userNames">A string array of user names to be added to the specified roles.</param>
        /// <param name="roleNames">A string array of the role names to add the specified user names to.</param>
        public override void AddUsersToRoles(string[] userNames, string[] roleNames)
        {
            using (InventoryManagementEntities context = new InventoryManagementEntities())
            {
                IQueryable <aspnet_Roles> roles = context.aspnet_Roles.Where(MatchRoleApplication()).Where(ProviderUtils.BuildContainsExpression <aspnet_Roles, string>(r => r.RoleName, roleNames));
                if (roles.Count() != roleNames.Length)
                {
                    throw new ProviderException("Role not found.");
                }

                IQueryable <aspnet_Membership> users = context.aspnet_Membership.Where(MatchUserApplication()).Where(ProviderUtils.BuildContainsExpression <aspnet_Membership, string>(u => u.aspnet_Users.UserName, userNames));
                if (users.Count() != userNames.Length)
                {
                    throw new ProviderException("User not found.");
                }

                try
                {
                    foreach (aspnet_Membership user in users)
                    {
                        foreach (aspnet_Roles role in roles)
                        {
                            // Check whether user is already in role
                            if (IsUserInRole(user.aspnet_Users.UserName, role.RoleName))
                            {
                                throw new ProviderException(string.Format("User is already in role '{0}'.", role.RoleName));
                            }

                            user.aspnet_Users.aspnet_Roles.Add(role);
                        }
                    }

                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    if (WriteExceptionsToEventLog)
                    {
                        WriteToEventLog(ex, "AddUsersToRoles");
                    }
                    else
                    {
                        throw;
                    }
                }
                finally
                {
                    //context..Connection.Close();
                }
            }
        }