示例#1
0
        public IActionResult CapabilitiesList(int userId)
        {
            if (userId == 0)
            {
                return(R.Fail.WithError(ErrorCodes.ParentEntityMustBeNonZero).Result);
            }

            User user = null;

            if (userId < 0 || (user = _userService.Get(userId)) == null)
            {
                return(NotFound());
            }

            var roleIds               = user.Roles.Select(x => x.Id).ToArray();
            var roleCapabilities      = _capabilityService.GetByRolesConsolidated(roleIds).OrderBy(x => x.Name);
            var userCapabilities      = _capabilityService.GetByUser(userId);
            var allCapabilities       = _capabilityService.Get(out int _, x => true, x => x.Name);
            var availableCapabilities = allCapabilities.Where(x => roleCapabilities.All(y => y.Id != x.Id)).ToList();

            var activeCapabilityModels = roleCapabilities.Select(_roleModelFactory.Create);

            var availableCapabilitiesModel = availableCapabilities.Select(x =>
            {
                var model    = _roleModelFactory.Create(x);
                model.Active = userCapabilities?.Any(y => y.Id == x.Id) ?? false;
                return(model);
            }).ToList();
            var roleModels = user.Roles.Select(_roleModelFactory.Create);

            return(R.Success.With("capabilities", activeCapabilityModels)
                   .With("availableCapabilities", availableCapabilitiesModel)
                   .With("roles", roleModels).Result);
        }
示例#2
0
        public IActionResult RoleEditor(int roleId)
        {
            var role = roleId > 0 ? _roleService.Get(roleId) : new Role();

            if (role == null)
            {
                return(NotFound());
            }
            var roleModel = _roleModelFactory.Create(role);

            roleModel.Capabilities = role.Capabilities?.Select(x => x.Id.ToString()).ToList();
            var availableCapabilities      = _capabilityService.Get(x => true).OrderBy(x => x.Name).ToList();
            var availableCapabilitiesModel =
                SelectListHelper.GetSelectItemList(availableCapabilities, x => x.Id, x => x.Name);

            return(R.Success.With("role", roleModel).With("availableCapabilities", availableCapabilitiesModel).Result);
        }