Search in sources :

Example 1 with UserPK

use of org.apache.airavata.registry.core.experiment.catalog.model.UserPK 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();
        }
    }
}
Also used : ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) EntityManager(javax.persistence.EntityManager) UserPK(org.apache.airavata.registry.core.experiment.catalog.model.UserPK) Gateway(org.apache.airavata.registry.core.experiment.catalog.model.Gateway) Users(org.apache.airavata.registry.core.experiment.catalog.model.Users) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RegistryException(org.apache.airavata.registry.cpi.RegistryException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RegistryException(org.apache.airavata.registry.cpi.RegistryException) ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException)

Aggregations

NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 EntityManager (javax.persistence.EntityManager)1 ApplicationSettingsException (org.apache.airavata.common.exception.ApplicationSettingsException)1 Gateway (org.apache.airavata.registry.core.experiment.catalog.model.Gateway)1 UserPK (org.apache.airavata.registry.core.experiment.catalog.model.UserPK)1 Users (org.apache.airavata.registry.core.experiment.catalog.model.Users)1 RegistryException (org.apache.airavata.registry.cpi.RegistryException)1