Example #1
0
        public static bool UpdateRole(RoleViewModel _modifiedRole)
        {
            bool _retVal = false;

            try
            {
                using (GovHistoryDbContext db = new GovHistoryDbContext())
                {
                    ApplicationRole _role2Modify = db.Roles.Where(p => p.Id == _modifiedRole.Id).Include("PERMISSIONS").FirstOrDefault();

                    db.Entry(_role2Modify).Entity.Name            = _modifiedRole.Name;
                    db.Entry(_role2Modify).Entity.RoleDescription = _modifiedRole.RoleDescription;
                    db.Entry(_role2Modify).Entity.IsSysAdmin      = _modifiedRole.IsSysAdmin;
                    db.Entry(_role2Modify).Entity.LastModified    = System.DateTime.Now;
                    db.Entry(_role2Modify).State = EntityState.Modified;
                    db.SaveChanges();

                    _retVal = true;
                }
            }
            catch (Exception)
            {
            }
            return(_retVal);
        }
Example #2
0
        public static bool UpdateUser(UserViewModel _user)
        {
            bool _retVal = false;

            try
            {
                using (GovHistoryDbContext db = new GovHistoryDbContext())
                {
                    ApplicationUser _user2Modify = GetUser(db, _user.Id);

                    db.Entry(_user2Modify).Entity.UserName     = _user.UserName;
                    db.Entry(_user2Modify).Entity.Email        = _user.Email;
                    db.Entry(_user2Modify).Entity.Firstname    = _user.Firstname;
                    db.Entry(_user2Modify).Entity.Lastname     = _user.Lastname;
                    db.Entry(_user2Modify).Entity.LastModified = System.DateTime.Now;
                    db.Entry(_user2Modify).State = EntityState.Modified;
                    db.SaveChanges();

                    _retVal = true;
                }
            }
            catch (Exception ex)
            {
                string x = ex.Message;
            }
            return(_retVal);
        }
Example #3
0
        public static bool RemoveUser4Role(int _userId, int _roleId)
        {
            bool _retVal = false;

            try
            {
                using (GovHistoryDbContext db = new GovHistoryDbContext())
                {
                    ApplicationUser _user = GetUser(db, _userId);
                    if (_user.Roles.Where(p => p.RoleId == _roleId).Count() > 0)
                    {
                        _user.Roles.Remove(_user.Roles.Where(p => p.RoleId == _roleId).FirstOrDefault());
                        _user.LastModified    = DateTime.Now;
                        db.Entry(_user).State = EntityState.Modified;
                        db.SaveChanges();

                        _retVal = true;
                    }
                }
            }
            catch (Exception)
            {
            }
            return(_retVal);
        }
Example #4
0
        public static bool AddUser2Role(int _userId, int _roleId)
        {
            bool _retVal = false;

            try
            {
                using (GovHistoryDbContext db = new GovHistoryDbContext())
                {
                    ApplicationUser _user = GetUser(db, _userId);
                    if (_user.Roles.Where(p => p.RoleId == _roleId).Count() == 0)
                    {
                        //_user.UserRoles.Add(_role);

                        ApplicationUserRole _identityRole = new ApplicationUserRole {
                            UserId = _userId, RoleId = _roleId
                        };
                        if (!_user.Roles.Contains(_identityRole))
                        {
                            _user.Roles.Add(_identityRole);
                        }

                        _user.LastModified    = DateTime.Now;
                        db.Entry(_user).State = EntityState.Modified;
                        db.SaveChanges();

                        _retVal = true;
                    }
                }
            }
            catch (Exception)
            {
            }
            return(_retVal);
        }
Example #5
0
        public static bool RemovePermission4Role(int _roleId, int _permissionId)
        {
            bool _retVal = false;

            try
            {
                using (GovHistoryDbContext db = new GovHistoryDbContext())
                {
                    ApplicationRole _role2Modify = db.Roles.Where(p => p.Id == _roleId).Include("PERMISSIONS").FirstOrDefault();
                    PERMISSION      _permission  = db.PERMISSIONS.Where(p => p.PermissionId == _permissionId).Include("ROLES").FirstOrDefault();

                    if (_role2Modify.PERMISSIONS.Contains(_permission))
                    {
                        _role2Modify.PERMISSIONS.Remove(_permission);
                        _role2Modify.LastModified    = DateTime.Now;
                        db.Entry(_role2Modify).State = EntityState.Modified;
                        db.SaveChanges();

                        _retVal = true;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return(_retVal);
        }
Example #6
0
        public static bool AddPermission2Role(int _roleId, int _permissionId)
        {
            bool _retVal = false;

            try
            {
                using (GovHistoryDbContext db = new GovHistoryDbContext())
                {
                    ApplicationRole role = db.Roles.Where(p => p.Id == _roleId).Include("PERMISSIONS").FirstOrDefault();
                    if (role != null)
                    {
                        PERMISSION _permission = db.PERMISSIONS.Where(p => p.PermissionId == _permissionId).Include("ROLES").FirstOrDefault();
                        if (!role.PERMISSIONS.Contains(_permission))
                        {
                            role.PERMISSIONS.Add(_permission);
                            role.LastModified    = DateTime.Now;
                            db.Entry(role).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(_retVal);
        }
Example #7
0
        public static bool AddAllPermissions2Role(int _roleId)
        {
            bool _retVal = false;

            try
            {
                using (GovHistoryDbContext db = new GovHistoryDbContext())
                {
                    ApplicationRole role = db.Roles.Where(p => p.Id == _roleId).Include("PERMISSIONS").FirstOrDefault();
                    if (role != null)
                    {
                        List <PERMISSION> _permissions = db.PERMISSIONS.Include("ROLES").ToList();
                        foreach (PERMISSION _permission in _permissions)
                        {
                            if (!role.PERMISSIONS.Contains(_permission))
                            {
                                role.PERMISSIONS.Add(_permission);
                            }
                        }
                        role.LastModified    = DateTime.Now;
                        db.Entry(role).State = EntityState.Modified;
                        db.SaveChanges();
                        _retVal = true;
                    }
                }
            }
            catch
            {
            }
            return(_retVal);
        }
Example #8
0
        public static ApplicationUser GetUser(GovHistoryDbContext db, int _userId)
        {
            ApplicationUser _retVal = null;

            try
            {
                _retVal = db.Users.Where(p => p.Id == _userId).Include("Roles").Include(x => x.Roles.Select(r => r.Role.PERMISSIONS)).FirstOrDefault();
            }
            catch (Exception)
            {
            }

            return(_retVal);
        }
Example #9
0
        public static List <PERMISSION> GetPermissions()
        {
            List <PERMISSION> _retVal = null;

            try
            {
                using (GovHistoryDbContext db = new GovHistoryDbContext())
                {
                    _retVal = db.PERMISSIONS.OrderBy(p => p.PermissionDescription).Include("ROLES").ToList();
                }
            }
            catch (Exception)
            {
            }
            return(_retVal);
        }
Example #10
0
        public static PERMISSION GetPermission(int _permissionid)
        {
            PERMISSION _retVal = null;

            try
            {
                using (GovHistoryDbContext db = new GovHistoryDbContext())
                {
                    _retVal = db.PERMISSIONS.Where(p => p.PermissionId == _permissionid).Include("ROLES").FirstOrDefault();
                }
            }
            catch (Exception)
            {
            }
            return(_retVal);
        }
Example #11
0
        internal static int GetPermissionsCount()
        {
            int _retVal = 0;

            try
            {
                using (GovHistoryDbContext db = new GovHistoryDbContext())
                {
                    _retVal = db.PERMISSIONS.OrderBy(p => p.PermissionDescription).Count();
                }
            }
            catch (Exception)
            {
                throw;
            }
            return(_retVal);
        }
Example #12
0
        internal static List <PERMISSION> GetPermissions(int start, int limit)
        {
            List <PERMISSION> _retVal = null;

            try
            {
                using (GovHistoryDbContext db = new GovHistoryDbContext())
                {
                    _retVal = db.PERMISSIONS.OrderBy(p => p.PermissionDescription).Include("ROLES").OrderByDescending(c => c.PermissionId).Skip(start).Take(limit).ToList();
                }
            }
            catch (Exception)
            {
                throw;
            }
            return(_retVal);
        }
Example #13
0
        public static List <ApplicationUser> GetUsers4Surname(string _surname)
        {
            List <ApplicationUser> _retVal = null;

            try
            {
                using (GovHistoryDbContext db = new GovHistoryDbContext())
                {
                    _retVal = db.Users.Where(r => r.Inactive == false || r.Inactive == null & r.Lastname == _surname).OrderBy(r => r.Lastname).ThenBy(r => r.Firstname).ToList();
                }
            }
            catch (Exception)
            {
            }

            return(_retVal);
        }
Example #14
0
        public static List <ApplicationRole> GetRoles4SelectList()
        {
            List <ApplicationRole> _retVal = null;

            try
            {
                using (GovHistoryDbContext db = new GovHistoryDbContext())
                {
                    _retVal = db.Roles.OrderBy(p => p.Name).ToList();
                }
            }
            catch (Exception)
            {
            }

            return(_retVal);
        }
Example #15
0
        public static List <ApplicationUser> GetUsers4SelectList()
        {
            List <ApplicationUser> _retVal = null;

            try
            {
                using (GovHistoryDbContext db = new GovHistoryDbContext())
                {
                    _retVal = db.Users.Where(r => r.Inactive == false || r.Inactive == null).ToList();
                }
            }
            catch (Exception)
            {
            }

            return(_retVal);
        }
Example #16
0
        public static bool UpdatePermission(PERMISSION _permission)
        {
            bool _retVal = false;

            try
            {
                using (GovHistoryDbContext db = new GovHistoryDbContext())
                {
                    db.Entry(_permission).State = EntityState.Modified;
                    db.SaveChanges();
                    _retVal = true;
                }
            }
            catch (Exception)
            {
            }
            return(_retVal);
        }
Example #17
0
        public static List <ApplicationUser> GetUsers(int start, int limit)
        {
            List <ApplicationUser> _retVal = null;

            try
            {
                using (GovHistoryDbContext db = new GovHistoryDbContext())
                {
                    _retVal = db.Users.OrderBy(r => r.Lastname).ThenBy(r => r.Firstname).Skip(start).Take(limit).ToList();
                }
            }
            catch (Exception)
            {
                throw;
            }

            return(_retVal);
        }
Example #18
0
        public static bool DeletePermission(int _permissionId)
        {
            bool _retVal = false;

            try
            {
                using (GovHistoryDbContext db = new GovHistoryDbContext())
                {
                    PERMISSION _permission = db.PERMISSIONS.Where(p => p.PermissionId == _permissionId).Include("ROLES").FirstOrDefault();

                    _permission.ROLES.Clear();
                    db.Entry(_permission).State = EntityState.Deleted;
                    db.SaveChanges();
                    _retVal = true;
                }
            }
            catch (Exception)
            {
            }
            return(_retVal);
        }
Example #19
0
        public static bool DeleteRole(int _roleId)
        {
            bool _retVal = false;

            try
            {
                using (GovHistoryDbContext db = new GovHistoryDbContext())
                {
                    ApplicationRole _role2Delete = db.Roles.Where(p => p.Id == _roleId).Include("PERMISSIONS").FirstOrDefault();
                    if (_role2Delete != null)
                    {
                        _role2Delete.PERMISSIONS.Clear();
                        db.Entry(_role2Delete).State = EntityState.Deleted;
                        db.SaveChanges();
                        _retVal = true;
                    }
                }
            }
            catch (Exception)
            {
            }
            return(_retVal);
        }
Example #20
0
        public static bool DeleteUser(int _userId)
        {
            bool _retVal = false;

            try
            {
                using (GovHistoryDbContext db = new GovHistoryDbContext())
                {
                    //ApplicationUser _user = db.Users.Where(p => p.Id == _userId).Include("ROLES").FirstOrDefault();
                    ApplicationUser _user = GetUser(db, _userId);

                    _user.Roles.Clear();
                    db.Entry(_user).State = EntityState.Deleted;
                    db.SaveChanges();

                    _retVal = true;
                }
            }
            catch (Exception)
            {
            }
            return(_retVal);
        }
Example #21
0
 public ApplicationRoleStore(GovHistoryDbContext context)
     : base(context)
 {
 }