private void SetUserRights(Entities objectContext, User user) { Tools.CheckObjectContext(objectContext); if (user == null) { throw new ArgumentNullException("user"); } user.User_Roles.Load(); if (user.User_Roles.Count < 1) { throw new ArgumentOutOfRangeException("user doesnt have roles"); } List<User_Role> activeRoles = user.User_Roles.Where(ur => ur.active == true).ToList(); if (activeRoles == null) { throw new ArgumentOutOfRangeException("user doesnt have active roles."); } if(activeRoles.Count > 1) { throw new ArgumentOutOfRangeException("user have more than 1 active roles."); } activeRoles.First().RoleReference.Load(); Role userRole = activeRoles.First().Role; SetRights(objectContext, userRole); }
public Role GetUserRole(Entities objectContext, User user) { Tools.CheckObjectContext(objectContext); if (user == null) { throw new ArgumentNullException("user"); } if (user.isAdmin == true) { throw new InvalidOperationException("user is admin"); } user.User_Roles.Load(); User_Role userRole = user.User_Roles.FirstOrDefault(ur => ur.active == true); if (userRole == null) { throw new ArgumentNullException("user doesnt have role set."); } userRole.RoleReference.Load(); Role role = userRole.Role; return(role); }
public List <User> SearchForUser(Entities objectContext, string name) { Tools.CheckObjectContext(objectContext); if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException("name is null or empty"); } // by default it must be done with stored procedure List <User> users = objectContext.UserSet.ToList(); List <User> results = new List <User>(); if (users != null && users.Count > 0) { foreach (User user in users) { if (user.name.ToLower().Contains(name) == true) { results.Add(user); } } } return(results); }
public void AddRole(Entities objectContext, string name, NewRoleRights roleRights) { Tools.CheckObjectContext(objectContext); if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } if (roleRights == null) { throw new ArgumentNullException("roleRights"); } lock (addingRoleSync) { if (GetRole(objectContext, name, false) != null) { throw new InvalidOperationException("There is already role with same name"); } Role newRole = new Role(); newRole.name = name; objectContext.AddToRoleSet(newRole); Tools.Save(objectContext); AddRoleRights(objectContext, newRole, roleRights); } }
public void AddReport(Entities objectContext, User currUser, string about, string description) { Tools.CheckObjectContext(objectContext); if (currUser == null) { throw new ArgumentNullException("currUser"); } if (string.IsNullOrEmpty(about)) { throw new ArgumentNullException("about"); } if (string.IsNullOrEmpty(description)) { throw new ArgumentNullException("description"); } Report newReport = new Report(); newReport.User = currUser; newReport.about = about; newReport.description = description; newReport.dateAdded = DateTime.Now; newReport.solved = false; newReport.visible = true; objectContext.AddToReportSet(newReport); Tools.Save(objectContext); }
public User GetUser(Entities objectContext, string username, string password, bool throwExcIfNull) { Tools.CheckObjectContext(objectContext); if (string.IsNullOrEmpty(username)) { throw new ArgumentException("username is null or empty"); } if (string.IsNullOrEmpty(username)) { throw new ArgumentException("password is null or empty"); } string hashedPass = GetHashed(password); User user = objectContext.UserSet.FirstOrDefault(usr => usr.username == username && usr.password == hashedPass); if (user == null && throwExcIfNull == true) { throw new ArgumentOutOfRangeException(string.Format("No user with username = {0} and pass = ***", username)); } return(user); }
public List <User> GetAllNonAdminUsers(Entities objectContext) { Tools.CheckObjectContext(objectContext); List <User> users = objectContext.UserSet.Where(usr => usr.isAdmin == false).ToList(); return(users); }
public List <Role> GetRoles(Entities objectContext) { Tools.CheckObjectContext(objectContext); List <Role> roles = objectContext.RoleSet.ToList(); return(roles); }
public int RegisteredUsersCount(Entities objectContext) { Tools.CheckObjectContext(objectContext); int count = objectContext.UserSet.Count(); return(count); }
/// <summary> /// Counts non admin users /// </summary> /// <param name="objectContext"></param> /// <returns></returns> public int CountUsers(Entities objectContext) { Tools.CheckObjectContext(objectContext); int result = 0; result = objectContext.UserSet.Count(usr => usr.isAdmin == false); return(result); }
/// <summary> /// Counts number of roles /// </summary> /// <param name="objectContext"></param> /// <returns></returns> public int CountRoles(Entities objectContext) { Tools.CheckObjectContext(objectContext); int result = 0; result = objectContext.RoleSet.Count(); return(result); }
/// <summary> /// Check role rights and sets them accordingly /// </summary> /// <param name="objectContext"></param> /// <param name="user"></param> public UserOrRoleRights(Entities objectContext, Role role) { Tools.CheckObjectContext(objectContext); if (role == null) { throw new ArgumentNullException("role"); } SetRights(objectContext, role); }
public Role GetRole(Entities objectContext, long id, bool throwExcIfNull) { Tools.CheckObjectContext(objectContext); Role role = objectContext.RoleSet.FirstOrDefault(rle => rle.ID == id); if (role == null && throwExcIfNull == true) { throw new ArgumentOutOfRangeException(string.Format("No role with id = {0}", id)); } return(role); }
public void DeactivateUser(Entities objectContext, User user) { Tools.CheckObjectContext(objectContext); if (user == null) { throw new ArgumentNullException("user is null or empty"); } if (user.isActive == true) { user.isActive = false; Tools.Save(objectContext); } }
public void DeleteReport(Entities objectContext, Report report) { Tools.CheckObjectContext(objectContext); if (report == null) { throw new ArgumentNullException("report"); } if (report.visible == false) { throw new InvalidOperationException("report is already deleted."); } report.visible = false; Tools.Save(objectContext); }
public void AddUser(Entities objectContext, string username, string name, string password, bool isAdmin) { Tools.CheckObjectContext(objectContext); if (string.IsNullOrEmpty(username)) { throw new ArgumentException("username is null or empty"); } if (string.IsNullOrEmpty(name)) { throw new ArgumentException("name is null or empty"); } if (string.IsNullOrEmpty(password)) { throw new ArgumentException("password is null or empty"); } lock (addingUserSync) { if (GetUser(objectContext, username, false) != null) { throw new InvalidOperationException("There is already user with same username"); } bool isFirstAdmin = false; User firstUser = objectContext.UserSet.FirstOrDefault(); if (firstUser == null) { isFirstAdmin = true; } User newUser = new User(); newUser.username = username; newUser.name = name; newUser.isAdmin = isAdmin; newUser.password = GetHashed(password); newUser.isActive = true; newUser.mainAdmin = isFirstAdmin; objectContext.AddToUserSet(newUser); Tools.Save(objectContext); } }
public List <Report> GetAllReports(Entities objectContext, bool onlyVisible) { Tools.CheckObjectContext(objectContext); List <Report> reports = new List <Report>(); if (onlyVisible == true) { reports = objectContext.ReportSet.Where(rp => rp.visible == true).ToList(); } else { reports = objectContext.ReportSet.Where(rp => rp.visible == false).ToList(); } return(reports); }
private List <Right> GetAllRights(Entities objectContext) { Tools.CheckObjectContext(objectContext); List <Right> allRights = objectContext.RightSet.ToList(); if (allRights == null) { throw new ArgumentNullException("There are no rights (controls) in database"); } if (allRights.Count != UserRights.RightsCount) { throw new ArgumentOutOfRangeException("The rights (controls) in database are not 6"); } return(allRights); }
public User GetUser(Entities objectContext, string username, bool throwExcIfNull) { Tools.CheckObjectContext(objectContext); if (string.IsNullOrEmpty(username)) { throw new ArgumentException("username is null or empty"); } User user = objectContext.UserSet.FirstOrDefault(usr => usr.username == username); if (user == null && throwExcIfNull == true) { throw new ArgumentOutOfRangeException(string.Format("No user with username = {0}", username)); } return(user); }
public Role GetRole(Entities objectContext, string name, bool throwExcIfNull) { Tools.CheckObjectContext(objectContext); if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } Role role = objectContext.RoleSet.FirstOrDefault(rle => rle.name == name); if (role == null && throwExcIfNull == true) { throw new ArgumentOutOfRangeException(string.Format("No role with name = {0}", name)); } return(role); }
public User GetUser(Entities objectContext, long id, bool throwExcIfNull) { Tools.CheckObjectContext(objectContext); if (id < 1) { throw new ArgumentOutOfRangeException("id < 1"); } User user = objectContext.UserSet.FirstOrDefault(usr => usr.ID == id); if (user == null && throwExcIfNull == true) { throw new ArgumentOutOfRangeException(string.Format("No user with id = {0}", id)); } return(user); }
/// <summary> /// Check user rights and sets them accordingly /// </summary> /// <param name="user"></param> public UserOrRoleRights(Entities objectContext, User user) { Tools.CheckObjectContext(objectContext); if (user == null) { throw new ArgumentNullException("user"); } if (user.isAdmin == true) { UserIsAdmin(); } else { SetUserRights(objectContext, user); } }
public void EditRole(Entities objectContext, Role currRole, string newName, NewRoleRights editedRights) { Tools.CheckObjectContext(objectContext); if (currRole == null) { throw new ArgumentNullException("currRole"); } lock (editingRoleSync) { if (!string.IsNullOrEmpty(newName)) { currRole.name = newName; Tools.Save(objectContext); } ChangeRoleRights(objectContext, currRole, editedRights); } }
public List <Report> GetUserReports(Entities objectContext, User currUser, bool onlyVisible) { Tools.CheckObjectContext(objectContext); if (currUser == null) { throw new ArgumentNullException("currUser"); } List <Report> reports = new List <Report>(); if (onlyVisible == true) { reports = objectContext.ReportSet.Where(rp => rp.User.ID == currUser.ID && rp.visible == true).ToList(); } else { reports = objectContext.ReportSet.Where(rp => rp.User.ID == currUser.ID && rp.visible == false).ToList(); } return(reports); }
private void AddRoleRights(Entities objectContext, Role newRole, NewRoleRights roleRights) { Tools.CheckObjectContext(objectContext); if (roleRights == null) { throw new ArgumentNullException("roleRights"); } if (newRole == null) { throw new ArgumentNullException("newRole"); } newRole.Role_Rights.Load(); if (newRole.Role_Rights.Count > 0) { throw new InvalidOperationException("newRole already have rights attached."); } List <Right> allRights = GetAllRights(objectContext); Role_Right rSearchUsers = new Role_Right(); rSearchUsers.Role = newRole; rSearchUsers.Right = allRights[0]; rSearchUsers.enabled = roleRights.SearchForUsersEnabled; rSearchUsers.visible = roleRights.SearchForUsersVisible; objectContext.AddToRole_RightSet(rSearchUsers); Role_Right rAddReport = new Role_Right(); rAddReport.Role = newRole; rAddReport.Right = allRights[1]; rAddReport.enabled = roleRights.AddReportEnabled; rAddReport.visible = roleRights.AddReportVisible; objectContext.AddToRole_RightSet(rAddReport); Role_Right rSeeVisibleReports = new Role_Right(); rSeeVisibleReports.Role = newRole; rSeeVisibleReports.Right = allRights[2]; rSeeVisibleReports.enabled = roleRights.SeeVisibleReportsEnabled; rSeeVisibleReports.visible = roleRights.SeeVisibleReportsVisible; objectContext.AddToRole_RightSet(rSeeVisibleReports); Role_Right rSeeDeletedReports = new Role_Right(); rSeeDeletedReports.Role = newRole; rSeeDeletedReports.Right = allRights[3]; rSeeDeletedReports.enabled = roleRights.SeeDeletedReportsEnabled; rSeeDeletedReports.visible = roleRights.SeeDeletedReportVisible; objectContext.AddToRole_RightSet(rSeeDeletedReports); Role_Right rMarkReportResolved = new Role_Right(); rMarkReportResolved.Role = newRole; rMarkReportResolved.Right = allRights[4]; rMarkReportResolved.enabled = roleRights.MarkReportAsResolvedEnabled; rMarkReportResolved.visible = roleRights.MarkReportAsResolvedVisible; objectContext.AddToRole_RightSet(rMarkReportResolved); Role_Right rDeleteReport = new Role_Right(); rDeleteReport.Role = newRole; rDeleteReport.Right = allRights[5]; rDeleteReport.enabled = roleRights.DeleteReportEnabled; rDeleteReport.visible = roleRights.DeleteReportVisible; objectContext.AddToRole_RightSet(rDeleteReport); Tools.Save(objectContext); }
private void ChangeRoleRights(Entities objectContext, Role currRole, NewRoleRights newRights) { Tools.CheckObjectContext(objectContext); if (currRole == null) { throw new ArgumentNullException("currRole"); } if (newRights == null) { throw new ArgumentNullException("newRights"); } Role_Right currRight = objectContext.Role_RightSet.FirstOrDefault(rr => rr.Right.ID == UserRights.SearchForUsers && rr.Role.ID == currRole.ID); if (currRight == null) { throw new InvalidOperationException("role doesn't have set right ID : 1"); } currRight.enabled = newRights.SearchForUsersEnabled; currRight.visible = newRights.SearchForUsersVisible; Tools.Save(objectContext); currRight = objectContext.Role_RightSet.FirstOrDefault(rr => rr.Right.ID == UserRights.AddReport && rr.Role.ID == currRole.ID); if (currRight == null) { throw new InvalidOperationException("role doesn't have set right ID : 2"); } currRight.enabled = newRights.AddReportEnabled; currRight.visible = newRights.AddReportVisible; Tools.Save(objectContext); currRight = objectContext.Role_RightSet.FirstOrDefault(rr => rr.Right.ID == UserRights.SeeVisibleReports && rr.Role.ID == currRole.ID); if (currRight == null) { throw new InvalidOperationException("role doesn't have set right ID : 3"); } currRight.enabled = newRights.SeeVisibleReportsEnabled; currRight.visible = newRights.SeeVisibleReportsVisible; Tools.Save(objectContext); currRight = objectContext.Role_RightSet.FirstOrDefault(rr => rr.Right.ID == UserRights.SeeDeletedReports && rr.Role.ID == currRole.ID); if (currRight == null) { throw new InvalidOperationException("role doesn't have set right ID : 4"); } currRight.enabled = newRights.SeeDeletedReportsEnabled; currRight.visible = newRights.SeeDeletedReportVisible; Tools.Save(objectContext); currRight = objectContext.Role_RightSet.FirstOrDefault(rr => rr.Right.ID == UserRights.MarkReportAsSolved && rr.Role.ID == currRole.ID); if (currRight == null) { throw new InvalidOperationException("role doesn't have set right ID : 5"); } currRight.enabled = newRights.MarkReportAsResolvedEnabled; currRight.visible = newRights.MarkReportAsResolvedVisible; Tools.Save(objectContext); currRight = objectContext.Role_RightSet.FirstOrDefault(rr => rr.Right.ID == UserRights.DeleteReports && rr.Role.ID == currRole.ID); if (currRight == null) { throw new InvalidOperationException("role doesn't have set right ID : 6"); } currRight.enabled = newRights.DeleteReportEnabled; currRight.visible = newRights.DeleteReportVisible; Tools.Save(objectContext); }
public void AddRoleToUser(Entities objectContext, Role role, User user) { Tools.CheckObjectContext(objectContext); if (role == null) { throw new ArgumentNullException("role"); } if (user == null) { throw new ArgumentNullException("user"); } if (user.isAdmin == true) { throw new InvalidOperationException("Roles cannot be added to admins"); } user.User_Roles.Load(); if (user.User_Roles.Count < 1) // First role to new user is being given { User_Role newUserRole = new User_Role(); newUserRole.User = user; newUserRole.Role = role; newUserRole.active = true; objectContext.AddToUser_RoleSet(newUserRole); Tools.Save(objectContext); } else // User role is being changed { User_Role currRole = user.User_Roles.FirstOrDefault(ur => ur.active == true && ur.Role.ID == role.ID); if (currRole != null) { return; // New role same as current } List <User_Role> activeRoles = user.User_Roles.Where(ur => ur.active == true).ToList(); if (activeRoles != null && activeRoles.Count > 0) { foreach (User_Role activeRole in activeRoles) { activeRole.active = false; } Tools.Save(objectContext); } User_Role oldInactiveRole = user.User_Roles.FirstOrDefault(ur => ur.active == false && ur.Role.ID == role.ID); if (oldInactiveRole != null) { oldInactiveRole.active = true; Tools.Save(objectContext); } else { User_Role newUserRole = new User_Role(); newUserRole.User = user; newUserRole.Role = role; newUserRole.active = true; objectContext.AddToUser_RoleSet(newUserRole); Tools.Save(objectContext); } } }