use of org.apache.airavata.registry.core.experiment.catalog.model.Users in project airavata by apache.
the class UserResource method save.
/**
* save user to the database
*/
public void save() throws RegistryException {
EntityManager em = null;
try {
em = ExpCatResourceUtils.getEntityManager();
UserPK userPK = new UserPK();
userPK.setGatewayId(gatewayId);
userPK.setUserName(userName);
Users existingUser = em.find(Users.class, userPK);
Gateway gateway = em.find(Gateway.class, gatewayId);
em.close();
em = ExpCatResourceUtils.getEntityManager();
em.getTransaction().begin();
Users user = new Users();
user.setAiravataInternalUserId(userName + "@" + gatewayId);
user.setUserName(userName);
user.setGatewayId(gateway.getGatewayId());
user.setGateway(gateway);
if (password != null && !password.equals("")) {
try {
user.setPassword(SecurityUtil.digestString(password, ServerSettings.getSetting("default.registry.password.hash.method")));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Error hashing default admin password. Invalid hash algorithm.", e);
} catch (ApplicationSettingsException e) {
throw new RuntimeException("Error reading hash algorithm from configurations", e);
}
}
if (existingUser != null) {
if (password != null && !password.equals("")) {
try {
existingUser.setPassword(SecurityUtil.digestString(password, ServerSettings.getSetting("default.registry.password.hash.method")));
existingUser.setGatewayId(gateway.getGatewayId());
existingUser.setGateway(gateway);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Error hashing default admin password. Invalid hash algorithm.", e);
} catch (ApplicationSettingsException e) {
throw new RuntimeException("Error reading hash algorithm from configurations", e);
}
}
user = em.merge(existingUser);
} else {
em.persist(user);
}
em.getTransaction().commit();
em.close();
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new RegistryException(e);
} finally {
if (em != null && em.isOpen()) {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
}
}
}
Aggregations