use of fi.otavanopisto.muikku.rest.RESTPermitUnimplemented 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.rest.RESTPermitUnimplemented in project muikku by otavanopisto.
the class UserGroupRESTService method findById.
@GET
@Path("/groups/{ID}")
@RESTPermitUnimplemented
public Response findById(@PathParam("ID") Long groupId) {
if (!sessionController.isLoggedIn()) {
return Response.status(Status.FORBIDDEN).build();
}
UserGroupEntity userGroupEntity = userGroupEntityController.findUserGroupEntityById(groupId);
if (userGroupEntity == null) {
return Response.status(Status.NOT_FOUND).build();
}
UserGroup userGroup = userGroupController.findUserGroup(userGroupEntity);
if (userGroup == null) {
logger.severe("UserGroupEntity without UserGroup");
return Response.status(Status.NOT_FOUND).build();
}
Long userCount = userGroupEntityController.getGroupUserCount(userGroupEntity);
return Response.ok(new fi.otavanopisto.muikku.rest.model.UserGroup(userGroupEntity.getId(), userGroup.getName(), userCount)).build();
}
use of fi.otavanopisto.muikku.rest.RESTPermitUnimplemented 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.rest.RESTPermitUnimplemented in project muikku by otavanopisto.
the class UserRESTService method findUser.
@GET
@Path("/users/{ID}")
@RESTPermitUnimplemented
public Response findUser(@Context Request request, @PathParam("ID") Long id) {
if (!sessionController.isLoggedIn()) {
return Response.status(Status.FORBIDDEN).build();
}
UserEntity userEntity = userEntityController.findUserEntityById(id);
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);
User user = userController.findUserByDataSourceAndIdentifier(userEntity.getDefaultSchoolDataSource(), userEntity.getDefaultIdentifier());
if (user == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
return Response.ok(createRestModel(userEntity, user)).cacheControl(cacheControl).tag(tag).build();
}
use of fi.otavanopisto.muikku.rest.RESTPermitUnimplemented in project muikku by otavanopisto.
the class AssessmentRequestRESTService method deleteAssessmentRequest.
@DELETE
@Path("/workspace/{WORKSPACEENTITYID}/assessmentRequests/{ID}")
@RESTPermitUnimplemented
public Response deleteAssessmentRequest(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId, @PathParam("ID") String assessmentRequestId) {
if (!sessionController.isLoggedIn()) {
return Response.status(Status.UNAUTHORIZED).build();
}
if (assessmentRequestId == null) {
return Response.status(Status.BAD_REQUEST).entity("Invalid assessmentRequestIdentifier").build();
}
SchoolDataIdentifier assessmentRequestIdentifier = SchoolDataIdentifier.fromId(assessmentRequestId);
if (assessmentRequestIdentifier == null) {
return Response.status(Status.BAD_REQUEST).entity("Invalid assessmentRequestIdentifier").build();
}
WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspaceEntityId);
if (workspaceEntity == null) {
return Response.status(Status.NOT_FOUND).entity("Workspace entity not found").build();
}
WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityController.findActiveWorkspaceUserByWorkspaceEntityAndUserIdentifier(workspaceEntity, sessionController.getLoggedUser());
if (workspaceUserEntity == null) {
return Response.status(Status.NOT_FOUND).entity("Workspace user entity not found").build();
}
SchoolDataIdentifier workspaceIdentifier = new SchoolDataIdentifier(workspaceEntity.getIdentifier(), workspaceEntity.getDataSource().getIdentifier());
SchoolDataIdentifier studentIdentifier = new SchoolDataIdentifier(workspaceUserEntity.getUserSchoolDataIdentifier().getIdentifier(), workspaceUserEntity.getUserSchoolDataIdentifier().getDataSource().getIdentifier());
if (!sessionController.getLoggedUser().equals(studentIdentifier)) {
return Response.status(Status.FORBIDDEN).build();
}
SchoolDataIdentifier workspaceStudentIdentifier = new SchoolDataIdentifier(workspaceUserEntity.getIdentifier(), workspaceUserEntity.getUserSchoolDataIdentifier().getDataSource().getIdentifier());
WorkspaceAssessmentRequest assessmentRequest = assessmentRequestController.findWorkspaceAssessmentRequest(assessmentRequestIdentifier, workspaceIdentifier, studentIdentifier);
if (assessmentRequest != null) {
SchoolDataIdentifier assessmentRequestWorkspaceUserIdentifier = new SchoolDataIdentifier(assessmentRequest.getWorkspaceUserIdentifier(), assessmentRequest.getSchoolDataSource());
if (assessmentRequestWorkspaceUserIdentifier.equals(workspaceStudentIdentifier)) {
assessmentRequestController.deleteWorkspaceAssessmentRequest(workspaceUserEntity, assessmentRequestIdentifier);
communicatorAssessmentRequestController.sendAssessmentRequestCancelledMessage(workspaceUserEntity);
} else {
return Response.status(Status.FORBIDDEN).build();
}
} else {
return Response.status(Status.NOT_FOUND).entity("Could not find assessment request").build();
}
return Response.noContent().build();
}
Aggregations