use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class AssessmentRequestRESTService method restModel.
private AssessmentRequestRESTModel restModel(WorkspaceAssessmentRequest workspaceAssessmentRequest) {
SchoolDataIdentifier workspaceUserIdentifier = new SchoolDataIdentifier(workspaceAssessmentRequest.getWorkspaceUserIdentifier(), workspaceAssessmentRequest.getWorkspaceUserSchoolDataSource());
WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityController.findWorkspaceUserEntityByWorkspaceUserIdentifier(workspaceUserIdentifier);
if (workspaceUserEntity != null) {
SchoolDataIdentifier userIdentifier = new SchoolDataIdentifier(workspaceUserEntity.getUserSchoolDataIdentifier().getIdentifier(), workspaceUserEntity.getUserSchoolDataIdentifier().getDataSource().getIdentifier());
SchoolDataIdentifier workspaceAssessmentRequestIdentifier = new SchoolDataIdentifier(workspaceAssessmentRequest.getIdentifier(), workspaceAssessmentRequest.getSchoolDataSource());
WorkspaceEntity workspaceEntity = workspaceUserEntity.getWorkspaceEntity();
UserEntity userEntity = workspaceUserEntity.getUserSchoolDataIdentifier().getUserEntity();
AssessmentRequestRESTModel restAssessmentRequest = new AssessmentRequestRESTModel(workspaceAssessmentRequestIdentifier.toId(), userIdentifier.toId(), workspaceUserIdentifier.toId(), workspaceEntity.getId(), userEntity.getId(), workspaceAssessmentRequest.getRequestText(), workspaceAssessmentRequest.getDate());
return restAssessmentRequest;
}
return null;
}
use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class AssessmentRequestRESTService method listAssessmentRequestsByWorkspaceId.
@GET
@Path("/workspace/{WORKSPACEENTITYID}/assessmentRequests")
@RESTPermit(handling = Handling.INLINE)
public Response listAssessmentRequestsByWorkspaceId(@PathParam("WORKSPACEENTITYID") Long workspaceEntityId, @QueryParam("studentIdentifier") String studentId) {
WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspaceEntityId);
if (workspaceEntity == null) {
return Response.status(Status.NOT_FOUND).entity("Workspace not found").build();
}
SchoolDataIdentifier studentIdentifier = SchoolDataIdentifier.fromId(studentId);
if (studentIdentifier != null) {
if (!studentIdentifier.equals(sessionController.getLoggedUser())) {
if (!sessionController.hasPermission(AssessmentRequestPermissions.LIST_WORKSPACE_ASSESSMENTREQUESTS, workspaceEntity)) {
return Response.status(Status.FORBIDDEN).build();
}
}
WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityController.findWorkspaceUserEntityByWorkspaceAndUserIdentifier(workspaceEntity, studentIdentifier);
if (workspaceUserEntity == null) {
return Response.status(Status.BAD_REQUEST).entity("WorkspaceUserEntity could not find").build();
}
return Response.ok(restModel(assessmentRequestController.listByWorkspaceUser(workspaceUserEntity))).build();
} else {
if (!sessionController.hasPermission(AssessmentRequestPermissions.LIST_WORKSPACE_ASSESSMENTREQUESTS, workspaceEntity)) {
return Response.status(Status.FORBIDDEN).build();
}
List<WorkspaceAssessmentRequest> assessmentRequests = assessmentRequestController.listByWorkspace(workspaceEntity);
return Response.ok(restModel(assessmentRequests)).build();
}
}
use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class ChatRESTService method fetchCredentials.
@GET
@Path("/credentials")
@RESTPermit(handling = Handling.INLINE)
public Response fetchCredentials() {
if (!sessionController.isLoggedIn()) {
return Response.status(Status.FORBIDDEN).entity("Must be logged in").build();
}
PrivateKey privateKey = getPrivateKey();
if (privateKey == null) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Private key not set").build();
}
Instant now = Instant.now();
SchoolDataIdentifier loggedUserIdentifier = sessionController.getLoggedUser();
if (loggedUserIdentifier == null) {
return Response.status(Status.BAD_REQUEST).entity("Logged user identifier not found").build();
}
String userIdentifierString = loggedUserIdentifier.toId();
try {
XmppCredentials credentials = computeXmppCredentials(privateKey, now, userIdentifierString);
return Response.ok(credentials).build();
} catch (InvalidKeyException | SignatureException | NoSuchAlgorithmException ex) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(ex.getMessage()).build();
}
}
use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class CommunicatorAssessmentRequestController method sendAssessmentRequestCancelledMessage.
public void sendAssessmentRequestCancelledMessage(WorkspaceUserEntity workspaceUserEntity) {
CommunicatorMessageId communicatorMessageId = assessmentRequestController.findCommunicatorMessageId(workspaceUserEntity);
if (communicatorMessageId == null) {
communicatorMessageId = communicatorController.createMessageId();
}
UserSchoolDataIdentifier userSchoolDataIdentifier = workspaceUserEntity.getUserSchoolDataIdentifier();
if (userSchoolDataIdentifier == null) {
logger.severe(String.format("Could not find userSchoolDataIdentifier for workspace user entity %d", workspaceUserEntity.getId()));
return;
}
WorkspaceEntity workspaceEntity = workspaceUserEntity.getWorkspaceEntity();
if (workspaceEntity == null) {
logger.severe(String.format("Could not find workspaceEntity for workspace user entity %d", workspaceUserEntity.getId()));
return;
}
UserEntity studentEntity = userSchoolDataIdentifier.getUserEntity();
if (studentEntity == null) {
logger.severe(String.format("Could not find studentEntity for workspace user entity %d", workspaceUserEntity.getId()));
return;
}
schoolDataBridgeSessionController.startSystemSession();
try {
List<UserEntity> teachers = new ArrayList<UserEntity>();
List<WorkspaceUserEntity> workspaceTeachers = workspaceUserEntityController.listActiveWorkspaceStaffMembers(workspaceEntity);
for (WorkspaceUserEntity workspaceTeacher : workspaceTeachers) {
teachers.add(workspaceTeacher.getUserSchoolDataIdentifier().getUserEntity());
}
SchoolDataIdentifier studentIdentifier = new SchoolDataIdentifier(userSchoolDataIdentifier.getIdentifier(), userSchoolDataIdentifier.getDataSource().getIdentifier());
User student = userController.findUserByIdentifier(studentIdentifier);
if (student == null) {
logger.severe(String.format("Could not find student %s", studentIdentifier));
return;
}
Workspace workspace = workspaceController.findWorkspace(workspaceEntity);
if (workspace == null) {
logger.severe(String.format("Could not find workspace for workspace entity %d", workspaceEntity.getId()));
return;
}
String messageCategory = getText("plugin.communicator.assessmentrequest.category");
String messageTitle = assessmentCancelledTitle(workspace, student);
String messageBody = assessmentCancelledBody(workspace, student);
List<String> teacherEmails = new ArrayList<>(teachers.size());
for (UserEntity teacher : teachers) {
String teacherEmail = userEmailEntityController.getUserDefaultEmailAddress(teacher, false);
if (StringUtils.isNotBlank(teacherEmail)) {
teacherEmails.add(teacherEmail);
}
}
if (!teacherEmails.isEmpty()) {
mailer.sendMail(MailType.HTML, teacherEmails, messageTitle, messageBody);
}
communicatorController.replyToMessage(studentEntity, messageCategory, messageTitle, messageBody, teachers, communicatorMessageId);
} finally {
schoolDataBridgeSessionController.endSystemSession();
}
}
use of fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier in project muikku by otavanopisto.
the class DefaultSchoolDataUserListener method onSchoolDataUserUpdatedEvent.
public void onSchoolDataUserUpdatedEvent(@Observes SchoolDataUserUpdatedEvent event) {
Long userEntityId = event.getUserEntityId();
SchoolDataIdentifier defaultIdentifier = event.getDefaultIdentifier();
List<SchoolDataIdentifier> discoveredIdentifiers = event.getDiscoveredIdentifiers();
List<SchoolDataIdentifier> updatedIdentifiers = event.getUpdatedIdentifiers();
List<SchoolDataIdentifier> removedIdentifiers = event.getRemovedIdentifiers();
Collection<String> allEmails = event.getAllEmails();
if (allEmails.isEmpty()) {
logger.warning("Updating user without email addresses");
} else {
// Attempt to find existing users by given emails
Collection<UserEntity> emailUsers = userEntityController.listUserEntitiesByEmails(allEmails);
if (emailUsers.isEmpty()) {
// Could not find any users with given emails
} else if (emailUsers.size() > 1) {
logger.log(Level.SEVERE, String.format("Multiple users found with given emails (%s)", StringUtils.join(allEmails, ',')));
return;
} else {
UserEntity emailUser = emailUsers.iterator().next();
if (userEntityId != null) {
if (!emailUser.getId().equals(userEntityId)) {
logger.log(Level.SEVERE, String.format("One or more of emails %s belong to another user", StringUtils.join(allEmails, ',')));
return;
}
} else {
userEntityId = emailUser.getId();
logger.log(Level.INFO, String.format("Found userEntity (%d) by email, merging user to existing account", userEntityId));
}
}
}
UserEntity userEntity = null;
// If it's not an user delete event we need to create / update user into the system
if (!discoveredIdentifiers.isEmpty() || !updatedIdentifiers.isEmpty()) {
// UserEntityId has not been defined in the event and could not be found by email, so we create new user
if (userEntityId == null) {
userEntity = userEntityController.createUserEntity(defaultIdentifier.getDataSource(), defaultIdentifier.getIdentifier());
} else {
// Otherwise we use the existing one
userEntity = userEntityController.findUserEntityById(userEntityId);
if (userEntity == null) {
logger.log(Level.WARNING, "Could not find specified userEntityId %d, aborting synchronization", userEntityId);
return;
}
if (defaultIdentifier != null) {
if (!StringUtils.equals(userEntity.getDefaultIdentifier(), defaultIdentifier.getIdentifier()) || !StringUtils.equals(userEntity.getDefaultSchoolDataSource().getIdentifier(), defaultIdentifier.getDataSource())) {
logger.log(Level.FINE, String.format("Updating default identifier for user #%d into %s", userEntity.getId(), defaultIdentifier));
userEntityController.updateDefaultSchoolDataSource(userEntity, defaultIdentifier.getDataSource());
userEntityController.updateDefaultIdentifier(userEntity, defaultIdentifier.getIdentifier());
}
}
}
// Attach discovered identities to user
for (SchoolDataIdentifier identifier : discoveredIdentifiers) {
List<String> identifierEmails = event.getEmails().get(identifier);
UserSchoolDataIdentifier userSchoolDataIdentifier = userSchoolDataIdentifierController.findUserSchoolDataIdentifierByDataSourceAndIdentifierIncludeArchived(identifier.getDataSource(), identifier.getIdentifier());
if (userSchoolDataIdentifier == null) {
userSchoolDataIdentifier = userSchoolDataIdentifierController.createUserSchoolDataIdentifier(identifier.getDataSource(), identifier.getIdentifier(), userEntity);
logger.log(Level.FINE, String.format("Added new identifier %s for user %d", identifier, userEntity.getId()));
} else if (userSchoolDataIdentifier.getArchived()) {
userSchoolDataIdentifierController.unarchiveUserSchoolDataIdentifier(userSchoolDataIdentifier);
}
userEmailEntityController.setUserEmails(userSchoolDataIdentifier, getValidEmails(identifierEmails));
}
for (SchoolDataIdentifier identifier : updatedIdentifiers) {
List<String> emails = event.getEmails().get(identifier);
userEmailEntityController.setUserEmails(identifier, getValidEmails(emails));
}
for (SchoolDataIdentifier identifier : removedIdentifiers) {
List<String> emails = event.getEmails().get(identifier);
userEmailEntityController.setUserEmails(identifier, getValidEmails(emails));
}
// Update users environment role
if (event.getEnvironmentRoleIdentifier() != null) {
EnvironmentRoleEntity environmentRoleEntity = environmentRoleEntityController.findEnvironmentRoleEntity(event.getEnvironmentRoleIdentifier().getDataSource(), event.getEnvironmentRoleIdentifier().getIdentifier());
if (environmentRoleEntity != null) {
EnvironmentUser environmentUser = environmentUserController.findEnvironmentUserByUserEntity(userEntity);
if (environmentUser == null) {
logger.fine(String.format("UserEntity %d did not have an environment user so created new one into role %s", userEntity.getId(), environmentRoleEntity.getName()));
environmentUserController.createEnvironmentUser(userEntity, environmentRoleEntity);
} else {
if (environmentUser.getRole() == null || !environmentUser.getRole().getId().equals(environmentRoleEntity.getId())) {
logger.fine(String.format("Updated UserEntity %d role into %s", userEntity.getId(), environmentRoleEntity.getName()));
environmentUserController.updateEnvironmentUserRole(environmentUser, environmentRoleEntity);
}
}
} else {
logger.severe(String.format("Could not find specified environment role entity %s", event.getEnvironmentRoleIdentifier()));
}
} else {
// Users new role has been set to null which means that we need to remove the environment role from the user
EnvironmentUser environmentUser = environmentUserController.findEnvironmentUserByUserEntity(userEntity);
if (environmentUser != null) {
logger.info(String.format("Removed UserEntity %d environment role", userEntity.getId()));
environmentUserController.updateEnvironmentUserRole(environmentUser, null);
}
}
}
// Remove identifiers in the removed list
for (SchoolDataIdentifier identifier : removedIdentifiers) {
UserSchoolDataIdentifier userSchoolDataIdentifier = userSchoolDataIdentifierController.findUserSchoolDataIdentifierByDataSourceAndIdentifier(identifier.getDataSource(), identifier.getIdentifier());
if (userSchoolDataIdentifier != null) {
logger.log(Level.FINE, String.format("Removing user school data identifier %s", identifier));
userSchoolDataIdentifierController.archiveUserSchoolDataIdentifier(userSchoolDataIdentifier);
if (userEntity == null) {
userEntity = userSchoolDataIdentifier.getUserEntity();
}
}
}
// Finally check if user has any identifiers left, if not archive the user from the system
if (userEntity != null) {
if (userSchoolDataIdentifierController.listUserSchoolDataIdentifiersByUserEntity(userEntity).isEmpty()) {
logger.log(Level.INFO, String.format("UserEntity #%d has no identities left, archiving userEntity", userEntity.getId()));
userEntityController.archiveUserEntity(userEntity);
}
}
}
Aggregations