LoginController.java 1.89 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.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;

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

    @Autowired
    private SessionService sessionService;

    @Override
    @RestrictedTo(authentifie = false)
    public ResponseEntity<?> updateLogin(@Valid @RequestBody UpdateLoginRequest updateLoginRequest) {
        return null;
    }
    public ResponseEntity<?> updateLogin(@RequestParam String login, @RequestParam String password) {
        Personne personne = personneRepository.findByLogin(login);
        if (personne == null) {
            return new ResponseEntity<Object>(HttpStatus.UNAUTHORIZED);
        }

        // TODO Vraie vérification du mot de passe
        if (!password.equals("test")) {
            return new ResponseEntity<Object>(HttpStatus.UNAUTHORIZED);
        }

        Session session = sessionService.createSession(personne);

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


    @Override
    public ResponseEntity<?> deleteLoginByToken(@PathVariable String token) {
        return null;
    }
}