use of fi.otavanopisto.muikku.model.users.Flag in project muikku by otavanopisto.
the class UserRESTService method searchStudents.
@GET
@Path("/students")
@RESTPermit(handling = Handling.INLINE)
public Response searchStudents(@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("userEntityId") Long userEntityId, @DefaultValue("false") @QueryParam("includeInactiveStudents") Boolean includeInactiveStudents, @DefaultValue("false") @QueryParam("includeHidden") Boolean includeHidden, @QueryParam("flags") Long[] flagIds) {
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();
}
List<Flag> flags = null;
if (flagIds != null && flagIds.length > 0) {
flags = new ArrayList<>(flagIds.length);
for (Long flagId : flagIds) {
Flag flag = flagController.findFlagById(flagId);
if (flag == null) {
return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid flag id %d", flagId)).build();
}
if (!flagController.hasFlagPermission(flag, sessionController.getLoggedUser())) {
return Response.status(Status.FORBIDDEN).entity(String.format("You don't have permission to use flag %d", flagId)).build();
}
flags.add(flag);
}
}
List<fi.otavanopisto.muikku.rest.model.Student> students = new ArrayList<>();
UserEntity loggedUser = sessionController.getLoggedUserEntity();
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);
}
}
List<SchoolDataIdentifier> userIdentifiers = null;
if (flags != null) {
if (userIdentifiers == null) {
userIdentifiers = new ArrayList<>();
}
userIdentifiers.addAll(flagController.getFlaggedStudents(flags));
}
if (Boolean.TRUE.equals(includeInactiveStudents)) {
if (!sessionController.hasEnvironmentPermission(MuikkuPermissions.LIST_INACTIVE_STUDENTS)) {
if (userEntityId == null) {
return Response.status(Status.FORBIDDEN).build();
} else {
if (!sessionController.getLoggedUserEntity().getId().equals(userEntityId)) {
return Response.status(Status.FORBIDDEN).build();
}
}
}
}
if (Boolean.TRUE.equals(includeHidden)) {
if (!sessionController.hasEnvironmentPermission(MuikkuPermissions.LIST_HIDDEN_STUDENTS)) {
if (userEntityId == null) {
return Response.status(Status.FORBIDDEN).build();
} else {
if (!sessionController.getLoggedUserEntity().getId().equals(userEntityId)) {
return Response.status(Status.FORBIDDEN).build();
}
}
}
}
if (userEntityId != null) {
List<SchoolDataIdentifier> userEntityIdentifiers = new ArrayList<>();
UserEntity userEntity = userEntityController.findUserEntityById(userEntityId);
if (userEntity == null) {
return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid userEntityId %d", userEntityId)).build();
}
List<UserSchoolDataIdentifier> schoolDataIdentifiers = userSchoolDataIdentifierController.listUserSchoolDataIdentifiersByUserEntity(userEntity);
for (UserSchoolDataIdentifier schoolDataIdentifier : schoolDataIdentifiers) {
userEntityIdentifiers.add(new SchoolDataIdentifier(schoolDataIdentifier.getIdentifier(), schoolDataIdentifier.getDataSource().getIdentifier()));
}
if (userIdentifiers == null) {
userIdentifiers = userEntityIdentifiers;
} else {
userIdentifiers.retainAll(userEntityIdentifiers);
}
}
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<>(myWorkspaceIds);
} else if (!CollectionUtils.isEmpty(workspaceIds)) {
// Defined workspaces
workspaceFilters = new HashSet<>(workspaceIds);
}
SearchProvider elasticSearchProvider = getProvider("elastic-search");
if (elasticSearchProvider != null) {
String[] fields = new String[] { "firstName", "lastName", "nickName", "email" };
SearchResult result = elasticSearchProvider.searchUsers(searchString, fields, Arrays.asList(EnvironmentRoleArchetype.STUDENT), userGroupFilters, workspaceFilters, userIdentifiers, includeInactiveStudents, includeHidden, false, firstResult, maxResults);
List<Map<String, Object>> results = result.getResults();
boolean hasImage = false;
if (results != null && !results.isEmpty()) {
for (Map<String, Object> o : results) {
String studentId = (String) o.get("id");
if (StringUtils.isBlank(studentId)) {
logger.severe("Could not process user found from search index because it had a null id");
continue;
}
String[] studentIdParts = studentId.split("/", 2);
SchoolDataIdentifier studentIdentifier = studentIdParts.length == 2 ? new SchoolDataIdentifier(studentIdParts[0], studentIdParts[1]) : null;
if (studentIdentifier == null) {
logger.severe(String.format("Could not process user found from search index with id %s", studentId));
continue;
}
UserEntity userEntity = userEntityController.findUserEntityByUserIdentifier(studentIdentifier);
String emailAddress = userEntity != null ? userEmailEntityController.getUserDefaultEmailAddress(userEntity, true) : null;
Date studyStartDate = getDateResult(o.get("studyStartDate"));
Date studyEndDate = getDateResult(o.get("studyEndDate"));
Date studyTimeEnd = getDateResult(o.get("studyTimeEnd"));
students.add(new fi.otavanopisto.muikku.rest.model.Student(studentIdentifier.toId(), (String) o.get("firstName"), (String) o.get("lastName"), (String) o.get("nickName"), (String) o.get("studyProgrammeName"), hasImage, (String) o.get("nationality"), (String) o.get("language"), (String) o.get("municipality"), (String) o.get("school"), emailAddress, studyStartDate, studyEndDate, studyTimeEnd, (String) o.get("curriculumIdentifier"), userEntity.getUpdatedByStudent()));
}
}
}
return Response.ok(students).build();
}
use of fi.otavanopisto.muikku.model.users.Flag in project muikku by otavanopisto.
the class UserRESTService method createFlagShare.
@DELETE
@Path("/flags/{FLAGID}/shares/{ID}")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response createFlagShare(@PathParam("FLAGID") Long flagId, @PathParam("ID") Long id) {
if (!sessionController.isLoggedIn()) {
return Response.status(Status.UNAUTHORIZED).build();
}
Flag flag = flagController.findFlagById(flagId);
if (flag == null) {
return Response.status(Status.NOT_FOUND).entity(String.format("Flag#%d not found", id)).build();
}
if (flag.getArchived()) {
return Response.status(Status.NOT_FOUND).entity(String.format("Flag#%d not found", id)).build();
}
if (!flagController.hasFlagPermission(flag, sessionController.getLoggedUser())) {
return Response.status(Status.FORBIDDEN).entity(String.format("You do not have permission to flag#%d", flag.getId())).build();
}
FlagShare flagShare = flagController.findFlagShare(id);
if (flagShare == null) {
return Response.status(Status.NOT_FOUND).entity(String.format("Could not find flag share %d", id)).build();
}
flagController.deleteFlagShare(flagShare);
return Response.noContent().build();
}
use of fi.otavanopisto.muikku.model.users.Flag in project muikku by otavanopisto.
the class AcceptanceTestsRESTService method createStudentFlag.
@POST
@Path("/students/{ID}/flags/{FLAGID}")
@RESTPermit(handling = Handling.UNSECURED)
public Response createStudentFlag(@PathParam("ID") Long studentId, @PathParam("FLAGID") Long flagId) {
SchoolDataIdentifier studentIdentifier = new SchoolDataIdentifier("STUDENT-" + studentId, "PYRAMUS");
Flag flag = flagController.findFlagById(flagId);
if (flag == null) {
return Response.status(Status.NOT_FOUND).entity(String.format("Flag #%d not found", flagId)).build();
}
return Response.ok(createRestEntity(flagController.flagStudent(flag, studentIdentifier))).build();
}
use of fi.otavanopisto.muikku.model.users.Flag in project muikku by otavanopisto.
the class WorkspaceRESTService method listWorkspaceStudents.
@GET
@Path("/workspaces/{ID}/students")
@RESTPermit(handling = Handling.INLINE)
public Response listWorkspaceStudents(@PathParam("ID") Long workspaceEntityId, @QueryParam("active") Boolean active, @QueryParam("requestedAssessment") Boolean requestedAssessment, @QueryParam("assessed") Boolean assessed, @QueryParam("studentIdentifier") String studentId, @QueryParam("search") String searchString, @QueryParam("flags") Long[] flagIds, @QueryParam("maxResults") Integer maxResults, @QueryParam("orderBy") String orderBy) {
List<SchoolDataIdentifier> studentIdentifiers = null;
if (StringUtils.isNotBlank(studentId)) {
SchoolDataIdentifier studentIdentifier = SchoolDataIdentifier.fromId(studentId);
if (studentIdentifier == null) {
return Response.status(Status.BAD_REQUEST).entity(String.format("Malformed student identifier %s", studentId)).build();
}
studentIdentifiers = Collections.singletonList(studentIdentifier);
}
// Workspace
WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspaceEntityId);
if (workspaceEntity == null) {
return Response.status(Status.NOT_FOUND).build();
}
// Access check
if (!sessionController.hasWorkspacePermission(MuikkuPermissions.LIST_WORKSPACE_MEMBERS, workspaceEntity)) {
if (studentIdentifiers == null || studentIdentifiers.size() != 1 || !studentIdentifiers.get(0).equals(sessionController.getLoggedUser())) {
return Response.status(Status.FORBIDDEN).build();
}
}
List<fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser> workspaceUsers = null;
if (searchString != null) {
studentIdentifiers = new ArrayList<>();
Iterator<SearchProvider> searchProviderIterator = searchProviders.iterator();
if (!searchProviderIterator.hasNext()) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity("No search provider found").build();
}
SearchProvider elasticSearchProvider = searchProviderIterator.next();
if (elasticSearchProvider != null) {
String[] fields = new String[] { "firstName", "lastName", "nickName", "email" };
SearchResult result = elasticSearchProvider.searchUsers(searchString, fields, Arrays.asList(EnvironmentRoleArchetype.STUDENT), (Collection<Long>) null, Collections.singletonList(workspaceEntityId), (Collection<SchoolDataIdentifier>) null, Boolean.FALSE, Boolean.FALSE, false, 0, maxResults != null ? maxResults : Integer.MAX_VALUE);
List<Map<String, Object>> results = result.getResults();
if (results != null && !results.isEmpty()) {
for (Map<String, Object> o : results) {
String foundStudentId = (String) o.get("id");
if (StringUtils.isBlank(foundStudentId)) {
logger.severe("Could not process user found from search index because it had a null id");
continue;
}
String[] studentIdParts = foundStudentId.split("/", 2);
SchoolDataIdentifier foundStudentIdentifier = studentIdParts.length == 2 ? new SchoolDataIdentifier(studentIdParts[0], studentIdParts[1]) : null;
if (foundStudentIdentifier == null) {
logger.severe(String.format("Could not process user found from search index with id %s", studentId));
continue;
}
studentIdentifiers.add(foundStudentIdentifier);
}
}
}
}
List<Flag> flags = null;
if (flagIds != null && flagIds.length > 0) {
flags = new ArrayList<>(flagIds.length);
for (Long flagId : flagIds) {
Flag flag = flagController.findFlagById(flagId);
if (flag == null) {
return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid flag id %d", flagId)).build();
}
if (!flagController.hasFlagPermission(flag, sessionController.getLoggedUser())) {
return Response.status(Status.FORBIDDEN).entity(String.format("You don't have permission to use flag %d", flagId)).build();
}
flags.add(flag);
}
}
if (flags != null) {
List<SchoolDataIdentifier> flaggedStudents = flagController.getFlaggedStudents(flags);
if (studentIdentifiers != null) {
studentIdentifiers.retainAll(flaggedStudents);
} else {
studentIdentifiers = flaggedStudents;
}
}
List<WorkspaceStudent> result = new ArrayList<>();
if (studentIdentifiers != null) {
workspaceUsers = new ArrayList<>();
for (SchoolDataIdentifier studentIdentifier : studentIdentifiers) {
WorkspaceUserEntity wue = workspaceUserEntityController.findWorkspaceUserByWorkspaceEntityAndUserIdentifier(workspaceEntity, studentIdentifier);
if (wue == null) {
continue;
}
if (active != null && !active.equals(wue.getActive())) {
continue;
}
WorkspaceUser workspaceUser = workspaceController.findWorkspaceUser(wue);
if (workspaceUser == null) {
continue;
}
workspaceUsers.add(workspaceUser);
}
} else {
// Students via WorkspaceSchoolDataBridge
workspaceUsers = workspaceController.listWorkspaceStudents(workspaceEntity);
}
if (workspaceUsers == null || workspaceUsers.isEmpty()) {
return Response.noContent().build();
}
Map<String, WorkspaceUserEntity> workspaceUserEntityMap = new HashMap<>();
List<WorkspaceUserEntity> workspaceUserEntities = workspaceUserEntityController.listWorkspaceUserEntities(workspaceEntity);
for (WorkspaceUserEntity workspaceUserEntity : workspaceUserEntities) {
workspaceUserEntityMap.put(new SchoolDataIdentifier(workspaceUserEntity.getIdentifier(), workspaceUserEntity.getUserSchoolDataIdentifier().getDataSource().getIdentifier()).toId(), workspaceUserEntity);
}
if (maxResults != null && workspaceUsers.size() > maxResults) {
workspaceUsers.subList(maxResults, workspaceUsers.size() - 1).clear();
}
for (fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser workspaceUser : workspaceUsers) {
SchoolDataIdentifier workspaceUserIdentifier = workspaceUser.getIdentifier();
WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityMap.get(workspaceUserIdentifier.toId());
if (workspaceUserEntity == null) {
logger.log(Level.WARNING, String.format("Workspace student %s does not exist in Muikku", workspaceUserIdentifier.toId()));
continue;
}
if (active == null || active.equals(workspaceUserEntity.getActive())) {
if (requestedAssessment != null) {
boolean hasAssessmentRequest = workspaceUserEntity != null && !assessmentRequestController.listByWorkspaceUser(workspaceUserEntity).isEmpty();
if (requestedAssessment != hasAssessmentRequest) {
continue;
}
}
if (assessed != null) {
boolean isAssessed = !gradingController.listWorkspaceAssessments(workspaceUser.getWorkspaceIdentifier(), workspaceUser.getUserIdentifier()).isEmpty();
if (assessed != isAssessed) {
continue;
}
}
SchoolDataIdentifier userIdentifier = workspaceUser.getUserIdentifier();
User user = userController.findUserByIdentifier(userIdentifier);
if (user != null) {
UserEntity userEntity = null;
if (workspaceUserEntity != null) {
userEntity = workspaceUserEntity.getUserSchoolDataIdentifier().getUserEntity();
} else {
userEntity = userEntityController.findUserEntityByDataSourceAndIdentifier(user.getSchoolDataSource(), user.getIdentifier());
}
result.add(createRestModel(userEntity, user, workspaceUser, workspaceUserEntity != null && workspaceUserEntity.getActive()));
} else {
logger.log(Level.SEVERE, String.format("Could not find user for identifier %s", userIdentifier));
}
}
}
// Sorting
if (StringUtils.equals(orderBy, "name")) {
Collections.sort(result, new Comparator<WorkspaceStudent>() {
@Override
public int compare(WorkspaceStudent o1, WorkspaceStudent o2) {
String s1 = String.format("%s, %s", StringUtils.defaultString(o1.getLastName(), ""), StringUtils.defaultString(o1.getFirstName(), ""));
String s2 = String.format("%s, %s", StringUtils.defaultString(o2.getLastName(), ""), StringUtils.defaultString(o2.getFirstName(), ""));
return s1.compareTo(s2);
}
});
}
// Response
return Response.ok(result).build();
}
Aggregations