use of fi.otavanopisto.muikku.schooldata.entity.UserGroup in project muikku by otavanopisto.
the class CommunicatorRESTModels method restUserGroupRecipient.
public fi.otavanopisto.muikku.rest.model.UserGroup restUserGroupRecipient(CommunicatorMessageRecipientUserGroup userGroup) {
schoolDataBridgeSessionController.startSystemSession();
try {
UserGroupEntity entity = userGroupEntityController.findUserGroupEntityById(userGroup.getUserGroupEntityId());
if (entity != null) {
Long userCount = userGroupEntityController.getGroupUserCount(entity);
UserGroup group = userGroupController.findUserGroup(entity);
if (group != null)
return new fi.otavanopisto.muikku.rest.model.UserGroup(entity.getId(), group.getName(), userCount);
}
return null;
} finally {
schoolDataBridgeSessionController.endSystemSession();
}
}
use of fi.otavanopisto.muikku.schooldata.entity.UserGroup in project muikku by otavanopisto.
the class NotificationController method sendNotification.
public void sendNotification(String category, String subject, String content, UserEntity recipient, SchoolDataIdentifier recipientIdentifier, String notificationType) {
HashMap<String, Object> map = new HashMap<>();
map.put("notificationType", notificationType);
map.put("recipient", recipient.getId());
map.put("recipientIdentifier", recipientIdentifier.toId());
map.put("time", String.valueOf(System.currentTimeMillis()));
UserEntity guidanceCounselor = null;
SchoolDataIdentifier userIdentifier = new SchoolDataIdentifier(recipient.getDefaultIdentifier(), recipient.getDefaultSchoolDataSource().getIdentifier());
List<UserGroupEntity> userGroupEntities = userGroupEntityController.listUserGroupsByUserIdentifier(userIdentifier);
// #3089: An awkward workaround to use the latest guidance group based on its identifier. Assumes a larger
// identifier means a more recent entity. A more proper fix would be to sync group creation dates from
// Pyramus and include them in the Elastic index. Then again, user groups would have to be refactored
// entirely, as Pyramus handles group members as students (one study programme) while Muikku handles
// them as user entities (all study programmes)...
userGroupEntities.sort(new Comparator<UserGroupEntity>() {
public int compare(UserGroupEntity o1, UserGroupEntity o2) {
long l1 = NumberUtils.toLong(StringUtils.substringAfterLast(o1.getIdentifier(), "-"), -1);
long l2 = NumberUtils.toLong(StringUtils.substringAfterLast(o2.getIdentifier(), "-"), -1);
return (int) (l2 - l1);
}
});
userGroupEntities: for (UserGroupEntity userGroupEntity : userGroupEntities) {
UserGroup userGroup = userGroupController.findUserGroup(userGroupEntity);
if (userGroup.isGuidanceGroup()) {
List<GroupUser> groupUsers = userGroupController.listUserGroupStaffMembers(userGroup);
for (GroupUser groupUser : groupUsers) {
User user = userGroupController.findUserByGroupUser(groupUser);
guidanceCounselor = userEntityController.findUserEntityByUser(user);
break userGroupEntities;
}
}
}
LogProvider provider = getProvider(LOG_PROVIDER);
if (provider != null) {
provider.log(COLLECTION_NAME, map);
}
if (isDryRun()) {
String recipientEmail = getRecipientEmail();
if (recipientEmail == null) {
logger.log(Level.INFO, String.format("Sending notification %s - %s to %s", category, subject, recipient.getDefaultIdentifier()));
} else {
mailer.sendMail(MailType.HTML, Arrays.asList(recipientEmail), subject, "SENT TO: " + recipient.getDefaultIdentifier() + "<br/><br/><br/>" + content);
}
} else {
ArrayList<UserEntity> recipients = new ArrayList<>();
recipients.add(recipient);
if (guidanceCounselor != null) {
recipients.add(guidanceCounselor);
}
String studentEmail = userEmailEntityController.getUserDefaultEmailAddress(recipient, Boolean.FALSE);
if (studentEmail != null) {
mailer.sendMail(MailType.HTML, Arrays.asList(studentEmail), subject, content);
} else {
logger.log(Level.WARNING, String.format("Cannot send email notification to student %s because no email address was found", recipient.getDefaultIdentifier()));
}
communicatorController.postMessage(recipient, category, subject, content, recipients);
}
}
use of fi.otavanopisto.muikku.schooldata.entity.UserGroup in project muikku by otavanopisto.
the class WorkspacePermissionsManagementBackingBean method init.
@RequestAction
public String init() {
String urlName = getWorkspaceUrlName();
if (StringUtils.isBlank(urlName)) {
return NavigationRules.NOT_FOUND;
}
WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityByUrlName(urlName);
if (workspaceEntity == null) {
return NavigationRules.NOT_FOUND;
}
if (!sessionController.hasWorkspacePermission(MuikkuPermissions.WORKSPACE_MANAGE_PERMISSIONS, workspaceEntity)) {
return NavigationRules.NOT_FOUND;
}
workspaceEntityId = workspaceEntity.getId();
workspaceBackingBean.setWorkspaceUrlName(urlName);
workspaceName = workspaceBackingBean.getWorkspaceName();
userGroupBeans = new ArrayList<WorkspacePermissionsManagementBackingBean.UserGroupBean>();
permissions = new ArrayList<Permission>();
// TODO: atm we only support the sign up permission
Permission permission = permissionController.findByName(MuikkuPermissions.WORKSPACE_SIGNUP);
permissions.add(permission);
List<UserGroupEntity> userGroupEntities;
String permissionGroupIds = pluginSettingsController.getPluginSetting("workspace", "permission-group-ids");
if (permissionGroupIds == null) {
userGroupEntities = userGroupEntityController.listUserGroupEntities();
} else {
userGroupEntities = new ArrayList<UserGroupEntity>();
String[] idArray = permissionGroupIds.split(",");
for (int i = 0; i < idArray.length; i++) {
Long groupId = NumberUtils.createLong(idArray[i]);
if (groupId != null) {
UserGroupEntity userGroupEntity = userGroupEntityController.findUserGroupEntityById(groupId);
if (userGroupEntity == null) {
logger.warning(String.format("Missing group %d in plugin setting workspace.permission-group-ids", groupId));
} else {
userGroupEntities.add(userGroupEntity);
}
} else {
logger.warning(String.format("Malformatted plugin setting workspace.permission-group-ids %s", permissionGroupIds));
}
}
}
for (UserGroupEntity userGroupEntity : userGroupEntities) {
UserGroup userGroup = userGroupController.findUserGroup(userGroupEntity);
userGroupBeans.add(new UserGroupBean(userGroupEntity.getId(), userGroup.getName()));
}
Collections.sort(userGroupBeans, new Comparator<UserGroupBean>() {
@Override
public int compare(UserGroupBean o1, UserGroupBean o2) {
return o1.getName().compareTo(o2.getName());
}
});
return null;
}
use of fi.otavanopisto.muikku.schooldata.entity.UserGroup 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.entity.UserGroup 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();
}
Aggregations