public boolean saveUser(BeanFieldGroup<User> fieldGroup, String clearPass) throws FieldGroup.CommitException, BLException { fieldGroup.commit(); BeanItem<User> item = fieldGroup.getItemDataSource(); try { return (boolean) DB.execWithTransaction( (db) -> { User u = item.getBean(); db.save(u); if (clearPass != null && !clearPass.isEmpty()) { UserManager mgr = new UserManager(db); try { mgr.setPassword(u, clearPass); } catch (BLException e) { return false; } addRevisionCreated( db, getEntityName(), item.getItemProperty("id").getValue().toString()); u.setForcePasswordChange(true); db.session().update(u); return true; } return false; }); } catch (Exception e) { getApp().getLog().error(e); return false; } }
@Override public boolean removeEntity(BeanFieldGroup fieldGroup) { // Users have a deleted flag, they are not completely removed. BeanItem item = fieldGroup.getItemDataSource(); User t = (User) item.getBean(); try { return t != null && (boolean) DB.execWithTransaction( (db) -> { User user = db.session().get(User.class, t.getId()); if (user == null) return false; t.setDeleted(true); db.session().merge(t); addRevisionRemoved(db, getEntityName(), String.valueOf(t.getId())); return true; }); } catch (Exception e) { getApp().getLog().error(e); return false; } }
public String resetUserPassword(User user) { String generatedPassword = PasswordGenerator.generateRandomPassword(); try { DB.execWithTransaction( (db) -> { db.session().refresh(user); user.getPasswordhistory(); // hack to avoid LazyInitialization UserManager mgr = new UserManager(db); try { mgr.setPassword(user, generatedPassword); } catch (BLException e) { getApp().displayNotification("errorMessage.resetPassword"); return false; } user.setForcePasswordChange(true); db.session().saveOrUpdate(user); return true; }); } catch (Exception e) { getApp().getLog().error(e); return null; } return generatedPassword; }
public boolean updateUser( BeanFieldGroup<User> fieldGroup, String currentPass, String newClearPass) throws BLException, FieldGroup.CommitException, CloneNotSupportedException { BeanItem<User> old = fieldGroup.getItemDataSource(); Object oldUser = old.getBean().clone(); fieldGroup.commit(); BeanItem<User> item = fieldGroup.getItemDataSource(); User u = item.getBean(); boolean userUpdated = false; try { userUpdated = (boolean) DB.execWithTransaction( (db) -> { User user = (User) db.session().merge(u); UserManager mgr = new UserManager(db); boolean updated = false; if (!newClearPass.isEmpty()) { boolean passwordOK = false; boolean newPasswordOK = false; try { passwordOK = mgr.checkPassword(user, currentPass); newPasswordOK = mgr.checkNewPassword(user, newClearPass); if (passwordOK && newPasswordOK) { mgr.setPassword(user, newClearPass); updated = true; } else if (!newPasswordOK) { throw new BLException("This password has already been used"); } } catch (BLException e) { // do nothing return false; } } updated = updated || addRevisionUpdated( db, getEntityName(), String.valueOf(u.getId()), oldUser, u, new String[] { "nick", "name", "email", "active", "roles", "password" }); return updated; }); } catch (Exception e) { getApp().getLog().error(e); return false; } if (userUpdated && u.equals(getApp().getUser())) { try { DB.exec( (db) -> { db.session().refresh(getApp().getUser()); return null; }); } catch (Exception e) { getApp().getLog().error(e); } } return userUpdated; }