Commit 4cd3b257 authored by michael.simon's avatar michael.simon
Browse files

show different policies according to script resolution

parent 708478e3
......@@ -34,20 +34,15 @@ public class PolicyEntity extends AbstractBaseEntity {
@OneToOne(targetEntity = AgreementTextEntity.class)
private AgreementTextEntity actualAgreement;
@Column(name = "mandatory")
private Boolean mandatory;
@Column(name = "show_only")
private Boolean showOnly;
@Column(name = "hidden")
private Boolean hidden;
@Column(name = "name", length = 128)
private String name;
public Boolean getMandatory() {
return mandatory;
}
public void setMandatory(Boolean mandatory) {
this.mandatory = mandatory;
}
public String getName() {
return name;
}
......@@ -80,4 +75,20 @@ public class PolicyEntity extends AbstractBaseEntity {
this.actualAgreement = actualAgreement;
}
public Boolean getShowOnly() {
return showOnly;
}
public void setShowOnly(Boolean showOnly) {
this.showOnly = showOnly;
}
public Boolean getHidden() {
return hidden;
}
public void setHidden(Boolean hidden) {
this.hidden = hidden;
}
}
......@@ -10,10 +10,16 @@
******************************************************************************/
package edu.kit.scc.webreg.service;
import java.util.List;
import edu.kit.scc.webreg.entity.PolicyEntity;
import edu.kit.scc.webreg.entity.ServiceEntity;
import edu.kit.scc.webreg.entity.UserEntity;
public interface PolicyService extends BaseService<PolicyEntity, Long> {
PolicyEntity findWithAgreemets(Long id);
List<PolicyEntity> resolvePoliciesForService(ServiceEntity service, UserEntity user);
}
......@@ -10,12 +10,29 @@
******************************************************************************/
package edu.kit.scc.webreg.service.impl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import org.slf4j.Logger;
import edu.kit.scc.webreg.dao.BaseDao;
import edu.kit.scc.webreg.dao.PolicyDao;
import edu.kit.scc.webreg.dao.ServiceDao;
import edu.kit.scc.webreg.dao.UserDao;
import edu.kit.scc.webreg.entity.PolicyEntity;
import edu.kit.scc.webreg.entity.ScriptEntity;
import edu.kit.scc.webreg.entity.ServiceEntity;
import edu.kit.scc.webreg.entity.UserEntity;
import edu.kit.scc.webreg.script.ScriptingEnv;
import edu.kit.scc.webreg.service.PolicyService;
@Stateless
......@@ -23,9 +40,75 @@ public class PolicyServiceImpl extends BaseServiceImpl<PolicyEntity, Long> imple
private static final long serialVersionUID = 1;
@Inject
private Logger logger;
@Inject
private PolicyDao dao;
@Inject
private ServiceDao serviceDao;
@Inject
private UserDao userDao;
@Inject
private ScriptingEnv scriptingEnv;
@Override
public List<PolicyEntity> resolvePoliciesForService(ServiceEntity service, UserEntity user) {
List<PolicyEntity> policyList = new ArrayList<PolicyEntity>();
service = serviceDao.merge(service);
user = userDao.merge(user);
List<PolicyEntity> allPolicyList = new ArrayList<>(service.getPolicies());
for (PolicyEntity p : allPolicyList) {
if (p.getHidden() == null || p.getHidden().equals(Boolean.FALSE)) {
policyList.add(p);
}
}
if (service.getServiceProps().containsKey("override_policy_script")) {
String scriptName = service.getServiceProps().get("override_policy_script");
ScriptEntity scriptEntity = scriptingEnv.getScriptDao().findByName(scriptName);
if (scriptEntity == null) {
logger.warn("override_policy_script {} is defined for service {}, but not in database", scriptName, service.getName());
}
else {
if (scriptEntity.getScriptType().equalsIgnoreCase("javascript")) {
ScriptEngine engine = (new ScriptEngineManager()).getEngineByName(scriptEntity.getScriptEngine());
if (engine == null) {
logger.warn("override_policy_script {} for service {}, cannot load script enginge: {}", scriptName, service.getName(), scriptEntity.getScriptEngine());
}
else {
try {
engine.eval(scriptEntity.getScript());
Invocable invocable = (Invocable) engine;
invocable.invokeFunction("resolvePolicies", scriptingEnv, allPolicyList, policyList, service, user, logger);
} catch (ScriptException | NoSuchMethodException e) {
logger.warn("override_policy_script {} for service {} threw error: {}", scriptName, service.getName(), e.getMessage());
}
}
}
}
}
Collections.sort(policyList, new Comparator<PolicyEntity>() {
@Override
public int compare(PolicyEntity p1, PolicyEntity p2) {
return p1.getId().compareTo(p2.getId());
}
});
return policyList;
}
@Override
public PolicyEntity findWithAgreemets(Long id) {
return dao.findWithAgreemets(id);
......
......@@ -10,6 +10,7 @@
******************************************************************************/
package edu.kit.scc.webreg.service.reg;
import java.util.List;
import java.util.Set;
import edu.kit.scc.webreg.audit.Auditor;
......@@ -28,6 +29,9 @@ public interface RegisterUserService {
RegistryEntity registerUser(UserEntity user, ServiceEntity service, String executor)
throws RegisterException;
RegistryEntity registerUser(UserEntity user, ServiceEntity service, List<Long> policiesIdList, String executor)
throws RegisterException;
void reconsiliation(RegistryEntity registry, Boolean fullRecon,
String executor, Auditor parentAuditor) throws RegisterException;
......
......@@ -10,6 +10,7 @@
******************************************************************************/
package edu.kit.scc.webreg.service.reg.impl;
import java.util.List;
import java.util.Set;
import javax.ejb.Asynchronous;
......@@ -31,6 +32,12 @@ public class RegisterUserServiceImpl implements RegisterUserService {
@Inject
private Registrator registrator;
@Override
public RegistryEntity registerUser(UserEntity user, ServiceEntity service, List<Long> policiesIdList, String executor)
throws RegisterException {
return registrator.registerUser(user, service, executor, policiesIdList, true, null);
}
@Override
public RegistryEntity registerUser(UserEntity user, ServiceEntity service, String executor)
throws RegisterException {
......@@ -40,13 +47,13 @@ public class RegisterUserServiceImpl implements RegisterUserService {
@Override
public RegistryEntity registerUser(UserEntity user, ServiceEntity service, String executor, Boolean sendGroupUpdate)
throws RegisterException {
return registrator.registerUser(user, service, executor, sendGroupUpdate, null);
return registrator.registerUser(user, service, executor, null, sendGroupUpdate, null);
}
@Override
public RegistryEntity registerUser(UserEntity user, ServiceEntity service, String executor, Boolean sendGroupUpdate, Auditor parentAuditor)
throws RegisterException {
return registrator.registerUser(user, service, executor, sendGroupUpdate, parentAuditor);
return registrator.registerUser(user, service, executor, null, sendGroupUpdate, parentAuditor);
}
@Override
......
......@@ -31,6 +31,7 @@ import edu.kit.scc.webreg.audit.ServiceAuditor;
import edu.kit.scc.webreg.audit.ServiceRegisterAuditor;
import edu.kit.scc.webreg.bootstrap.ApplicationConfig;
import edu.kit.scc.webreg.dao.GroupDao;
import edu.kit.scc.webreg.dao.PolicyDao;
import edu.kit.scc.webreg.dao.RegistryDao;
import edu.kit.scc.webreg.dao.ServiceDao;
import edu.kit.scc.webreg.dao.ServiceEventDao;
......@@ -128,6 +129,9 @@ public class Registrator implements Serializable {
@Inject
private ScriptingEnv scriptingEnv;
@Inject
private PolicyDao policyDao;
@Inject
private Approvor approvor;
......@@ -138,10 +142,10 @@ public class Registrator implements Serializable {
public RegistryEntity registerUser(UserEntity user, ServiceEntity service, String executor, Boolean sendGroupUpdate)
throws RegisterException {
return registerUser(user, service, executor, sendGroupUpdate, null);
return registerUser(user, service, executor, null, sendGroupUpdate, null);
}
public RegistryEntity registerUser(UserEntity user, ServiceEntity service, String executor, Boolean sendGroupUpdate, Auditor parentAuditor)
public RegistryEntity registerUser(UserEntity user, ServiceEntity service, String executor, List<Long> policiesIdList, Boolean sendGroupUpdate, Auditor parentAuditor)
throws RegisterException {
user = userDao.merge(user);
......@@ -166,7 +170,7 @@ public class Registrator implements Serializable {
RegistryStatus.DELETED, RegistryStatus.DEPROVISIONED);
if (r.size() == 0) {
logger.info("User {} is not registered with parent service {} yet", user.getEppn(), service.getParentService().getName());
registerUser(user, service.getParentService(), executor, true, auditor);
registerUser(user, service.getParentService(), executor, null, true, auditor);
}
else {
logger.debug("User {} is already registered with parent service {}", user.getEppn(), service.getParentService().getName());
......@@ -177,12 +181,20 @@ public class Registrator implements Serializable {
try {
ApproverRoleEntity approverRole = service.getApproverRole();
Set<AgreementTextEntity> agrs = new HashSet<AgreementTextEntity>();
for (PolicyEntity policy : service.getPolicies())
agrs.add(policy.getActualAgreement());
if (policiesIdList == null) {
for (PolicyEntity policy : service.getPolicies())
agrs.add(policy.getActualAgreement());
}
else {
for (Long policyId : policiesIdList) {
agrs.add(policyDao.findById(policyId).getActualAgreement());
}
}
registry.setAgreedTexts(agrs);
registry.setAgreedTime(new Date());
registry.setService(service);
registry.setUser(user);
registry.setIdentity(user.getIdentity());
......
......@@ -13,8 +13,6 @@ package edu.kit.scc.webreg.bean;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -44,6 +42,7 @@ import edu.kit.scc.webreg.entity.identity.IdentityEntity;
import edu.kit.scc.webreg.exc.MisconfiguredServiceException;
import edu.kit.scc.webreg.exc.RegisterException;
import edu.kit.scc.webreg.exc.UserUpdateException;
import edu.kit.scc.webreg.service.PolicyService;
import edu.kit.scc.webreg.service.RegistryService;
import edu.kit.scc.webreg.service.ServiceService;
import edu.kit.scc.webreg.service.UserService;
......@@ -99,6 +98,9 @@ public class RegisterServiceBean implements Serializable {
@Inject
private RegisterUserService registerUserService;
@Inject
private PolicyService policyService;
@Inject
private SessionManager sessionManager;
......@@ -128,7 +130,7 @@ public class RegisterServiceBean implements Serializable {
id = service.getId();
}
service = serviceService.findByIdWithAttrs(id, "policies", "attributeSourceService", "serviceProps");
service = serviceService.findByIdWithAttrs(id, "attributeSourceService", "serviceProps");
List<RegistryEntity> r = registryService.findByServiceAndIdentityAndNotStatus(service, getIdentity(),
RegistryStatus.DELETED, RegistryStatus.DEPROVISIONED);
......@@ -151,13 +153,7 @@ public class RegisterServiceBean implements Serializable {
policyHolderList = new ArrayList<RegisterServiceBean.PolicyHolder>();
List<PolicyEntity> policiesTemp = new ArrayList<PolicyEntity>(service.getPolicies());
Collections.sort(policiesTemp, new Comparator<PolicyEntity>() {
@Override
public int compare(PolicyEntity p1, PolicyEntity p2) {
return p1.getId().compareTo(p2.getId());
}
});
List<PolicyEntity> policiesTemp = policyService.resolvePoliciesForService(service, selectedUserEntity);
for (PolicyEntity policy : policiesTemp) {
PolicyHolder ph = new PolicyHolder();
......@@ -294,7 +290,16 @@ public class RegisterServiceBean implements Serializable {
RegistryEntity registry;
try {
registry = registerUserService.registerUser(selectedUserEntity, service, "user-self");
if (policyHolderList.size() == 0) {
registry = registerUserService.registerUser(selectedUserEntity, service, "user-self");
}
else {
List<Long> policyIdList = new ArrayList<Long>();
for (PolicyHolder ph : policyHolderList) {
policyIdList.add(ph.getPolicy().getId());
}
registry = registerUserService.registerUser(selectedUserEntity, service, policyIdList, "user-self");
}
sessionManager.setUnregisteredServiceCreated(null);
} catch (RegisterException e) {
FacesContext.getCurrentInstance().addMessage("need_check",
......
......@@ -33,6 +33,16 @@
<bw:inputText id="nameField" label="#{messages.name}"
value="#{editPolicyBean.entity.name}" required="true" />
<h:outputText value="#{messages.hidden}" />
<p:selectBooleanButton id="hiddenField"
onLabel="#{messages.yes}" offLabel="#{messages.no}"
value="#{editPolicyBean.entity.hidden}"/>
<h:outputText value="#{messages.show_only}" />
<p:selectBooleanButton id="onlyShowField"
onLabel="#{messages.yes}" offLabel="#{messages.no}"
value="#{editPolicyBean.entity.showOnly}"/>
<p:outputLabel for="selectAgreement" value="#{messages.actual_agreement}:"/>
<h:selectOneMenu id="selectAgreement" value="#{editPolicyBean.selectedAgreement}"
......
......@@ -33,6 +33,12 @@
<h:outputText value="#{messages.name}:"/>
<h:outputText value="#{showPolicyBean.entity.name} (Service: #{showPolicyBean.entity.service.name})"/>
<h:outputText value="#{messages.hidden}:" />
<h:outputText value="#{showPolicyBean.entity.hidden ? messages.yes : messages.no}" />
<h:outputText value="#{messages.show_only}:" />
<h:outputText value="#{showPolicyBean.entity.showOnly ? messages.yes : messages.no}" />
<h:outputText value="#{messages.actual_agreement}:"/>
<h:outputText value="#{showPolicyBean.entity.actualAgreement.name}"/>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment