use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class UserRESTService method listStudentAddressses.
@GET
@Path("/students/{ID}/addresses")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response listStudentAddressses(@PathParam("ID") String id) {
if (!sessionController.isLoggedIn()) {
return Response.status(Status.UNAUTHORIZED).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 studentEntity = userEntityController.findUserEntityByUserIdentifier(studentIdentifier);
if (studentEntity == null) {
return Response.status(Response.Status.BAD_REQUEST).entity(String.format("Could not find user entity for identifier %s", id)).build();
}
if (!studentEntity.getId().equals(sessionController.getLoggedUserEntity().getId())) {
if (!sessionController.hasEnvironmentPermission(MuikkuPermissions.LIST_STUDENT_ADDRESSES)) {
return Response.status(Status.FORBIDDEN).build();
}
}
List<UserAddress> addresses = userController.listUserAddresses(studentIdentifier);
Collections.sort(addresses, new Comparator<UserAddress>() {
@Override
public int compare(UserAddress o1, UserAddress o2) {
return o1.getDefaultAddress() ? -1 : o2.getDefaultAddress() ? 1 : 0;
}
});
return Response.ok(createRestModel(addresses.toArray(new UserAddress[0]))).build();
}
use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class UserRESTService method findUserBasicInfo.
@GET
@Path("/users/{ID}/basicinfo")
@RESTPermitUnimplemented
public Response findUserBasicInfo(@Context Request request, @PathParam("ID") String id) {
if (!sessionController.isLoggedIn()) {
return Response.status(Status.FORBIDDEN).build();
}
UserEntity userEntity = null;
SchoolDataIdentifier userIdentifier = SchoolDataIdentifier.fromId(id);
if (userIdentifier == null) {
if (!StringUtils.isNumeric(id)) {
return Response.status(Response.Status.BAD_REQUEST).entity(String.format("Invalid user id %s", id)).build();
}
userEntity = userEntityController.findUserEntityById(NumberUtils.createLong(id));
userIdentifier = new SchoolDataIdentifier(userEntity.getDefaultIdentifier(), userEntity.getDefaultSchoolDataSource().getIdentifier());
} else {
userEntity = userEntityController.findUserEntityByUserIdentifier(userIdentifier);
}
if (userEntity == null) {
return Response.status(Response.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);
schoolDataBridgeSessionController.startSystemSession();
try {
User user = userController.findUserByIdentifier(userIdentifier);
if (user == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
// TODO: User image
boolean hasImage = false;
return Response.ok(new UserBasicInfo(userEntity.getId(), user.getFirstName(), user.getLastName(), user.getNickName(), user.getStudyProgrammeName(), hasImage, user.hasEvaluationFees(), user.getCurriculumIdentifier())).cacheControl(cacheControl).tag(tag).build();
} finally {
schoolDataBridgeSessionController.endSystemSession();
}
}
use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class UserRESTService method searchStaffMembers.
@GET
@Path("/staffMembers")
@RESTPermit(handling = Handling.INLINE)
public Response searchStaffMembers(@QueryParam("searchString") String searchString, @QueryParam("properties") String properties, @QueryParam("workspaceEntityId") Long workspaceEntityId, @QueryParam("firstResult") @DefaultValue("0") Integer firstResult, @QueryParam("maxResults") @DefaultValue("10") Integer maxResults) {
if (!sessionController.isLoggedIn()) {
return Response.status(Status.FORBIDDEN).build();
}
List<fi.otavanopisto.muikku.rest.model.StaffMember> staffMembers = new ArrayList<>();
Set<Long> userGroupFilters = null;
Set<Long> workspaceFilters = workspaceEntityId == null ? null : Collections.singleton(workspaceEntityId);
List<SchoolDataIdentifier> userIdentifiers = null;
SearchProvider elasticSearchProvider = getProvider("elastic-search");
if (elasticSearchProvider != null) {
String[] fields;
if (StringUtils.isNumeric(searchString)) {
fields = new String[] { "firstName", "lastName", "userEntityId", "email" };
} else {
fields = new String[] { "firstName", "lastName", "email" };
}
List<EnvironmentRoleArchetype> nonStudentArchetypes = new ArrayList<>(Arrays.asList(EnvironmentRoleArchetype.values()));
nonStudentArchetypes.remove(EnvironmentRoleArchetype.STUDENT);
SearchResult result = elasticSearchProvider.searchUsers(searchString, fields, nonStudentArchetypes, userGroupFilters, workspaceFilters, userIdentifiers, false, false, false, firstResult, maxResults);
List<Map<String, Object>> results = result.getResults();
if (results != null && !results.isEmpty()) {
WorkspaceEntity workspaceEntity = workspaceEntityId == null ? null : workspaceEntityController.findWorkspaceEntityById(workspaceEntityId);
String[] propertyArray = StringUtils.isEmpty(properties) ? new String[0] : properties.split(",");
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;
}
if (studentIdentifier.getIdentifier().startsWith("STUDENT-")) {
// the elasticsearch query returns both. We need to filter them after the fact.
continue;
}
String email = userEmailEntityController.getUserDefaultEmailAddress(studentIdentifier, false);
Long userEntityId = new Long((Integer) o.get("userEntityId"));
UserEntity userEntity = userEntityController.findUserEntityById(userEntityId);
Map<String, String> propertyMap = new HashMap<String, String>();
if (userEntity != null) {
for (int i = 0; i < propertyArray.length; i++) {
UserEntityProperty userEntityProperty = userEntityController.getUserEntityPropertyByKey(userEntity, propertyArray[i]);
propertyMap.put(propertyArray[i], userEntityProperty == null ? null : userEntityProperty.getValue());
}
}
if (workspaceEntity != null) {
WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityController.findActiveWorkspaceUserByWorkspaceEntityAndUserEntity(workspaceEntity, userEntity);
if (workspaceUserEntity == null || workspaceUserEntity.getWorkspaceUserRole().getArchetype() != WorkspaceRoleArchetype.TEACHER) {
continue;
}
}
staffMembers.add(new fi.otavanopisto.muikku.rest.model.StaffMember(studentIdentifier.toId(), new Long((Integer) o.get("userEntityId")), (String) o.get("firstName"), (String) o.get("lastName"), email, propertyMap));
}
}
}
return Response.ok(staffMembers).build();
}
use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class UserRESTService method deleteStudentFlag.
@DELETE
@Path("/students/{STUDENTID}/flags/{ID}")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response deleteStudentFlag(@Context Request request, @PathParam("STUDENTID") String studentId, @PathParam("ID") Long id) {
if (!sessionController.isLoggedIn()) {
return Response.status(Status.FORBIDDEN).build();
}
SchoolDataIdentifier studentIdentifier = SchoolDataIdentifier.fromId(studentId);
if (studentIdentifier == null) {
return Response.status(Response.Status.BAD_REQUEST).entity(String.format("Invalid studentIdentifier %s", studentId)).build();
}
FlagStudent flagStudent = flagController.findFlagStudentById(id);
if (flagStudent == null) {
return Response.status(Response.Status.NOT_FOUND).entity(String.format("Flag not found %d", id)).build();
}
if (!flagController.hasFlagPermission(flagStudent.getFlag(), sessionController.getLoggedUser())) {
return Response.status(Status.FORBIDDEN).entity(String.format("You do not have permission to remove student flag %d", flagStudent.getId())).build();
}
flagController.unflagStudent(flagStudent);
return Response.noContent().build();
}
use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class UserRESTService method deleteFlag.
@DELETE
@Path("/flags/{ID}")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response deleteFlag(@PathParam("ID") long flagId) {
Flag flag = flagController.findFlagById(flagId);
if (flag == null) {
return Response.status(Status.NOT_FOUND).build();
}
boolean isOwner = false;
UserSchoolDataIdentifier ownerIdentifier = flag.getOwnerIdentifier();
SchoolDataIdentifier loggedIdentifier = sessionController.getLoggedUser();
if (loggedIdentifier == null) {
return Response.status(Status.BAD_REQUEST).entity("Must be logged in.").build();
}
UserSchoolDataIdentifier loggedUserIdentifier = userSchoolDataIdentifierController.findUserSchoolDataIdentifierBySchoolDataIdentifier(loggedIdentifier);
if (loggedUserIdentifier == null) {
return Response.status(Status.BAD_REQUEST).entity("No user school data identifier for logged user").build();
}
if (Objects.equals(ownerIdentifier.getIdentifier(), loggedUserIdentifier.getIdentifier()) && Objects.equals(ownerIdentifier.getDataSource().getIdentifier(), loggedUserIdentifier.getDataSource().getIdentifier())) {
isOwner = true;
}
if (!flagController.hasFlagPermission(flag, loggedIdentifier)) {
return Response.status(Status.FORBIDDEN).entity("You don't have the permission to delete this flag").build();
}
if (isOwner) {
flagController.deleteFlagCascade(flag);
return Response.noContent().build();
} else {
flagController.unshareFlag(flag, loggedUserIdentifier);
return Response.noContent().build();
}
}
Aggregations