コード例 #1
0
  @Override
  public String add(RoledUser user) throws ServiceException {
    // Assert
    if (user == null) throw new ServiceException("Wrong parameter");
    if (!(user instanceof GreenUser)) throw new ServiceException("Wrong parameter");
    if (user.getUserName() == null || user.getUserName().trim().equals(""))
      throw new ServiceException("Username not defined");
    if (user.getPassword() == null || user.getPassword().trim().equals(""))
      throw new ServiceException("Password not defined");

    // Save
    GreenUser u = new GreenUser();
    u.setUsername(user.getUserName());
    MD5 md5 = new MD5();
    try {
      md5.Update(user.getPassword(), null);
    } catch (UnsupportedEncodingException e) {
      throw new ServiceException(e);
    }
    String hash = md5.asHex();
    String encPass = hash;
    u.setPassword(encPass);
    u.setEnabled(0);
    u.setName(((GreenUser) user).getName());
    u.setLastname(((GreenUser) user).getLastname());
    String newIndex = CodeGenerator.createCode();
    u.setId(newIndex);
    Session session = getCurrentSession();
    String userId = (String) session.save(u);
    return userId;
  }
コード例 #2
0
  @Override
  public GreenUser active(String id, Boolean active) throws ServiceException {
    // Assert
    if (id == null) throw new ServiceException("id not defined");

    // Logic
    Session session = getCurrentSession();
    GreenUser u = (GreenUser) session.get(GreenUser.class, id);

    if (u != null) {
      if (active) {
        u.setEnabled(1);
      } else {
        u.setEnabled(0);
      }
    }
    session.update(u);
    return u;
  }