Search in sources :

Example 1 with UserEmailInUseException

use of fi.otavanopisto.pyramus.framework.UserEmailInUseException 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();
}
Also used : Role(fi.otavanopisto.pyramus.domainmodel.users.Role) UserEmailInUseException(fi.otavanopisto.pyramus.framework.UserEmailInUseException) Email(fi.otavanopisto.pyramus.domainmodel.base.Email) StaffMember(fi.otavanopisto.pyramus.domainmodel.users.StaffMember) UserEmailInUseException(fi.otavanopisto.pyramus.framework.UserEmailInUseException) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.pyramus.rest.annotation.RESTPermit) PUT(javax.ws.rs.PUT)

Example 2 with UserEmailInUseException

use of fi.otavanopisto.pyramus.framework.UserEmailInUseException in project pyramus by otavanopisto.

the class StudentEmailRESTService method updateStudentEmail.

@Path("/{ID:[0-9]*}")
@PUT
@RESTPermit(handling = Handling.INLINE)
public Response updateStudentEmail(@PathParam("STUDENTID") Long studentId, @PathParam("ID") Long id, fi.otavanopisto.pyramus.rest.model.Email body) {
    Student student = studentController.findStudentById(studentId);
    Status studentStatus = checkStudent(student);
    if (studentStatus != Status.OK) {
        return Response.status(studentStatus).build();
    }
    if (!restSecurity.hasPermission(new String[] { StudentPermissions.UPDATE_STUDENTEMAIL }, student) && !restSecurity.hasPermission(new String[] { StudentPermissions.STUDENT_OWNER }, student)) {
        return Response.status(Status.FORBIDDEN).build();
    }
    Email email = commonController.findEmailById(id);
    if (email == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    if (!email.getContactInfo().getId().equals(student.getContactInfo().getId())) {
        return Response.status(Status.NOT_FOUND).build();
    }
    ContactType contactType = commonController.findContactTypeById(body.getContactTypeId());
    try {
        email = studentController.updateStudentEmail(student, email, contactType, body.getAddress(), body.getDefaultAddress());
        return Response.ok(objectFactory.createModel(email)).build();
    } catch (UserEmailInUseException e) {
        return Response.status(Status.BAD_REQUEST).build();
    }
}
Also used : Status(javax.ws.rs.core.Response.Status) UserEmailInUseException(fi.otavanopisto.pyramus.framework.UserEmailInUseException) Email(fi.otavanopisto.pyramus.domainmodel.base.Email) ContactType(fi.otavanopisto.pyramus.domainmodel.base.ContactType) Student(fi.otavanopisto.pyramus.domainmodel.students.Student) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.pyramus.rest.annotation.RESTPermit) PUT(javax.ws.rs.PUT)

Example 3 with UserEmailInUseException

use of fi.otavanopisto.pyramus.framework.UserEmailInUseException in project pyramus by otavanopisto.

the class StudentEmailRESTService method createStudentEmail.

@Path("/")
@POST
@RESTPermit(handling = Handling.INLINE)
public Response createStudentEmail(@PathParam("STUDENTID") Long studentId, fi.otavanopisto.pyramus.rest.model.Email email) {
    if (email == null) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    Student student = studentController.findStudentById(studentId);
    Status studentStatus = checkStudent(student);
    if (studentStatus != Status.OK) {
        return Response.status(studentStatus).build();
    }
    if (!restSecurity.hasPermission(new String[] { StudentPermissions.CREATE_STUDENTEMAIL, StudentPermissions.STUDENT_OWNER }, student, Style.OR)) {
        return Response.status(Status.FORBIDDEN).build();
    }
    Long contactTypeId = email.getContactTypeId();
    Boolean defaultAddress = email.getDefaultAddress();
    String address = email.getAddress();
    if (contactTypeId == null || defaultAddress == null || StringUtils.isBlank(address)) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    ContactType contactType = commonController.findContactTypeById(contactTypeId);
    if (contactType == null) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    try {
        return Response.ok(objectFactory.createModel(studentController.addStudentEmail(student, contactType, address, defaultAddress))).build();
    } catch (UserEmailInUseException ueiue) {
        return Response.status(Status.FORBIDDEN).build();
    }
}
Also used : Status(javax.ws.rs.core.Response.Status) UserEmailInUseException(fi.otavanopisto.pyramus.framework.UserEmailInUseException) ContactType(fi.otavanopisto.pyramus.domainmodel.base.ContactType) Student(fi.otavanopisto.pyramus.domainmodel.students.Student) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.pyramus.rest.annotation.RESTPermit) POST(javax.ws.rs.POST)

Aggregations

UserEmailInUseException (fi.otavanopisto.pyramus.framework.UserEmailInUseException)3 RESTPermit (fi.otavanopisto.pyramus.rest.annotation.RESTPermit)3 Path (javax.ws.rs.Path)3 ContactType (fi.otavanopisto.pyramus.domainmodel.base.ContactType)2 Email (fi.otavanopisto.pyramus.domainmodel.base.Email)2 Student (fi.otavanopisto.pyramus.domainmodel.students.Student)2 PUT (javax.ws.rs.PUT)2 Status (javax.ws.rs.core.Response.Status)2 Role (fi.otavanopisto.pyramus.domainmodel.users.Role)1 StaffMember (fi.otavanopisto.pyramus.domainmodel.users.StaffMember)1 POST (javax.ws.rs.POST)1