Search in sources :

Example 1 with EnvironmentUser

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

the class UserRESTService method findStudent.

@GET
@Path("/students/{ID}")
@RESTPermit(handling = Handling.INLINE)
public Response findStudent(@Context Request request, @PathParam("ID") String id) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.FORBIDDEN).build();
    }
    SchoolDataIdentifier studentIdentifier = SchoolDataIdentifier.fromId(id);
    if (studentIdentifier == null) {
        return Response.status(Response.Status.BAD_REQUEST).entity(String.format("Invalid studentIdentifier %s", id)).build();
    }
    UserEntity userEntity = userEntityController.findUserEntityByUserIdentifier(studentIdentifier);
    if (userEntity == null) {
        return Response.status(Status.NOT_FOUND).entity("UserEntity not found").build();
    }
    // Bug fix #2966: REST endpoint should only return students
    EnvironmentUser environmentUser = environmentUserController.findEnvironmentUserByUserEntity(userEntity);
    if (environmentUser != null) {
        EnvironmentRoleEntity userRole = environmentUser.getRole();
        if (userRole == null || userRole.getArchetype() != EnvironmentRoleArchetype.STUDENT) {
            return Response.status(Status.NOT_FOUND).build();
        }
    }
    EntityTag tag = new EntityTag(DigestUtils.md5Hex(String.valueOf(userEntity.getVersion())));
    ResponseBuilder builder = request.evaluatePreconditions(tag);
    if (builder != null) {
        return builder.build();
    }
    CacheControl cacheControl = new CacheControl();
    cacheControl.setMustRevalidate(true);
    // TODO: There's no permission handling, this is relying on schooldatacontroller to check for permission
    User user = userController.findUserByIdentifier(studentIdentifier);
    if (user == null) {
        return Response.status(Status.NOT_FOUND).entity("User not found").build();
    }
    String emailAddress = userEmailEntityController.getUserDefaultEmailAddress(userEntity, true);
    Date studyStartDate = user.getStudyStartDate() != null ? Date.from(user.getStudyStartDate().toInstant()) : null;
    Date studyEndDate = user.getStudyEndDate() != null ? Date.from(user.getStudyEndDate().toInstant()) : null;
    Date studyTimeEnd = user.getStudyTimeEnd() != null ? Date.from(user.getStudyTimeEnd().toInstant()) : null;
    Student student = new Student(studentIdentifier.toId(), user.getFirstName(), user.getLastName(), user.getNickName(), user.getStudyProgrammeName(), false, user.getNationality(), user.getLanguage(), user.getMunicipality(), user.getSchool(), emailAddress, studyStartDate, studyEndDate, studyTimeEnd, user.getCurriculumIdentifier(), userEntity.getUpdatedByStudent());
    return Response.ok(student).cacheControl(cacheControl).tag(tag).build();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) 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) EntityTag(javax.ws.rs.core.EntityTag) CacheControl(javax.ws.rs.core.CacheControl) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) FlagStudent(fi.otavanopisto.muikku.model.users.FlagStudent) Student(fi.otavanopisto.muikku.rest.model.Student) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) Date(java.util.Date) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 2 with EnvironmentUser

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

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

the class EnvironmentUserDAO method create.

public EnvironmentUser create(UserEntity user, EnvironmentRoleEntity role) {
    EnvironmentUser environmentUser = new EnvironmentUser();
    environmentUser.setUser(user);
    environmentUser.setRole(role);
    getEntityManager().persist(environmentUser);
    return environmentUser;
}
Also used : EnvironmentUser(fi.otavanopisto.muikku.model.users.EnvironmentUser)

Example 4 with EnvironmentUser

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

the class EnvironmentUserDAO method listByArchived.

public List<EnvironmentUser> listByArchived(Boolean archived) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<EnvironmentUser> criteria = criteriaBuilder.createQuery(EnvironmentUser.class);
    Root<EnvironmentUser> root = criteria.from(EnvironmentUser.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.equal(root.get(EnvironmentUser_.archived), archived));
    return entityManager.createQuery(criteria).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EnvironmentUser(fi.otavanopisto.muikku.model.users.EnvironmentUser) EntityManager(javax.persistence.EntityManager)

Example 5 with EnvironmentUser

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

the class EnvironmentUserDAO method findByUserAndArchived.

public EnvironmentUser findByUserAndArchived(UserEntity user, Boolean archived) {
    EntityManager entityManager = getEntityManager();
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<EnvironmentUser> criteria = criteriaBuilder.createQuery(EnvironmentUser.class);
    Root<EnvironmentUser> root = criteria.from(EnvironmentUser.class);
    criteria.select(root);
    criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(EnvironmentUser_.user), user), criteriaBuilder.equal(root.get(EnvironmentUser_.archived), archived)));
    return getSingleResult(entityManager.createQuery(criteria));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EnvironmentUser(fi.otavanopisto.muikku.model.users.EnvironmentUser) EntityManager(javax.persistence.EntityManager)

Aggregations

EnvironmentUser (fi.otavanopisto.muikku.model.users.EnvironmentUser)11 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)8 User (fi.otavanopisto.muikku.schooldata.entity.User)5 EnvironmentRoleEntity (fi.otavanopisto.muikku.model.users.EnvironmentRoleEntity)4 SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)4 UserSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier)3 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)3 Path (javax.ws.rs.Path)3 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)2 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)2 EntityManager (javax.persistence.EntityManager)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 GET (javax.ws.rs.GET)2 CacheControl (javax.ws.rs.core.CacheControl)2 EntityTag (javax.ws.rs.core.EntityTag)2 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)2 Permission (fi.otavanopisto.muikku.model.security.Permission)1 EnvironmentRoleArchetype (fi.otavanopisto.muikku.model.users.EnvironmentRoleArchetype)1 FlagStudent (fi.otavanopisto.muikku.model.users.FlagStudent)1 RoleEntity (fi.otavanopisto.muikku.model.users.RoleEntity)1