Search in sources :

Example 6 with EnvironmentRoleEntity

use of fi.otavanopisto.muikku.model.users.EnvironmentRoleEntity in project muikku by otavanopisto.

the class TranscriptofRecordsRESTService method updateHops.

@PUT
@Consumes("application/json")
@Path("/hops")
@RESTPermit(handling = Handling.INLINE)
public Response updateHops(HopsRESTModel model) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.FORBIDDEN).entity("Must be logged in").build();
    }
    SchoolDataIdentifier userIdentifier = sessionController.getLoggedUser();
    User user = userController.findUserByIdentifier(userIdentifier);
    UserEntity userEntity = sessionController.getLoggedUserEntity();
    EnvironmentUser environmentUser = environmentUserController.findEnvironmentUserByUserEntity(userEntity);
    EnvironmentRoleEntity roleEntity = environmentUser.getRole();
    if (!EnvironmentRoleArchetype.STUDENT.equals(roleEntity.getArchetype())) {
        return Response.status(Status.FORBIDDEN).entity("Must be a student").build();
    }
    vopsController.saveStringProperty(user, "goalSecondarySchoolDegree", model.getGoalSecondarySchoolDegree());
    vopsController.saveStringProperty(user, "goalMatriculationExam", model.getGoalMatriculationExam());
    vopsController.saveStringProperty(user, "vocationalYears", model.getVocationalYears());
    vopsController.saveStringProperty(user, "goalJustMatriculationExam", model.getGoalJustMatriculationExam());
    vopsController.saveStringProperty(user, "justTransferCredits", model.getJustTransferCredits());
    vopsController.saveStringProperty(user, "transferCreditYears", model.getTransferCreditYears());
    vopsController.saveStringProperty(user, "completionYears", model.getCompletionYears());
    vopsController.saveStringProperty(user, "mathSyllabus", model.getMathSyllabus());
    vopsController.saveStringProperty(user, "finnish", model.getFinnish());
    vopsController.saveBoolProperty(user, "swedish", model.isSwedish());
    vopsController.saveBoolProperty(user, "english", model.isEnglish());
    vopsController.saveBoolProperty(user, "german", model.isGerman());
    vopsController.saveBoolProperty(user, "french", model.isFrench());
    vopsController.saveBoolProperty(user, "italian", model.isItalian());
    vopsController.saveBoolProperty(user, "spanish", model.isSpanish());
    vopsController.saveStringProperty(user, "science", model.getScience());
    vopsController.saveStringProperty(user, "religion", model.getReligion());
    vopsController.saveStringProperty(user, "additionalInfo", model.getAdditionalInfo());
    return Response.ok().entity(model).build();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) EnvironmentUser(fi.otavanopisto.muikku.model.users.EnvironmentUser) EnvironmentRoleEntity(fi.otavanopisto.muikku.model.users.EnvironmentRoleEntity) User(fi.otavanopisto.muikku.schooldata.entity.User) EnvironmentUser(fi.otavanopisto.muikku.model.users.EnvironmentUser) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 7 with EnvironmentRoleEntity

use of fi.otavanopisto.muikku.model.users.EnvironmentRoleEntity in project muikku by otavanopisto.

the class DefaultSchoolDataRoleListener method onSchoolDataEnvironmentRoleDiscoveredEvent.

public void onSchoolDataEnvironmentRoleDiscoveredEvent(@Observes SchoolDataEnvironmentRoleDiscoveredEvent event) {
    String discoverId = "ER-" + event.getDataSource() + "/" + event.getIdentifier();
    if (discoveredEnvironmentRoles.containsKey(discoverId)) {
        event.setDiscoveredEnvironmentRoleEntityId(discoveredEnvironmentRoles.get(discoverId));
        return;
    }
    EnvironmentRoleEntity environmentRoleEntity = environmentRoleEntityController.findEnvironmentRoleEntity(event.getDataSource(), event.getIdentifier());
    if (environmentRoleEntity == null) {
        EnvironmentRoleArchetype roleArchetype = EnvironmentRoleArchetype.valueOf(event.getArchetype().name());
        environmentRoleEntity = environmentRoleEntityController.createEnvironmentRoleEntity(event.getDataSource(), event.getIdentifier(), roleArchetype, event.getName());
        discoveredEnvironmentRoles.put(discoverId, environmentRoleEntity.getId());
        event.setDiscoveredEnvironmentRoleEntityId(environmentRoleEntity.getId());
    } else {
        logger.warning("EnvironmentRoleEntity for " + event.getIdentifier() + "/" + event.getDataSource() + " already exists");
    }
}
Also used : EnvironmentRoleEntity(fi.otavanopisto.muikku.model.users.EnvironmentRoleEntity) EnvironmentRoleArchetype(fi.otavanopisto.muikku.model.users.EnvironmentRoleArchetype)

Example 8 with EnvironmentRoleEntity

use of fi.otavanopisto.muikku.model.users.EnvironmentRoleEntity in project muikku by otavanopisto.

the class DefaultSchoolDataUserListener method onSchoolDataUserUpdatedEvent.

public void onSchoolDataUserUpdatedEvent(@Observes SchoolDataUserUpdatedEvent event) {
    Long userEntityId = event.getUserEntityId();
    SchoolDataIdentifier defaultIdentifier = event.getDefaultIdentifier();
    List<SchoolDataIdentifier> discoveredIdentifiers = event.getDiscoveredIdentifiers();
    List<SchoolDataIdentifier> updatedIdentifiers = event.getUpdatedIdentifiers();
    List<SchoolDataIdentifier> removedIdentifiers = event.getRemovedIdentifiers();
    Collection<String> allEmails = event.getAllEmails();
    if (allEmails.isEmpty()) {
        logger.warning("Updating user without email addresses");
    } else {
        // Attempt to find existing users by given emails
        Collection<UserEntity> emailUsers = userEntityController.listUserEntitiesByEmails(allEmails);
        if (emailUsers.isEmpty()) {
        // Could not find any users with given emails
        } else if (emailUsers.size() > 1) {
            logger.log(Level.SEVERE, String.format("Multiple users found with given emails (%s)", StringUtils.join(allEmails, ',')));
            return;
        } else {
            UserEntity emailUser = emailUsers.iterator().next();
            if (userEntityId != null) {
                if (!emailUser.getId().equals(userEntityId)) {
                    logger.log(Level.SEVERE, String.format("One or more of emails %s belong to another user", StringUtils.join(allEmails, ',')));
                    return;
                }
            } else {
                userEntityId = emailUser.getId();
                logger.log(Level.INFO, String.format("Found userEntity (%d) by email, merging user to existing account", userEntityId));
            }
        }
    }
    UserEntity userEntity = null;
    // If it's not an user delete event we need to create / update user into the system
    if (!discoveredIdentifiers.isEmpty() || !updatedIdentifiers.isEmpty()) {
        // UserEntityId has not been defined in the event and could not be found by email, so we create new user
        if (userEntityId == null) {
            userEntity = userEntityController.createUserEntity(defaultIdentifier.getDataSource(), defaultIdentifier.getIdentifier());
        } else {
            // Otherwise we use the existing one
            userEntity = userEntityController.findUserEntityById(userEntityId);
            if (userEntity == null) {
                logger.log(Level.WARNING, "Could not find specified userEntityId %d, aborting synchronization", userEntityId);
                return;
            }
            if (defaultIdentifier != null) {
                if (!StringUtils.equals(userEntity.getDefaultIdentifier(), defaultIdentifier.getIdentifier()) || !StringUtils.equals(userEntity.getDefaultSchoolDataSource().getIdentifier(), defaultIdentifier.getDataSource())) {
                    logger.log(Level.FINE, String.format("Updating default identifier for user #%d into %s", userEntity.getId(), defaultIdentifier));
                    userEntityController.updateDefaultSchoolDataSource(userEntity, defaultIdentifier.getDataSource());
                    userEntityController.updateDefaultIdentifier(userEntity, defaultIdentifier.getIdentifier());
                }
            }
        }
        // Attach discovered identities to user
        for (SchoolDataIdentifier identifier : discoveredIdentifiers) {
            List<String> identifierEmails = event.getEmails().get(identifier);
            UserSchoolDataIdentifier userSchoolDataIdentifier = userSchoolDataIdentifierController.findUserSchoolDataIdentifierByDataSourceAndIdentifierIncludeArchived(identifier.getDataSource(), identifier.getIdentifier());
            if (userSchoolDataIdentifier == null) {
                userSchoolDataIdentifier = userSchoolDataIdentifierController.createUserSchoolDataIdentifier(identifier.getDataSource(), identifier.getIdentifier(), userEntity);
                logger.log(Level.FINE, String.format("Added new identifier %s for user %d", identifier, userEntity.getId()));
            } else if (userSchoolDataIdentifier.getArchived()) {
                userSchoolDataIdentifierController.unarchiveUserSchoolDataIdentifier(userSchoolDataIdentifier);
            }
            userEmailEntityController.setUserEmails(userSchoolDataIdentifier, getValidEmails(identifierEmails));
        }
        for (SchoolDataIdentifier identifier : updatedIdentifiers) {
            List<String> emails = event.getEmails().get(identifier);
            userEmailEntityController.setUserEmails(identifier, getValidEmails(emails));
        }
        for (SchoolDataIdentifier identifier : removedIdentifiers) {
            List<String> emails = event.getEmails().get(identifier);
            userEmailEntityController.setUserEmails(identifier, getValidEmails(emails));
        }
        // Update users environment role
        if (event.getEnvironmentRoleIdentifier() != null) {
            EnvironmentRoleEntity environmentRoleEntity = environmentRoleEntityController.findEnvironmentRoleEntity(event.getEnvironmentRoleIdentifier().getDataSource(), event.getEnvironmentRoleIdentifier().getIdentifier());
            if (environmentRoleEntity != null) {
                EnvironmentUser environmentUser = environmentUserController.findEnvironmentUserByUserEntity(userEntity);
                if (environmentUser == null) {
                    logger.fine(String.format("UserEntity %d did not have an environment user so created new one into role %s", userEntity.getId(), environmentRoleEntity.getName()));
                    environmentUserController.createEnvironmentUser(userEntity, environmentRoleEntity);
                } else {
                    if (environmentUser.getRole() == null || !environmentUser.getRole().getId().equals(environmentRoleEntity.getId())) {
                        logger.fine(String.format("Updated UserEntity %d role into %s", userEntity.getId(), environmentRoleEntity.getName()));
                        environmentUserController.updateEnvironmentUserRole(environmentUser, environmentRoleEntity);
                    }
                }
            } else {
                logger.severe(String.format("Could not find specified environment role entity %s", event.getEnvironmentRoleIdentifier()));
            }
        } else {
            // Users new role has been set to null which means that we need to remove the environment role from the user
            EnvironmentUser environmentUser = environmentUserController.findEnvironmentUserByUserEntity(userEntity);
            if (environmentUser != null) {
                logger.info(String.format("Removed UserEntity %d environment role", userEntity.getId()));
                environmentUserController.updateEnvironmentUserRole(environmentUser, null);
            }
        }
    }
    // Remove identifiers in the removed list
    for (SchoolDataIdentifier identifier : removedIdentifiers) {
        UserSchoolDataIdentifier userSchoolDataIdentifier = userSchoolDataIdentifierController.findUserSchoolDataIdentifierByDataSourceAndIdentifier(identifier.getDataSource(), identifier.getIdentifier());
        if (userSchoolDataIdentifier != null) {
            logger.log(Level.FINE, String.format("Removing user school data identifier %s", identifier));
            userSchoolDataIdentifierController.archiveUserSchoolDataIdentifier(userSchoolDataIdentifier);
            if (userEntity == null) {
                userEntity = userSchoolDataIdentifier.getUserEntity();
            }
        }
    }
    // Finally check if user has any identifiers left, if not archive the user from the system
    if (userEntity != null) {
        if (userSchoolDataIdentifierController.listUserSchoolDataIdentifiersByUserEntity(userEntity).isEmpty()) {
            logger.log(Level.INFO, String.format("UserEntity #%d has no identities left, archiving userEntity", userEntity.getId()));
            userEntityController.archiveUserEntity(userEntity);
        }
    }
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) EnvironmentUser(fi.otavanopisto.muikku.model.users.EnvironmentUser) EnvironmentRoleEntity(fi.otavanopisto.muikku.model.users.EnvironmentRoleEntity) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity)

Example 9 with EnvironmentRoleEntity

use of fi.otavanopisto.muikku.model.users.EnvironmentRoleEntity in project muikku by otavanopisto.

the class EnvironmentRoleEntityController method createEnvironmentRoleEntity.

/* Environment Role Entities */
public EnvironmentRoleEntity createEnvironmentRoleEntity(String dataSource, String identifier, EnvironmentRoleArchetype archetype, String name) {
    SchoolDataSource schoolDataSource = schoolDataSourceDAO.findByIdentifier(dataSource);
    if (schoolDataSource == null) {
        logger.severe("Could not find datasource " + dataSource);
        return null;
    }
    EnvironmentRoleEntity environmentRoleEntity = environmentRoleEntityDAO.create(archetype, name);
    roleSchoolDataIdentifierDAO.create(schoolDataSource, identifier, environmentRoleEntity);
    return environmentRoleEntity;
}
Also used : SchoolDataSource(fi.otavanopisto.muikku.model.base.SchoolDataSource) EnvironmentRoleEntity(fi.otavanopisto.muikku.model.users.EnvironmentRoleEntity)

Example 10 with EnvironmentRoleEntity

use of fi.otavanopisto.muikku.model.users.EnvironmentRoleEntity in project muikku by otavanopisto.

the class PermissionsPluginController method resetPermissions.

public void resetPermissions(Set<RoleEntity> resetRoleEntities) {
    if (CollectionUtils.isEmpty(resetRoleEntities))
        return;
    // TODO Only handles environment and workspace scopes
    for (MuikkuPermissionCollection collection : permissionCollections) {
        logger.log(Level.INFO, "Processing permission collection " + collection.getClass().getSimpleName());
        List<String> permissions = collection.listPermissions();
        for (String permissionName : permissions) {
            Permission permission = permissionDAO.findByName(permissionName);
            if (permission != null) {
                try {
                    String permissionScope = collection.getPermissionScope(permissionName);
                    if (permissionScope != null) {
                        if (!PermissionScope.PERSONAL.equals(permissionScope)) {
                            // Current roles
                            String[] pseudoRoles = collection.getDefaultPseudoRoles(permissionName);
                            EnvironmentRoleArchetype[] environmentRoles = collection.getDefaultEnvironmentRoles(permissionName);
                            WorkspaceRoleArchetype[] workspaceRoles = collection.getDefaultWorkspaceRoles(permissionName);
                            List<RoleEntity> currentRoles = new ArrayList<RoleEntity>();
                            if (pseudoRoles != null) {
                                for (String pseudoRole : pseudoRoles) {
                                    RoleEntity roleEntity = roleEntityDAO.findByName(pseudoRole);
                                    if (roleEntity != null) {
                                        currentRoles.add(roleEntity);
                                    }
                                }
                            }
                            if (environmentRoles != null) {
                                for (EnvironmentRoleArchetype environmentRole : environmentRoles) {
                                    List<EnvironmentRoleEntity> envRoles = environmentRoleEntityDAO.listByArchetype(environmentRole);
                                    currentRoles.addAll(envRoles);
                                }
                            }
                            if (workspaceRoles != null) {
                                for (WorkspaceRoleArchetype workspaceRole : workspaceRoles) {
                                    List<WorkspaceRoleEntity> wsRoles = workspaceRoleEntityDAO.listByArchetype(workspaceRole);
                                    currentRoles.addAll(wsRoles);
                                }
                            }
                            logger.info(String.format("Permission %s applies to %d roles", permissionName, currentRoles.size()));
                            if (PermissionScope.ENVIRONMENT.equals(permissionScope) || PermissionScope.WORKSPACE.equals(permissionScope)) {
                                List<RolePermission> databasePermissions = rolePermissionDAO.listByPermission(permission);
                                removeNonHandledRoles(currentRoles, databasePermissions, resetRoleEntities);
                                for (RolePermission databasePermission : databasePermissions) {
                                    int index = indexOfRoleEntity(currentRoles, databasePermission);
                                    if (index >= 0) {
                                        currentRoles.remove(index);
                                    } else {
                                        logger.info(String.format("Removing %s from %s", databasePermission.getRole().getName(), permission.getName()));
                                        rolePermissionDAO.delete(databasePermission);
                                    }
                                }
                                for (RoleEntity currentRole : currentRoles) {
                                    logger.info(String.format("Adding environment role %s for %s", currentRole.getName(), permission.getName()));
                                    rolePermissionDAO.create(currentRole, permission);
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    logger.log(Level.SEVERE, "Permission handling failed for " + permissionName);
                }
            }
        }
    }
}
Also used : EnvironmentRoleEntity(fi.otavanopisto.muikku.model.users.EnvironmentRoleEntity) EnvironmentRoleArchetype(fi.otavanopisto.muikku.model.users.EnvironmentRoleArchetype) MuikkuPermissionCollection(fi.otavanopisto.muikku.security.MuikkuPermissionCollection) ArrayList(java.util.ArrayList) WorkspaceRoleEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceRoleEntity) RoleEntity(fi.otavanopisto.muikku.model.users.RoleEntity) EnvironmentRoleEntity(fi.otavanopisto.muikku.model.users.EnvironmentRoleEntity) WorkspaceRoleEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceRoleEntity) RolePermission(fi.otavanopisto.muikku.model.security.RolePermission) Permission(fi.otavanopisto.muikku.model.security.Permission) WorkspaceRoleArchetype(fi.otavanopisto.muikku.model.workspace.WorkspaceRoleArchetype) RolePermission(fi.otavanopisto.muikku.model.security.RolePermission)

Aggregations

EnvironmentRoleEntity (fi.otavanopisto.muikku.model.users.EnvironmentRoleEntity)13 RoleEntity (fi.otavanopisto.muikku.model.users.RoleEntity)5 Permission (fi.otavanopisto.muikku.model.security.Permission)4 EnvironmentUser (fi.otavanopisto.muikku.model.users.EnvironmentUser)4 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)4 EnvironmentRoleArchetype (fi.otavanopisto.muikku.model.users.EnvironmentRoleArchetype)3 WorkspaceRoleEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceRoleEntity)3 SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)3 User (fi.otavanopisto.muikku.schooldata.entity.User)3 SchoolDataSource (fi.otavanopisto.muikku.model.base.SchoolDataSource)2 RolePermission (fi.otavanopisto.muikku.model.security.RolePermission)2 SystemRoleEntity (fi.otavanopisto.muikku.model.users.SystemRoleEntity)2 UserSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier)2 WorkspaceRoleArchetype (fi.otavanopisto.muikku.model.workspace.WorkspaceRoleArchetype)2 MuikkuPermissionCollection (fi.otavanopisto.muikku.security.MuikkuPermissionCollection)2 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)2 ArrayList (java.util.ArrayList)2 Path (javax.ws.rs.Path)2 RequestAction (org.ocpsoft.rewrite.annotation.RequestAction)2 FlagStudent (fi.otavanopisto.muikku.model.users.FlagStudent)1