//don't get hidden routes if it's set to false.
        public static List <DanpheRoute> GetRoutesForUser(int userId, bool getHiearrchy)
        {
            List <DanpheRoute> allRoutes = new List <DanpheRoute>();

            List <RbacPermission> userAllPerms = GetUserAllPermissions(userId);

            allRoutes = (from route in RBAC.GetAllRoutes()
                         join perm in userAllPerms
                         on route.PermissionId equals perm.PermissionId
                         where route.IsActive == true
                         select route).Distinct().OrderBy(r => r.DisplaySeq).ToList();

            if (getHiearrchy)
            {
                //don't get hidden routes if it's set to false.
                List <DanpheRoute> parentRoutes = allRoutes.Where(a => a.ParentRouteId == null && a.DefaultShow == true).ToList();

                foreach (var route in parentRoutes)
                {
                    route.ChildRoutes = GetChildRouteHierarchy(allRoutes, route);
                }

                return(parentRoutes);
            }
            else
            {
                return(allRoutes.ToList());
            }
        }
Exemple #2
0
        static void TestRoutes()
        {
            RbacDbContext dbContext = new RbacDbContext(connStr);

            List <DanpheRoute> allUserRoutes = RBAC.GetRoutesForUser(11);

            //below works fine..
            //List<RbacUser> allUsers = dbContext.Users.ToList();
            //List<RbacApplication> applications = dbContext.Applications.ToList();
            //List<RbacPermission> permissions = dbContext.Permissions.ToList();
            //List<RbacRole> roles = dbContext.Roles.ToList();
            //List<DanpheRoute> routes = dbContext.Routes.ToList();
            //List<UserRoleMap> userrolemaps = dbContext.UserRoleMaps.ToList();
            //List<RolePermissionMap> rolePermMaps = dbContext.RolePermissionMaps.ToList();
        }
        public static bool IsValidUser(string userName, string password)
        {
            //username is not case-sensitive but password is
            List <RbacUser> allUsrs = RBAC.GetAllUsers();
            RbacUser        usr     = allUsrs.Where(a => a.UserName.ToLower() == userName.ToLower() && a.Password == a.Password)
                                      .Select(a => a).FirstOrDefault();

            if (usr != null)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public static List <RbacRole> GetUserAllRoles(int userid)
        {
            List <RbacRole>    retList       = new List <RbacRole>();
            List <RbacRole>    allRoles      = RBAC.GetAllRoles();
            List <UserRoleMap> allUsrRoleMap = RBAC.GetAllUserRoleMaps();

            //return only roles which are mapped to this user.
            retList = (from role in allRoles
                       join map in allUsrRoleMap
                       on role.RoleId equals map.RoleId
                       where map.UserId == userid
                       select role).Distinct().ToList();



            return(retList);
        }
        public static RbacUser GetUser(int userId)
        {
            //username is not case-sensitive but password is
            List <RbacUser> allUsrs = RBAC.GetAllUsers();
            RbacUser        usr     = allUsrs.Where(a => a.UserId == userId)
                                      .Select(a => a).FirstOrDefault();

            //sending a clone so that my current object won't be modified outside.
            if (usr != null)
            {
                return((RbacUser)usr.Clone());
            }
            //don't clone if user is null (nullreferenceException)
            else
            {
                return(usr);
            }
        }
        public static bool UserHasPermission(int userId, string applicationCode, string permissionName)
        {
            RbacApplication currApplication = RBAC.GetAllApplications()
                                              .Where(a => a.ApplicationCode == applicationCode).FirstOrDefault();

            if (currApplication != null)
            {
                //filter from all permissions of current user.
                List <RbacPermission> userPerms = (from uPerm in RBAC.GetUserAllPermissions(userId)
                                                   where uPerm.PermissionName == permissionName &&
                                                   uPerm.ApplicationId == currApplication.ApplicationId
                                                   select uPerm).ToList();
                if (userPerms != null && userPerms.Count > 0)
                {
                    return(true);
                }
            }
            return(false);
        }
        public static List <RbacPermission> GetUserAllPermissions(int userId)
        {
            List <RbacPermission> retList = (List <RbacPermission>)DanpheCache.Get("RBAC-UserPermissions-UserId" + userId);

            if (retList == null)
            {
                var isUsrSysAdmin = (from usRole in RBAC.GetAllUserRoleMaps()
                                     where usRole.UserId == userId
                                     join role in RBAC.GetAllRoles()
                                     on usRole.RoleId equals role.RoleId
                                     where role.IsSysAdmin == true
                                     select role).Count() > 0;
                //return all permissions if current user is systemadmin.
                if (isUsrSysAdmin)
                {
                    retList = RBAC.GetAllPermissions();
                }
                else
                {
                    retList = (from urole in RBAC.GetAllUserRoleMaps()
                               where urole.UserId == userId && urole.IsActive == true
                               join role in RBAC.GetAllRoles()
                               on urole.RoleId equals role.RoleId
                               join rolePmap in RBAC.GetAllRolePermissionMaps()
                               on urole.RoleId equals rolePmap.RoleId
                               join perm in RBAC.GetAllPermissions()
                               on rolePmap.PermissionId equals perm.PermissionId
                               where rolePmap.IsActive == true
                               join app in RBAC.GetAllApplications()
                               on perm.ApplicationId equals app.ApplicationId
                               where app.IsActive == true
                               select perm).ToList();
                }
                DanpheCache.Add("RBAC-UserPermissions-UserId" + userId, retList, cacheExpiryMinutes);
            }
            return(retList);
        }
        public static RbacUser UpdateDefaultPasswordOfUser(string userName, string password, string confirmpassword)
        {
            RbacDbContext   rbacDbcontxt = new RbacDbContext(connStringName);
            List <RbacUser> alluser      = RBAC.GetAllUsers();
            RbacUser        usr          = alluser.Where(a => a.UserName.ToLower() == userName.ToLower() && a.Password == EncryptPassword(password))
                                           .Select(a => a).FirstOrDefault();

            ////this condition is for that if user has enter wrong current password
            if (usr == null)
            {
                return(null);
            }
            else
            {
                usr.Password                  = EncryptPassword(confirmpassword);
                usr.ModifiedOn                = DateTime.Now;
                usr.ModifiedBy                = usr.EmployeeId;
                usr.NeedsPasswordUpdate       = false;
                rbacDbcontxt.Entry(usr).State = EntityState.Modified;
                rbacDbcontxt.SaveChanges();

                return(usr);
            }
        }