diff --git a/src/main/java/info/bukova/isspst/data/FilterableRequirement.java b/src/main/java/info/bukova/isspst/data/FilterableRequirement.java new file mode 100644 index 00000000..27159aaf --- /dev/null +++ b/src/main/java/info/bukova/isspst/data/FilterableRequirement.java @@ -0,0 +1,16 @@ +package info.bukova.isspst.data; + +import info.bukova.isspst.security.RequirementFilterEvaluator; + +/** + * Pomocné rozhraní pro filtrování záznamů pře vyhodnocovač prav {@link RequirementFilterEvaluator}. + * + * @author pepa + * + */ +public interface FilterableRequirement { + + Workgroup getCentre(); + Workgroup getWorkgroup(); + +} diff --git a/src/main/java/info/bukova/isspst/data/JoinedItem.java b/src/main/java/info/bukova/isspst/data/JoinedItem.java new file mode 100644 index 00000000..48a3abb8 --- /dev/null +++ b/src/main/java/info/bukova/isspst/data/JoinedItem.java @@ -0,0 +1,56 @@ +package info.bukova.isspst.data; + +public class JoinedItem implements FilterableRequirement { + + private RequirementItem item; + private Workgroup workgroup; + private Workgroup centre; + private User ownedBy; + + public JoinedItem(RequirementItem item, Workgroup workgroup, + Workgroup centre, User owner) { + super(); + this.item = item; + this.workgroup = workgroup; + this.centre = centre; + this.ownedBy = owner; + } + + public JoinedItem(RequirementItem item) { + super(); + this.item = item; + } + + public RequirementItem getItem() { + return item; + } + + public void setItem(RequirementItem item) { + this.item = item; + } + + public Workgroup getWorkgroup() { + return workgroup; + } + + public void setWorkgroup(Workgroup workgroup) { + this.workgroup = workgroup; + } + + public Workgroup getCentre() { + return centre; + } + + public void setCentre(Workgroup centre) { + this.centre = centre; + } + + public User getOwnedBy() { + return ownedBy; + } + + public void setOwnedBy(User owner) { + this.ownedBy = owner; + } + +} diff --git a/src/main/java/info/bukova/isspst/data/Requirement.java b/src/main/java/info/bukova/isspst/data/Requirement.java index fd08c2bb..0d8d222d 100644 --- a/src/main/java/info/bukova/isspst/data/Requirement.java +++ b/src/main/java/info/bukova/isspst/data/Requirement.java @@ -9,7 +9,6 @@ import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; -import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.persistence.Table; @@ -17,8 +16,7 @@ import javax.persistence.Table; @Table(name = "REQUIREMENT") public class Requirement extends RequirementBase { - @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) - @JoinColumn(name = "REQUIREMENT_ID") + @OneToMany(fetch = FetchType.EAGER, mappedBy = "requirement", cascade = CascadeType.ALL) private List items; @Column(name = "DELIVERYDATE") diff --git a/src/main/java/info/bukova/isspst/data/RequirementBase.java b/src/main/java/info/bukova/isspst/data/RequirementBase.java index fc1854eb..14b4309c 100644 --- a/src/main/java/info/bukova/isspst/data/RequirementBase.java +++ b/src/main/java/info/bukova/isspst/data/RequirementBase.java @@ -19,7 +19,7 @@ import org.hibernate.annotations.LazyCollection; import org.hibernate.annotations.LazyCollectionOption; @MappedSuperclass -public class RequirementBase extends BaseData { +public class RequirementBase extends BaseData implements FilterableRequirement { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "TYPE_ID") diff --git a/src/main/java/info/bukova/isspst/data/RequirementItem.java b/src/main/java/info/bukova/isspst/data/RequirementItem.java index 4bc5f6aa..c9b9ca78 100644 --- a/src/main/java/info/bukova/isspst/data/RequirementItem.java +++ b/src/main/java/info/bukova/isspst/data/RequirementItem.java @@ -24,6 +24,10 @@ public class RequirementItem @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "REQSUBJECT_ID") private RequirementSubject reqSubject; + + @ManyToOne + @JoinColumn(name = "REQUIREMENT_ID") + private Requirement requirement; @Column(name = "CODE") private String code; @@ -162,4 +166,14 @@ public class RequirementItem return false; } + + public Requirement getRequirement() + { + return requirement; + } + + public void setRequirement(Requirement requirement) + { + this.requirement = requirement; + } } diff --git a/src/main/java/info/bukova/isspst/security/AbstractRequirementEvaluator.java b/src/main/java/info/bukova/isspst/security/AbstractRequirementEvaluator.java index e5cb6daf..4becbafb 100644 --- a/src/main/java/info/bukova/isspst/security/AbstractRequirementEvaluator.java +++ b/src/main/java/info/bukova/isspst/security/AbstractRequirementEvaluator.java @@ -6,6 +6,12 @@ import info.bukova.isspst.data.RequirementState; import org.springframework.security.core.Authentication; +/** + * Abstraktní třída pro vyhodnocení práv editace nových požadavků. + * + * @author pepa + * + */ public abstract class AbstractRequirementEvaluator extends AbstractModuleEvaluator implements Evaluator { protected abstract Class getServiceClass(); diff --git a/src/main/java/info/bukova/isspst/security/Evaluator.java b/src/main/java/info/bukova/isspst/security/Evaluator.java index f403cc8b..9bb62f18 100644 --- a/src/main/java/info/bukova/isspst/security/Evaluator.java +++ b/src/main/java/info/bukova/isspst/security/Evaluator.java @@ -1,9 +1,26 @@ package info.bukova.isspst.security; +import org.springframework.security.access.prepost.PostFilter; +import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.Authentication; +/** + * Rozhraní vyhodnocovačů práv. + * + * @author pepa + * + */ public interface Evaluator { + /** + * Metoda se zavolá pro vyhodnocení práv k danému objektu. Volá ji Spring security + * na základě anotace {@link PreAuthorize}, případně {@link PostFilter}. + * + * @param authentication objekt přihlášeného uživatele + * @param targetDomainObject objekt ke kterému se vyhodnocují práva + * @param permission práva potřebná k vykonání anotované metody + * @return true, pokud je dovoleno metodu vykonat + */ public boolean evaluate(Authentication authentication, Object targetDomainObject, String permission); diff --git a/src/main/java/info/bukova/isspst/security/EvaluatorsHolder.java b/src/main/java/info/bukova/isspst/security/EvaluatorsHolder.java index d5800b8c..06c2853b 100644 --- a/src/main/java/info/bukova/isspst/security/EvaluatorsHolder.java +++ b/src/main/java/info/bukova/isspst/security/EvaluatorsHolder.java @@ -2,6 +2,31 @@ package info.bukova.isspst.security; import java.util.Map; +import org.springframework.security.access.prepost.PostFilter; +import org.springframework.security.access.prepost.PreAuthorize; + +/** + * Kontainer vyhodnocovačů práv. Nastavuje se přes Spring kontext: + *
+ * {@code
+ * 
+ *		
+ *			
+ *				
+ *				
+ *				
+ *				
+ *			
+ *		
+ *		.......
+ * }
+ * 
+ * Vyhodnocovače jsou registrovány pro třídy jejichž některé metody podléhají kontrole práv - mají + * anotace {@link PreAuthorize} nebo {@link PostFilter} + * + * @author pepa + * + */ public class EvaluatorsHolder { private Map, Evaluator> globalEvaluators; @@ -15,6 +40,13 @@ public class EvaluatorsHolder { this.specialEvaluators = specialEvaluators; } + /** + * Podle třídy/rozhraní předaného objektu vrátí instanci vyhodnocovače. + * + * @param object objekt, ve kterém se provádí kontrola práv + * @param special true, pokud se má kontrolovat speciální oprávnění vstažené ke středisku/komisi + * @return instanci vyhodnocovače {@link Evaluator}. Pokud třída objektu nemá zaregistrovaný vyhodnocovač, vrací null. + */ public Evaluator getForObject(Object object, boolean special) { Map, Evaluator> evals; diff --git a/src/main/java/info/bukova/isspst/security/RequirementFilterEvaluator.java b/src/main/java/info/bukova/isspst/security/RequirementFilterEvaluator.java index db70c8da..8ac5dd6d 100644 --- a/src/main/java/info/bukova/isspst/security/RequirementFilterEvaluator.java +++ b/src/main/java/info/bukova/isspst/security/RequirementFilterEvaluator.java @@ -1,9 +1,9 @@ package info.bukova.isspst.security; import info.bukova.isspst.Constants; +import info.bukova.isspst.data.FilterableRequirement; import info.bukova.isspst.data.Permission; import info.bukova.isspst.data.PermissionType; -import info.bukova.isspst.data.RequirementBase; import info.bukova.isspst.data.Role; import info.bukova.isspst.data.User; import info.bukova.isspst.data.Workgroup; @@ -11,8 +11,17 @@ import info.bukova.isspst.services.workgroups.WorkgroupService; import java.util.List; +import org.springframework.security.access.prepost.PostFilter; import org.springframework.security.core.Authentication; +/** + * Filtr kolekce objektů {@link FilterableRequirement}. Podle role, kterou má uživatel + * na středisku/komisi ve které je požadavek, se vyhodnotí, zda má uživatel právo ho + * zobrazit. Požití s anotací {@link PostFilter}. + * + * @author pepa + * + */ public class RequirementFilterEvaluator implements Evaluator { private WorkgroupService wgService; @@ -21,11 +30,19 @@ public class RequirementFilterEvaluator implements Evaluator { this.wgService = wgService; } + /** + * + * @see info.bukova.isspst.security.Evaluator#evaluate(org.springframework.security.core.Authentication, java.lang.Object, java.lang.String) + * @param authentication objekt přihlášeného uživatele + * @param targetDomainObject objekt požadavku + * @param permission oprávnění nutné k zobrazení + * + */ @Override public boolean evaluate(Authentication authentication, Object targetDomainObject, String permission) { - RequirementBase req = (RequirementBase) targetDomainObject; + FilterableRequirement req = (FilterableRequirement) targetDomainObject; Workgroup reqWg; if (!(authentication.getPrincipal() instanceof User)) { diff --git a/src/main/java/info/bukova/isspst/security/ServiceEvaluator.java b/src/main/java/info/bukova/isspst/security/ServiceEvaluator.java index bb673fcb..019bdf52 100644 --- a/src/main/java/info/bukova/isspst/security/ServiceEvaluator.java +++ b/src/main/java/info/bukova/isspst/security/ServiceEvaluator.java @@ -2,6 +2,12 @@ package info.bukova.isspst.security; import org.springframework.security.core.Authentication; +/** + * Vyhodnocuje základní práva modulů - číst, upravit, přidat, mazat. + * + * @author pepa + * + */ public class ServiceEvaluator extends AbstractModuleEvaluator implements Evaluator { @Override diff --git a/src/main/java/info/bukova/isspst/security/UserEvaluator.java b/src/main/java/info/bukova/isspst/security/UserEvaluator.java index c90e88d5..27eb45aa 100644 --- a/src/main/java/info/bukova/isspst/security/UserEvaluator.java +++ b/src/main/java/info/bukova/isspst/security/UserEvaluator.java @@ -5,6 +5,12 @@ import info.bukova.isspst.data.User; import org.springframework.security.core.Authentication; +/** + * Vyhodnocuje právo k editaci vlastního záznamu uživatele + * + * @author pepa + * + */ public class UserEvaluator implements Evaluator { @Override diff --git a/src/main/java/info/bukova/isspst/security/WorkgroupAwareServiceEvaluator.java b/src/main/java/info/bukova/isspst/security/WorkgroupAwareServiceEvaluator.java index dacfee26..b03f8738 100644 --- a/src/main/java/info/bukova/isspst/security/WorkgroupAwareServiceEvaluator.java +++ b/src/main/java/info/bukova/isspst/security/WorkgroupAwareServiceEvaluator.java @@ -12,6 +12,14 @@ import java.util.List; import org.springframework.security.core.Authentication; +/** + * Vyhodnocovač práv vstahujících se ke členství ve středisku nebo komisi. + * Pokud má uživatel v některém ze středisek/komisí jichž je členem zadané + * oprávnění, je vyhodnoceno jako true a anotovaná metoda se provede. + * + * @author pepa + * + */ public class WorkgroupAwareServiceEvaluator implements Evaluator { private WorkgroupService wgService; diff --git a/src/main/java/info/bukova/isspst/services/requirement/RequirementService.java b/src/main/java/info/bukova/isspst/services/requirement/RequirementService.java index e743d507..2ee44f3f 100644 --- a/src/main/java/info/bukova/isspst/services/requirement/RequirementService.java +++ b/src/main/java/info/bukova/isspst/services/requirement/RequirementService.java @@ -1,8 +1,11 @@ package info.bukova.isspst.services.requirement; +import java.util.List; + +import info.bukova.isspst.data.JoinedItem; import info.bukova.isspst.data.Requirement; public interface RequirementService extends RequirementBaseService { - + public List getItemsForOrder(); } diff --git a/src/main/java/info/bukova/isspst/services/requirement/RequirementServiceImpl.java b/src/main/java/info/bukova/isspst/services/requirement/RequirementServiceImpl.java index a34559ab..69e37b1a 100644 --- a/src/main/java/info/bukova/isspst/services/requirement/RequirementServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/requirement/RequirementServiceImpl.java @@ -1,15 +1,23 @@ package info.bukova.isspst.services.requirement; import info.bukova.isspst.Constants; +import info.bukova.isspst.data.JoinedItem; import info.bukova.isspst.data.Requirement; +import info.bukova.isspst.data.RequirementItem; import info.bukova.isspst.data.RequirementState; import info.bukova.isspst.data.User; import info.bukova.isspst.data.Workflow; +import info.bukova.isspst.data.Workgroup; +import java.util.ArrayList; import java.util.Date; import java.util.List; +import org.hibernate.Query; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.access.prepost.PostFilter; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.transaction.annotation.Transactional; public class RequirementServiceImpl extends RequirementBaseServiceImpl implements RequirementService, @@ -46,4 +54,23 @@ public class RequirementServiceImpl extends return false; } + @SuppressWarnings("unchecked") + @Override + @Transactional + @PreAuthorize("hasPermission(this, 'PERM_SHOW_CENTRE_REQ')") + @PostFilter("hasPermission(filterObject, 'PERM_SHOW_CENTRE_REQ')") + public List getItemsForOrder() { + List wgList = workgroupService.getUserCentres(getLoggedInUser()); + Query q = dao.getQuery("select item from RequirementItem item left join item.requirement rq join rq.centre c where rq.kind is not null and rq.state = :state and c in (:wgList)"); + q.setParameterList("wgList", wgList); + q.setParameter("state", RequirementState.APPROVED); + List items = new ArrayList(); + + for (RequirementItem it : (List)q.list()) { + items.add(new JoinedItem(it, it.getRequirement().getWorkgroup(), it.getRequirement().getCentre(), it.getRequirement().getOwnedBy())); + } + + return items; + } + } diff --git a/src/main/webapp/WEB-INF/spring/root-context.xml b/src/main/webapp/WEB-INF/spring/root-context.xml index fa73325e..abc4569c 100644 --- a/src/main/webapp/WEB-INF/spring/root-context.xml +++ b/src/main/webapp/WEB-INF/spring/root-context.xml @@ -108,6 +108,7 @@ +