use of fi.otavanopisto.pyramus.domainmodel.users.Role in project pyramus by otavanopisto.
the class ImportLDAPUsersViewController method processSend.
public void processSend(PageRequestContext requestContext) {
EmailDAO emailDAO = DAOFactory.getInstance().getEmailDAO();
UserDAO userDAO = DAOFactory.getInstance().getUserDAO();
List<User> createdUsers = new ArrayList<User>();
int rowCount = requestContext.getInteger("importTable.rowCount");
for (int i = 0; i < rowCount; i++) {
String colPrefix = "importTable." + i;
if ("1".equals(requestContext.getString(colPrefix + ".import"))) {
String email = requestContext.getString(colPrefix + ".email");
String firstName = requestContext.getString(colPrefix + ".firstName");
String lastName = requestContext.getString(colPrefix + ".lastName");
String roleName = requestContext.getString(colPrefix + ".role");
String id = requestContext.getString(colPrefix + ".id");
Role role = Enum.valueOf(Role.class, roleName);
User user = userDAO.create(firstName, lastName, id, "LDAP", role);
emailDAO.create(user.getContactInfo(), null, Boolean.TRUE, email);
createdUsers.add(user);
}
}
requestContext.getRequest().setAttribute("createdUsers", createdUsers);
requestContext.setRedirectURL(requestContext.getRequest().getContextPath() + "system/importldapusers.page");
}
use of fi.otavanopisto.pyramus.domainmodel.users.Role in project pyramus by otavanopisto.
the class ImportExportPermissionsViewController method processSend.
@Override
public void processSend(PageRequestContext requestContext) {
String permissionJson = requestContext.getString("permissionJson");
JSONObject permissionMap = JSONObject.fromObject(permissionJson);
PermissionDAO permissionDAO = DAOFactory.getInstance().getPermissionDAO();
EnvironmentRolePermissionDAO environmentRolePermissionDAO = DAOFactory.getInstance().getEnvironmentRolePermissionDAO();
List<EnvironmentRolePermission> allPermissions = environmentRolePermissionDAO.listAll();
for (EnvironmentRolePermission erp : allPermissions) {
environmentRolePermissionDAO.delete(erp);
}
for (Object roleObject : permissionMap.keySet()) {
String roleValue = (String) roleObject;
Role role = Role.getRole(Integer.valueOf(roleValue));
if (role == null) {
Logger.getLogger(getClass().getName()).severe(String.format("Role with value %s not found from system", roleValue));
continue;
}
for (Object permissionNameObject : permissionMap.getJSONArray(roleValue)) {
String permissionName = (String) permissionNameObject;
Permission permission = permissionDAO.findByName(permissionName);
if (permission == null) {
Logger.getLogger(getClass().getName()).severe(String.format("Permission %s not found from system", permissionName));
continue;
}
environmentRolePermissionDAO.create(role, permission);
}
}
requestContext.setRedirectURL("/system/managepermissions.page");
}
use of fi.otavanopisto.pyramus.domainmodel.users.Role in project pyramus by otavanopisto.
the class ManagePermissionsViewController method processSend.
@Override
public void processSend(PageRequestContext requestContext) {
String resetRole = requestContext.getString("roleReset");
if (StringUtils.isBlank(resetRole)) {
PermissionDAO permissionDAO = DAOFactory.getInstance().getPermissionDAO();
EnvironmentRolePermissionDAO environmentRolePermissionDAO = DAOFactory.getInstance().getEnvironmentRolePermissionDAO();
List<Permission> permissions = permissionDAO.listAll();
for (Permission permission : permissions) {
for (Role role : manageableRoles()) {
String paramName = permission.getId().toString() + '.' + role.name();
EnvironmentRolePermission rolePermission = environmentRolePermissionDAO.findByUserRoleAndPermission(role, permission);
boolean isSet = new Integer(1).equals(requestContext.getInteger(paramName));
boolean exists = rolePermission != null;
if (isSet != exists) {
if (isSet) {
environmentRolePermissionDAO.create(role, permission);
} else {
environmentRolePermissionDAO.delete(rolePermission);
}
}
}
}
} else {
resetRoles(Role.valueOf(resetRole));
}
processForm(requestContext);
}
use of fi.otavanopisto.pyramus.domainmodel.users.Role in project pyramus by otavanopisto.
the class MuikkuRESTService method updateUser.
@Path("/users/{IDENTIFIER}")
@PUT
@RESTPermit(MuikkuPermissions.MUIKKU_UPDATE_STAFF_MEMBER)
public Response updateUser(@Context HttpServletRequest request, @PathParam("IDENTIFIER") String identifier, StaffMemberPayload payload) {
if (!StringUtils.equals(payload.getIdentifier(), identifier)) {
return Response.status(Status.BAD_REQUEST).entity("Payload identifier doesn't match path identifier").build();
}
if (StringUtils.isAnyBlank(payload.getFirstName(), payload.getLastName(), payload.getEmail(), payload.getRole())) {
return Response.status(Status.BAD_REQUEST).entity("Empty fields in payload").build();
}
// Test allowed roles
Role role;
try {
role = Role.valueOf(payload.getRole());
if (role != Role.MANAGER && role != Role.TEACHER) {
return Response.status(Status.BAD_REQUEST).entity(String.format("Unsupported role %s", payload.getRole())).build();
}
} catch (Exception e) {
return Response.status(Status.BAD_REQUEST).entity(String.format("Unsupported role %s", payload.getRole())).build();
}
// Find user
Long staffMemberId = Long.valueOf(payload.getIdentifier());
StaffMember staffMember = userController.findStaffMemberById(staffMemberId);
if (staffMember == null || !UserUtils.canAccessOrganization(sessionController.getUser(), staffMember.getOrganization())) {
return Response.status(Status.NOT_FOUND).build();
}
Role existingRole = staffMember.getRole();
if (existingRole != Role.MANAGER && existingRole != Role.TEACHER) {
role = existingRole;
}
List<Email> staffMemberEmails = userController.listStaffMemberEmails(staffMember);
if (staffMemberEmails.size() != 1) {
return Response.status(Status.BAD_REQUEST).entity("User has several emails").build();
}
Email email = staffMemberEmails.get(0);
String address = StringUtils.trim(StringUtils.lowerCase(payload.getEmail()));
if (!UserUtils.isAllowedEmail(address, email.getContactType(), staffMember.getPerson().getId())) {
return Response.status(Status.CONFLICT).entity(getMessage(request.getLocale(), "error.emailInUse")).build();
}
// Update user
staffMember = userController.updateStaffMember(staffMember, staffMember.getOrganization(), payload.getFirstName(), payload.getLastName(), role);
// Update email
try {
email = userController.updateStaffMemberEmail(staffMember, email, email.getContactType(), address, email.getDefaultAddress());
} catch (UserEmailInUseException e) {
// Set the transaction as rollback only
sessionContext.setRollbackOnly();
return Response.status(Status.CONFLICT).entity(getMessage(request.getLocale(), "error.emailInUse")).build();
}
return Response.ok(toRestModel(staffMember, email)).build();
}
use of fi.otavanopisto.pyramus.domainmodel.users.Role in project pyramus by otavanopisto.
the class StudentCrossOrganizationPermissionsTestsIT method testListStudents.
@ParameterizedTest
@EnumSource(Role.class)
public void testListStudents(Role role) throws NoSuchFieldException {
if (roleIsAllowed(role, studentPermissions, StudentPermissions.LIST_STUDENTS)) {
Person person = tools().createPerson();
Student studentFromAnotherOrg = tools().createStudent(person.getId(), studyProgramme.getId());
Person person2 = tools().createPerson();
Student studentFromSameOrg = tools().createStudent(person2.getId(), 1L);
try {
Response response = given().headers(getAuthHeaders(role)).get("/students/students");
Student[] listStudents = response.as(Student[].class);
if (!roleIsAllowed(role, studentPermissions, StudentPermissions.FEATURE_OWNED_GROUP_STUDENTS_RESTRICTION)) {
// User has no group restriction
assertTrue(Arrays.stream(listStudents).anyMatch(student -> studentFromSameOrg.getId().equals(student.getId())), String.format("Couldn't list student from same org (%s).", role));
} else {
// User has group restriction (study guider)
assertFalse(Arrays.stream(listStudents).anyMatch(student -> studentFromSameOrg.getId().equals(student.getId())), String.format("Couldn't list student from same org (%s).", role));
}
if (roleIsAllowed(role, organizationPermissions, OrganizationPermissions.ACCESS_ALL_ORGANIZATIONS)) {
// Can access all organizations -> can list student from another org.
assertTrue(Arrays.stream(listStudents).anyMatch(student -> studentFromAnotherOrg.getId().equals(student.getId())), String.format("Listed student from another org (%s).", role));
} else {
// Can access only own organizations -> can't list student from another org.
assertFalse(Arrays.stream(listStudents).anyMatch(student -> studentFromAnotherOrg.getId().equals(student.getId())), String.format("Listed student from another org (%s).", role));
}
} finally {
tools().deleteStudent(studentFromSameOrg);
tools().deletePerson(person2);
tools().deleteStudent(studentFromAnotherOrg);
tools().deletePerson(person);
}
} else {
given().headers(getAuthHeaders(role)).get("/students/students").then().statusCode(403);
}
}
Aggregations