示例#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)
 {
     try
     {
         lock (SyncRoot)
         {
             foreach (string rolename in roleNames)
             {
                 if (!RoleExists(rolename))
                 {
                     throw new ProviderException("Role name not found.");
                 }
             }
             foreach (string rolename in roleNames)
             {
                 XmlRole role = this.Store.GetRole(rolename);
                 foreach (string username in usernames)
                 {
                     role.Users.Remove(username);
                 }
             }
             this.Store.Save();
         }
     }
     catch { throw; }
 }
示例#2
0
        /// <summary>
        /// Removes a role from the data source for the configured applicationName.
        /// </summary>
        /// <param name="roleName">The name of the role to delete.</param>
        /// <param name="throwOnPopulatedRole">If true, throw an exception if roleName has one or more members and do not delete roleName.</param>
        /// <returns>
        /// true if the role was successfully deleted; otherwise, false.
        /// </returns>
        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            if (!RoleExists(roleName))
            {
                throw new ProviderException("Role does not exist.");
            }
            if (GetUsersInRole(roleName).Length > 0)
            {
                throw new ProviderException("Cannot delete a populated role.");
            }

            try
            {
                lock (SyncRoot)
                {
                    XmlRole role = this.Store.GetRole(roleName);
                    if (role != null)
                    {
                        this.Store.Roles.Remove(role);
                        this.Store.Save();
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            catch { throw; }
        }
示例#3
0
        /// <summary>
        /// Adds a new role to the data source for the configured applicationName.
        /// </summary>
        /// <param name="roleName">The name of the role to create.</param>
        public override void CreateRole(string roleName)
        {
            if (roleName.IndexOf(',') > 0)
            {
                throw new ArgumentException("Role name cannot contain commas.");
            }
            if (RoleExists(roleName))
            {
                throw new ProviderException("Role name already exists.");
            }

            try
            {
                lock (SyncRoot)
                {
                    XmlRole role = new XmlRole();
                    role.Name  = roleName;
                    role.Users = new List <string>();
                    this.Store.Roles.Add(role);
                    this.Store.Save();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
示例#4
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)
 {
     // TODO way to optimize checks and adds!?
     try
     {
         lock (SyncRoot)
         {
             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 already in role.");
                     }
                 }
             }
             foreach (string username in usernames)
             {
                 foreach (string rolename in roleNames)
                 {
                     XmlRole role = this.Store.GetRole(rolename);
                     if (role != null)
                     {
                         role.Users.Add(username);
                     }
                 }
             }
             this.Store.Save();
         }
     }
     catch { throw; }
 }
示例#5
0
        /// <summary>
        /// Gets an array of user names in a role where the user name contains the specified user name to match.
        /// </summary>
        /// <param name="roleName">The role to search in.</param>
        /// <param name="usernameToMatch">The user name to search for.</param>
        /// <returns>
        /// A string array containing the names of all the users where the user name matches usernameToMatch and the user is a member of the specified role.
        /// </returns>
        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            if (!RoleExists(roleName))
            {
                throw new ProviderException("Role does not exist.");
            }

            try
            {
                lock (SyncRoot)
                {
                    XmlRole role = this.Store.GetRole(roleName);
                    if (role != null)
                    {
                        return(role.Users.ToArray());
                    }
                    else
                    {
                        return new string[] { }
                    };
                }
            }
            catch { throw; }
        }