Esempio n. 1
0
        public static bool AddAllPermissions2Role(int _roleId)
        {
            bool _retVal = false;

            try
            {
                using (RBACDbContext db = new RBACDbContext())
                {
                    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);
        }
Esempio n. 2
0
    private static void AddEvent(AUDITEVENT EventItem)
    {
        if (!IsAduitEnabled())
        {
            return;
        }

        string IPAddress = GetIPAddress();

        if (EventItem.IPAddress == "::1")
        {
            IPAddress = "localhost";
        }

        using (RBACDbContext db = new RBACDbContext())
        {
            AUDITEVENT Event = new AUDITEVENT();
            Event.EventType       = EventItem.EventType;
            Event.Description     = EventItem.Description;
            Event.MemberEmail     = EventItem.MemberEmail;
            Event.Details         = EventItem.Details;
            Event.IPAddress       = IPAddress;
            Event.Created         = System.DateTime.Now;
            db.Entry(Event).State = EntityState.Added;
            db.SaveChanges();
        }
    }
Esempio n. 3
0
        public static bool UpdateUser(UserViewModel _user)
        {
            bool _retVal = false;

            try
            {
                using (RBACDbContext db = new RBACDbContext())
                {
                    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);
        }
Esempio n. 4
0
        public static bool RemovePermission4Role(int _roleId, int _permissionId)
        {
            bool _retVal = false;

            try
            {
                using (RBACDbContext db = new RBACDbContext())
                {
                    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)
            {
            }
            return(_retVal);
        }
Esempio n. 5
0
        public static bool RemoveUser4Role(int _userId, int _roleId)
        {
            bool _retVal = false;

            try
            {
                using (RBACDbContext db = new RBACDbContext())
                {
                    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);
        }
Esempio n. 6
0
        public static bool AddUser2Role(int _userId, int _roleId)
        {
            bool _retVal = false;

            try
            {
                using (RBACDbContext db = new RBACDbContext())
                {
                    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);
        }
Esempio n. 7
0
        public static bool AddPermission2Role(int _roleId, int _permissionId)
        {
            bool _retVal = false;

            try
            {
                using (RBACDbContext db = new RBACDbContext())
                {
                    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);
        }
Esempio n. 8
0
        public static bool UpdateRole(RoleViewModel _modifiedRole)
        {
            bool _retVal = false;

            try
            {
                using (RBACDbContext db = new RBACDbContext())
                {
                    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);
        }
Esempio n. 9
0
    public void RefreshAppParameters()
    {
        HttpContext.Current.Application.Lock();
        RBACDbContext database = new RBACDbContext();

        HttpContext.Current.Application["Parameters"] = database.PARAMETERS.ToList();
        HttpContext.Current.Application.UnLock();
    }
Esempio n. 10
0
        public static ApplicationUser GetUser(RBACDbContext 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);
        }
Esempio n. 11
0
        public static List <PERMISSION> GetPermissions()
        {
            List <PERMISSION> _retVal = null;

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

            try
            {
                using (RBACDbContext db = new RBACDbContext())
                {
                    _retVal = db.PERMISSIONS.Where(p => p.PermissionId == _permissionid).Include("ROLES").FirstOrDefault();
                }
            }
            catch (Exception)
            {
            }
            return(_retVal);
        }
Esempio n. 13
0
        public static List <ApplicationUser> GetUsers4Surname(string _surname)
        {
            List <ApplicationUser> _retVal = null;

            try
            {
                using (RBACDbContext db = new RBACDbContext())
                {
                    _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);
        }
Esempio n. 14
0
        public static List <ApplicationRole> GetRoles4SelectList()
        {
            List <ApplicationRole> _retVal = null;

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

            return(_retVal);
        }
Esempio n. 15
0
        public static List <ApplicationUser> GetUsers4SelectList()
        {
            List <ApplicationUser> _retVal = null;

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

            return(_retVal);
        }
Esempio n. 16
0
        public static bool UpdatePermission(PERMISSION _permission)
        {
            bool _retVal = false;

            try
            {
                using (RBACDbContext db = new RBACDbContext())
                {
                    db.Entry(_permission).State = EntityState.Modified;
                    db.SaveChanges();
                    _retVal = true;
                }
            }
            catch (Exception)
            {
            }
            return(_retVal);
        }
Esempio n. 17
0
        /*public static PERMISSION GetPermission4Description(string _permDescription)
         * {
         *  PERMISSION _retVal = null;
         *  try
         *  {
         *      using (RBACDbContext db = new RBACDbContext())
         *      {
         *          _retVal = db.PERMISSIONS.Where(p => p.PermissionDescription == _permDescription).Include("ROLES").FirstOrDefault();
         *      }
         *  }
         *  catch (Exception)
         *  {
         *  }
         *  return _retVal;
         * }*/


        public static bool AddPermission(PERMISSION _newPermission)
        {
            bool _retVal = false;

            try
            {
                using (RBACDbContext db = new RBACDbContext())
                {
                    db.PERMISSIONS.Add(_newPermission);
                    db.Entry(_newPermission).State = EntityState.Added;
                    db.SaveChanges();
                    _retVal = true;
                }
            }
            catch (Exception)
            {
            }
            return(_retVal);
        }
Esempio n. 18
0
        public static bool DeletePermission(int _permissionId)
        {
            bool _retVal = false;

            try
            {
                using (RBACDbContext db = new RBACDbContext())
                {
                    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);
        }
Esempio n. 19
0
        public static bool DeleteUser(int _userId)
        {
            bool _retVal = false;

            try
            {
                using (RBACDbContext db = new RBACDbContext())
                {
                    //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);
        }
Esempio n. 20
0
        public static bool DeleteRole(int _roleId)
        {
            bool _retVal = false;

            try
            {
                using (RBACDbContext db = new RBACDbContext())
                {
                    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);
        }
Esempio n. 21
0
 public ApplicationUserStore(RBACDbContext context)
     : base(context)
 {
 }
Esempio n. 22
0
 public ApplicationRoleStore(RBACDbContext context)
     : base(context)
 {
 }