package be.mentoringsystems.merke.security; import be.mentoringsystems.merke.model.QueryParams; import be.mentoringsystems.merke.model.db.Attendance; import be.mentoringsystems.merke.model.db.Certificate; import be.mentoringsystems.merke.model.db.Context; import be.mentoringsystems.merke.model.db.File; import be.mentoringsystems.merke.model.db.Login; import be.mentoringsystems.merke.model.db.Remark; import be.mentoringsystems.merke.model.db.Vendor; import be.mentoringsystems.merke.model.db.VendorRequest; import be.mentoringsystems.merke.model.db.Vendorstall; import be.mentoringsystems.merke.model.db.VendorstallPlacement; import be.mentoringsystems.merke.model.dto.AttendanceDTO; import be.mentoringsystems.merke.model.dto.CertificateDTO; import be.mentoringsystems.merke.model.dto.FileDTO; import be.mentoringsystems.merke.model.dto.LoginDTO; import be.mentoringsystems.merke.model.dto.RemarkDTO; import be.mentoringsystems.merke.model.dto.VendorDTO; import be.mentoringsystems.merke.model.dto.VendorRequestDTO; import be.mentoringsystems.merke.model.dto.VendorstallDTO; import be.mentoringsystems.merke.model.dto.VendorstallPlacementDTO; import be.mentoringsystems.merke.service.AttendanceService; import be.mentoringsystems.merke.service.CertificateService; import be.mentoringsystems.merke.service.ContextService; import be.mentoringsystems.merke.service.FileService; import be.mentoringsystems.merke.service.LoginService; import be.mentoringsystems.merke.service.RemarkService; import be.mentoringsystems.merke.service.VendorRequestService; import be.mentoringsystems.merke.service.VendorService; import be.mentoringsystems.merke.service.VendorstallPlacementService; import be.mentoringsystems.merke.service.VendorstallService; import java.util.UUID; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.AccessDeniedException; import org.springframework.stereotype.Component; /** * * @author anthonyarents */ @Component("methodSecurity") public class MethodSecurity { @Autowired private transient LoginService loginService; @Autowired private transient VendorService vendorService; @Autowired private transient VendorstallService vendorstallService; @Autowired private transient VendorstallPlacementService vendorstallPlacementService; @Autowired private transient AttendanceService attendanceService; @Autowired private transient CertificateService certificateService; @Autowired private transient FileService fileService; @Autowired private transient RemarkService remarkService; @Autowired private transient VendorRequestService vendorRequestService; @Autowired private transient ContextService contextService; // QueryParams public boolean vendorLimited(final QueryParams queryParams) throws AccessDeniedException { final Login login = loginService.getCurrentLogin(); if (!login.isAdmin() && !login.isMSAdmin() && !login.isFireman()) { final Vendor vendor = vendorService.getByLoginId(login.getId()); if (!vendor.getId().toString().equals(queryParams.getFilterValue("vendorId"))) { return false; } } return true; } // Attendance public boolean hasAttendanceAccess(final AttendanceDTO dto, final UUID id) { dto.setId(id); return hasAttendanceAccess(dto); } public boolean hasAttendanceAccess(final AttendanceDTO dto) { boolean hasAccess = true; if (dto.getVendorId() != null) { hasAccess = hasVendorAccess(dto.getVendorId()); } if (hasAccess && dto.getVendorstallId() != null) { hasAccess = hasVendorstallAccess(dto.getVendorstallId()); } if (hasAccess && dto.getId() != null) { return hasAttendanceAccess(dto.getId()); } return hasAccess; } public boolean hasAttendanceAccess(final UUID id) { boolean hasAccess = true; Attendance obj = attendanceService.getById(id); if (obj == null) { hasAccess = false; } else { if (hasAccess && obj.getVendorId() != null) { hasAccess = hasVendorAccess(obj.getVendorId()); } if (hasAccess && obj.getVendorstallId() != null) { hasAccess = hasVendorstallAccess(obj.getVendorstallId()); } } return hasAccess; } // Certificate public boolean hasCertificateAccess(final CertificateDTO dto, final UUID id) { dto.setId(id); return hasCertificateAccess(dto); } public boolean hasCertificateAccess(final CertificateDTO dto) { boolean hasAccess = true; if (dto.getVendorId() != null) { hasAccess = hasVendorAccess(dto.getVendorId()); } if (hasAccess && dto.getVendorstallId() != null) { hasAccess = hasVendorstallAccess(dto.getVendorstallId()); } if (hasAccess && dto.getId() != null) { return hasCertificateAccess(dto.getId()); } return hasAccess; } public boolean hasCertificateAccess(final UUID id) { boolean hasAccess = true; Certificate obj = certificateService.getById(id); if (obj == null) { hasAccess = false; } else { if (hasAccess && obj.getVendorId() != null) { hasAccess = hasVendorAccess(obj.getVendorId()); } if (hasAccess && obj.getVendorstallId() != null) { hasAccess = hasVendorstallAccess(obj.getVendorstallId()); } } return hasAccess; } // Certificate public boolean hasFileAccess(final FileDTO dto, final UUID id) { dto.setId(id); return hasFileAccess(dto); } public boolean hasFileAccess(final FileDTO dto) { boolean hasAccess = true; if (dto.getVendorId() != null) { hasAccess = hasVendorAccess(dto.getVendorId()); } if (hasAccess && dto.getVendorstallId() != null) { hasAccess = hasVendorstallAccess(dto.getVendorstallId()); } if (hasAccess && dto.getId() != null) { return hasFileAccess(dto.getId()); } return hasAccess; } public boolean hasFileAccess(final UUID id) { boolean hasAccess = true; File obj = fileService.getById(id); if (obj == null) { hasAccess = false; } else { if (hasAccess && obj.getVendorId() != null) { hasAccess = hasVendorAccess(obj.getVendorId()); } if (hasAccess && obj.getVendorstallId() != null) { hasAccess = hasVendorstallAccess(obj.getVendorstallId()); } } return hasAccess; } // Remark public boolean hasRemarkAccess(final RemarkDTO dto, final UUID id) { dto.setId(id); return hasRemarkAccess(dto); } public boolean hasRemarkAccess(final RemarkDTO dto) { boolean hasAccess = true; if (dto.getVendorId() != null) { hasAccess = hasVendorAccess(dto.getVendorId()); } if (hasAccess && dto.getVendorstallId() != null) { hasAccess = hasVendorstallAccess(dto.getVendorstallId()); } if (hasAccess && dto.getId() != null) { return hasRemarkAccess(dto.getId()); } return hasAccess; } public boolean hasRemarkAccess(final UUID id) { boolean hasAccess = true; Remark obj = remarkService.getById(id); if (obj == null) { hasAccess = false; } else { if (hasAccess && obj.getVendorId() != null) { hasAccess = hasVendorAccess(obj.getVendorId()); } if (hasAccess && obj.getVendorstallId() != null) { hasAccess = hasVendorstallAccess(obj.getVendorstallId()); } } return hasAccess; } // Vendor public boolean hasVendorAccess(final VendorDTO dto, final UUID vendorId) { dto.setId(vendorId); return hasVendorAccess(dto); } public boolean hasVendorAccess(final VendorDTO dto) { return hasVendorAccess(dto.getId()); } public boolean hasVendorAccess(final UUID vendorId) { Login login = loginService.getCurrentLogin(); return hasVendorAccess(login, vendorId); } // VendorRequest public boolean hasVendorRequestAccess(final VendorRequestDTO dto, final UUID id) { dto.setId(id); return hasVendorRequestAccess(dto); } public boolean hasVendorRequestAccess(final VendorRequestDTO dto) { boolean hasAccess = true; if (dto.getVendorId() != null) { hasAccess = hasVendorAccess(dto.getVendorId()); } if (hasAccess && dto.getVendorstallId() != null) { hasAccess = hasVendorstallAccess(dto.getVendorstallId()); } if (hasAccess && dto.getId() != null) { return hasVendorRequestAccess(dto.getId()); } return hasAccess; } public boolean hasVendorRequestAccess(final UUID id) { boolean hasAccess = true; VendorRequest obj = vendorRequestService.getById(id); if (obj == null) { hasAccess = false; } else { if (hasAccess && obj.getVendorId() != null) { hasAccess = hasVendorAccess(obj.getVendorId()); } if (hasAccess && obj.getVendorstallId() != null) { hasAccess = hasVendorstallAccess(obj.getVendorstallId()); } } return hasAccess; } // Vendorstall public boolean hasVendorstallAccess(final VendorstallDTO dto, final UUID vendorstallId) { dto.setId(vendorstallId); return hasVendorstallAccess(dto); } public boolean hasVendorstallAccess(final VendorstallDTO dto) { return hasVendorstallAccess(dto.getId()); } public boolean hasVendorstallAccess(final UUID vendorstallId) { Login login = loginService.getCurrentLogin(); return hasVendorstallAccess(login, vendorstallId); } public boolean hasVendorstallPlacementAccess(final VendorstallPlacementDTO dto, final UUID vendorstallPlacementId) { dto.setId(vendorstallPlacementId); return hasVendorstallPlacementAccess(dto); } public boolean hasVendorstallPlacementAccess(final VendorstallPlacementDTO dto) { return hasVendorstallPlacementAccess(dto.getId()); } public boolean hasVendorstallPlacementAccess(final UUID vendorstallPlacementId) { Login login = loginService.getCurrentLogin(); return hasVendorstallPlacementAccess(login, vendorstallPlacementId); } public boolean hasLoginAccess(final LoginDTO dto, final UUID loginId) { dto.setId(loginId); return hasLoginAccess(dto); } public boolean hasLoginAccess(final LoginDTO dto) { return hasLoginAccess(dto.getId()); } public boolean hasLoginAccess(final UUID loginId) { Login login = loginService.getCurrentLogin(); return hasLoginAccess(login, loginId); } public boolean hasLoginAccess(final Login login, final UUID loginId) { if (login == null) { return false; } else if (login.isMSAdmin()) { return true; } else { if (loginId == null) { return false; } else { if (loginId.equals(login.getId())) { return true; } else { if (login.getContextId() == null) { return false; } else { final Context context = contextService.getById(login.getContextId()); final Vendor v = vendorService.getByLoginId(loginId); if (v != null && v.getContexts().contains(context)) { return login.isAdmin() || login.isFireman() || login.isPolice(); } else { return false; } } } } } } // HelperMethods private boolean hasVendorAccess(final Login login, final UUID vendorId) { if (login == null) { return false; } else if (login.isMSAdmin()) { return true; } else { if (vendorId == null) { return false; } else { Vendor v = vendorService.getById(vendorId); if (v == null) { return false; } else { if (v.getLoginId().equals(login.getId())) { return true; } else { if (login.getContextId() == null) { return false; } else { final Context context = contextService.getById(login.getContextId()); if (v.getContexts().contains(context)) { return login.isAdmin() || login.isFireman() || login.isPolice(); } else { return false; } } } } } } } private boolean hasVendorAdminAccess(final Login login, final Vendor vendor) { if (vendor == null) { return login.isMSAdmin(); } else { //TODO extra checks for vendor return true; } } public boolean hasVendorAdminAccess(final UUID vendorId) { final Login login = loginService.getCurrentLogin(); return hasVendorAdminAccess(login, vendorId); } private boolean hasVendorAdminAccess(final Login login, final UUID vendorId) { if (login == null) { return false; } else if (login.isMSAdmin()) { return true; } else if (login.isAdmin()) { if (vendorId == null) { return true; // creating a new obj } else { final Vendor v = vendorService.getById(vendorId); return hasVendorAdminAccess(login, v); } } else { return false; } } private boolean hasVendorstallAccess(final Login login, final UUID vendorstallId) { if (login == null) { return false; } else if (login.isMSAdmin()) { return true; } else { if (vendorstallId == null) { return true; } else { final Vendorstall vendorstall = vendorstallService.getById(vendorstallId); if (vendorstall == null) { return false; } else { return hasVendorAccess(login, vendorstall.getVendorId()); } } } } private boolean hasVendorstallPlacementAccess(final Login login, final UUID vendorstallPlacementId) { if (login == null) { return false; } else if (login.isMSAdmin()) { return true; } else { if (vendorstallPlacementId == null) { return true; } else { final VendorstallPlacement vendorstallPlacement = vendorstallPlacementService.getById(vendorstallPlacementId); if (vendorstallPlacement == null) { return false; } else { return hasVendorstallAccess(login, vendorstallPlacement.getVendorstallId()); } } } } }