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);
}
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();
}
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.");
}
}
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;
}
Aggregations