Search in sources :

Example 16 with UserSchoolDataIdentifier

use of fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier 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 17 with UserSchoolDataIdentifier

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

the class DefaultSchoolDataWorkspaceListener method onSchoolDataWorkspaceUserDiscoveredEvent.

public synchronized void onSchoolDataWorkspaceUserDiscoveredEvent(@Observes SchoolDataWorkspaceUserDiscoveredEvent event) {
    String discoverId = "WSU-" + event.getDataSource() + "/" + event.getIdentifier();
    if (discoveredWorkspaceUsers.containsKey(discoverId)) {
        event.setDiscoveredWorkspaceUserEntityId(discoveredWorkspaceUsers.get(discoverId));
        return;
    }
    WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceByDataSourceAndIdentifier(event.getWorkspaceDataSource(), event.getWorkspaceIdentifier());
    if (workspaceEntity != null) {
        WorkspaceRoleEntity workspaceUserRole = workspaceController.findWorkspaceRoleEntityByDataSourceAndIdentifier(event.getRoleDataSource(), event.getRoleIdentifier());
        if (workspaceUserRole != null) {
            UserSchoolDataIdentifier userSchoolDataIdentifier = userSchoolDataIdentifierController.findUserSchoolDataIdentifierByDataSourceAndIdentifier(event.getUserDataSource(), event.getUserIdentifier());
            if (userSchoolDataIdentifier != null) {
                WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityController.findWorkspaceUserEntityByWorkspaceAndUserSchoolDataIdentifierIncludeArchived(workspaceEntity, userSchoolDataIdentifier);
                if (workspaceUserEntity == null) {
                    workspaceUserEntity = workspaceUserEntityController.createWorkspaceUserEntity(userSchoolDataIdentifier, workspaceEntity, event.getIdentifier(), workspaceUserRole);
                    discoveredWorkspaceUsers.put(discoverId, workspaceUserEntity.getId());
                    event.setDiscoveredWorkspaceUserEntityId(workspaceUserEntity.getId());
                } else {
                    if (!workspaceUserEntity.getIdentifier().equals(event.getIdentifier())) {
                        workspaceUserEntityController.updateIdentifier(workspaceUserEntity, event.getIdentifier());
                    }
                    // #3806: WorkspaceUserEntity exists; check if role has changed
                    if (workspaceUserEntity.getWorkspaceUserRole() == null || !workspaceUserRole.getId().equals(workspaceUserEntity.getWorkspaceUserRole().getId())) {
                        workspaceUserEntityController.updateWorkspaceUserRole(workspaceUserEntity, workspaceUserRole);
                    }
                    workspaceUserEntityController.unarchiveWorkspaceUserEntity(workspaceUserEntity);
                }
                if (!workspaceUserEntity.getActive().equals(event.getIsActive())) {
                    workspaceUserEntityController.updateActive(workspaceUserEntity, event.getIsActive());
                }
            } else {
                logger.warning("could not add workspace user because userSchoolDataIdentifier #" + event.getUserIdentifier() + '/' + event.getUserDataSource() + " could not be found");
            }
        } else {
            logger.warning("could not init workspace user because workspace role #" + event.getRoleIdentifier() + '/' + event.getRoleDataSource() + " could not be found");
        }
    } else {
        logger.warning("could not init workspace user because workspace entity #" + event.getWorkspaceIdentifier() + '/' + event.getWorkspaceDataSource() + " could not be found");
    }
}
Also used : UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceRoleEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceRoleEntity)

Example 18 with UserSchoolDataIdentifier

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

the class DefaultSchoolDataWorkspaceListener method onSchoolDataWorkspaceUserUpdatedEvent.

public void onSchoolDataWorkspaceUserUpdatedEvent(@Observes SchoolDataWorkspaceUserUpdatedEvent event) {
    UserEntity userEntity = userEntityController.findUserEntityByDataSourceAndIdentifier(event.getUserDataSource(), event.getUserIdentifier());
    if (userEntity != null) {
        WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceByDataSourceAndIdentifier(event.getWorkspaceDataSource(), event.getWorkspaceIdentifier());
        if (workspaceEntity != null) {
            SchoolDataIdentifier workspaceUserIdentifier = new SchoolDataIdentifier(event.getIdentifier(), event.getDataSource());
            WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityController.findWorkspaceUserEntityByWorkspaceUserIdentifierIncludeArchived(workspaceUserIdentifier);
            if (workspaceUserEntity != null) {
                String currentUserIdentifier = workspaceUserEntity.getUserSchoolDataIdentifier().getIdentifier();
                if (!StringUtils.equals(currentUserIdentifier, event.getUserIdentifier())) {
                    // WorkspaceUserEntity found, but it points to a new study program
                    UserSchoolDataIdentifier newUserIdentifier = userSchoolDataIdentifierController.findUserSchoolDataIdentifierByDataSourceAndIdentifier(event.getUserDataSource(), event.getUserIdentifier());
                    if (newUserIdentifier == null) {
                        logger.warning(String.format("Unable to update workspace user. UserSchoolDataIdentifier for %s/%s not found", event.getUserDataSource(), event.getUserIdentifier()));
                    } else {
                        WorkspaceUserEntity existingUser = workspaceUserEntityController.findWorkspaceUserEntityByWorkspaceAndUserSchoolDataIdentifierIncludeArchived(workspaceEntity, newUserIdentifier);
                        if (existingUser != null) {
                            if (!existingUser.getArchived().equals(workspaceUserEntity.getArchived())) {
                                if (existingUser.getArchived()) {
                                    workspaceUserEntityController.unarchiveWorkspaceUserEntity(existingUser);
                                } else {
                                    workspaceUserEntityController.archiveWorkspaceUserEntity(existingUser);
                                }
                            }
                            workspaceUserEntityController.updateIdentifier(existingUser, workspaceUserEntity.getIdentifier());
                            // #3308: If the new study program is active, reactivate the corresponding workspace student in Muikku
                            if (event.getIsActive() && !existingUser.getActive()) {
                                workspaceUserEntityController.updateActive(existingUser, event.getIsActive());
                            }
                            workspaceUserEntityController.archiveWorkspaceUserEntity(workspaceUserEntity);
                        } else {
                            workspaceUserEntityController.updateUserSchoolDataIdentifier(workspaceUserEntity, newUserIdentifier);
                            // #3308: If the new study program is active, reactivate the corresponding workspace student in Muikku
                            if (event.getIsActive() && !workspaceUserEntity.getActive()) {
                                workspaceUserEntityController.updateActive(workspaceUserEntity, event.getIsActive());
                            }
                        }
                    }
                } else {
                    WorkspaceRoleEntity workspaceRoleEntity = workspaceController.findWorkspaceRoleEntityByDataSourceAndIdentifier(event.getRoleDataSource(), event.getRoleIdentifier());
                    if (workspaceRoleEntity != null && !workspaceRoleEntity.getId().equals(workspaceUserEntity.getWorkspaceUserRole().getId())) {
                        workspaceUserEntityController.updateWorkspaceUserRole(workspaceUserEntity, workspaceRoleEntity);
                    }
                }
                if (!event.getIsActive() && workspaceUserEntity.getActive()) {
                    workspaceUserEntityController.updateActive(workspaceUserEntity, event.getIsActive());
                }
            }
        } else {
            logger.warning("could not update workspace user because workspace entity #" + event.getWorkspaceIdentifier() + '/' + event.getWorkspaceDataSource() + " could not be found");
        }
    } else {
        logger.warning("could not update workspace user because user entity #" + event.getUserIdentifier() + '/' + event.getUserDataSource() + " could not be found");
    }
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceRoleEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceRoleEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity)

Example 19 with UserSchoolDataIdentifier

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

the class FlagController method unflagStudent.

public void unflagStudent(Flag flag, SchoolDataIdentifier studentIdentifier) {
    UserSchoolDataIdentifier studentSchoolDataIdentifier = userSchoolDataIdentifierController.findUserSchoolDataIdentifierBySchoolDataIdentifier(studentIdentifier);
    if (studentSchoolDataIdentifier == null) {
        logger.severe(String.format("Could not find school data user by identifier %s", studentIdentifier));
        return;
    }
    FlagStudent flagStudent = flagStudentDAO.findByFlagAndStudentIdentifier(flag, studentSchoolDataIdentifier);
    if (flagStudent != null) {
        unflagStudent(flagStudent);
    }
}
Also used : UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) FlagStudent(fi.otavanopisto.muikku.model.users.FlagStudent)

Example 20 with UserSchoolDataIdentifier

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

the class FlagController method flagStudent.

public FlagStudent flagStudent(Flag flag, SchoolDataIdentifier studentIdentifier) {
    UserSchoolDataIdentifier studentSchoolDataIdentifier = userSchoolDataIdentifierController.findUserSchoolDataIdentifierBySchoolDataIdentifier(studentIdentifier);
    if (studentSchoolDataIdentifier == null) {
        logger.severe(String.format("Could not find school data user by identifier %s", studentIdentifier));
        return null;
    }
    FlagStudent flagStudent = flagStudentDAO.findByFlagAndStudentIdentifier(flag, studentSchoolDataIdentifier);
    if (flagStudent != null) {
        return flagStudent;
    }
    return flagStudentDAO.create(flag, studentSchoolDataIdentifier, new Date());
}
Also used : UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) FlagStudent(fi.otavanopisto.muikku.model.users.FlagStudent) Date(java.util.Date)

Aggregations

UserSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier)37 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)14 SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)14 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)11 UserGroupUserEntity (fi.otavanopisto.muikku.model.users.UserGroupUserEntity)10 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)10 ArrayList (java.util.ArrayList)10 UserGroupEntity (fi.otavanopisto.muikku.model.users.UserGroupEntity)9 EntityManager (javax.persistence.EntityManager)7 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)7 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)6 Path (javax.ws.rs.Path)6 FlagStudent (fi.otavanopisto.muikku.model.users.FlagStudent)5 User (fi.otavanopisto.muikku.schooldata.entity.User)5 WorkspaceRoleEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceRoleEntity)4 Date (java.util.Date)4 HashMap (java.util.HashMap)4 CommunicatorMessageId (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageId)3 Workspace (fi.otavanopisto.muikku.schooldata.entity.Workspace)3 Flag (fi.otavanopisto.muikku.model.users.Flag)2