Search in sources :

Example 1 with User

use of com.vsct.vboard.models.User in project vboard by voyages-sncf-technologies.

the class AuthenticationController method ensureUserHasRightsToAlterPin.

// Check whether the user has the authorization to do that action (the author or an admins)
public void ensureUserHasRightsToAlterPin(String pinAuthor) {
    final User sessionUser = this.getSessionUser();
    final String userString = sessionUser.getUserString();
    if (!(userString.equals(pinAuthor) || sessionUser.isAdmin() || hasModeratorRole())) {
        throw new VBoardException("Unauthorized Access - User cannot update nor delete pins: " + userString);
    }
}
Also used : User(com.vsct.vboard.models.User) VBoardException(com.vsct.vboard.models.VBoardException)

Example 2 with User

use of com.vsct.vboard.models.User in project vboard by voyages-sncf-technologies.

the class AuthenticationController method ensureUserHasRightsToAlterComment.

// Check whether the user has the authorization to do that action (the author or an admins)
public void ensureUserHasRightsToAlterComment(String commentAuthor) {
    final User sessionUser = this.getSessionUser();
    final String userString = sessionUser.getUserString();
    if (!(userString.equals(commentAuthor) || sessionUser.isAdmin() || this.getSessionUser().getEmail().equals(commentAuthor) || hasModeratorRole())) {
        throw new VBoardException("Unauthorized Access - The user does not have the authorization to do that action(" + userString + ")");
    }
}
Also used : User(com.vsct.vboard.models.User) VBoardException(com.vsct.vboard.models.VBoardException)

Example 3 with User

use of com.vsct.vboard.models.User in project vboard by voyages-sncf-technologies.

the class UsersController method updateUser.

@RequestMapping(value = "/update", method = RequestMethod.POST)
@ResponseBody
@Valid
public // Parsing the params in the JSON body requires using a dedicated @RequestBody annotated class instead of simple @RequestParam arguments
User updateUser(@Valid @RequestBody UserParamsUpdate params) {
    permission.ensureEmailMatchesSessionUser(params.getEmail());
    this.logger.debug("Updating user {}", params.getEmail());
    final String email = params.getEmail();
    final String team = params.getTeam();
    final User user = this.userDAO.findByEmail(email);
    List<String> previousList = Arrays.asList(user.getTeam().split(","));
    List<String> newList = Arrays.asList(team.split(","));
    List<String> removedTeam = new ArrayList<>();
    if (!user.getTeam().isEmpty()) {
        for (String t : previousList) {
            if (!newList.contains(t)) {
                removedTeam.add(t);
            }
        }
    }
    if (!removedTeam.isEmpty()) {
        for (String t : removedTeam) {
            teamsController.removeMember(t, permission.getSessionUser().getUserString());
        }
    }
    user.setTeam(team);
    // unchanged means that the avatar has not been changed by the user and thus no need to change it
    if (!"unchanged".equals(params.getAvatar())) {
        user.setHasCustomAvatar(!"default".equals(params.getAvatar()));
        uploadsManager.saveAvatar(params.getAvatar(), email);
    }
    final String info = params.getInfo();
    user.setInfo(info);
    user.setReceiveNlEmails(params.isReceiveNlEmails());
    user.setReceiveLeaderboardEmails(params.isReceiveLeaderboardEmails());
    user.setReceivePopularPinsEmails(params.isReceivePopularPins());
    user.setReceiveRecapEmails(params.isReceiveRecapEmails());
    try {
        this.logger.debug("User updated: email={} - team={} - info={}", email, team, info);
        this.userDAO.save(user);
    } catch (UnexpectedRollbackException e) {
        throw new VBoardException(e.getMessage(), e.getMostSpecificCause());
    }
    return user;
}
Also used : User(com.vsct.vboard.models.User) VBoardException(com.vsct.vboard.models.VBoardException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) Valid(javax.validation.Valid)

Example 4 with User

use of com.vsct.vboard.models.User in project vboard by voyages-sncf-technologies.

the class UsersController method updateFavoriteLabels.

@RequestMapping(value = "/favoriteLabels", method = RequestMethod.POST)
@ResponseBody
@Valid
public User updateFavoriteLabels(@Valid @RequestBody String labels) {
    User user = permission.getSessionUserWithSyncFromDB();
    labels = JavaUtils.extractJSONObject(labels, "labels");
    user.setFavoriteLabels(labels);
    try {
        this.userDAO.save(user);
        this.logger.debug("User {} updated its favorite labels: {}", user.getNiceName(), labels);
    } catch (UnexpectedRollbackException e) {
        throw new VBoardException(e.getMessage(), e.getMostSpecificCause());
    }
    return user;
}
Also used : User(com.vsct.vboard.models.User) VBoardException(com.vsct.vboard.models.VBoardException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) Valid(javax.validation.Valid)

Example 5 with User

use of com.vsct.vboard.models.User in project vboard by voyages-sncf-technologies.

the class UserControllerTest method teamGetAll.

@Test
public void teamGetAll() {
    Set<String> teams = userController.getTeams();
    Assert.assertEquals(teams.size(), 0);
    this.userDAO.save(new User("email", "fname", "lname", false, "team1", "info"));
    teams = userController.getTeams();
    Assert.assertEquals(teams.size(), 1);
    this.userDAO.save(new User("email2", "fname", "lname", false, "team1", "info"));
    teams = userController.getTeams();
    Assert.assertEquals(teams.size(), 1);
    this.userDAO.save(new User("email3", "fname", "lname", false, "team2", "info"));
    teams = userController.getTeams();
    Assert.assertEquals(teams.size(), 2);
    this.userDAO.save(new User("email4", "fname", "lname", false, "team1,team2,team3", "info"));
    teams = userController.getTeams();
    Assert.assertEquals(teams.size(), 3);
    this.userDAO.save(new User("email5", "fname", "lname", false, "team4,team5,team6", "info"));
    teams = userController.getTeams();
    Assert.assertEquals(teams.size(), 6);
}
Also used : User(com.vsct.vboard.models.User) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

User (com.vsct.vboard.models.User)20 Valid (javax.validation.Valid)12 VBoardException (com.vsct.vboard.models.VBoardException)8 Test (org.junit.Test)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)5 DateTime (org.joda.time.DateTime)3 Comment (com.vsct.vboard.models.Comment)2 Pin (com.vsct.vboard.models.Pin)2 UserParams (com.vsct.vboard.parameterFormat.UserParams)1 UserParamsUpdate (com.vsct.vboard.parameterFormat.UserParamsUpdate)1 NotNull (javax.validation.constraints.NotNull)1 KeycloakPrincipal (org.keycloak.KeycloakPrincipal)1 IDToken (org.keycloak.representations.IDToken)1 Authentication (org.springframework.security.core.Authentication)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1