Exemplo n.º 1
0
  /**
   * Set up a temporary password for the user
   *
   * <p>User will have to reset the temporary password next time they log in.
   *
   * @param id User id
   * @param pass A Temporary password
   */
  @Path("{id}/reset-password")
  @PUT
  @Consumes(MediaType.APPLICATION_JSON)
  public void resetPassword(@PathParam("id") String id, CredentialRepresentation pass) {
    auth.requireManage();

    UserModel user = session.users().getUserById(id, realm);
    if (user == null) {
      throw new NotFoundException("User not found");
    }
    if (pass == null
        || pass.getValue() == null
        || !CredentialRepresentation.PASSWORD.equals(pass.getType())) {
      throw new BadRequestException("No password provided");
    }

    UserCredentialModel cred = RepresentationToModel.convertCredential(pass);
    try {
      session.users().updateCredential(realm, user, cred);
    } catch (IllegalStateException ise) {
      throw new BadRequestException("Resetting to N old passwords is not allowed.");
    } catch (ModelReadOnlyException mre) {
      throw new BadRequestException("Can't reset password as account is read only");
    }
    if (pass.isTemporary() != null && pass.isTemporary())
      user.addRequiredAction(UserModel.RequiredAction.UPDATE_PASSWORD);

    adminEvent.operation(OperationType.ACTION).resourcePath(uriInfo).success();
  }