Search in sources :

Example 36 with UserEntity

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

the class SchoolDataSearchReindexListener method reindexUsers.

private boolean reindexUsers() {
    try {
        List<UserEntity> users = userEntityController.listUserEntities();
        int userIndex = getOffset("userIndex");
        if (userIndex < users.size()) {
            int last = Math.min(users.size(), userIndex + getBatchSize());
            for (int i = userIndex; i < last; i++) {
                try {
                    UserEntity userEntity = users.get(i);
                    userIndexer.indexUser(userEntity);
                } catch (Exception uex) {
                    logger.log(Level.SEVERE, "Failed indexing userentity", uex);
                }
            }
            logger.log(Level.INFO, "Reindexed batch of users (" + userIndex + "-" + last + ")");
            setOffset("userIndex", userIndex + getBatchSize());
            return false;
        } else
            return true;
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Could not finish indexing user entities.", ex);
        return true;
    }
}
Also used : UserEntity(fi.otavanopisto.muikku.model.users.UserEntity)

Example 37 with UserEntity

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

the class UserIndexer method indexUser.

public void indexUser(String dataSource, String identifier) {
    schoolDataBridgeSessionController.startSystemSession();
    try {
        User user = userController.findUserByDataSourceAndIdentifier(dataSource, identifier);
        if (user != null) {
            EnvironmentRoleArchetype archetype = null;
            UserEntity userEntity = userEntityController.findUserEntityByDataSourceAndIdentifier(user.getSchoolDataSource(), user.getIdentifier());
            if (userEntity != null) {
                EnvironmentUser eu = environmentUserController.findEnvironmentUserByUserEntity(userEntity);
                if ((eu != null) && (eu.getRole() != null))
                    archetype = eu.getRole().getArchetype();
            }
            if ((archetype != null) && (userEntity != null)) {
                SchoolDataIdentifier userIdentifier = new SchoolDataIdentifier(user.getIdentifier(), user.getSchoolDataSource());
                boolean isDefaultIdentifier = (userEntity.getDefaultIdentifier() != null && userEntity.getDefaultSchoolDataSource() != null) ? userEntity.getDefaultIdentifier().equals(user.getIdentifier()) && userEntity.getDefaultSchoolDataSource().getIdentifier().equals(user.getSchoolDataSource()) : false;
                Map<String, Object> extra = new HashMap<>();
                extra.put("archetype", archetype);
                extra.put("userEntityId", userEntity.getId());
                extra.put("isDefaultIdentifier", isDefaultIdentifier);
                Set<Long> workspaceEntityIds = new HashSet<Long>();
                Set<Long> userGroupIds = new HashSet<Long>();
                // List workspaces in which the student is active (TODO Should we have a separate variable for all workspaces?)
                List<WorkspaceEntity> workspaces = workspaceUserEntityController.listActiveWorkspaceEntitiesByUserIdentifier(userIdentifier);
                for (WorkspaceEntity workspace : workspaces) {
                    workspaceEntityIds.add(workspace.getId());
                }
                extra.put("workspaces", workspaceEntityIds);
                List<UserGroupEntity> userGroups = userGroupEntityController.listUserGroupsByUserIdentifier(userIdentifier);
                for (UserGroupEntity userGroup : userGroups) {
                    userGroupIds.add(userGroup.getId());
                }
                extra.put("groups", userGroupIds);
                if (EnvironmentRoleArchetype.TEACHER.equals(archetype) || EnvironmentRoleArchetype.STUDY_GUIDER.equals(archetype) || EnvironmentRoleArchetype.STUDY_PROGRAMME_LEADER.equals(archetype) || EnvironmentRoleArchetype.MANAGER.equals(archetype) || EnvironmentRoleArchetype.ADMINISTRATOR.equals(archetype)) {
                    String userDefaultEmailAddress = userEmailEntityController.getUserDefaultEmailAddress(userEntity, false);
                    extra.put("email", userDefaultEmailAddress);
                }
                indexer.index(User.class.getSimpleName(), user, extra);
            } else
                indexer.index(User.class.getSimpleName(), user);
        } else {
            logger.info(String.format("Removing user %s/%s from index", identifier, dataSource));
            removeUser(dataSource, identifier);
        }
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Indexing of user identifier " + identifier + " failed.", ex);
    } finally {
        schoolDataBridgeSessionController.endSystemSession();
    }
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) EnvironmentUser(fi.otavanopisto.muikku.model.users.EnvironmentUser) User(fi.otavanopisto.muikku.schooldata.entity.User) EnvironmentUser(fi.otavanopisto.muikku.model.users.EnvironmentUser) EnvironmentRoleArchetype(fi.otavanopisto.muikku.model.users.EnvironmentRoleArchetype) HashMap(java.util.HashMap) UserGroupEntity(fi.otavanopisto.muikku.model.users.UserGroupEntity) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) HashSet(java.util.HashSet)

Example 38 with UserEntity

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

the class WorkspaceJournalEntryDAO method listByWorkspaceEntityAndUserEntities.

public List<WorkspaceJournalEntry> listByWorkspaceEntityAndUserEntities(WorkspaceEntity workspaceEntity, Collection<UserEntity> userEntities, int firstResult, int maxResults) {
    EntityManager entityManager = getEntityManager();
    Set<Long> userEntityIds = userEntities.stream().map(userEntity -> userEntity.getId()).collect(Collectors.toSet());
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<WorkspaceJournalEntry> criteria = criteriaBuilder.createQuery(WorkspaceJournalEntry.class);
    Root<WorkspaceJournalEntry> root = criteria.from(WorkspaceJournalEntry.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(WorkspaceJournalEntry_.workspaceEntityId), workspaceEntity.getId()), root.get(WorkspaceJournalEntry_.userEntityId).in(userEntityIds), criteriaBuilder.equal(root.get(WorkspaceJournalEntry_.archived), Boolean.FALSE)));
    criteria.orderBy(criteriaBuilder.desc(root.get(WorkspaceJournalEntry_.created)));
    TypedQuery<WorkspaceJournalEntry> query = entityManager.createQuery(criteria);
    query.setFirstResult(firstResult);
    query.setMaxResults(maxResults);
    return query.getResultList();
}
Also used : CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Date(java.util.Date) WorkspaceJournalEntry(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceJournalEntry) Collection(java.util.Collection) Set(java.util.Set) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) EntityManager(javax.persistence.EntityManager) Collectors(java.util.stream.Collectors) TypedQuery(javax.persistence.TypedQuery) List(java.util.List) WorkspaceJournalEntry_(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceJournalEntry_) CorePluginsDAO(fi.otavanopisto.muikku.plugins.CorePluginsDAO) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Root(javax.persistence.criteria.Root) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) WorkspaceJournalEntry(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceJournalEntry) EntityManager(javax.persistence.EntityManager)

Example 39 with UserEntity

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

the class ForgotPasswordController method getUsername.

public String getUsername(String confirmationHash) {
    UserPendingPasswordChange userPendingPasswordChange = userPendingPasswordChangeDAO.findByConfirmationHash(confirmationHash);
    if (userPendingPasswordChange != null) {
        Long userEntityId = userPendingPasswordChange.getUserEntity();
        if (userEntityId == null) {
            logger.severe(String.format("UserPendingPasswordChange with hash %s did not contain userEnityId", confirmationHash));
            return null;
        }
        UserEntity userEntity = userEntityController.findUserEntityById(userEntityId);
        if (userEntity == null) {
            logger.severe(String.format("UserPendingPasswordChange with hash %s contained invalid userEnityId", confirmationHash));
            return null;
        }
        schoolDataBridgeSessionController.startSystemSession();
        try {
            User user = userSchoolDataController.findUser(userEntity.getDefaultSchoolDataSource(), userEntity.getDefaultIdentifier());
            if (user == null) {
                logger.severe(String.format("Failed to retrieve user for UserEntity %d", userEntity.getId()));
                return null;
            }
            SchoolDataIdentifier userIdentifier = new SchoolDataIdentifier(user.getIdentifier(), user.getSchoolDataSource());
            try {
                return userSchoolDataController.findUsername(user);
            } catch (Exception e) {
                logger.log(Level.SEVERE, String.format("Failed to fetch username for user %s", userIdentifier.toId()));
                return null;
            }
        } finally {
            schoolDataBridgeSessionController.endSystemSession();
        }
    }
    return null;
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) User(fi.otavanopisto.muikku.schooldata.entity.User) UserPendingPasswordChange(fi.otavanopisto.muikku.plugins.user.UserPendingPasswordChange) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) SchoolDataBridgeUnauthorizedException(fi.otavanopisto.muikku.schooldata.SchoolDataBridgeUnauthorizedException)

Example 40 with UserEntity

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

the class ForgotPasswordRESTService method confirmResetPassword.

@Path("/confirm")
@GET
@RESTPermitUnimplemented
public Response confirmResetPassword(ConfirmResetPassword crp) {
    UserPendingPasswordChange passwordChange = userPendingPasswordChangeDAO.findByConfirmationHash(crp.getResetCode());
    UserEntity userEntity = userEntityController.findUserEntityById(passwordChange.getUserEntity());
    // TODO: tis a guesstimate of the datasource
    SchoolDataSource schoolDataSource = userEntity.getDefaultSchoolDataSource();
    try {
        userSchoolDataController.confirmResetPassword(schoolDataSource, crp.getResetCode(), crp.getNewPassword());
        return Response.noContent().build();
    } catch (SchoolDataBridgeUnauthorizedException e) {
        return Response.status(Status.FORBIDDEN).build();
    }
}
Also used : SchoolDataSource(fi.otavanopisto.muikku.model.base.SchoolDataSource) UserPendingPasswordChange(fi.otavanopisto.muikku.plugins.user.UserPendingPasswordChange) SchoolDataBridgeUnauthorizedException(fi.otavanopisto.muikku.schooldata.SchoolDataBridgeUnauthorizedException) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) Path(javax.ws.rs.Path) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented) GET(javax.ws.rs.GET)

Aggregations

UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)163 Path (javax.ws.rs.Path)101 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)88 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)65 GET (javax.ws.rs.GET)58 SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)54 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)50 User (fi.otavanopisto.muikku.schooldata.entity.User)36 ArrayList (java.util.ArrayList)35 UserSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier)27 POST (javax.ws.rs.POST)26 Date (java.util.Date)24 CommunicatorMessageId (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessageId)22 HashMap (java.util.HashMap)20 WorkspaceMaterial (fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial)18 EnvironmentUser (fi.otavanopisto.muikku.model.users.EnvironmentUser)14 CommunicatorMessage (fi.otavanopisto.muikku.plugins.communicator.model.CommunicatorMessage)14 RESTPermitUnimplemented (fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)13 UserGroupEntity (fi.otavanopisto.muikku.model.users.UserGroupEntity)12 PUT (javax.ws.rs.PUT)12