use of fi.otavanopisto.muikku.model.users.UserGroupEntity in project muikku by otavanopisto.
the class PyramusUpdater method updateStudentGroupUsers.
public int updateStudentGroupUsers(Long studentGroupId) {
String userGroupIdentifier = identifierMapper.getStudentGroupIdentifier(studentGroupId);
UserGroupEntity userGroupEntity = userGroupEntityController.findUserGroupEntityByDataSourceAndIdentifier(SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE, userGroupIdentifier, true);
if (userGroupEntity != null) {
if (userGroupEntity.getArchived()) {
// Just skip archived user groups
return 0;
}
List<UserGroupUserEntity> existingUsers = userGroupEntityController.listUserGroupUserEntitiesByUserGroupEntity(userGroupEntity);
List<String> existingGroupUserIds = new ArrayList<String>();
for (UserGroupUserEntity existingUser : existingUsers) {
existingGroupUserIds.add(existingUser.getIdentifier());
}
List<String> foundGroupUserIds = new ArrayList<String>();
int count = 0;
StudentGroupUser[] userGroupStaffMembers = pyramusClient.get().get(String.format("/students/studentGroups/%d/staffmembers", studentGroupId), StudentGroupUser[].class);
if (userGroupStaffMembers != null) {
for (StudentGroupUser sgStaffMember : userGroupStaffMembers) {
String identifier = identifierMapper.getStudentGroupStaffMemberIdentifier(sgStaffMember.getId());
foundGroupUserIds.add(identifier);
// If not existing, then it's a new one
if (!existingGroupUserIds.contains(identifier)) {
String staffMemberIdentifier = identifierMapper.getStaffIdentifier(sgStaffMember.getStaffMemberId());
fireUserGroupUserDiscovered(identifier, userGroupIdentifier, staffMemberIdentifier);
}
}
count += userGroupStaffMembers.length;
}
StudentGroupStudent[] userGroupStudents = pyramusClient.get().get(String.format("/students/studentGroups/%d/students", studentGroupId), StudentGroupStudent[].class);
if (userGroupStudents != null) {
for (StudentGroupStudent sgs : userGroupStudents) {
String identifier = identifierMapper.getStudentGroupStudentIdentifier(sgs.getId());
foundGroupUserIds.add(identifier);
// If not existing, then it's a new one
if (!existingGroupUserIds.contains(identifier)) {
String studentIdentifier = identifierMapper.getStudentIdentifier(sgs.getStudentId());
fireUserGroupUserDiscovered(identifier, userGroupIdentifier, studentIdentifier);
}
}
count += userGroupStudents.length;
}
// Remove found ids from existing and we'll get the ones to remove
existingGroupUserIds.removeAll(foundGroupUserIds);
for (String identifier : existingGroupUserIds) {
UserGroupUserEntity ugu = userGroupEntityController.findUserGroupUserEntityByDataSourceAndIdentifier(SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE, identifier);
if (ugu != null)
fireUserGroupUserRemoved(identifier, userGroupIdentifier, ugu.getUserSchoolDataIdentifier().getIdentifier());
}
return count;
} else {
logger.log(Level.WARNING, String.format("UserGroup is null for id %d - update of users is skipped", studentGroupId));
}
return 0;
}
use of fi.otavanopisto.muikku.model.users.UserGroupEntity 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.model.users.UserGroupEntity 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.model.users.UserGroupEntity 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.UserGroupEntity in project muikku by otavanopisto.
the class PermissionRESTService method setWorkspaceUserGroupPermission.
@PUT
@Path("/workspaceUserGroupPermissions")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response setWorkspaceUserGroupPermission(WorkspaceUserGroupPermission payload) {
UserGroupEntity userGroupEntity = userGroupEntityController.findUserGroupEntityById(payload.getUserGroupId());
Permission permission = permissionDAO.findById(payload.getPermissionId());
WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(payload.getWorkspaceId());
if (!sessionController.hasPermission(MuikkuPermissions.WORKSPACE_MANAGEWORKSPACESETTINGS, workspaceEntity)) {
return Response.status(Status.FORBIDDEN).build();
}
if ((userGroupEntity == null) || (permission == null)) {
return Response.status(Response.Status.NOT_FOUND).build();
}
try {
if (payload.getPermitted())
permissionController.addWorkspaceGroupPermission(workspaceEntity, userGroupEntity, permission);
else {
WorkspaceGroupPermission workspaceGroupPermission = permissionController.findWorkspaceGroupPermission(workspaceEntity, userGroupEntity, permission);
if (workspaceGroupPermission != null)
permissionController.removeWorkspaceGroupPermission(workspaceGroupPermission);
else
return Response.status(Response.Status.NOT_FOUND).build();
}
return Response.noContent().build();
} catch (ConstraintViolationException violationException) {
return getConstraintViolations(violationException);
}
}
Aggregations