Search in sources :

Example 1 with User

use of io.irontest.models.User in project irontest by zheng-wang.

the class UserResource method delete.

@DELETE
@Path("{userId}")
@RolesAllowed(IronTestConstants.USER_ROLE_ADMIN)
public void delete(@PathParam("userId") long userId) {
    User user = userDAO.findById(userId);
    if (user != null && IronTestConstants.SYSADMIN_USER.equals(user.getUsername())) {
        throw new RuntimeException("Can not delete " + IronTestConstants.SYSADMIN_USER);
    }
    userDAO.deleteById(userId);
}
Also used : User(io.irontest.models.User) RolesAllowed(javax.annotation.security.RolesAllowed)

Example 2 with User

use of io.irontest.models.User in project irontest by zheng-wang.

the class ResourceAuthenticator method authenticate.

@Override
public Optional<SimplePrincipal> authenticate(BasicCredentials credentials) {
    User user = userDAO.findByUsername(credentials.getUsername());
    if (user != null && user.getPassword().equals(PasswordUtils.hashPassword(credentials.getPassword(), user.getSalt()))) {
        SimplePrincipal principal = new SimplePrincipal(credentials.getUsername());
        principal.getRoles().addAll(user.getRoles());
        return Optional.of(principal);
    }
    return Optional.absent();
}
Also used : User(io.irontest.models.User)

Example 3 with User

use of io.irontest.models.User in project irontest by zheng-wang.

the class UserResource method updatePassword.

@PUT
@Path("{userId}/password")
@PermitAll
public void updatePassword(@PathParam("userId") long userId, @QueryParam("newPassword") String newPassword, @Context SecurityContext context) {
    SimplePrincipal principal = (SimplePrincipal) context.getUserPrincipal();
    User user = userDAO.findByUsername(principal.getName());
    if (user.getId() == userId) {
        userDAO.updatePassword(userId, newPassword);
    } else {
        throw new RuntimeException("You can't change other user's password.");
    }
}
Also used : User(io.irontest.models.User) SimplePrincipal(io.irontest.auth.SimplePrincipal) PermitAll(javax.annotation.security.PermitAll)

Example 4 with User

use of io.irontest.models.User in project irontest by zheng-wang.

the class UserMapper method map.

@Override
public User map(int index, ResultSet rs, StatementContext ctx) throws SQLException {
    List<String> fields = IronTestUtils.getFieldsPresentInResultSet(rs);
    User user = new User();
    user.setId(rs.getLong("id"));
    user.setUsername(rs.getString("username"));
    user.setPassword(fields.contains("password") ? rs.getString("password") : null);
    user.setSalt(fields.contains("salt") ? rs.getString("salt") : null);
    if (fields.contains("roles") && rs.getString("roles") != null) {
        try {
            user.getRoles().addAll(new ObjectMapper().readValue(rs.getString("roles"), HashSet.class));
        } catch (IOException e) {
            throw new SQLException("Failed to deserialize roles JSON.", e);
        }
    }
    return user;
}
Also used : User(io.irontest.models.User) SQLException(java.sql.SQLException) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet)

Aggregations

User (io.irontest.models.User)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SimplePrincipal (io.irontest.auth.SimplePrincipal)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 HashSet (java.util.HashSet)1 PermitAll (javax.annotation.security.PermitAll)1 RolesAllowed (javax.annotation.security.RolesAllowed)1