예제 #1
0
        public async Task <IEnumerable <PermissionDto> > GetAllsync(string name = null, string key = null, string url = null, string targetClientId = null, PermissionType?type = null, IEnumerable <string> allowedClientIds = null, string excludeRoleId = null, IEnumerable <string> excludePermIds = null)
        {
            var permissions = await _permissionRepo.GetAllAsync(name, key, url, targetClientId, type, allowedClientIds, excludeRoleId, excludePermIds);

            return(permissions?.Select(itm => new PermissionDto
            {
                Id = itm.Id,
                Key = itm.Key,
                Name = itm.Name,
                Desc = itm.Description,
                Type = itm.Type,
                ClientId = itm.ClientId,
                ParentId = itm.Parent == null ? "" : itm.Parent.Id,
                Url = itm.Url,
                Icon = itm.Icon,
                Level = itm.Level,
                Order = itm.Order
            }));
        }
예제 #2
0
        public async Task <IEnumerable <RolePermissionDto> > GetPermissionsAsync(string id, bool getAllPermissions = false, IEnumerable <string> allowedClientIds = null)
        {
            var role = await _roleRepo.GetAsync(id, true);

            if (role == null)
            {
                throw new IamException(HttpStatusCode.BadRequest, "角色不存在");
            }

            if (allowedClientIds != null && allowedClientIds.Any() && !allowedClientIds.Contains(role.ClientId))
            {
                throw new IamException(HttpStatusCode.BadRequest, "无权操作");
            }

            if (!getAllPermissions)
            {
                return(role.Permissions.Select(itm => new RolePermissionDto
                {
                    Id = itm.Permission.Id,
                    Key = itm.Permission.Key,
                    Name = itm.Permission.Name,
                    Icon = itm.Permission.Icon,
                    Type = itm.Permission.Type,
                    ParentId = itm.Permission.Parent?.Id,
                    IsOwned = true,
                    Description = itm.Permission.Description,
                    Url = itm.Permission.Url,
                    Order = itm.Permission.Order,
                    Level = itm.Permission.Level
                }));
            }

            var perms = await _permissionRepo.GetAllAsync(allowedClientIds : allowedClientIds);

            var rolePerms = role.Permissions.Select(itm => itm.PermissionId);

            return(perms.Select(itm => new RolePermissionDto
            {
                Id = itm.Id,
                Key = itm.Key,
                Name = itm.Name,
                Icon = itm.Icon,
                Type = itm.Type,
                ParentId = itm.Parent?.Id,
                IsOwned = rolePerms.Any(permId => permId == itm.Id),
                Description = itm.Description,
                Url = itm.Url,
                Order = itm.Order,
                Level = itm.Level,
            }));
        }