use of fi.otavanopisto.muikku.model.users.UserGroupEntity in project muikku by otavanopisto.
the class UserRESTService method searchUsers.
@GET
@Path("/users")
@RESTPermitUnimplemented
public Response searchUsers(@QueryParam("searchString") String searchString, @QueryParam("firstResult") @DefaultValue("0") Integer firstResult, @QueryParam("maxResults") @DefaultValue("10") Integer maxResults, @QueryParam("userGroupIds") List<Long> userGroupIds, @QueryParam("myUserGroups") Boolean myUserGroups, @QueryParam("workspaceIds") List<Long> workspaceIds, @QueryParam("myWorkspaces") Boolean myWorkspaces, @QueryParam("archetype") String archetype, @DefaultValue("false") @QueryParam("onlyDefaultUsers") Boolean onlyDefaultUsers) {
if (!sessionController.isLoggedIn()) {
return Response.status(Status.FORBIDDEN).build();
}
if (CollectionUtils.isNotEmpty(userGroupIds) && Boolean.TRUE.equals(myUserGroups))
return Response.status(Status.BAD_REQUEST).build();
if (CollectionUtils.isNotEmpty(workspaceIds) && Boolean.TRUE.equals(myWorkspaces))
return Response.status(Status.BAD_REQUEST).build();
UserEntity loggedUser = sessionController.getLoggedUserEntity();
EnvironmentRoleArchetype roleArchetype = archetype != null ? EnvironmentRoleArchetype.valueOf(archetype) : null;
Set<Long> userGroupFilters = null;
Set<Long> workspaceFilters = null;
if (!sessionController.hasEnvironmentPermission(RoleFeatures.ACCESS_ONLY_GROUP_STUDENTS)) {
if ((myUserGroups != null) && myUserGroups) {
userGroupFilters = new HashSet<Long>();
// Groups where user is a member
List<UserGroupEntity> userGroups = userGroupEntityController.listUserGroupsByUserIdentifier(sessionController.getLoggedUser());
for (UserGroupEntity userGroup : userGroups) {
userGroupFilters.add(userGroup.getId());
}
} else if (!CollectionUtils.isEmpty(userGroupIds)) {
userGroupFilters = new HashSet<Long>();
// Defined user groups
userGroupFilters.addAll(userGroupIds);
}
} else {
// User can only list users from his/her own user groups
userGroupFilters = new HashSet<Long>();
// Groups where user is a member and the ids of the groups
List<UserGroupEntity> userGroups = userGroupEntityController.listUserGroupsByUserIdentifier(sessionController.getLoggedUser());
Set<Long> accessibleUserGroupEntityIds = userGroups.stream().map(UserGroupEntity::getId).collect(Collectors.toSet());
if (CollectionUtils.isNotEmpty(userGroupIds)) {
// if there are specified user groups, they need to be subset of the groups that the user can access
if (!CollectionUtils.isSubCollection(userGroupIds, accessibleUserGroupEntityIds))
return Response.status(Status.BAD_REQUEST).build();
userGroupFilters.addAll(userGroupIds);
} else {
userGroupFilters.addAll(accessibleUserGroupEntityIds);
}
}
if ((myWorkspaces != null) && myWorkspaces) {
// Workspaces where user is a member
List<WorkspaceEntity> workspaces = workspaceUserEntityController.listWorkspaceEntitiesByUserEntity(loggedUser);
Set<Long> myWorkspaceIds = new HashSet<Long>();
for (WorkspaceEntity ws : workspaces) myWorkspaceIds.add(ws.getId());
workspaceFilters = new HashSet<Long>(myWorkspaceIds);
} else if (!CollectionUtils.isEmpty(workspaceIds)) {
// Defined workspaces
workspaceFilters = new HashSet<Long>(workspaceIds);
}
SearchProvider elasticSearchProvider = getProvider("elastic-search");
if (elasticSearchProvider != null) {
String[] fields = new String[] { "firstName", "lastName", "nickName", "email" };
SearchResult result = elasticSearchProvider.searchUsers(searchString, fields, roleArchetype != null ? Arrays.asList(roleArchetype) : null, userGroupFilters, workspaceFilters, null, false, false, onlyDefaultUsers, firstResult, maxResults);
List<Map<String, Object>> results = result.getResults();
boolean hasImage = false;
List<fi.otavanopisto.muikku.rest.model.User> ret = new ArrayList<fi.otavanopisto.muikku.rest.model.User>();
if (!results.isEmpty()) {
for (Map<String, Object> o : results) {
String[] id = ((String) o.get("id")).split("/", 2);
UserEntity userEntity = userEntityController.findUserEntityByDataSourceAndIdentifier(id[1], id[0]);
if (userEntity != null) {
String emailAddress = userEmailEntityController.getUserDefaultEmailAddress(userEntity, true);
Date studyStartDate = getDateResult(o.get("studyStartDate"));
Date studyTimeEnd = getDateResult(o.get("studyTimeEnd"));
ret.add(new fi.otavanopisto.muikku.rest.model.User(userEntity.getId(), (String) o.get("firstName"), (String) o.get("lastName"), (String) o.get("nickName"), hasImage, (String) o.get("nationality"), (String) o.get("language"), (String) o.get("municipality"), (String) o.get("school"), emailAddress, studyStartDate, studyTimeEnd));
}
}
return Response.ok(ret).build();
} else
return Response.noContent().build();
}
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
}
use of fi.otavanopisto.muikku.model.users.UserGroupEntity 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();
}
}
use of fi.otavanopisto.muikku.model.users.UserGroupEntity in project muikku by otavanopisto.
the class UserGroupEntityDAO method listByUserIdentifierExcludeArchived.
public List<UserGroupEntity> listByUserIdentifierExcludeArchived(UserSchoolDataIdentifier userSchoolDataIdentifier) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<UserGroupEntity> criteria = criteriaBuilder.createQuery(UserGroupEntity.class);
Root<UserGroupUserEntity> root = criteria.from(UserGroupUserEntity.class);
Join<UserGroupUserEntity, UserGroupEntity> groupJoin = root.join(UserGroupUserEntity_.userGroupEntity);
criteria.select(root.get(UserGroupUserEntity_.userGroupEntity));
criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(UserGroupUserEntity_.userSchoolDataIdentifier), userSchoolDataIdentifier), criteriaBuilder.equal(groupJoin.get(UserGroupEntity_.archived), Boolean.FALSE), criteriaBuilder.equal(root.get(UserGroupUserEntity_.archived), Boolean.FALSE)));
return entityManager.createQuery(criteria).getResultList();
}
use of fi.otavanopisto.muikku.model.users.UserGroupEntity in project muikku by otavanopisto.
the class UserGroupEntityDAO method listByDataSource.
public List<UserGroupEntity> listByDataSource(SchoolDataSource schoolDataSource, Integer firstResult, Integer maxResults) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<UserGroupEntity> criteria = criteriaBuilder.createQuery(UserGroupEntity.class);
Root<UserGroupEntity> root = criteria.from(UserGroupEntity.class);
criteria.select(root);
criteria.where(criteriaBuilder.equal(root.get(UserGroupEntity_.schoolDataSource), schoolDataSource));
TypedQuery<UserGroupEntity> query = entityManager.createQuery(criteria);
if (firstResult != null) {
query.setFirstResult(firstResult);
}
if (maxResults != null) {
query.setMaxResults(maxResults);
}
return query.getResultList();
}
use of fi.otavanopisto.muikku.model.users.UserGroupEntity in project muikku by otavanopisto.
the class UserGroupEntityDAO method findByDataSourceAndIdentifierAndArchived.
public UserGroupEntity findByDataSourceAndIdentifierAndArchived(SchoolDataSource schoolDataSource, String identifier, Boolean archived) {
EntityManager entityManager = getEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<UserGroupEntity> criteria = criteriaBuilder.createQuery(UserGroupEntity.class);
Root<UserGroupEntity> root = criteria.from(UserGroupEntity.class);
criteria.select(root);
criteria.where(criteriaBuilder.and(criteriaBuilder.equal(root.get(UserGroupEntity_.archived), archived), criteriaBuilder.equal(root.get(UserGroupEntity_.schoolDataSource), schoolDataSource), criteriaBuilder.equal(root.get(UserGroupEntity_.identifier), identifier)));
return getSingleResult(entityManager.createQuery(criteria));
}
Aggregations