use of fi.otavanopisto.muikku.model.users.UserGroupEntity in project muikku by otavanopisto.
the class CommunicatorRESTService method postMessage.
@POST
@Path("/messages")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response postMessage(CommunicatorNewMessageRESTModel newMessage) throws AuthorizationException {
UserEntity userEntity = sessionController.getLoggedUserEntity();
CommunicatorMessageId communicatorMessageId = communicatorController.createMessageId();
Set<Tag> tagList = parseTags(newMessage.getTags());
List<UserEntity> recipients = new ArrayList<UserEntity>();
for (Long recipientId : newMessage.getRecipientIds()) {
UserEntity recipient = userEntityController.findUserEntityById(recipientId);
if (recipient != null)
recipients.add(recipient);
}
List<UserGroupEntity> userGroupRecipients = null;
List<WorkspaceEntity> workspaceStudentRecipients = null;
List<WorkspaceEntity> workspaceTeacherRecipients = null;
if (!CollectionUtils.isEmpty(newMessage.getRecipientGroupIds())) {
if (sessionController.hasEnvironmentPermission(CommunicatorPermissionCollection.COMMUNICATOR_GROUP_MESSAGING)) {
userGroupRecipients = new ArrayList<UserGroupEntity>();
for (Long groupId : newMessage.getRecipientGroupIds()) {
UserGroupEntity group = userGroupEntityController.findUserGroupEntityById(groupId);
userGroupRecipients.add(group);
}
} else {
// Trying to feed group ids when you don't have permission greets you with bad request
return Response.status(Status.BAD_REQUEST).build();
}
}
if (!CollectionUtils.isEmpty(newMessage.getRecipientStudentsWorkspaceIds())) {
workspaceStudentRecipients = new ArrayList<WorkspaceEntity>();
for (Long workspaceId : newMessage.getRecipientStudentsWorkspaceIds()) {
WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceId);
if (sessionController.hasPermission(CommunicatorPermissionCollection.COMMUNICATOR_WORKSPACE_MESSAGING, workspaceEntity))
workspaceStudentRecipients.add(workspaceEntity);
else
return Response.status(Status.BAD_REQUEST).build();
}
}
if (!CollectionUtils.isEmpty(newMessage.getRecipientTeachersWorkspaceIds())) {
workspaceTeacherRecipients = new ArrayList<WorkspaceEntity>();
for (Long workspaceId : newMessage.getRecipientTeachersWorkspaceIds()) {
WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceId);
if (sessionController.hasPermission(CommunicatorPermissionCollection.COMMUNICATOR_WORKSPACE_MESSAGING, workspaceEntity))
workspaceTeacherRecipients.add(workspaceEntity);
else
return Response.status(Status.BAD_REQUEST).build();
}
}
if (StringUtils.isBlank(newMessage.getCategoryName())) {
return Response.status(Status.BAD_REQUEST).entity("CategoryName missing").build();
}
// TODO Category not existing at this point would technically indicate an invalid state
CommunicatorMessageCategory categoryEntity = communicatorController.persistCategory(newMessage.getCategoryName());
CommunicatorMessage message = communicatorController.createMessage(communicatorMessageId, userEntity, recipients, userGroupRecipients, workspaceStudentRecipients, workspaceTeacherRecipients, categoryEntity, newMessage.getCaption(), newMessage.getContent(), tagList);
sendNewMessageNotifications(message);
return Response.ok(restModels.restFullMessage(message)).build();
}
use of fi.otavanopisto.muikku.model.users.UserGroupEntity in project muikku by otavanopisto.
the class CommunicatorRESTService method postMessageReply.
@POST
@Path("/messages/{COMMUNICATORMESSAGEID}")
@RESTPermit(handling = Handling.INLINE, requireLoggedIn = true)
public Response postMessageReply(@PathParam("COMMUNICATORMESSAGEID") Long communicatorMessageId, CommunicatorNewMessageRESTModel newMessage) throws AuthorizationException {
UserEntity userEntity = sessionController.getLoggedUserEntity();
CommunicatorMessageId communicatorMessageId2 = communicatorController.findCommunicatorMessageId(communicatorMessageId);
Set<Tag> tagList = parseTags(newMessage.getTags());
List<UserEntity> recipients = new ArrayList<UserEntity>();
for (Long recipientId : newMessage.getRecipientIds()) {
UserEntity recipient = userEntityController.findUserEntityById(recipientId);
if (recipient != null)
recipients.add(recipient);
}
List<UserGroupEntity> userGroupRecipients = null;
List<WorkspaceEntity> workspaceStudentRecipients = null;
List<WorkspaceEntity> workspaceTeacherRecipients = null;
if (!CollectionUtils.isEmpty(newMessage.getRecipientGroupIds())) {
if (sessionController.hasEnvironmentPermission(CommunicatorPermissionCollection.COMMUNICATOR_GROUP_MESSAGING)) {
userGroupRecipients = new ArrayList<UserGroupEntity>();
for (Long groupId : newMessage.getRecipientGroupIds()) {
UserGroupEntity group = userGroupEntityController.findUserGroupEntityById(groupId);
userGroupRecipients.add(group);
}
} else {
// Trying to feed group ids when you don't have permission greets you with bad request
return Response.status(Status.BAD_REQUEST).build();
}
}
if (!CollectionUtils.isEmpty(newMessage.getRecipientStudentsWorkspaceIds())) {
workspaceStudentRecipients = new ArrayList<WorkspaceEntity>();
for (Long workspaceId : newMessage.getRecipientStudentsWorkspaceIds()) {
WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceId);
if (sessionController.hasPermission(CommunicatorPermissionCollection.COMMUNICATOR_WORKSPACE_MESSAGING, workspaceEntity))
workspaceStudentRecipients.add(workspaceEntity);
else
return Response.status(Status.BAD_REQUEST).build();
}
}
if (!CollectionUtils.isEmpty(newMessage.getRecipientTeachersWorkspaceIds())) {
workspaceTeacherRecipients = new ArrayList<WorkspaceEntity>();
for (Long workspaceId : newMessage.getRecipientTeachersWorkspaceIds()) {
WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceId);
if (sessionController.hasPermission(CommunicatorPermissionCollection.COMMUNICATOR_WORKSPACE_MESSAGING, workspaceEntity))
workspaceTeacherRecipients.add(workspaceEntity);
else
return Response.status(Status.BAD_REQUEST).build();
}
}
// TODO Category not existing at this point would technically indicate an invalid state
CommunicatorMessageCategory categoryEntity = communicatorController.persistCategory(newMessage.getCategoryName());
CommunicatorMessage message = communicatorController.createMessage(communicatorMessageId2, userEntity, recipients, userGroupRecipients, workspaceStudentRecipients, workspaceTeacherRecipients, categoryEntity, newMessage.getCaption(), newMessage.getContent(), tagList);
sendNewMessageNotifications(message);
return Response.ok(restModels.restFullMessage(message)).build();
}
use of fi.otavanopisto.muikku.model.users.UserGroupEntity in project muikku by otavanopisto.
the class PermissionsPluginController method processPermissions.
public void processPermissions() {
logger.log(Level.INFO, "Starting permission gathering");
for (SystemRoleType systemRoleType : SystemRoleType.values()) {
if (systemRoleEntityDAO.findByRoleType(systemRoleType) == null)
systemRoleEntityDAO.create(systemRoleType.name(), systemRoleType);
}
for (MuikkuPermissionCollection collection : permissionCollections) {
logger.log(Level.INFO, "Processing permission collection " + collection.getClass().getSimpleName());
List<String> permissions = collection.listPermissions();
for (String permissionName : permissions) {
Permission permission = permissionDAO.findByName(permissionName);
if (permission == null) {
logger.log(Level.INFO, "Recording new permission " + permissionName);
try {
final String permissionScope = collection.getPermissionScope(permissionName);
if (permissionScope != null) {
permission = permissionDAO.create(permissionName, permissionScope);
if (!PermissionScope.PERSONAL.equals(permissionScope)) {
String[] pseudoRoles = collection.getDefaultPseudoRoles(permissionName);
EnvironmentRoleArchetype[] environmentRoles = collection.getDefaultEnvironmentRoles(permissionName);
WorkspaceRoleArchetype[] workspaceRoles = collection.getDefaultWorkspaceRoles(permissionName);
List<RoleEntity> roles = new ArrayList<RoleEntity>();
if (pseudoRoles != null) {
for (String pseudoRole : pseudoRoles) {
RoleEntity roleEntity = roleEntityDAO.findByName(pseudoRole);
if (roleEntity != null)
roles.add(roleEntity);
}
}
if (environmentRoles != null) {
for (EnvironmentRoleArchetype envRole : environmentRoles) {
List<EnvironmentRoleEntity> envRoles = environmentRoleEntityDAO.listByArchetype(envRole);
roles.addAll(envRoles);
}
}
if (workspaceRoles != null) {
for (WorkspaceRoleArchetype arc : workspaceRoles) {
List<WorkspaceRoleEntity> wsRoles = workspaceRoleEntityDAO.listByArchetype(arc);
roles.addAll(wsRoles);
}
}
switch(permissionScope) {
case PermissionScope.ENVIRONMENT:
case PermissionScope.WORKSPACE:
for (RoleEntity role : roles) {
rolePermissionDAO.create(role, permission);
}
break;
case PermissionScope.USERGROUP:
List<UserGroupEntity> userGroups = userGroupDAO.listAll();
for (RoleEntity role : roles) {
// TODO Workspace creation & templates - is this necessary and bulletproof?
for (UserGroupEntity userGroup : userGroups) {
userGroupRolePermissionDAO.create(userGroup, role, permission);
}
}
break;
default:
permissionDiscoveredEvent.select(new PermissionScopeBinding() {
private static final long serialVersionUID = 9009824962970938515L;
@Override
public String value() {
return permissionScope;
}
}).fire(new PermissionDiscoveredEvent(permission));
break;
}
}
} else
logger.log(Level.WARNING, "PermissionScope null for " + permissionName);
} catch (Exception e) {
logger.log(Level.SEVERE, "Permission handling failed for " + permissionName);
}
}
}
}
logger.log(Level.INFO, "Finished permission gathering");
}
use of fi.otavanopisto.muikku.model.users.UserGroupEntity in project muikku by otavanopisto.
the class PyramusSchoolDataUserListener method handlePyramusUserRemoved.
private void handlePyramusUserRemoved(SchoolDataIdentifier identifier) {
UserSchoolDataIdentifier userSchoolDataIdentifier = userSchoolDataIdentifierController.findUserSchoolDataIdentifierByDataSourceAndIdentifier(identifier.getDataSource(), identifier.getIdentifier());
List<UserGroupUserEntity> userGroupUsers = userGroupEntityController.listUserGroupUsersByUserSchoolDataIdentifier(userSchoolDataIdentifier);
for (UserGroupUserEntity userGroupUser : userGroupUsers) {
UserGroupEntity userGroup = userGroupUser.getUserGroupEntity();
StudentGroupType studentGroupType = identifierMapper.getStudentGroupType(userGroup.getIdentifier());
if (studentGroupType == StudentGroupType.STUDYPROGRAMME) {
fireUserGroupUserRemoved(userGroupUser.getIdentifier(), userGroup.getIdentifier(), identifier.getIdentifier());
}
}
}
use of fi.otavanopisto.muikku.model.users.UserGroupEntity in project muikku by otavanopisto.
the class PyramusUpdater method updateStudentGroup.
public void updateStudentGroup(Long pyramusId) {
StudentGroup studentGroup = pyramusClient.get().get(String.format("/students/studentGroups/%d", pyramusId), StudentGroup.class);
String identifier = identifierMapper.getStudentGroupIdentifier(pyramusId);
UserGroupEntity userGroupEntity = userGroupEntityController.findUserGroupEntityByDataSourceAndIdentifier(SchoolDataPyramusPluginDescriptor.SCHOOL_DATA_SOURCE, identifier, true);
if (studentGroup == null) {
if (userGroupEntity != null)
fireUserGroupRemoved(identifier);
} else {
if (userGroupEntity == null) {
fireUserGroupDiscovered(identifier);
} else {
fireUserGroupUpdated(identifier);
}
}
}
Aggregations