示例#1
0
 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;
   }
 }
示例#2
0
 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;
 }
示例#3
0
 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;
 }