/// <summary> /// Determine if user is in role /// </summary> public bool IsUserInRole(string userName, string roleName) { using (var dataContext = this.m_configuration.Provider.GetReadonlyConnection()) { try { dataContext.Open(); DbSecurityUser user = dataContext.SingleOrDefault <DbSecurityUser>(u => u.UserName.ToLower() == userName.ToLower()); if (user == null) { throw new KeyNotFoundException(String.Format("Could not locate user {0}", userName)); } DbSecurityRole role = dataContext.SingleOrDefault <DbSecurityRole>(r => r.Name == roleName); if (role == null) { throw new KeyNotFoundException(String.Format("Could not locate role {0}", roleName)); } // Select return(dataContext.Any <DbSecurityUserRole>(o => o.UserKey == user.Key && o.RoleKey == role.Key)); } catch (Exception e) { throw new DataPersistenceException($"Error determining role membership between {userName} and {roleName}"); } } }
public void AddUsersToRoles(string[] users, string[] roles, IPrincipal authPrincipal) { this.VerifyPrincipal(authPrincipal, PermissionPolicyIdentifiers.AlterRoles); // Add users to role using (DataContext dataContext = this.m_configuration.Provider.GetWriteConnection()) { try { dataContext.Open(); using (var tx = dataContext.BeginTransaction()) { try { foreach (var un in users) { DbSecurityUser user = dataContext.SingleOrDefault <DbSecurityUser>(u => u.UserName.ToLower() == un.ToLower()); if (user == null) { throw new KeyNotFoundException(String.Format("Could not locate user {0}", un)); } foreach (var rol in roles) { DbSecurityRole role = dataContext.SingleOrDefault <DbSecurityRole>(r => r.Name == rol); if (role == null) { throw new KeyNotFoundException(String.Format("Could not locate role {0}", rol)); } if (!dataContext.Any <DbSecurityUserRole>(o => o.RoleKey == role.Key && o.UserKey == user.Key)) { // Insert dataContext.Insert(new DbSecurityUserRole() { UserKey = user.Key, RoleKey = role.Key }); } } } tx.Commit(); } catch { tx.Rollback(); throw; } } } catch (Exception e) { this.m_tracer.TraceEvent(TraceEventType.Error, e.HResult, "Error adding {0} to {1} : {2}", String.Join(",", users), String.Join(",", roles), e); throw; } } }
/// <summary> /// Remove users from roles /// </summary> public void RemoveUsersFromRoles(string[] users, string[] roles, IPrincipal principal) { this.VerifyPrincipal(principal, PermissionPolicyIdentifiers.AlterRoles); using (DataContext dataContext = this.m_configuration.Provider.GetWriteConnection()) try { dataContext.Open(); using (var tx = dataContext.BeginTransaction()) { try { foreach (var un in users) { DbSecurityUser user = dataContext.SingleOrDefault <DbSecurityUser>(u => u.UserName.ToLower() == un.ToLower()); if (user == null) { throw new KeyNotFoundException(String.Format("Could not locate user {0}", un)); } foreach (var rol in roles) { DbSecurityRole role = dataContext.SingleOrDefault <DbSecurityRole>(r => r.Name == rol); if (role == null) { throw new KeyNotFoundException(String.Format("Could not locate role {0}", rol)); } // Insert dataContext.Delete <DbSecurityUserRole>(o => o.UserKey == user.Key && o.RoleKey == role.Key); } } tx.Commit(); } catch { tx.Rollback(); throw; } } } catch (Exception e) { this.m_tracer.TraceEvent(EventLevel.Error, "Error removing {0} from {1} : {2}", String.Join(",", users), String.Join(",", roles), e); throw; } }