public async Task <UserPermission> Handle(GetUserPermissionRequest request, CancellationToken cancellationToken)
        {
            RoleAssignment roleAssignment;

            if (request.RoleAssignmentID == Guid.Empty)
            {
                roleAssignment = dbContext.RoleAssignments.Include(p => p.Role).Include(p => p.Scope)
                                 .Where(p => p.Principal.PrincipalCode == currentUserInfo.UserCode).OrderBy(p => p.Role.SortNO).First();
            }
            else
            {
                roleAssignment = dbContext.RoleAssignments.Include(p => p.Role).Include(p => p.Scope)
                                 .Where(p => p.ID == request.RoleAssignmentID).First();
            }
            if (roleAssignment == null)
            {
                throw new FriendlyException()
                      {
                          ExceptionCode    = (int)HttpStatusCode.NotFound,
                          ExceptionMessage = $"The RoleAssignment id: {request.RoleAssignmentID} does not exist."
                      };
            }

            UserPermission currentUserPermission = new UserPermission();

            currentUserPermission.RoleCode = roleAssignment.Role.RoleCode;

            List <string>       allowCodes = dbContext.RolePermissions.Where(p => p.Role.ID == roleAssignment.RoleID).Select(p => p.ResourceCode).ToList();
            List <ResourceData> resources  = await systemDataCache.GetResourceData();

            var menuCodes   = resources.Where(p => p.ResourceType == EnumResourceType.Menu).Select(p => p.ResourceCode);
            var actionCodes = resources.Where(p => p.ResourceType == EnumResourceType.Action).Select(p => p.ResourceCode);
            var btnCodes    = resources.Where(p => p.ResourceType == EnumResourceType.Button).Select(p => p.ResourceCode);

            currentUserPermission.AllowMenuCodes   = menuCodes.Where(p => allowCodes.Contains(p)).ToList();
            currentUserPermission.AllowActionCodes = actionCodes.Where(p => allowCodes.Contains(p)).ToList();
            currentUserPermission.AllowBtnCodes    = btnCodes.Where(p => allowCodes.Contains(p)).ToList();

            currentUserPermission.ScopeCode       = roleAssignment.Scope.ScopeCode;
            currentUserPermission.AllowScopeCodes = dbContext.Scopes.Where(p => p.ScopeCode.StartsWith(roleAssignment.Scope.ScopeCode)).Select(p => p.ScopeCode).ToList();

            await userPermissionCache.SetCurrentUserPermission(currentUserPermission);

            return(currentUserPermission);
        }
Ejemplo n.º 2
0
        public async Task <List <UserMenu> > Handle(GetUserMenusRequest request, CancellationToken cancellationToken)
        {
            UserPermission userPermission = await userPermissionCache.GetCurrentUserPermission();

            List <ResourceData> resources = await systemDataCache.GetResourceData();

            resources = resources.Where(p => p.ResourceType == EnumResourceType.Menu && userPermission.AllowMenuCodes.Contains(p.ResourceCode)).ToList();

            var rootResource = resources.First(p => p.ID == Guid.Parse("88888888-8888-8888-8888-888888888888"));

            resources.Remove(rootResource);
            List <UserMenu> userMenus = new List <UserMenu>();
            UserMenu        rootMenu  = new UserMenu()
            {
                MenuCode = rootResource.ResourceCode, MenuName = rootResource.ResourceName, SortNO = rootResource.SortNO
            };

            userMenus.Add(rootMenu);
            BuildUserMenus(resources, rootResource, rootMenu);
            return(userMenus);
        }