Exemplo n.º 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();
            }
        }
Exemplo n.º 2
0
        public override void CreateUserRole(User user, Role role)
        {
            using (var transaction = new TransactionScope(mConfiguration))
            {
                var usersInRolesStore = new UserInRoleDataStore(transaction);
                var userInRole        = new UserInRole(ApplicationName, user, role);
                usersInRolesStore.Insert(userInRole);

                transaction.Commit();
            }
        }
Exemplo n.º 3
0
 public override void DeleteUserFromRole(User user, Role role)
 {
     using (var transaction = new TransactionScope(mConfiguration))
     {
         var uirDS = new UserInRoleDataStore(transaction);
         IList <UserInRole> userInRoles = uirDS.FindForUserAndRole(mApplicationName, user, role);
         if (userInRoles.Count > 0)
         {
             foreach (UserInRole uir in userInRoles)
             {
                 uirDS.Delete(uir.Id);
             }
             transaction.Commit();
         }
     }
 }
Exemplo n.º 4
0
 public override IList <Role> GetRolesByUser(User user)
 {
     using (var transaction = new TransactionScope(mConfiguration))
     {
         IList <Role>       runningList    = new List <Role>();
         var                uirds          = new UserInRoleDataStore(transaction);
         IList <UserInRole> listUserInRole = uirds.FindForUser(ApplicationName, user);
         foreach (UserInRole ur in listUserInRole)
         {
             IEnumerable <Role> q = from x in runningList where x.Id.Equals(ur.User.Id) select x;
             if (!q.Any())
             {
                 runningList.Add(RoleManager.GetRole(ur.Role.Id));
             }
         }
         return(runningList);
     }
 }
Exemplo n.º 5
0
        public override bool IsUserInRole(string username, string roleName)
        {
            using (var transaction = new TransactionScope(mConfiguration))
            {
                var  rDS  = new RoleDataStore(transaction);
                Role role = rDS.FindByName(mApplicationName, roleName);
                var  uDS  = new UserDataStore(transaction);
                User user = uDS.FindByName(mApplicationName, username);
                var  userInRoleDataStore = new UserInRoleDataStore(transaction);

                if (userInRoleDataStore.Find(ApplicationName, user, role) != null)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
Exemplo n.º 6
0
        public override string[] GetUsersInRole(string roleName)
        {
            var userNames = new List <string>();

            using (var transaction = new TransactionScope(mConfiguration))
            {
                var  rDS  = new RoleDataStore(transaction);
                Role role = rDS.FindByName(mApplicationName, roleName);
                var  userInRoleDataStore = new UserInRoleDataStore(transaction);

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

                foreach (UserInRole ur in listUserInRole)
                {
                    userNames.Add(ur.User.Name);
                }
            }

            return(userNames.ToArray());
        }
Exemplo n.º 7
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);
        }