use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class PyramusUpdater method updateCourseStaffMember.
/**
* Updates course staff member from Pyramus
*
* @param courseStaffMemberId id of course staff member in Pyramus
* @param courseId id of course in Pyramus
* @param staffMemberId id of staff member in Pyramus
* @return returns whether new staff member was created or not
*/
public boolean updateCourseStaffMember(Long courseStaffMemberId, Long courseId, Long staffMemberId) {
String workspaceIdentifier = identifierMapper.getWorkspaceIdentifier(courseId);
String identifier = identifierMapper.getWorkspaceStaffIdentifier(courseStaffMemberId);
// TODO: course staff member removals can be left unnoticed if
// webhooks fails because they do not have archived flag
WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityByDataSourceAndIdentifier(SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE, workspaceIdentifier);
if (workspaceEntity == null) {
updateCourse(courseId);
workspaceEntity = workspaceController.findWorkspaceEntityByDataSourceAndIdentifier(SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE, workspaceIdentifier);
}
if (workspaceEntity != null) {
SchoolDataIdentifier workspaceUserIdentifier = new SchoolDataIdentifier(identifier, SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE);
WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityController.findWorkspaceUserEntityByWorkspaceUserIdentifierIncludeArchived(workspaceUserIdentifier);
CourseStaffMember staffMember = pyramusClient.get().get("/courses/courses/" + courseId + "/staffMembers/" + courseStaffMemberId, CourseStaffMember.class);
if (staffMember != null) {
if (workspaceUserEntity == null) {
fireCourseStaffMemberDiscovered(staffMember);
return true;
} else {
fireCourseStaffMemberUpdated(staffMember);
}
} else {
if (workspaceUserEntity != null) {
fireCourseStaffMemberRemoved(courseStaffMemberId, staffMemberId, courseId);
return true;
}
}
}
return false;
}
use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class PyramusUpdater method updateCourseStaffMembers.
/**
* Updates course staff members from Pyramus
*
* @param courseId id of course in Pyramus
* @return count of updated courses staff members
*/
public int updateCourseStaffMembers(Long courseId) {
// List of staff members according to Muikku
List<WorkspaceUserEntity> currentStaffMembers;
String workspaceIdentifier = identifierMapper.getWorkspaceIdentifier(courseId);
WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceByDataSourceAndIdentifier(SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE, workspaceIdentifier);
if (workspaceEntity != null) {
currentStaffMembers = workspaceUserEntityController.listActiveWorkspaceStaffMembers(workspaceEntity);
} else {
currentStaffMembers = Collections.emptyList();
}
// List of staff members according to Pyramus
CourseStaffMember[] staffMembers = pyramusClient.get().get("/courses/courses/" + courseId + "/staffMembers", CourseStaffMember[].class);
if (staffMembers != null) {
for (CourseStaffMember staffMember : staffMembers) {
String staffIdentifier = identifierMapper.getWorkspaceStaffIdentifier(staffMember.getId());
SchoolDataIdentifier workspaceUserIdentifier = new SchoolDataIdentifier(staffIdentifier, SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE);
WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityController.findWorkspaceUserEntityByWorkspaceUserIdentifier(workspaceUserIdentifier);
if (workspaceUserEntity == null) {
fireCourseStaffMemberDiscovered(staffMember);
} else {
for (int i = currentStaffMembers.size() - 1; i >= 0; i--) {
if (currentStaffMembers.get(i).getId().equals(workspaceUserEntity.getId())) {
currentStaffMembers.remove(i);
}
}
}
}
}
for (WorkspaceUserEntity currentStaffMember : currentStaffMembers) {
try {
String courseStaffMemberIdentifier = currentStaffMember.getIdentifier();
String staffMemberIdentifier = currentStaffMember.getUserSchoolDataIdentifier().getUserEntity().getDefaultIdentifier();
Long courseStaffMemberId = identifierMapper.getPyramusCourseStaffId(courseStaffMemberIdentifier);
Long staffMemberId = identifierMapper.getPyramusStaffId(staffMemberIdentifier);
fireCourseStaffMemberRemoved(courseStaffMemberId, staffMemberId, courseId);
} catch (Exception e) {
logger.log(Level.WARNING, String.format("Error re-syncing WorkspaceUserEntity %d", currentStaffMember.getId()), e);
}
}
return 0;
}
use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class PyramusUserSchoolDataBridge method listUserEmailsByUserIdentifier.
@Override
public List<UserEmail> listUserEmailsByUserIdentifier(String userIdentifier) {
Email[] emails = null;
Long studentId = identifierMapper.getPyramusStudentId(userIdentifier);
if (studentId != null) {
emails = pyramusClient.get(String.format("/students/students/%d/emails", studentId), Email[].class);
} else {
Long staffId = identifierMapper.getPyramusStaffId(userIdentifier);
if (staffId != null) {
emails = pyramusClient.get(String.format("/staff/members/%d/emails", staffId), Email[].class);
} else {
logger.severe(String.format("PyramusUserSchoolDataBridge.listUserEmailsByUserIdentifier malformed user identifier %s\n%s", userIdentifier, ExceptionUtils.getStackTrace(new Throwable())));
}
}
if (emails != null) {
List<UserEmail> result = new ArrayList<>(emails.length);
for (Email email : emails) {
ContactType contactType = email != null ? pyramusClient.get("/common/contactTypes/" + email.getContactTypeId(), ContactType.class) : null;
UserEmail userEmail = entityFactory.createEntity(new SchoolDataIdentifier(userIdentifier, getSchoolDataSource()), email, contactType);
if (userEmail != null) {
result.add(userEmail);
}
}
return result;
}
return Collections.emptyList();
}
use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class UserGroupRESTService method searchUserGroups.
@GET
@Path("/groups")
@RESTPermitUnimplemented
public Response searchUserGroups(@QueryParam("userIdentifier") String userIdentifier, @QueryParam("searchString") String searchString, @QueryParam("firstResult") @DefaultValue("0") Integer firstResult, @QueryParam("maxResults") @DefaultValue("10") Integer maxResults) {
if (!sessionController.isLoggedIn()) {
return Response.status(Status.FORBIDDEN).build();
}
List<UserGroupEntity> entities = new ArrayList<>();
if (userIdentifier != null) {
SchoolDataIdentifier identifier = SchoolDataIdentifier.fromId(userIdentifier);
if (identifier == null) {
Response.status(Status.BAD_REQUEST).entity("Malformed userIdentifier").build();
}
UserEntity loggedUserEntity = sessionController.getLoggedUserEntity();
UserEntity userEntity = userEntityController.findUserEntityByUserIdentifier(identifier);
if (userEntity == null) {
return Response.status(Status.NOT_FOUND).build();
}
// Check for group-user-only roles - no shared groups, no rights
if (sessionController.hasEnvironmentPermission(RoleFeatures.ACCESS_ONLY_GROUP_STUDENTS) && !userGroupEntityController.haveSharedUserGroups(loggedUserEntity, userEntity)) {
return Response.status(Status.FORBIDDEN).build();
}
if (!(loggedUserEntity.getId().equals(userEntity.getId()) || sessionController.hasEnvironmentPermission(MuikkuPermissions.LIST_USER_USERGROUPS))) {
return Response.status(Status.FORBIDDEN).build();
}
if (identifier != null) {
entities = userGroupEntityController.listUserGroupsByUserIdentifier(identifier);
// For someone with the role feature the group entities are not necessarily accessible
if (sessionController.hasEnvironmentPermission(RoleFeatures.ACCESS_ONLY_GROUP_STUDENTS)) {
List<UserGroupEntity> guiderGroups = userGroupEntityController.listUserGroupsByUserEntity(loggedUserEntity);
Set<Long> guiderGroupIds = guiderGroups.stream().map(UserGroupEntity::getId).collect(Collectors.toSet());
entities = entities.stream().filter((UserGroupEntity uge) -> guiderGroupIds.contains(uge.getId())).collect(Collectors.toList());
}
}
} else {
SearchProvider elasticSearchProvider = getProvider("elastic-search");
if (elasticSearchProvider != null) {
String[] fields = new String[] { "name" };
SearchResult result = null;
if (StringUtils.isBlank(searchString)) {
result = elasticSearchProvider.matchAllSearch(firstResult, maxResults, UserGroup.class);
} else {
result = elasticSearchProvider.search(searchString, fields, firstResult, maxResults, UserGroup.class);
}
List<Map<String, Object>> results = result.getResults();
if (!results.isEmpty()) {
for (Map<String, Object> o : results) {
String[] id = ((String) o.get("id")).split("/", 2);
UserGroupEntity userGroupEntity = userGroupEntityController.findUserGroupEntityByDataSourceAndIdentifier(id[1], id[0]);
if (userGroupEntity != null) {
entities.add(userGroupEntity);
}
}
}
}
}
if (entities.isEmpty()) {
return Response.noContent().build();
} else {
List<fi.otavanopisto.muikku.rest.model.UserGroup> ret = new ArrayList<fi.otavanopisto.muikku.rest.model.UserGroup>();
for (UserGroupEntity entity : entities) {
Long userCount = userGroupEntityController.getGroupUserCount(entity);
UserGroup group = userGroupController.findUserGroup(entity);
if (group != null)
ret.add(new fi.otavanopisto.muikku.rest.model.UserGroup(entity.getId(), group.getName(), userCount));
else
logger.log(Level.WARNING, "Group not found");
}
return Response.ok(ret).build();
}
}
use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class UserRESTService method updateStudent.
@PUT
@Path("/students/{ID}")
@RESTPermit(handling = Handling.INLINE)
public Response updateStudent(@PathParam("ID") String id, Student student) {
if (!sessionController.isLoggedIn()) {
return Response.status(Status.FORBIDDEN).build();
}
SchoolDataIdentifier loggedUserIdentifier = sessionController.getLoggedUser();
SchoolDataIdentifier studentIdentifier = SchoolDataIdentifier.fromId(id);
if (studentIdentifier == null) {
return Response.status(Response.Status.BAD_REQUEST).entity(String.format("Invalid studentIdentifier %s", id)).build();
}
if (!(Objects.equals(studentIdentifier.getIdentifier(), loggedUserIdentifier.getIdentifier()) && Objects.equals(studentIdentifier.getDataSource(), loggedUserIdentifier.getDataSource()))) {
return Response.status(Status.FORBIDDEN).entity("Trying to modify non-logged-in student").build();
}
User user = userController.findUserByIdentifier(studentIdentifier);
UserEntity userEntity = userEntityController.findUserEntityByUser(user);
userEntityController.markAsUpdatedByStudent(userEntity);
// TODO: update other fields too
user.setMunicipality(student.getMunicipality());
userController.updateUser(user);
return Response.ok().entity(student).build();
}
Aggregations