Commit d2ba14ed authored by michael.simon's avatar michael.simon
Browse files

add scripting environment to classes

parent b80cc313
......@@ -28,9 +28,12 @@ public class ScriptEntity extends AbstractBaseEntity {
@Type(type = "org.hibernate.type.TextType")
private String script;
@Column(name = "rule_type", length = 32)
@Column(name = "script_type", length = 32)
private String scriptType;
@Column(name = "script_engine", length = 64)
private String scriptEngine;
@Column(name = "name", length = 128)
private String name;
......@@ -49,4 +52,20 @@ public class ScriptEntity extends AbstractBaseEntity {
public void setScript(String script) {
this.script = script;
}
public String getScriptType() {
return scriptType;
}
public void setScriptType(String scriptType) {
this.scriptType = scriptType;
}
public String getScriptEngine() {
return scriptEngine;
}
public void setScriptEngine(String scriptEngine) {
this.scriptEngine = scriptEngine;
}
}
package edu.kit.scc.webreg.script;
import java.io.Serializable;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import edu.kit.scc.webreg.dao.GroupDao;
import edu.kit.scc.webreg.dao.RegistryDao;
import edu.kit.scc.webreg.dao.ScriptDao;
import edu.kit.scc.webreg.dao.ServiceDao;
import edu.kit.scc.webreg.dao.UserDao;
@ApplicationScoped
public class ScriptingEnv implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
private ScriptDao scriptDao;
@Inject
private UserDao userDao;
@Inject
private GroupDao groupDao;
@Inject
private ServiceDao serviceDao;
@Inject
private RegistryDao registryDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public GroupDao getGroupDao() {
return groupDao;
}
public void setGroupDao(GroupDao groupDao) {
this.groupDao = groupDao;
}
public ServiceDao getServiceDao() {
return serviceDao;
}
public void setServiceDao(ServiceDao serviceDao) {
this.serviceDao = serviceDao;
}
public RegistryDao getRegistryDao() {
return registryDao;
}
public void setRegistryDao(RegistryDao registryDao) {
this.registryDao = registryDao;
}
public ScriptDao getScriptDao() {
return scriptDao;
}
public void setScriptDao(ScriptDao scriptDao) {
this.scriptDao = scriptDao;
}
}
/*******************************************************************************
* Copyright (c) 2014 Michael Simon.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*
* Contributors:
* Michael Simon - initial
******************************************************************************/
package edu.kit.scc.webreg.service.reg;
import edu.kit.scc.webreg.script.ScriptingEnv;
public interface ScriptingWorkflow {
void setScriptingEnv(ScriptingEnv env);
}
......@@ -47,9 +47,11 @@ import edu.kit.scc.webreg.event.MultipleGroupEvent;
import edu.kit.scc.webreg.event.ServiceRegisterEvent;
import edu.kit.scc.webreg.exc.EventSubmitException;
import edu.kit.scc.webreg.exc.RegisterException;
import edu.kit.scc.webreg.script.ScriptingEnv;
import edu.kit.scc.webreg.service.reg.ApprovalService;
import edu.kit.scc.webreg.service.reg.ApprovalWorkflow;
import edu.kit.scc.webreg.service.reg.RegisterUserWorkflow;
import edu.kit.scc.webreg.service.reg.ScriptingWorkflow;
@Stateless
public class ApprovalServiceImpl implements ApprovalService {
......@@ -84,6 +86,9 @@ public class ApprovalServiceImpl implements ApprovalService {
@Inject
private ApplicationConfig appConfig;
@Inject
private ScriptingEnv scriptingEnv;
@Override
public void registerApproval(RegistryEntity registry, Auditor parentAuditor) throws RegisterException {
ApprovalWorkflow workflow = getApprovalWorkflowInstance(registry.getApprovalBean());
......@@ -140,7 +145,7 @@ public class ApprovalServiceImpl implements ApprovalService {
registry.getUser().getEppn(), registry.getService().getName());
RegisterUserWorkflow workflow = getRegisterWorkflowInstance(registry.getRegisterBean());
try {
ServiceEntity serviceEntity = serviceDao.findByIdWithServiceProps(registry.getService().getId());
......@@ -205,8 +210,12 @@ public class ApprovalServiceImpl implements ApprovalService {
private RegisterUserWorkflow getRegisterWorkflowInstance(String className) {
try {
Object o = Class.forName(className).newInstance();
if (o instanceof RegisterUserWorkflow)
if (o instanceof RegisterUserWorkflow) {
if (o instanceof ScriptingWorkflow)
((ScriptingWorkflow) o).setScriptingEnv(scriptingEnv);
return (RegisterUserWorkflow) o;
}
else {
logger.warn("Service Register bean misconfigured, Object not Type RegisterUserWorkflow but: {}", o.getClass());
return null;
......
......@@ -64,12 +64,14 @@ import edu.kit.scc.webreg.event.MultipleGroupEvent;
import edu.kit.scc.webreg.event.ServiceRegisterEvent;
import edu.kit.scc.webreg.exc.EventSubmitException;
import edu.kit.scc.webreg.exc.RegisterException;
import edu.kit.scc.webreg.script.ScriptingEnv;
import edu.kit.scc.webreg.service.reg.ApprovalService;
import edu.kit.scc.webreg.service.reg.GroupCapable;
import edu.kit.scc.webreg.service.reg.GroupUtil;
import edu.kit.scc.webreg.service.reg.PasswordUtil;
import edu.kit.scc.webreg.service.reg.RegisterUserService;
import edu.kit.scc.webreg.service.reg.RegisterUserWorkflow;
import edu.kit.scc.webreg.service.reg.ScriptingWorkflow;
import edu.kit.scc.webreg.service.reg.SetPasswordCapable;
@Stateless
......@@ -120,6 +122,9 @@ public class RegisterUserServiceImpl implements RegisterUserService {
@Inject
private ApplicationConfig appConfig;
@Inject
private ScriptingEnv scriptingEnv;
@Override
public void registerUser(UserEntity user, ServiceEntity service, String executor)
throws RegisterException {
......@@ -343,6 +348,7 @@ public class RegisterUserServiceImpl implements RegisterUserService {
group = groupDao.findWithUsers(group.getId());
RegisterUserWorkflow workflow = getWorkflowInstance(service.getRegisterBean());
if (! (workflow instanceof GroupCapable)) {
logger.warn("Workflow " + workflow.getClass() + " is not GroupCapable! But Group will be deleted anyway.");
return;
......@@ -597,8 +603,12 @@ public class RegisterUserServiceImpl implements RegisterUserService {
public RegisterUserWorkflow getWorkflowInstance(String className) {
try {
Object o = Class.forName(className).newInstance();
if (o instanceof RegisterUserWorkflow)
if (o instanceof RegisterUserWorkflow) {
if (o instanceof ScriptingWorkflow)
((ScriptingWorkflow) o).setScriptingEnv(scriptingEnv);
return (RegisterUserWorkflow) o;
}
else {
logger.warn("Service Register bean misconfigured, Object not Type RegisterUserWorkflow but: {}", o.getClass());
return null;
......
......@@ -18,29 +18,41 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.xml.registry.RegistryException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import edu.kit.scc.webreg.audit.Auditor;
import edu.kit.scc.webreg.entity.GroupEntity;
import edu.kit.scc.webreg.entity.RegistryEntity;
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.entity.audit.AuditStatus;
import edu.kit.scc.webreg.exc.PropertyReaderException;
import edu.kit.scc.webreg.exc.RegisterException;
import edu.kit.scc.webreg.script.ScriptingEnv;
import edu.kit.scc.webreg.service.reg.GroupCapable;
import edu.kit.scc.webreg.service.reg.Infotainment;
import edu.kit.scc.webreg.service.reg.InfotainmentCapable;
import edu.kit.scc.webreg.service.reg.RegisterUserWorkflow;
import edu.kit.scc.webreg.service.reg.ScriptingWorkflow;
import edu.kit.scc.webreg.service.reg.SetPasswordCapable;
import edu.kit.scc.webreg.service.reg.impl.GroupUpdateStructure;
import jcifs.util.Hexdump;
import jcifs.util.MD4;
public class ScriptedLdapRegisterWorkflow
implements RegisterUserWorkflow, InfotainmentCapable, GroupCapable, SetPasswordCapable {
implements RegisterUserWorkflow, InfotainmentCapable, GroupCapable, SetPasswordCapable, ScriptingWorkflow {
protected static Logger logger = LoggerFactory.getLogger(ScriptedLdapRegisterWorkflow.class);
protected ScriptingEnv scriptingEnv;
@Override
public void registerUser(UserEntity user, ServiceEntity service,
......@@ -69,12 +81,33 @@ public class ScriptedLdapRegisterWorkflow
RegistryEntity registry, Auditor auditor) throws RegisterException {
PropertyReader prop = PropertyReader.newRegisterPropReader(service);
/*
* Compare values from user and registry store. Found differences trigger
* a full reconsiliation
*/
Map<String, String> reconMap = new HashMap<String, String>();
try {
String scriptName = prop.readProp("script_name");
ScriptEntity scriptEntity = scriptingEnv.getScriptDao().findByName(scriptName);
if (scriptEntity.getScriptType().equalsIgnoreCase("javascript")) {
ScriptEngine engine = (new ScriptEngineManager()).getEngineByName(scriptEntity.getScriptEngine());
engine.eval(scriptEntity.getScript());
Invocable invocable = (Invocable) engine;
Object result = invocable.invokeFunction("updateRegistry", scriptingEnv, reconMap, user, registry, service, auditor);
}
} catch (PropertyReaderException e) {
throw new RegisterException(e);
} catch (ScriptException e) {
throw new RegisterException(e);
} catch (NoSuchMethodException e) {
throw new RegisterException(e);
}
String homeId = user.getAttributeStore().get("http://bwidm.de/bwidmOrgId");
homeId = homeId.toLowerCase();
......@@ -98,7 +131,6 @@ public class ScriptedLdapRegisterWorkflow
reconMap.put("gidNumber", "" + user.getPrimaryGroup().getGidNumber());
reconMap.put("description", registry.getId().toString());
//TODO add script capability here
reconMap.put("groupName", user.getPrimaryGroup().getName());
reconMap.put("localUid", homeUid);
reconMap.put("homeDir", "/home/" + homeUid);
......@@ -305,5 +337,10 @@ public class ScriptedLdapRegisterWorkflow
return "";
}
}
@Override
public void setScriptingEnv(ScriptingEnv env) {
this.scriptingEnv = scriptingEnv;
}
}
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