Пример #1
0
        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            using (var transaction = new TransactionScope(mConfiguration))
            {
                var usersInRolesStore = new UserInRoleDataStore(transaction);

                foreach (string userName in usernames)
                {
                    foreach (string roleName in roleNames)
                    {
                        var        rDS        = new RoleDataStore(transaction);
                        Role       role       = rDS.FindByName(mApplicationName, roleName);
                        var        uDS        = new UserDataStore(transaction);
                        User       user       = uDS.FindByName(mApplicationName, userName);
                        UserInRole userInRole = usersInRolesStore.Find(ApplicationName, user, role);
                        if (userInRole == null)
                        {
                            throw new UserInRoleNotFoundException(userName, roleName);
                        }

                        userInRole.Deleted = true;
                        usersInRolesStore.Update(userInRole);
                    }
                }

                transaction.Commit();
            }
        }
Пример #2
0
        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            using (var transaction = new TransactionScope(mConfiguration))
            {
                var roleStore           = new RoleDataStore(transaction);
                var userInRoleDataStore = new UserInRoleDataStore(transaction);

                //Find role
                Role role = roleStore.FindByName(ApplicationName, roleName);
                if (role == null)
                {
                    throw new RoleNotFoundException(roleName);
                }

                IList <UserInRole> listUserInRole = userInRoleDataStore.FindForRole(ApplicationName, role);

                if (throwOnPopulatedRole && listUserInRole.Count > 0)
                {
                    throw new ProviderException("Cannot delete a populated role.");
                }

                foreach (UserInRole ur in listUserInRole)
                {
                    ur.Deleted = true;
                    userInRoleDataStore.Update(ur);
                }

                role.Deleted = true;
                role.Name   += DateTimeHelper.GetCurrentTimestamp();
                roleStore.Update(role);

                transaction.Commit();
            }

            return(true);
        }