Exemplo n.º 1
0
        public void AddUserToRole(string username, string role)
        {
            var roles = _provider.GetAllRoles();

            _provider.RemoveUsersFromRoles(new string[] { username }, roles);
            _provider.AddUsersToRoles(new string[] { username }, new string[] { role });
        }
Exemplo n.º 2
0
 public void TakeAwayFromSpecialUserTest()
 {
     if (rp.IsUserInRole(specialUN, specialRN))
     {
         rp.RemoveUsersFromRoles(new[] { specialUN }, new[] { specialRN });
     }
     Assert.IsFalse(rp.IsUserInRole(specialUN, specialRN));
 }
 public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
 {
     if (!Initialized)
     {
         _prevProvider.RemoveUsersFromRoles(usernames, roleNames);
     }
     else
     {
         if (usernames.Where(username => string.IsNullOrEmpty(username)).Count() > 0 || usernames.Where(username => string.IsNullOrEmpty(username)).Count() > 0)
         {
             throw new ArgumentException(Resources.InvalidArrayValue);
         }
         using (MySqlDatabaseWrapper dbConn = new MySqlDatabaseWrapper(ConnectionString))
         {
             foreach (var userid in GetUsersId(usernames))
             {
                 foreach (var roleid in GetRolesId(roleNames))
                 {
                     if (userid > 0 && roleid > 0)
                     {
                         dbConn.ExecuteNonQuery(string.Format("delete from {0} where userid=? and roleid=?;", _userInRolesTable), userid, roleid);
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 4
0
        internal static void CreateRoles(RoleProvider provider)
        {
            ProviderRoles = provider.GetAllRoles().ToList <string>().FindAll(roleName => roleName.ToLower().Contains("test_"));
            foreach (string role in ProviderRoles)
            {
                if (provider.RoleExists(role))
                {
                    string[] usersInRoles = provider.GetUsersInRole(role);
                    if (usersInRoles.Length != 0)
                    {
                        provider.RemoveUsersFromRoles(usersInRoles, new string[] { role }); //remove users from role
                    }
                    provider.DeleteRole(role, false);                                       //remove role
                }
            }

            ProviderRoles.Clear();
            for (int x = 0; x < 20; x++)
            {
                string roleName = string.Format("TEST_ROLE:{0:000}", x);
                ProviderRoles.Add(roleName);
                provider.CreateRole(roleName);
            }

            //NOTE:  roles 0-9 are reserved for query type operations and should be considered read only
            //       roles 10-19 may be used for C-U-D operations
            addUserToRole(Users[0], ProviderRoles[0], provider);  // several users in one role
            addUserToRole(Users[2], ProviderRoles[0], provider);
            addUserToRole(Users[4], ProviderRoles[0], provider);

            addUserToRole(Users[6], ProviderRoles[1], provider); // same user in multiple roles
            addUserToRole(Users[6], ProviderRoles[2], provider);
            addUserToRole(Users[6], ProviderRoles[3], provider);

            addUserToRole(Users[10], ProviderRoles[1], provider); // same user in multiple roles
            addUserToRole(Users[10], ProviderRoles[2], provider);
            addUserToRole(Users[10], ProviderRoles[3], provider);

            addUserToRole(Users[8], ProviderRoles[1], provider);
            addUserToRole(Users[8], ProviderRoles[2], provider);

/*
 * TEST_ROLE:000
 *  Users[0]
 *  Users[2]
 *  Users[4]
 * TEST_ROLE:001
 *  Users[6]
 *  Users[8]
 * TEST_ROLE:002
 *  Users[6]
 *  Users[8]
 * TEST_ROLE:003
 *  Users[6]
 * TEST_ROLE:004
 *  -- no users
 *
 */
        }
 public void AddRemoveRoleForUser(string username, string rolename, bool isInRole)
 {
     if (isInRole)
     {
         _roleProvider.AddUsersToRoles(new string[] { username }, new string[] { rolename });
     }
     else
     {
         _roleProvider.RemoveUsersFromRoles(new string[] { username }, new string[] { rolename });
     }
 }
Exemplo n.º 6
0
        public void RemoveUserFromFole(User user, Role role, RoleProvider cfrp)
        {
            var rolesList = user.Roles.Select(a => a.RoleName).ToList();
            var userRoles = Context.Roles.Where(x => rolesList.Contains(role.RoleName)).ToList();
            var foundRole = !userRoles.Contains(role);

            if (foundRole)
            {
                Roles.RemoveUserFromRole(user.Username, role.RoleName);
                cfrp.RemoveUsersFromRoles(new[] { user.Username }, new[] { role.RoleName });
            }
        }
Exemplo n.º 7
0
        private void ResetRoles(string userName)
        {
            string[] users = { userName };

            foreach (var role in _roleProvider.GetRolesForUser(userName))
            {
                if (_roleProvider.IsUserInRole(userName, role))
                {
                    string[] roles = { role };
                    _roleProvider.RemoveUsersFromRoles(users, roles);
                }
            }
        }
Exemplo n.º 8
0
 public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
 {
     LogDebug("Entering CachedProvider.RemoveUsersFromRoles");
     foreach (string username in usernames)
     {
         if (_cache.Contains(username))
         {
             List <string> roles = (List <string>)_cache[username];
             foreach (string role in roleNames)
             {
                 roles.Remove(role);
             }
         }
     }
     _provider.RemoveUsersFromRoles(usernames, roleNames);
 }
Exemplo n.º 9
0
        public void UpdateUser(MembershipUser user, string[] roles)
        {
            using (var ts = new TransactionScope())
            {
                _provider.UpdateUser(user);
                var existingRoles = _roleProvider.GetRolesForUser(user.UserName);
                if (roles != null && roles.Length > 0)
                {
                    var rolesToBeAdded = roles.Except(existingRoles).ToArray();
                    _roleProvider.AddUsersToRoles(new[] { user.UserName }, rolesToBeAdded);
                }
                if (existingRoles.Length > 0)
                {
                    var rolesToBeDeleted = (roles != null ? existingRoles.Except(roles) : existingRoles).ToArray();
                    _roleProvider.RemoveUsersFromRoles(new[] { user.UserName }, rolesToBeDeleted);
                }

                ts.Complete();
            }
        }
Exemplo n.º 10
0
        public void Can_Remove_Users_From_Roles()
        {
            var user1 = new User {
                Username = "******", Password = "******"
            };
            var user2 = new User {
                Username = "******", Password = "******"
            };

            RoleProvider.CreateRole("admin");
            RoleProvider.CreateRole("engineering");
            RoleProvider.CreateRole("sales");

            MembershipProvider.CreateAccount(user1);
            MembershipProvider.CreateAccount(user2);

            RoleProvider.AddUsersToRoles(new[] { "sallen", "missmm" }, new[] { "admin", "engineering" });

            RoleProvider.RemoveUsersFromRoles(new[] { "sallen", "missmm" }, new[] { "sales", "admin" });

            Assert.True(RoleProvider.IsUserInRole("sallen", "engineering"));
            Assert.False(RoleProvider.IsUserInRole("sallen", "admin"));
        }
Exemplo n.º 11
0
        public string Process(string[] args)
        {
            var sb = new StringBuilder();

            var p = new OptionSet()
            {
                { "c|create", "create a role\nUSAGE: role --create rolename",
                  v => _command = Command.Create },
                { "d|delete", "delete a role\nUSAGE: role --delete rolename",
                  v => _command = Command.Delete },
                { "l|list", "return a list of roles\nUSAGE: role --list",
                  v => _command = Command.List },
                { "g|give", "add a user to a role\nUSAGE: role --give username rolename",
                  v => _command = Command.Give },
                { "t|take", "remove a user from a role\nUSAGE: role --take username rolename",
                  v => _command = Command.Take },
                { "u|users", "list all users in a role\nUSAGE: role --users rolename",
                  v => _command = Command.Users },
                { "r|roles", "list all roles for a user\nUSAGE: role --roles username",
                  v => _command = Command.Roles },
                { "h|help", "show this list of options\nUSAGE --help",
                  v => _command = null }
            };

            List <string> extras;

            try
            {
                extras = p.Parse(args);
            }
            catch (OptionException e)
            {
                sb.Append("role: ");
                sb.AppendLine(e.Message);
                sb.AppendLine("Try `role --help' for more information.");
                return(sb.ToString());
            }

            // perform the selected command
            if (_command == Command.Create)
            {
                if (extras.Count == 1)
                {
                    _provider.CreateRole(extras[0]);
                    sb.AppendLine("Role successfully created");
                }
                else
                {
                    sb.AppendLine("USAGE: role --create rolename");
                }
            }
            else if (_command == Command.Delete)
            {
                if (extras.Count == 1)
                {
                    try
                    {
                        var success = _provider.DeleteRole(extras[0], true);

                        if (success)
                        {
                            sb.AppendLine("Role successfully deleted");
                        }
                        else
                        {
                            sb.AppendFormat("Could not delete role: {0}", extras[0]);
                            sb.AppendLine();
                        }
                    }
                    catch (Exception ex)
                    {
                        sb.AppendFormat("Could not delete role: {0} - {1}", extras[0], ex.Message);
                        sb.AppendLine();
                    }
                }
                else
                {
                    sb.AppendLine("USAGE: role --delete rolename");
                }
            }
            else if (_command == Command.List)
            {
                if (extras.Count == 0)
                {
                    var roles = _provider.GetAllRoles();

                    if (roles.Count() > 0)
                    {
                        foreach (var role in roles)
                        {
                            sb.AppendLine(role);
                        }
                    }
                    else
                    {
                        sb.AppendLine("No roles found");
                    }
                }
                else
                {
                    sb.AppendLine("USAGE: role --list");
                }
            }
            else if (_command == Command.Give)
            {
                if (extras.Count == 2)
                {
                    _provider.AddUsersToRoles(new string[] { extras[0] }, new string[] { extras[1] });
                    sb.AppendFormat("Added {0} to {1}", extras[0], extras[1]);
                    sb.AppendLine();
                }
                else
                {
                    sb.AppendLine("USAGE: role --give username rolename");
                }
            }
            else if (_command == Command.Take)
            {
                if (extras.Count == 2)
                {
                    _provider.RemoveUsersFromRoles(new string[] { extras[0] }, new string[] { extras[1] });
                    sb.AppendFormat("Removed {0} from {1}", extras[0], extras[1]);
                    sb.AppendLine();
                }
                else
                {
                    sb.AppendLine("USAGE: role --take username rolename");
                }
            }
            else if (_command == Command.Users)
            {
                if (extras.Count == 1)
                {
                    var users = _provider.GetUsersInRole(extras[0]);

                    if (users.Count() > 0)
                    {
                        sb.AppendFormat("{0} users in {1}", users.Count(), extras[0]);
                        sb.AppendLine();

                        foreach (var u in users)
                        {
                            sb.AppendLine(u);
                        }
                    }
                    else
                    {
                        sb.AppendLine("No users in role");
                    }
                }
                else
                {
                    sb.AppendLine("USAGE: role --users rolename");
                }
            }
            else if (_command == Command.Roles)
            {
                if (extras.Count == 1)
                {
                    var roles = _provider.GetRolesForUser(extras[0]);

                    if (roles.Count() > 0)
                    {
                        sb.AppendFormat("{0} roles for {1}", roles.Count(), extras[0]);
                        sb.AppendLine();

                        foreach (var r in roles)
                        {
                            sb.AppendLine(r);
                        }
                    }
                    else
                    {
                        sb.AppendLine("No roles found for user");
                    }
                }
                else
                {
                    sb.AppendLine("USAGE: role --roles username");
                }
            }
            else
            {
                sb.AppendLine(ShowHelp(p));
            }

            return(sb.ToString());
        }
Exemplo n.º 12
0
 public void RemoveUserFromRole(string userName, string roleName)
 {
     string[] userNames = new string[] { userName };
     string[] roleNames = new string[] { roleName };
     _provider.RemoveUsersFromRoles(userNames, roleNames);
 }
Exemplo n.º 13
0
 /// <summary>
 /// Removes from role.
 /// </summary>
 /// <param name="user">The user.</param>
 /// <param name="roleName">Name of the role.</param>
 public void RemoveFromRole(MembershipUser user, string roleName)
 {
     _roleProvider.RemoveUsersFromRoles(new[] { user.UserName }, new[] { roleName });
 }
Exemplo n.º 14
0
 public void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
 {
     _provider.RemoveUsersFromRoles(usernames, roleNames);
 }
Exemplo n.º 15
0
 public void RemoveUserFromRole(string username, string roleName)
 {
     roleProvider.RemoveUsersFromRoles(new string[] { username }, new string[] { roleName });
 }
Exemplo n.º 16
0
 public static void RemoveUserFromRole(this RoleProvider roleProvider, string userName, string role)
 {
     roleProvider.RemoveUsersFromRoles(new[] { userName }, new[] { role });
 }
Exemplo n.º 17
0
 public void RemoveUsersToRole(string[] users, string[] roles)
 {
     roleProvider.RemoveUsersFromRoles(users, roles);
 }
 /// <summary>
 /// DeleteUserRole() Method <a name="DeleteUserRole"></a>
 /// The DeleteUserRole method deletes the user from the specified role.
 /// Other relevant sources:
 /// + <a href="DeleteUserRole.htm" style="color:green">DeleteUserRole Stored Procedure</a>
 /// </summary>
 /// <param name="roleID">The role ID.</param>
 /// <param name="userID">The user ID.</param>
 public void DeleteUserRole(Guid roleID, Guid userID)
 {
     RoleProvider.RemoveUsersFromRoles(CurrentPortalSettings.PortalAlias, new Guid[] { userID }, new Guid[] { roleID });
 }
        public void MembershipRole_RemoveUsersFromRolesTests()
        {
            string[] users = new string[] { TestUtils.Users[16].Username };
            string[] roles = new string[] { };
            //-------------------------------------------------------
            // empty role list, error
            //-------------------------------------------------------
            try
            {
                _provider.RemoveUsersFromRoles(users, roles);
                Assert.Fail("Provider did not throw expected ArgumentException on empty roleName list");
            }
            catch (ArgumentException)
            {
                //ignore expected exception
            }

            //-------------------------------------------------------
            // empty user list, error
            //-------------------------------------------------------
            users = new string[] { };
            roles = new string[] { TestUtils.ProviderRoles[0] };
            try
            {
                _provider.RemoveUsersFromRoles(users, roles);
                Assert.Fail("Provider did not throw expected ArgumentException on empty users list");
            }
            catch (ArgumentException)
            {
                //ignore expected exception
            }

            //-------------------------------------------------------
            // null user list, error
            //-------------------------------------------------------
            users = null;
            roles = new string[] { TestUtils.ProviderRoles[0] };
            try
            {
                _provider.RemoveUsersFromRoles(users, roles);
                Assert.Fail("Provider did not throw expected ArgumentException on null user list");
            }
            catch (ArgumentException)
            {
                //ignore expected exception
            }

            //-------------------------------------------------------
            // null role list, error
            //-------------------------------------------------------
            users = new string[] { TestUtils.Users[14].Username };
            roles = null;
            try
            {
                _provider.RemoveUsersFromRoles(users, roles);
                Assert.Fail("Provider did not throw expected ArgumentNullException on null role list");
            }
            catch (ArgumentNullException)
            {
                //ignore expected exception
            }

            //-------------------------------------------------------
            // remove existing user from exiting role
            //-------------------------------------------------------
            users = new string[] { TestUtils.Users[8].Username };
            roles = new string[] { TestUtils.ProviderRoles[1] };
            _provider.RemoveUsersFromRoles(users, roles);

            //-------------------------------------------------------
            // remove several users from existing roles, some users
            //  not members of existing roles, error
            //-------------------------------------------------------
            users = new string[] { TestUtils.Users[4].Username, TestUtils.Users[6].Username };
            roles = new string[] { TestUtils.ProviderRoles[0], TestUtils.ProviderRoles[3] };
            try
            {
                _provider.RemoveUsersFromRoles(users, roles);
                Assert.Fail("Provider did not throw expected ProviderException");
            }
            catch (ProviderException)
            {
                //ignore expected order
            }

            //-------------------------------------------------------
            // remove existing users from existing roles except one role is null
            //-------------------------------------------------------
            users = new string[] { TestUtils.Users[6].Username, TestUtils.Users[10].Username };
            roles = new string[] { TestUtils.ProviderRoles[1], null };
            try
            {
                _provider.RemoveUsersFromRoles(users, roles);
                Assert.Fail("Provider did not throw expected ArgumentNullException when one role is null in the list");
            }
            catch (ArgumentNullException)
            {
                //ignore expected exception
            }

            //-------------------------------------------------------
            // remove existing users from existing roles except one role is empty
            //-------------------------------------------------------
            users = new string[] { TestUtils.Users[20].Username, TestUtils.Users[22].Username };
            roles = new string[] { TestUtils.ProviderRoles[10], string.Empty };
            try
            {
                _provider.RemoveUsersFromRoles(users, roles);
                Assert.Fail("Provider did not throw expected ArgumentException when one role is null in the list");
            }
            catch (ArgumentException)
            {
                //ignore expected exception
            }

            //-------------------------------------------------------
            // remove existing users from existing roles except one role is non-existant
            //-------------------------------------------------------
            users = new string[] { TestUtils.Users[20].Username, TestUtils.Users[22].Username };
            roles = new string[] { TestUtils.ProviderRoles[10], "TEST_" + Guid.NewGuid().ToString() };
            try
            {
                _provider.RemoveUsersFromRoles(users, roles);
                Assert.Fail("Provider did not throw expected ProviderException when one role did not exist");
            }
            catch (ProviderException)
            {
                //ignore expected exception
            }

            //-------------------------------------------------------
            // remove existing users (except one user is null) from existing roles
            //-------------------------------------------------------
            users = new string[] { TestUtils.Users[20].Username, null };
            roles = new string[] { TestUtils.ProviderRoles[10], TestUtils.ProviderRoles[11] };
            try
            {
                _provider.RemoveUsersFromRoles(users, roles);
                Assert.Fail("Provider did not throw expected ArgumentNull expception when one user is null");
            }
            catch (ArgumentNullException)
            {
                //ignore expected exception
            }

            //-------------------------------------------------------
            // remove existing users (except one user is empty) from existing roles
            //-------------------------------------------------------
            users = new string[] { TestUtils.Users[20].Username, string.Empty };
            roles = new string[] { TestUtils.ProviderRoles[10], TestUtils.ProviderRoles[11] };
            try
            {
                _provider.RemoveUsersFromRoles(users, roles);
            }
            catch (ArgumentException)
            {
                //ignore expected exception
            }

            //-------------------------------------------------------
            // remove existing users from existing roles except one user
            //  doesn't exist
            //-------------------------------------------------------
            users = new string[] { TestUtils.Users[20].Username, "TEST_" + Guid.NewGuid().ToString() };
            roles = new string[] { TestUtils.ProviderRoles[10], TestUtils.ProviderRoles[11] };
            try
            {
                _provider.RemoveUsersFromRoles(users, roles);
            }
            catch (ProviderException)
            {
                //ignore expected exception
            }

            //-------------------------------------------------------
            // remove existing user from existing roles except one user is not member of that role
            //-------------------------------------------------------
            users = new string[] { TestUtils.Users[30].Username, TestUtils.Users[6].Username };
            roles = new string[] { TestUtils.ProviderRoles[6], TestUtils.ProviderRoles[10] };
            try
            {
                _provider.RemoveUsersFromRoles(users, roles);
            }
            catch (ProviderException)
            {
                //ignore expected exception
            }

            //-------------------------------------------------------
            // duplicate rolenames in list
            //-------------------------------------------------------
            users = new string[] { TestUtils.Users[30].Username, TestUtils.Users[31].Username };
            roles = new string[] { TestUtils.ProviderRoles[18], TestUtils.ProviderRoles[18] };
            try
            {
                _provider.RemoveUsersFromRoles(users, roles);
            }
            catch (ArgumentException)
            {
                //ignore expected exception
            }

            //-------------------------------------------------------
            // duplicate users in list
            //-------------------------------------------------------
            users = new string[] { TestUtils.Users[30].Username, TestUtils.Users[30].Username };
            roles = new string[] { TestUtils.ProviderRoles[18], TestUtils.ProviderRoles[19] };
            try
            {
                _provider.RemoveUsersFromRoles(users, roles);
            }
            catch (ArgumentException)
            {
                //ignore expected exception
            }
        }