コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
ファイル: BUsers.cs プロジェクト: hnjm/PermissionsManager
        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);
        }
コード例 #4
0
        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);
            }
        }
コード例 #5
0
        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);
        }
コード例 #6
0
ファイル: BUsers.cs プロジェクト: hnjm/PermissionsManager
        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);
        }
コード例 #7
0
ファイル: BUsers.cs プロジェクト: hnjm/PermissionsManager
        public List <User> GetAllNonAdminUsers(Entities objectContext)
        {
            Tools.CheckObjectContext(objectContext);

            List <User> users = objectContext.UserSet.Where(usr => usr.isAdmin == false).ToList();

            return(users);
        }
コード例 #8
0
        public List <Role> GetRoles(Entities objectContext)
        {
            Tools.CheckObjectContext(objectContext);

            List <Role> roles = objectContext.RoleSet.ToList();

            return(roles);
        }
コード例 #9
0
ファイル: BUsers.cs プロジェクト: hnjm/PermissionsManager
        public int RegisteredUsersCount(Entities objectContext)
        {
            Tools.CheckObjectContext(objectContext);

            int count = objectContext.UserSet.Count();

            return(count);
        }
コード例 #10
0
ファイル: BUsers.cs プロジェクト: hnjm/PermissionsManager
        /// <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);
        }
コード例 #11
0
        /// <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);
        }
コード例 #12
0
        /// <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);
        }
コード例 #13
0
        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);
        }
コード例 #14
0
ファイル: BUsers.cs プロジェクト: hnjm/PermissionsManager
        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);
            }
        }
コード例 #15
0
        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);
        }
コード例 #16
0
ファイル: BUsers.cs プロジェクト: hnjm/PermissionsManager
        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);
            }
        }
コード例 #17
0
        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);
        }
コード例 #18
0
        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);
        }
コード例 #19
0
ファイル: BUsers.cs プロジェクト: hnjm/PermissionsManager
        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);
        }
コード例 #20
0
        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);
        }
コード例 #21
0
ファイル: BUsers.cs プロジェクト: hnjm/PermissionsManager
        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);
        }
コード例 #22
0
        /// <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);
            }

        }
コード例 #23
0
        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);
            }
        }
コード例 #24
0
        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);
        }
コード例 #25
0
        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);
        }
コード例 #26
0
        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);
        }
コード例 #27
0
        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);
                }
            }
        }