-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
1 parent
40d59e0
commit 77f5498
Showing
4 changed files
with
275 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
webapp/src/main/java/ar/edu/itba/paw/webapp/controllers/SessionController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
webapp/src/main/java/ar/edu/itba/paw/webapp/models/StudentSessionDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
webapp/src/main/java/ar/edu/itba/paw/webapp/models/UserSessionDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |