예제 #1
0
        public void CreateRoleTest()
        {
            string roleName = Guid.NewGuid().ToString().Substring(0, 18).Replace("-", "");

            rp.CreateRole(roleName);
            Assert.IsTrue(rp.RoleExists(roleName));
        }
        public void MembershipRole_CreateRoleTests()
        {
            string rolename;

            //-------------------------------------------------------
            //  valid name
            //-------------------------------------------------------
            rolename = "TEST_NEWROLE";
            _provider.CreateRole(rolename);

            //-------------------------------------------------------
            //  null name
            //-------------------------------------------------------
            rolename = null;
            try
            {
                _provider.CreateRole(rolename);
                Assert.Fail("Provider did not throw expected ArgumentNullException on null role name");
            }
            catch (ArgumentNullException)
            {
                //ignore expected exception
            }

            //-------------------------------------------------------
            //  empty name
            //-------------------------------------------------------
            rolename = string.Empty;
            try
            {
                _provider.CreateRole(rolename);
                Assert.Fail("Provider did not throw expected ArgumentException on empty role name");
            }
            catch (ArgumentException)
            {
                //ignore expected exception
            }

            //-------------------------------------------------------
            //  existing name - expect duplicate error
            //-------------------------------------------------------
            rolename = TestUtils.ProviderRoles[0];
            try
            {
                _provider.CreateRole(rolename);
            }
            catch (ProviderException)
            {
                //ignore expected exception
            }
        }
        public override void CreateRole(string roleName)
        {
            if (!Initialized)
            {
                _prevProvider.CreateRole(roleName);
            }
            else
            {
                if (string.IsNullOrEmpty(roleName))
                {
                    MySqlSimpleMembershipProvider.NullArgumentException("roleName");
                }

                using (MySqlDatabaseWrapper dbConn = new MySqlDatabaseWrapper(ConnectionString))
                {
                    int roleid = GetRoleId(roleName);
                    if (roleid != 0)
                    {
                        throw new InvalidOperationException(string.Format(Resources.RoleAlreadyExists, roleName));
                    }
                    if (dbConn.ExecuteNonQuery(string.Format("insert into {0} (rolename) values(?);", _rolesTable), roleName) <= 0)
                    {
                        throw new ProviderException(string.Format(Resources.CreateRoleFailed, roleName));
                    }
                }
            }
        }
예제 #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
 *
 */
        }
예제 #5
0
        public void Can_Delete_Role()
        {
            RoleProvider.CreateRole("admin");

            RoleProvider.DeleteRole("admin", true);

            Assert.False(Verifier.Query <Role>().Any(r => r.Name == "admin"));
        }
예제 #6
0
 public void CreateRole(string roleName)
 {
     if (String.IsNullOrEmpty(roleName))
     {
         throw new ArgumentException("Value cannot be null or empty.", "roleName");
     }
     _provider.CreateRole(roleName);
 }
예제 #7
0
        public void Can_Delete_Role()
        {
            RoleProvider.CreateRole("admin");

            RoleProvider.DeleteRole("admin", true);

            Assert.False(_db.CanFindRole("admin"));
        }
예제 #8
0
        public bool CreateRole(string roleName)
        {
            if (!roleProvider.RoleExists(roleName))
            {
                roleProvider.CreateRole(roleName);
            }

            return(roleProvider.RoleExists(roleName));
        }
예제 #9
0
        public void Can_Add_Users_To_Role()
        {
            var user = new User {Username = "******", Password="******"};
            RoleProvider.CreateRole("admin");
            MembershipProvider.CreateAccount(user);

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

            Assert.True(_db.UserIsInRole("sallen", "admin"));
        }
예제 #10
0
        public void Can_Get_All_Roles()
        {
            RoleProvider.CreateRole("admin");
            RoleProvider.CreateRole("engineering");
            RoleProvider.CreateRole("sales");

            var result = RoleProvider.GetAllRoles();

            Assert.Equal(3, result.Length);
        }
예제 #11
0
 private void ObtainRequiredRoles(params string[] roles)
 {
     foreach (var rolename in roles)
     {
         if (_roleProvider.RoleExists(rolename))
         {
             continue;
         }
         _roleProvider.CreateRole(rolename);
     }
 }
        private void CreateRoles(Dictionary <string, string> usersToCreate)
        {
            var roles = _roleProvider.GetAllRoles();

            foreach (var u in usersToCreate)
            {
                if (!roles.Contains(u.Value))
                {
                    _roleProvider.CreateRole(u.Value);
                }
            }
        }
예제 #13
0
 private static void CreateSpecialRole(RoleProvider roleProvider)
 {
     if (!roleProvider.RoleExists(specialRN))
     {
         roleProvider.CreateRole(specialRN);
         Assert.IsTrue(roleProvider.RoleExists(specialRN));
     }
     else
     {
         Assert.Pass("Special role is already created!");
     }
 }
예제 #14
0
        public void Can_Throw_When_Users_In_Role()
        {
            var user = new User {
                Username = "******", Password = "******"
            };

            RoleProvider.CreateRole("admin");
            MembershipProvider.CreateAccount(user);
            RoleProvider.AddUsersToRoles(new[] { "sallen" }, new[] { "admin" });

            Assert.Throws <ProviderException>(() => RoleProvider.DeleteRole("admin", true));
        }
예제 #15
0
        public void Doesnt_Find_User_Not_In_Role()
        {
            var user = new User {
                Username = "******", Password = "******"
            };

            RoleProvider.CreateRole("admin");
            MembershipProvider.CreateAccount(user);

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

            Assert.False(RoleProvider.IsUserInRole("sallen", "sales"));
        }
예제 #16
0
        public void Can_Add_Users_To_Role()
        {
            var user = new User {
                Username = "******", Password = "******"
            };

            RoleProvider.CreateRole("admin");
            MembershipProvider.CreateAccount(user);

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

            Assert.True(Verifier.Query <Role>().SingleOrDefault(r => r.Name == "admin").Users.Contains("sallen"));
        }
예제 #17
0
        public void Can_Delete_Role_With_Users()
        {
            var user = new User {
                Username = "******", Password = "******"
            };

            RoleProvider.CreateRole("admin");
            MembershipProvider.CreateAccount(user);
            RoleProvider.AddUsersToRoles(new[] { "sallen" }, new[] { "admin" });

            RoleProvider.DeleteRole("admin", false);

            Assert.False(Verifier.Query <Role>().Any(r => r.Name == "admin"));
        }
예제 #18
0
        public void Can_Delete_Role_With_Users()
        {
            var user = new User {
                Username = "******", Password = "******"
            };

            RoleProvider.CreateRole("admin");
            MembershipProvider.CreateAccount(user);
            RoleProvider.AddUsersToRoles(new[] { "sallen" }, new[] { "admin" });

            RoleProvider.DeleteRole("admin", false);

            Assert.False(_db.CanFindRole("admin"));
        }
        public static string[] WithUserInRoles(this RoleProvider roleProvider, string userName, params string[] roles)
        {
            roles.ToList().ForEach(
                x =>
            {
                if (!roleProvider.RoleExists(x))
                {
                    roleProvider.CreateRole(x);
                }

                roleProvider.AddUsersToRoles(new[] { userName }, new[] { x });
            });

            return(roles);
        }
예제 #20
0
        public void Can_Get_Roles_For_User()
        {
            var user = new User {
                Username = "******", Password = "******"
            };

            MembershipProvider.CreateAccount(user);

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

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

            Assert.True(RoleProvider.GetRolesForUser("sallen").Length == 2);
        }
예제 #21
0
        public int AddRole(RoleInfo role)
        {
            Requires.NotNull("role", role);

            var roleId = -1;

            if (provider.CreateRole(role))
            {
                AddMessage(role, EventLogController.EventLogType.ROLE_CREATED);
                AutoAssignUsers(role);
                roleId = role.RoleID;

                ClearRoleCache(role.PortalID);
            }

            return(roleId);
        }
예제 #22
0
    public string CreateGroup(string name)
    {
        // empty name is invalid
        if (string.IsNullOrEmpty(name))
        {
            return(null);
        }

        name = name.Trim();

        RoleInfo role = _GetDnnRole(name);

        if (role == null)
        {
            // create new group
            role = _GetNewDnnRole(name);
            _roleProvider.CreateRole(role);
        }

        return(string.Format("{0}, {1}", role.RoleID, name));
    }
예제 #23
0
        public void Can_Get_Users_In_Role()
        {
            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" });


            Assert.Equal(2, RoleProvider.GetUsersInRole("admin").Length);
        }
예제 #24
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"));
        }
        //private CryptoHelper cryphelp = null;

        #region Add Roles and Users

        /// <summary>
        /// AddRole() Method <a name="AddRole"></a>
        /// The AddRole method creates a new security role for the specified portal,
        /// and returns the new RoleID value.
        /// Other relevant sources:
        /// + <a href="AddRole.htm" style="color:green">AddRole Stored Procedure</a>
        /// </summary>
        /// <param name="roleName">Name of the role.</param>
        public Guid AddRole(string roleName)
        {
            return(RoleProvider.CreateRole(CurrentPortalSettings.PortalAlias, roleName));
        }
예제 #26
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());
        }
예제 #27
0
 /// <summary>
 /// Creates the specified role name.
 /// </summary>
 /// <param name="roleName">Name of the role.</param>
 public void Create(string roleName)
 {
     _roleProvider.CreateRole(roleName);
 }
예제 #28
0
 public override void CreateRole(string roleName)
 {
     LogDebug("Entering CachedProvider.CreateRole");
     _provider.CreateRole(roleName);
 }
예제 #29
0
 public override void CreateRole(string roleName)
 {
     _Provider.CreateRole(roleName);
 }
예제 #30
0
 public void Can_Create_Role()
 {
     RoleProvider.CreateRole("admin");
     Assert.NotNull(Verifier.Query <Role>().SingleOrDefault(r => r.Name == "admin"));
 }