LoginController.java 1.98 KB
package etunicorn.controller;

import etunicorn.RestrictedTo;
import etunicorn.entity.Personne;
import etunicorn.entity.Session;
import etunicorn.generated.model.UpdateLoginRequest;
import etunicorn.repository.PersonneRepository;
import etunicorn.service.SessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * etunicorn-server
 * Copyright © 2017 Le Club Info Polytech Lille
 * Tous droits réservés
 */
@RestController
public class LoginController extends BaseController implements etunicorn.generated.LoginController {
    @Autowired
    HttpServletRequest request;
    @Autowired
    private PersonneRepository personneRepository;
    @Autowired
    private SessionService sessionService;

    @Override
    @RestrictedTo(authentifie = false)
    public ResponseEntity<?> updateLogin(@Valid @RequestBody UpdateLoginRequest updateLoginRequest) {
        Personne personne = personneRepository.findByLogin(updateLoginRequest.getLogin());
        if (personne == null) {
            return generateError(HttpStatus.UNAUTHORIZED);
        }

        // TODO Vraie vérification du mot de passe
        if (!updateLoginRequest.getPassword().equals("test")) {
            return generateError(HttpStatus.UNAUTHORIZED);
        }

        Session session = sessionService.createSession(personne);

        return new ResponseEntity<Object>(session, HttpStatus.OK);
    }

    @Override
    @RestrictedTo(authentifie = false)
    public ResponseEntity<?> deleteLogin() {
        Session session = sessionService.getSession(request);
        if (session != null) {
            sessionService.deleteSession(session);
        }
        return new ResponseEntity<Object>(HttpStatus.NO_CONTENT);
    }


}