private SecretKey getKey (UUID keyId, char[] passPhrase) throws Exception { if (passPhrase == null && unlock == null) throw new SecurityException("Passphrase not available"); passPhrase = passPhrase != null ? passPhrase : unlock.get().toCharArray(); String v = DB.execWithTransaction(db -> { SysConfigManager mgr = new SysConfigManager(db, "key."); return mgr.get(keyId.toString(), null); }); if (v == null) { throw new SecurityException("Invalid key"); } byte[] key = PGPHelper.decrypt( v.getBytes(), privKeyRing, passPhrase ); return new SecretKeySpec(key, 0, key.length, "AES"); }
private void registerKey(String k, String v) throws Exception { DB.execWithTransaction(db -> { SysConfigManager mgr = new SysConfigManager(db, "key."); mgr.put(k, v, "security.read", "security.write"); return true; }); LogEvent evt = getLog().createLogEvent("security"); evt.addMessage("<id>" + k + "</id>"); evt.addMessage(System.lineSeparator() + v); Logger.log(evt); }
@Override public boolean updateEntity (Binder binder) throws BLException { try { return (boolean) DB.execWithTransaction( (db) -> { Role oldRole = (Role) ((Role) getOriginalEntity()).clone(); binder.writeBean(getOriginalEntity()); Role r = (Role) getOriginalEntity(); db.session().merge(r); return addRevisionUpdated(db, getEntityName(), String.valueOf(r.getId()), oldRole, r, new String[]{"name", "permissions"}); }); } catch (Exception e) { throw new BLException(e.getMessage()); } }
@Override public boolean removeEntity() { //Users have a deleted flag, they are not completely removed. User t = (User) getOriginalEntity(); 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; } }
@Override public boolean updateEntity(Binder binder) throws BLException { try { return DB.execWithTransaction((db) -> { Consumer oldConsumer = (Consumer) ((Consumer) getOriginalEntity()).clone(); binder.writeBean(getOriginalEntity()); Consumer newConsumer = (Consumer) getOriginalEntity(); db.session().merge(newConsumer); return addRevisionUpdated(db, getEntityName(), String.valueOf(newConsumer.getId()), oldConsumer, newConsumer, new String[]{"active","roles","startDate","endDate"}); }); } catch (Exception e) { getApp().getLog().error(e); return false; } }
@Override public boolean updateEntity(Binder binder) throws BLException { try { return DB.execWithTransaction((db) -> { Account oldAcct = (Account) ((Account) getOriginalEntity()).clone(); binder.writeBean(getOriginalEntity()); Account a = (Account) getOriginalEntity(); db.session().merge(a); return addRevisionUpdated(db, getEntityName(), String.valueOf(a.getId()), oldAcct, a, new String[]{"code","description","created","expired"}); }); } catch (Exception e) { getApp().getLog().error(e); return false; } }
protected boolean saveEntity (Binder binder, EntryGrid entryGrid) throws BLException { try { return (boolean) DB.execWithTransaction(db -> { if (binder.writeBeanIfValid(getOriginalEntity())) { GLTransaction txn = (GLTransaction) getOriginalEntity(); List<GLEntry> entries = entryGrid.getValue().getEntries(); //Reset ids to 0 for (GLEntry e : entries) e.setId(0); txn.setEntries(entries); txn.setTimestamp(new Date()); GLSession glSession = new GLSession(db); glSession.post(txn.getJournal(), txn); addRevisionCreated(db, getEntityName(), getItemId(getOriginalEntity())); return true; } return false; }); } catch (Exception e) { throw new BLException(e.getMessage()); } }
public boolean saveUser (Binder binder, String clearPass) throws BLException { User u = (User) getOriginalEntity(); if (binder.writeBeanIfValid(getOriginalEntity())) { try { return (boolean) DB.execWithTransaction((db) -> { 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(), u.getId().toString()); u.setForcePasswordChange(true); db.session().update(u); return true; } return false; }); } catch (Exception e) { getApp().getLog().error(e); return false; } } else { throw new BLException("Invalid user"); } }
protected boolean updateEntity (Binder binder, EntryGrid entryGrid, boolean shouldReverse) throws BLException { boolean wasUpdated; try { wasUpdated = (boolean) DB.execWithTransaction( (db) -> { GLTransaction oldTxn = ((GLTransaction) getOriginalEntity()).clone(); binder.writeBean(getOriginalEntity());
@Override public boolean updateEntity (Binder binder) throws BLException { try { return (boolean) DB.execWithTransaction((db) -> { SysConfig oldSysConfig = (SysConfig) ((SysConfig) getOriginalEntity()).clone(); binder.writeBean(getOriginalEntity()); SysConfig s = (SysConfig) getOriginalEntity(); //need to re-set prefix to id as it gets deleted on write s.setId(addPrefix(s.getId())); db.session().merge(s); return addRevisionUpdated(db, getEntityName(), String.valueOf(s.getId()), oldSysConfig, s, new String[]{"value"}); }); } catch (Exception e) { QI.getQI().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); user.setLoginAttempts(0); // reset login attempts db.session().saveOrUpdate(user); return true; }); } catch (Exception e) { getApp().getLog().error(e); return null; } return generatedPassword; }
@Override public boolean saveEntity (Binder binder) throws BLException { SysConfig entity = (SysConfig) getOriginalEntity(); if (binder.writeBeanIfValid(getOriginalEntity())) { String id = entity.getId(); id = prefix != null ? prefix + id : id; if (getSysConfig(id) == null) { final String finalId = id; try { return (boolean) DB.execWithTransaction((db) -> { SysConfigManager mgr = new SysConfigManager(db, prefix); mgr.put(entity.getId(), entity.getValue()); addRevisionCreated(db, "SYSCONFIG", finalId); return true; }); } catch (Exception e) { QI.getQI().getLog().error(e); return false; } } else { throw new BLException("SysConfig " + id + " already exists."); } } else { throw new BLException("Invalid SysConfig"); } }
public boolean updateUser (Binder binder, String currentPass, String newClearPass) throws BLException { boolean userUpdated; try { userUpdated = (boolean) DB.execWithTransaction((db) -> { UserManager mgr = new UserManager(db); User oldUser = (User) ((User)getOriginalEntity()).clone();