Пример #1
0
        public async Task <IActionResult> UpdateRole(RoleUpdateRequest request)
        {
            //Recupero l'elemento dal business layer
            var entity = AuthorizationLayer.GetRole(request.RoleId);

            //modifica solo se admin o se utente richiedente è lo stesso che ha creato
            if (entity == null)
            {
                return(NotFound());
            }

            //Aggiornamento dell'entità
            entity.Name        = request.Name;
            entity.Description = request.Description;

            //Salvataggio
            var validations = await AuthorizationLayer.UpdateRole(entity, PlatformUtils.GetIdentityUserId(User));

            if (validations.Count > 0)
            {
                return(BadRequest(validations));
            }


            var permissions = AuthorizationLayer.FetchPermissionsOnRole(entity.Id);

            //Confermo
            return(Ok(ContractUtils.GenerateContract(entity, permissions)));
        }
Пример #2
0
        public async Task <IActionResult> CreateUserRole(UserRoleCreateRequest request)
        {
            //Recupero l'elemento dal business layer
            var user = BasicLayer.GetShooter(request.UserId);

            if (user == null)
            {
                return(NotFound($"User with {request.UserId} not found"));
            }

            //Recupero l'elemento dal business layer
            var role = AuthorizationLayer.GetRole(request.RoleId);

            if (role == null)
            {
                return(NotFound($"Role with {request.RoleId} not found"));
            }

            var entity = new UserRole()
            {
                UserId   = user.Id,
                RoleId   = role.Id,
                EntityId = request.EntityId
            };
            //Invocazione del service layer
            var validations = await AuthorizationLayer.CreateUserRole(entity, PlatformUtils.GetIdentityUserId(User));

            if (validations.Count > 0)
            {
                return(BadRequest(validations));
            }

            var userRoles = AuthorizationLayer.FetchUserRole(role.Id);

            var userIds = userRoles.Select(x => x.UserId).ToList();

            var users = BasicLayer.FetchShootersByIds(userIds);

            //Return contract
            return(Ok(userRoles.As(x => ContractUtils.GenerateContract(x, users?.FirstOrDefault(s => s.Id == x.UserId)))));
        }
Пример #3
0
        public Task <IActionResult> GetRole(RoleRequest request)
        {
            var entity = AuthorizationLayer.GetRole(request.RoleId);

            //verifico validità dell'entità
            if (entity == null)
            {
                return(Task.FromResult <IActionResult>(NotFound()));
            }

            var permissions = AuthorizationLayer.FetchPermissionsOnRole(entity.Id);

            var userRoles = AuthorizationLayer.FetchUserRole(entity.Id);

            var userIds = userRoles.Select(x => x.UserId).ToList();

            var users = BasicLayer.FetchShootersByIds(userIds);

            //Serializzazione e conferma
            return(Reply(ContractUtils.GenerateContract(entity, permissions, userRoles, users)));
        }
Пример #4
0
        public async Task <IActionResult> DeleteRole(RoleRequest request)
        {
            //Recupero l'elemento dal business layer
            var entity = AuthorizationLayer.GetRole(request.RoleId);

            //Se l'utente non hai i permessi non posso rimuovere entità con userId nullo
            if (entity == null)
            {
                return(NotFound());
            }

            //Invocazione del service layer
            var validations = await AuthorizationLayer.DeleteRole(entity, PlatformUtils.GetIdentityUserId(User));

            if (validations.Count > 0)
            {
                return(BadRequest(validations));
            }

            //Return contract
            return(Ok(ContractUtils.GenerateContract(entity)));
        }
Пример #5
0
        public async Task <IActionResult> CreatePermissionOnRole(RolePermissionCreateRequest request)
        {
            //Recupero l'elemento dal business layer
            var permission = AuthorizationLayer.GetPermission(request.PermissionId);

            if (permission == null)
            {
                return(NotFound($"Permission with {request.PermissionId} not found"));
            }

            //Recupero l'elemento dal business layer
            var role = AuthorizationLayer.GetRole(request.RoleId);

            if (role == null)
            {
                return(NotFound($"Role with {request.RoleId} not found"));
            }

            var entity = new PermissionRole()
            {
                PermissionId = permission.Id,
                RoleId       = role.Id
            };
            //Invocazione del service layer
            var validations = await AuthorizationLayer.CreatePermissionRole(entity, PlatformUtils.GetIdentityUserId(User));

            if (validations.Count > 0)
            {
                return(BadRequest(validations));
            }

            var permissions = AuthorizationLayer.FetchPermissionsOnRole(role.Id);

            //Return contract
            return(Ok(permissions.As(ContractUtils.GenerateContract)));
        }