public override void AddUsersToRoles(string[] usernames, string[] roleNames) { using (TransactionScope transaction = new TransactionScope(mConfiguration)) { RoleDataStore roleStore = new RoleDataStore(transaction); //Find the roles Role[] roles = new Role[roleNames.Length]; for (int i = 0; i < roleNames.Length; i++) { //Find the role Role role = roleStore.FindByName(ApplicationName, roleNames[i]); if (role == null) throw new RoleNotFoundException(roleNames[i]); roles[i] = role; } UserInRoleDataStore usersInRolesStore = new UserInRoleDataStore(transaction); foreach (string userName in usernames) { foreach (Role role in roles) { UserInRole userInRole = new UserInRole(ApplicationName, userName, role.Name); usersInRolesStore.Insert(userInRole); } } transaction.Commit(); } }
public override bool DeleteUser(string username, bool deleteAllRelatedData) { try { using (var transaction = new TransactionScope(_mConfiguration)) { if (deleteAllRelatedData) { var dataStore = new UserDataStore(transaction); User user = dataStore.FindByName(ApplicationName, username); var userInRoleStore = new UserInRoleDataStore(transaction); IList <UserInRole> userInRoles = userInRoleStore.FindForUser(ApplicationName, user); foreach (UserInRole ur in userInRoles) { ur.Deleted = true; userInRoleStore.Update(ur); } } var dataStore1 = new UserDataStore(transaction); User user1 = dataStore1.FindByName(ApplicationName, username); if (user1 == null) { throw new UserNotFoundException(username); } // Rename the user before deleting, this will allow a new user with the same name. user1.Name = user1.Name + DateTimeHelper.GetCurrentTimestamp(); user1.Deleted = true; dataStore1.Update(user1); transaction.Commit(); } return(true); } catch (UserNotDeletedException ex) { LogException(ex, "DeleteUser"); return(false); } }
public override bool DeleteRole(string roleName, bool throwOnPopulatedRole) { using (TransactionScope transaction = new TransactionScope(mConfiguration)) { RoleDataStore roleStore = new RoleDataStore(transaction); UserInRoleDataStore 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, roleName); if (throwOnPopulatedRole && listUserInRole.Count > 0) throw new System.Configuration.Provider.ProviderException("Cannot delete a populated role."); foreach (UserInRole ur in listUserInRole) userInRoleDataStore.Delete(ur.Id); roleStore.Delete(role.Id); transaction.Commit(); } return true; }
public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames) { using (TransactionScope transaction = new TransactionScope(mConfiguration)) { UserInRoleDataStore usersInRolesStore = new UserInRoleDataStore(transaction); foreach (string userName in usernames) { foreach (string roleName in roleNames) { UserInRole userInRole = usersInRolesStore.Find(ApplicationName, userName, roleName); if (userInRole == null) throw new UserInRoleNotFoundException(userName, roleName); usersInRolesStore.Delete(userInRole.Id); } } transaction.Commit(); } }
public override bool IsUserInRole(string username, string roleName) { using (TransactionScope transaction = new TransactionScope(mConfiguration)) { UserInRoleDataStore userInRoleDataStore = new UserInRoleDataStore(transaction); if (userInRoleDataStore.Find(ApplicationName, username, roleName) != null) return true; else return false; } }
public override string[] GetUsersInRole(string roleName) { List<string> userNames = new List<string>(); using (TransactionScope transaction = new TransactionScope(mConfiguration)) { UserInRoleDataStore userInRoleDataStore = new UserInRoleDataStore(transaction); IList<UserInRole> listUserInRole = userInRoleDataStore.FindForRole(ApplicationName, roleName); foreach (UserInRole ur in listUserInRole) { userNames.Add(ur.UserName); } } return userNames.ToArray(); }