RoleController.java 4.41 KB
package etunicorn;


import etunicorn.generated.model.UpdateRoleByIdRequest;
import etunicorn.generated.model.UpdateRoleRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

/**
 * etunicorn-server
 * Copyright © 2017 Le Club Info Polytech Lille
 * Tous droits réservés
 */
@RestController
public class RoleController extends BaseController implements etunicorn.generated.RoleController {
    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private PermissionRepository permissionRepository;

    @Override
    public ResponseEntity<?> getRole() {
        return new ResponseEntity<List>((List) roleRepository.findAll(), HttpStatus.OK);
    }

    @Override
    @RestrictedTo("ROLE_ADD")
    public ResponseEntity<?> updateRole(@Valid @RequestBody UpdateRoleRequest updateRoleRequest) {
        return null;
    }
    public ResponseEntity<?> updateRole(@RequestParam String nom) {
        Role oldRole = roleRepository.findByNom(nom);
        if (oldRole != null) {
            return new ResponseEntity<Object>(HttpStatus.CONFLICT);
        }
        Role role = new Role();
        role.setNom(nom);
        try {
            roleRepository.save(role);
        } catch (DataIntegrityViolationException e) {
            return new ResponseEntity<Object>(HttpStatus.CONFLICT);
        }
        return new ResponseEntity<Object>(role, HttpStatus.CREATED);
    }

    @Override
    @RestrictedTo("ROLE_DELETE")
    public ResponseEntity<?> deleteRoleById(@PathVariable BigDecimal nomRole) {
        return null;
    }
    public ResponseEntity<?> deleteRoleById(@PathVariable String nomRole) {
        Role role = roleRepository.findByNom(nomRole);
        if (role == null) {
            return new ResponseEntity<Object>("Rôle inconnu", HttpStatus.NOT_FOUND);
        }
        roleRepository.delete(role);
        return new ResponseEntity<Object>(HttpStatus.NO_CONTENT);
    }

    @Override
    @RestrictedTo("ROLE_PERMISSION_ADD")
    public ResponseEntity<?> updateRoleById(@PathVariable BigDecimal nomRole, @Valid @RequestBody UpdateRoleByIdRequest updateRoleByIdRequest) {
        return null;
    }
    public ResponseEntity<?> updateRoleById(@PathVariable String nomRole, @RequestParam String nom) {
        Role role = roleRepository.findByNom(nomRole);
        if (role == null) {
            return new ResponseEntity<Object>("Rôle inconnu", HttpStatus.NOT_FOUND);
        }
        Permission permission = permissionRepository.findByNom(nom);
        if (permission == null) {
            return new ResponseEntity<Object>("Permission inconnue", HttpStatus.NOT_FOUND);
        }
        role.addPermission(permission);
        try {
            roleRepository.save(role);
        } catch (DataIntegrityViolationException e) {
            // Si la permission était déjà là, on fait rien
        }
        return new ResponseEntity<Object>(role, HttpStatus.ACCEPTED);
    }

    @Override
    @RestrictedTo("ROLE_PERMISSION_REMOVE")
    public ResponseEntity<?> deleteRoleByNomPermission(@PathVariable String nomPermission, @PathVariable BigDecimal nomRole) {
        return null;
    }
    public ResponseEntity<?> deleteRoleByNomPermission(@PathVariable String nomPermission, @PathVariable String nomRole) {
        Role role = roleRepository.findByNom(nomRole);
        if (role == null) {
            return new ResponseEntity<Object>("Rôle inconnu", HttpStatus.NOT_FOUND);
        }
        Permission permission = permissionRepository.findByNom(nomPermission);
        if (permission == null) {
            return new ResponseEntity<Object>("Permission inconnue", HttpStatus.NOT_FOUND);
        }
        role.delPermission(permission);
        roleRepository.save(role);
        return new ResponseEntity<Object>(role, HttpStatus.ACCEPTED);
    }

    @Override
    @RestrictedTo("ROLE_PERMISSION_LIST")
    public ResponseEntity<?> getPermission() {
        return new ResponseEntity<List>((List) permissionRepository.findAll(), HttpStatus.OK);
    }
}