Search in sources :

Example 1 with RegistryRoleConverter

use of net.geoprism.registry.conversion.RegistryRoleConverter in project geoprism-registry by terraframe.

the class UserInfo method applyUserWithRoles.

@Transaction
public static JSONObject applyUserWithRoles(JsonObject account, String[] roleNameArray, boolean isUserInvite) {
    GeoprismUser geoprismUser = deserialize(account);
    if (roleNameArray != null && roleNameArray.length == 0) {
        // TODO : Better Error
        throw new AttributeValueException("You're attempting to apply a user with zero roles?", "");
    }
    /*
     * Make sure they have permissions to all these new roles they want to
     * assign
     */
    if (!isUserInvite && Session.getCurrentSession() != null && Session.getCurrentSession().getUser() != null) {
        Set<RoleDAOIF> myRoles = Session.getCurrentSession().getUser().authorizedRoles();
        boolean hasSRA = false;
        for (RoleDAOIF myRole : myRoles) {
            if (RegistryRole.Type.isSRA_Role(myRole.getRoleName())) {
                hasSRA = true;
            }
        }
        if (!hasSRA && roleNameArray != null) {
            for (String roleName : roleNameArray) {
                boolean hasPermission = false;
                if (RegistryRole.Type.isOrgRole(roleName) && !RegistryRole.Type.isRootOrgRole(roleName)) {
                    String orgCodeArg = RegistryRole.Type.parseOrgCode(roleName);
                    for (RoleDAOIF myRole : myRoles) {
                        if (RegistryRole.Type.isRA_Role(myRole.getRoleName())) {
                            String myOrgCode = RegistryRole.Type.parseOrgCode(myRole.getRoleName());
                            if (myOrgCode.equals(orgCodeArg)) {
                                hasPermission = true;
                                break;
                            }
                        }
                    }
                } else if (RegistryRole.Type.isSRA_Role(roleName)) {
                    SRAException ex = new SRAException();
                    throw ex;
                } else {
                    hasPermission = true;
                }
                if (!hasPermission) {
                    OrganizationRAException ex = new OrganizationRAException();
                    throw ex;
                }
            }
        }
    }
    // They're not allowed to change the admin username
    if (!geoprismUser.isNew()) {
        GeoprismUser adminUser = getAdminUser();
        if (adminUser != null && adminUser.getOid().equals(geoprismUser.getOid()) && !geoprismUser.getUsername().equals(RegistryConstants.ADMIN_USER_NAME)) {
            // TODO : Better Error
            throw new AttributeValueException("You can't change the admin username", RegistryConstants.ADMIN_USER_NAME);
        }
    }
    geoprismUser.apply();
    if (roleNameArray != null) {
        List<Roles> newRoles = new LinkedList<Roles>();
        Set<String> roleIdSet = new HashSet<String>();
        for (String roleName : roleNameArray) {
            Roles role = Roles.findRoleByName(roleName);
            roleIdSet.add(role.getOid());
            newRoles.add(role);
        }
        List<ConfigurationIF> configurations = ConfigurationService.getConfigurations();
        for (ConfigurationIF configuration : configurations) {
            configuration.configureUserRoles(roleIdSet);
        }
        UserDAOIF user = UserDAO.get(geoprismUser.getOid());
        // Remove existing roles.
        Set<RoleDAOIF> userRoles = user.assignedRoles();
        for (RoleDAOIF roleDAOIF : userRoles) {
            RoleDAO roleDAO = RoleDAO.get(roleDAOIF.getOid()).getBusinessDAO();
            if (!(geoprismUser.getUsername().equals(RegistryConstants.ADMIN_USER_NAME) && (roleDAO.getRoleName().equals(RegistryConstants.REGISTRY_SUPER_ADMIN_ROLE) || roleDAO.getRoleName().equals(DefaultConfiguration.ADMIN)))) {
                roleDAO.deassignMember(user);
            }
        }
        // Delete existing relationships with Organizations.
        QueryFactory qf = new QueryFactory();
        OrganizationUserQuery q = new OrganizationUserQuery(qf);
        q.WHERE(q.childOid().EQ(geoprismUser.getOid()));
        OIterator<? extends OrganizationUser> i = q.getIterator();
        i.forEach(r -> r.delete());
        /*
       * Assign roles and associate with the user
       */
        Set<String> organizationSet = new HashSet<String>();
        for (Roles role : newRoles) {
            RoleDAO roleDAO = (RoleDAO) BusinessFacade.getEntityDAO(role);
            roleDAO.assignMember(user);
            RegistryRole registryRole = new RegistryRoleConverter().build(role);
            if (registryRole != null) {
                String organizationCode = registryRole.getOrganizationCode();
                if (organizationCode != null && !organizationCode.equals("") && !organizationSet.contains(organizationCode)) {
                    Organization organization = Organization.getByCode(organizationCode);
                    organization.addUsers(geoprismUser).apply();
                    organizationSet.add(organizationCode);
                }
            }
        }
    }
    UserInfo info = getByUser(geoprismUser);
    if (info == null) {
        info = new UserInfo();
        info.setGeoprismUser(geoprismUser);
    } else {
        info.lock();
    }
    if (account.has(UserInfo.ALTFIRSTNAME)) {
        info.setAltFirstName(account.get(UserInfo.ALTFIRSTNAME).getAsString());
    } else {
        info.setAltFirstName("");
    }
    if (account.has(UserInfo.ALTLASTNAME)) {
        info.setAltLastName(account.get(UserInfo.ALTLASTNAME).getAsString());
    } else {
        info.setAltLastName("");
    }
    if (account.has(UserInfo.ALTPHONENUMBER)) {
        info.setAltPhoneNumber(account.get(UserInfo.ALTPHONENUMBER).getAsString());
    } else {
        info.setAltPhoneNumber("");
    }
    if (account.has(UserInfo.POSITION)) {
        info.setPosition(account.get(UserInfo.POSITION).getAsString());
    } else {
        info.setPosition("");
    }
    if (account.has(UserInfo.DEPARTMENT)) {
        info.setDepartment(account.get(UserInfo.DEPARTMENT).getAsString());
    } else {
        info.setDepartment("");
    }
    if (account.has(UserInfo.EXTERNALSYSTEMOID)) {
        info.setExternalSystemOid(account.get(UserInfo.EXTERNALSYSTEMOID).getAsString());
    } else {
        info.setExternalSystemOid("");
    }
    info.apply();
    return serialize(geoprismUser, info);
}
Also used : RegistryRole(org.commongeoregistry.adapter.metadata.RegistryRole) QueryFactory(com.runwaysdk.query.QueryFactory) Roles(com.runwaysdk.system.Roles) AttributeValueException(com.runwaysdk.dataaccess.attributes.AttributeValueException) LinkedList(java.util.LinkedList) ConfigurationIF(net.geoprism.ConfigurationIF) RegistryRoleConverter(net.geoprism.registry.conversion.RegistryRoleConverter) RoleDAO(com.runwaysdk.business.rbac.RoleDAO) GeoprismUser(net.geoprism.GeoprismUser) RoleDAOIF(com.runwaysdk.business.rbac.RoleDAOIF) UserDAOIF(com.runwaysdk.business.rbac.UserDAOIF) HashSet(java.util.HashSet) Transaction(com.runwaysdk.dataaccess.transaction.Transaction)

Example 2 with RegistryRoleConverter

use of net.geoprism.registry.conversion.RegistryRoleConverter in project geoprism-registry by terraframe.

the class AccountService method addRolesForOrganization.

private void addRolesForOrganization(List<RegistryRole> registryRoleList, Organization organization) {
    LocalizedValue orgDisplayLabel = LocalizedValueConverter.convert(organization.getDisplayLabel());
    // Add the RA role
    Roles adminRole = organization.getRegistryAdminiRole();
    RegistryRole adminRegistryRole = new RegistryRoleConverter().build(adminRole);
    adminRegistryRole.setOrganizationLabel(orgDisplayLabel);
    registryRoleList.add(adminRegistryRole);
    Map<String, ServerGeoObjectType> geoObjectTypeInfo = organization.getGeoObjectTypes();
    for (String typeCode : geoObjectTypeInfo.keySet()) {
        ServerGeoObjectType type = geoObjectTypeInfo.get(typeCode);
        // The cannot be assigned directly to the child type.
        if (type.getSuperType() == null) {
            // Add the RM role
            String rmRoleName = RegistryRole.Type.getRM_RoleName(organization.getCode(), typeCode);
            Roles rmRole = Roles.findRoleByName(rmRoleName);
            RegistryRole rmRegistryRole = new RegistryRoleConverter().build(rmRole);
            rmRegistryRole.setOrganizationLabel(orgDisplayLabel);
            LocalizedValue label = type.getLabel();
            rmRegistryRole.setGeoObjectTypeLabel(label);
            registryRoleList.add(rmRegistryRole);
            // Add the RC role
            String rcRoleName = RegistryRole.Type.getRC_RoleName(organization.getCode(), typeCode);
            Roles rcRole = Roles.findRoleByName(rcRoleName);
            RegistryRole rcRegistryRole = new RegistryRoleConverter().build(rcRole);
            rcRegistryRole.setOrganizationLabel(orgDisplayLabel);
            rcRegistryRole.setGeoObjectTypeLabel(label);
            registryRoleList.add(rcRegistryRole);
            // Add the AC role
            String acRoleName = RegistryRole.Type.getAC_RoleName(organization.getCode(), typeCode);
            Roles acRole = Roles.findRoleByName(acRoleName);
            RegistryRole acRegistryRole = new RegistryRoleConverter().build(acRole);
            acRegistryRole.setOrganizationLabel(orgDisplayLabel);
            acRegistryRole.setGeoObjectTypeLabel(label);
            registryRoleList.add(acRegistryRole);
        }
    }
}
Also used : RegistryRole(org.commongeoregistry.adapter.metadata.RegistryRole) LocalizedValue(org.commongeoregistry.adapter.dataaccess.LocalizedValue) RegistryRoleConverter(net.geoprism.registry.conversion.RegistryRoleConverter) ServerGeoObjectType(net.geoprism.registry.model.ServerGeoObjectType) Roles(com.runwaysdk.system.Roles)

Example 3 with RegistryRoleConverter

use of net.geoprism.registry.conversion.RegistryRoleConverter in project geoprism-registry by terraframe.

the class AccountService method getRolesForOrganization.

private List<RegistryRole> getRolesForOrganization(String... organizationCodes) {
    List<RegistryRole> registryRoleList = new LinkedList<RegistryRole>();
    List<String> orgCodesToProcess = new LinkedList<String>();
    for (String organizationCode : organizationCodes) {
        if (!organizationCode.trim().equals("")) {
            orgCodesToProcess.add(organizationCode.trim());
        }
    }
    List<Organization> organizationList;
    if (orgCodesToProcess.size() == 0) {
        // Add the SRA role
        String sraRoleName = RegistryRole.Type.getSRA_RoleName();
        Roles sraRole = Roles.findRoleByName(sraRoleName);
        registryRoleList.add(new RegistryRoleConverter().build(sraRole));
        organizationList = Organization.getOrganizationsFromCache();
    } else {
        organizationList = new LinkedList<Organization>();
        for (String organizationCode : organizationCodes) {
            organizationList.add(Organization.getByCode(organizationCode));
        }
    }
    for (Organization organization : organizationList) {
        this.addRolesForOrganization(registryRoleList, organization);
    }
    return registryRoleList;
}
Also used : RegistryRole(org.commongeoregistry.adapter.metadata.RegistryRole) Organization(net.geoprism.registry.Organization) RegistryRoleConverter(net.geoprism.registry.conversion.RegistryRoleConverter) Roles(com.runwaysdk.system.Roles) LinkedList(java.util.LinkedList)

Example 4 with RegistryRoleConverter

use of net.geoprism.registry.conversion.RegistryRoleConverter in project geoprism-registry by terraframe.

the class AccountService method getRolesForUser.

/**
 * @param organizationCodes
 *          comma delimited list of registry codes. Returns all registry roles
 *          if empty.
 *
 * @return all of the roles are set to assigned equals false
 */
@Request(RequestType.SESSION)
public RegistryRole[] getRolesForUser(String sessionId, String userOID) {
    GeoprismUser geoPrismUser = GeoprismUser.get(userOID);
    List<RegistryRole> registryRoles = new LinkedList<RegistryRole>();
    Set<String> roleNameSet = new HashSet<String>();
    OIterator<? extends com.runwaysdk.system.Roles> roleIterator = geoPrismUser.getAllAssignedRole();
    for (Roles role : roleIterator) {
        RegistryRole registryRole = new RegistryRoleConverter().build(role);
        if (registryRole != null) {
            registryRole.setAssigned(true);
            LocalizedValueConverter.populateOrganizationDisplayLabel(registryRole);
            LocalizedValueConverter.populateGeoObjectTypeLabel(registryRole);
            registryRoles.add(registryRole);
            roleNameSet.add(registryRole.getName());
        }
    }
    // Add the registry roles that the user can be a member of based on their
    // organization affiliation
    OIterator<? extends Business> organizationIterators = geoPrismUser.getParents(OrganizationUser.CLASS);
    for (Business business : organizationIterators) {
        Organization organization = (Organization) business;
        List<RegistryRole> orgRoleIterator = this.getRolesForOrganization(organization.getCode());
        for (RegistryRole registryRole : orgRoleIterator) {
            if (!roleNameSet.contains(registryRole.getName())) {
                registryRoles.add(registryRole);
            }
        }
    }
    if (!roleNameSet.contains(RegistryConstants.REGISTRY_SUPER_ADMIN_ROLE)) {
        Roles sra = Roles.findRoleByName(RegistryConstants.REGISTRY_SUPER_ADMIN_ROLE);
        RegistryRole rrSRA = new RegistryRoleConverter().build(sra);
        rrSRA.setAssigned(false);
        registryRoles.add(rrSRA);
    }
    return registryRoles.stream().sorted(Comparator.comparing(RegistryRole::getOrganizationCode).thenComparing(RegistryRole::getGeoObjectTypeCode)).toArray(size -> new RegistryRole[size]);
}
Also used : RegistryRole(org.commongeoregistry.adapter.metadata.RegistryRole) Organization(net.geoprism.registry.Organization) Roles(com.runwaysdk.system.Roles) LinkedList(java.util.LinkedList) RegistryRoleConverter(net.geoprism.registry.conversion.RegistryRoleConverter) GeoprismUser(net.geoprism.GeoprismUser) HashSet(java.util.HashSet) Business(com.runwaysdk.business.Business) Request(com.runwaysdk.session.Request)

Example 5 with RegistryRoleConverter

use of net.geoprism.registry.conversion.RegistryRoleConverter in project geoprism-registry by terraframe.

the class OrganizationAndRoleTest method testRA_RoleToRegistryRole.

@Test
@Request
public void testRA_RoleToRegistryRole() {
    String organizationCode = FastTestDataset.ORG_CGOV.getCode();
    String raRoleName = RegistryRole.Type.getRA_RoleName(organizationCode);
    Roles raRole = Roles.findRoleByName(raRoleName);
    RegistryRole registryRole = new RegistryRoleConverter().build(raRole);
    Assert.assertEquals(raRoleName, registryRole.getName());
    Assert.assertEquals(organizationCode, registryRole.getOrganizationCode());
}
Also used : RegistryRole(org.commongeoregistry.adapter.metadata.RegistryRole) RegistryRoleConverter(net.geoprism.registry.conversion.RegistryRoleConverter) Roles(com.runwaysdk.system.Roles) Test(org.junit.Test) Request(com.runwaysdk.session.Request)

Aggregations

Roles (com.runwaysdk.system.Roles)7 RegistryRoleConverter (net.geoprism.registry.conversion.RegistryRoleConverter)7 RegistryRole (org.commongeoregistry.adapter.metadata.RegistryRole)7 Request (com.runwaysdk.session.Request)3 HashSet (java.util.HashSet)3 LinkedList (java.util.LinkedList)3 GeoprismUser (net.geoprism.GeoprismUser)2 Organization (net.geoprism.registry.Organization)2 JsonArray (com.google.gson.JsonArray)1 Business (com.runwaysdk.business.Business)1 RoleDAO (com.runwaysdk.business.rbac.RoleDAO)1 RoleDAOIF (com.runwaysdk.business.rbac.RoleDAOIF)1 UserDAOIF (com.runwaysdk.business.rbac.UserDAOIF)1 AttributeValueException (com.runwaysdk.dataaccess.attributes.AttributeValueException)1 Transaction (com.runwaysdk.dataaccess.transaction.Transaction)1 QueryFactory (com.runwaysdk.query.QueryFactory)1 ConfigurationIF (net.geoprism.ConfigurationIF)1 ServerGeoObjectType (net.geoprism.registry.model.ServerGeoObjectType)1 LocalizedValue (org.commongeoregistry.adapter.dataaccess.LocalizedValue)1 Test (org.junit.Test)1