Skip to content

Commit

Permalink
Add current user endpoint (#71)
Browse files Browse the repository at this point in the history
- Add endpoint to obtain the info for the current user
- Get information about the logged in user via the authentication token given in the **X-Auth-Header** HTTTP Header. The API endpoint is accessed with a **GET** method through the following resource:

**/api/v1/user**

- The information includes:
  - role (e.g. STUDENT | ADMIN)
  - docket (only in case that Role is STUDENT)
  - firstName
  - lastName
  - genre
  - birthday
  - email
  - dni
  - address (includes city, country, door, neighborhood, number, street)

- Examples
  - Answer when accessing with a STUDENT:

> {
"address": {
"city": "Caba",
"country": "Argentina",
"door": "",
"neighborhood": "Puerto Madero",
"number": 39222222,
"street": "E. Madero"
},
"birthday": "1994-01-24",
"dni": 12345687,
"docket": 1,
"email": "",
"firstName": "Hola1111",
"genre": "M",
"lastName": "Como Estas123123",
"role": "STUDENT"
}

  - Answer when accesing with an ADMIN:

> {
"address": {
"city": "CABA",
"country": "Argentina",
"neighborhood": "Puerto Madero",
"number": 399,
"street": "E. Madero"
},
"birthday": "1994-08-17",
"dni": 38457012,
"email": "mcomercio@bait.edu.ar",
"firstName": "Matias Nicolas",
"genre": "M",
"lastName": "Comercio Vazquez",
"role": "ADMIN"
}
  • Loading branch information
gibarsin authored and MatiasComercio committed Feb 2, 2017
1 parent 40d59e0 commit 77f5498
Show file tree
Hide file tree
Showing 4 changed files with 275 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
import ar.edu.itba.paw.models.*;
import ar.edu.itba.paw.models.users.Admin;
import ar.edu.itba.paw.models.users.Student;
import ar.edu.itba.paw.models.users.User;
import ar.edu.itba.paw.webapp.models.*;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.modelmapper.spi.MatchingStrategy;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Set;
import java.util.stream.Collectors;

public class DTOEntityMapper {

Expand Down Expand Up @@ -77,11 +75,11 @@ FinalInscriptionDTO convertToFinalInscriptionDTO(FinalInscription finalInscripti
return finalInscriptionDTO;
}

public FinalInscriptionIndexDTO convertToFinalInscriptionIndexDTO(FinalInscription finalInscription) {
FinalInscriptionIndexDTO convertToFinalInscriptionIndexDTO(FinalInscription finalInscription) {
return modelMapper.map(finalInscription, FinalInscriptionIndexDTO.class);
}

public FinalInscription convertToFinalInscription(FinalInscriptionDTO finalinscriptionDTO, Course course) {
FinalInscription convertToFinalInscription(FinalInscriptionDTO finalinscriptionDTO, Course course) {
FinalInscription finalinscription = modelMapper.map(finalinscriptionDTO, FinalInscription.class);
finalinscription.setState(FinalInscription.FinalInscriptionState.OPEN);
finalinscription.setId(null);
Expand All @@ -105,4 +103,23 @@ Admin convertToAdmin(final AdminsUpdateDTO adminsUpdateDTO) {
return modelMapper.map(adminsUpdateDTO, Admin.class);
}

UserSessionDTO convertToAdminSessionDTO(final User user) {
final UserSessionDTO userSessionDTO = modelMapper.map(user, UserSessionDTO.class);
final AddressDTO addressDTO = convertToAddressDTO(user.getAddress());

userSessionDTO.setAddress(addressDTO);
userSessionDTO.setRole(Role.ADMIN);

return userSessionDTO;
}

StudentSessionDTO convertToStudentSessionDTO(final User user) {
final StudentSessionDTO userSessionDTO = modelMapper.map(user, StudentSessionDTO.class);
final AddressDTO addressDTO = convertToAddressDTO(user.getAddress());

userSessionDTO.setAddress(addressDTO);
userSessionDTO.setRole(Role.STUDENT);

return userSessionDTO;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package ar.edu.itba.paw.webapp.controllers;

import ar.edu.itba.paw.interfaces.AdminService;
import ar.edu.itba.paw.interfaces.StudentService;
import ar.edu.itba.paw.interfaces.UserService;
import ar.edu.itba.paw.models.Role;
import ar.edu.itba.paw.models.users.Admin;
import ar.edu.itba.paw.models.users.Student;
import ar.edu.itba.paw.webapp.models.StudentSessionDTO;
import ar.edu.itba.paw.webapp.models.UserSessionDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;

@Component
@Path("/user")
public class SessionController {
private final static Logger LOGGER = LoggerFactory.getLogger(SessionController.class);

@Autowired
private DTOEntityMapper mapper;

@Autowired
private AdminService as;

@Autowired
private StudentService ss;

@Autowired
private UserService us;

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response sessionShow() {
final int dni = Integer.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
final List<Role> roles = us.getRole(dni);

if(roles.contains(Role.ADMIN)) {
final Admin admin = as.getByDni(dni);
final UserSessionDTO adminSessionDTO = mapper.convertToAdminSessionDTO(admin);

return Response.ok(adminSessionDTO).build();
} else if(roles.contains(Role.STUDENT)) {
final Student student = ss.getByDni(dni);
final StudentSessionDTO studentSessionDTO = mapper.convertToStudentSessionDTO(student);

return Response.ok(studentSessionDTO).build();
} else {
LOGGER.warn("User {} does not have Role ADMIN or STUDENT", dni);

return Response.serverError().build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package ar.edu.itba.paw.webapp.models;

import ar.edu.itba.paw.models.Role;
import ar.edu.itba.paw.models.users.User;

import java.time.LocalDate;

/**
* Used for returning info about the logged in user with role STUDENT
*
* Inheritance from UserSessionDTO was not used
* due to JAXB returning 'type' field with 'studentSessionDTO' value in the JSON response.
* XmlTransient annotation served useful to remove that field but it also ignored the docket field.
*
* Composition failed to work with modelMapper
*/
public class StudentSessionDTO {
private Integer dni;
private String firstName;
private String lastName;
private User.Genre genre;
private LocalDate birthday;
private String email;
private Role role;
private AddressDTO address;
private int docket;

public StudentSessionDTO() {
}

public Integer getDni() {
return dni;
}

public void setDni(Integer dni) {
this.dni = dni;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public User.Genre getGenre() {
return genre;
}

public void setGenre(User.Genre genre) {
this.genre = genre;
}

public LocalDate getBirthday() {
return birthday;
}

public void setBirthday(LocalDate birthday) {
this.birthday = birthday;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public Role getRole() {
return role;
}

public void setRole(Role role) {
this.role = role;
}

public AddressDTO getAddress() {
return address;
}

public void setAddress(AddressDTO address) {
this.address = address;
}

public int getDocket() {
return docket;
}

public void setDocket(int docket) {
this.docket = docket;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package ar.edu.itba.paw.webapp.models;

import ar.edu.itba.paw.models.Role;
import ar.edu.itba.paw.models.users.User;

import java.time.LocalDate;

/**
* Used for returning info about the logged in user with role ADMIN
*/
public class UserSessionDTO {

private Integer dni;
private String firstName;
private String lastName;
private User.Genre genre;
private LocalDate birthday;
private String email;
private Role role;
private AddressDTO address;

public UserSessionDTO() {
}

public Integer getDni() {
return dni;
}

public void setDni(final Integer dni) {
this.dni = dni;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(final String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(final String lastName) {
this.lastName = lastName;
}

public User.Genre getGenre() {
return genre;
}

public void setGenre(final User.Genre genre) {
this.genre = genre;
}

public LocalDate getBirthday() {
return birthday;
}

public void setBirthday(final LocalDate birthday) {
this.birthday = birthday;
}

public String getEmail() {
return email;
}

public void setEmail(final String email) {
this.email = email;
}

public Role getRole() {
return role;
}

public void setRole(final Role role) {
this.role = role;
}

public AddressDTO getAddress() {
return address;
}

public void setAddress(final AddressDTO address) {
this.address = address;
}
}

0 comments on commit 77f5498

Please sign in to comment.