/**
 * @license
 * Copyright 2017 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.nanos.auth;

import foam.core.FObject;
import foam.core.X;
import foam.dao.DAO;
import foam.dao.ProxyDAO;
import foam.util.Password;
import foam.util.SafetyUtil;

import java.util.Calendar;

public class UserPasswordHashingDAO
  extends ProxyDAO
{
  public UserPasswordHashingDAO(X x, DAO delegate) {
    setX(x);
    setDelegate(delegate);
  }

  @Override
  public FObject put_(X x, FObject obj) {
    User user = (User) obj;
    User stored = (User) getDelegate().find(user.getId());

    // hash desired password if provided
    if ( ! SafetyUtil.isEmpty(user.getDesiredPassword()) ) {
      user.setPassword(Password.hash(user.getDesiredPassword()));
      user.setPasswordLastModified(Calendar.getInstance().getTime());
      // set previous password if present
      if ( stored != null && ! SafetyUtil.isEmpty(stored.getPassword()) ) {
        user.setPreviousPassword(stored.getPassword());
      }
    }

    return super.put_(x, obj);
  }
}

/**
 * @license
 * Copyright 2018 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.nanos.auth;

import foam.core.FObject;
import foam.core.X;
import foam.dao.DAO;
import foam.dao.ProxyDAO;
import foam.util.Password;
import foam.util.SafetyUtil;
import java.util.Calendar;

public class UserPasswordHashingDAO
  extends ProxyDAO
{
  public UserPasswordHashingDAO(X x, DAO delegate) {
    setX(x);
    setDelegate(delegate);
  }

  @Override
  public FObject put_(X x, FObject obj) {
    User user   = (User) obj;
    User stored = (User) getDelegate().find(user.getId());

    // hash desired password if provided
    if ( ! SafetyUtil.isEmpty(user.getDesiredPassword()) ) {
      user.setPassword(Password.hash(user.getDesiredPassword()));
      user.setPasswordLastModified(Calendar.getInstance().getTime());
      // set previous password if present
      if ( stored != null && ! SafetyUtil.isEmpty(stored.getPassword()) ) {
        user.setPreviousPassword(stored.getPassword());
      }
    }

    return super.put_(x, obj);
  }
}

Fix copyright year.