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);
        }