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

connecting attribute source groups

parent 8ad4ccff
......@@ -12,6 +12,7 @@ package edu.kit.scc.webreg.dao;
import java.util.List;
import edu.kit.scc.webreg.dao.as.AttributeSourceGroupDao;
import edu.kit.scc.webreg.entity.GroupEntity;
import edu.kit.scc.webreg.entity.LocalGroupEntity;
import edu.kit.scc.webreg.entity.UserEntity;
......@@ -45,4 +46,6 @@ public interface GroupDao extends BaseDao<GroupEntity, Long> {
HomeOrgGroupDao getHomeOrgGroupDao();
AttributeSourceGroupDao getAttributeSourceGroupDao();
}
......@@ -22,5 +22,8 @@ public interface AttributeSourceGroupDao extends BaseDao<AttributeSourceGroupEnt
List<AttributeSourceGroupEntity> findByUserAndAS(UserEntity user,
AttributeSourceEntity attributeSource);
AttributeSourceGroupEntity findByNameAndAS(String name,
AttributeSourceEntity attributeSource);
}
......@@ -24,6 +24,7 @@ import javax.persistence.criteria.Root;
import edu.kit.scc.webreg.dao.GroupDao;
import edu.kit.scc.webreg.dao.HomeOrgGroupDao;
import edu.kit.scc.webreg.dao.LocalGroupDao;
import edu.kit.scc.webreg.dao.as.AttributeSourceGroupDao;
import edu.kit.scc.webreg.entity.GroupEntity;
import edu.kit.scc.webreg.entity.LocalGroupEntity;
import edu.kit.scc.webreg.entity.UserEntity;
......@@ -39,6 +40,9 @@ public class JpaGroupDao extends JpaBaseDao<GroupEntity, Long> implements GroupD
@Inject
private HomeOrgGroupDao homeOrgGroupDao;
@Inject
private AttributeSourceGroupDao attributeSourceGroupDao;
@Override
public void addUserToGroup(UserEntity user, GroupEntity group) {
UserGroupEntity userGroup = createNewUserGroup();
......@@ -173,4 +177,9 @@ public class JpaGroupDao extends JpaBaseDao<GroupEntity, Long> implements GroupD
public HomeOrgGroupDao getHomeOrgGroupDao() {
return homeOrgGroupDao;
}
@Override
public AttributeSourceGroupDao getAttributeSourceGroupDao() {
return attributeSourceGroupDao;
}
}
......@@ -14,6 +14,7 @@ import java.util.List;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
......@@ -22,7 +23,6 @@ import javax.persistence.criteria.Root;
import edu.kit.scc.webreg.dao.as.AttributeSourceGroupDao;
import edu.kit.scc.webreg.dao.jpa.JpaBaseDao;
import edu.kit.scc.webreg.entity.HomeOrgGroupEntity;
import edu.kit.scc.webreg.entity.UserEntity;
import edu.kit.scc.webreg.entity.as.AttributeSourceEntity;
import edu.kit.scc.webreg.entity.as.AttributeSourceGroupEntity;
......@@ -48,6 +48,20 @@ public class JpaAttributeSourceGroupDao extends JpaBaseDao<AttributeSourceGroupE
return query.getResultList();
}
@Override
public AttributeSourceGroupEntity findByNameAndAS(String name, AttributeSourceEntity attributeSource) {
try {
return (AttributeSourceGroupEntity) em.createQuery("select e from AttributeSourceGroupEntity e where e.name = :name"
+ " and e.attributeSource = :attributeSource")
.setParameter("name", name)
.setParameter("attributeSource", attributeSource)
.getSingleResult();
}
catch (NoResultException e) {
return null;
}
}
@Override
public Class<AttributeSourceGroupEntity> getEntityClass() {
return AttributeSourceGroupEntity.class;
......
package edu.kit.scc.webreg.as;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
......@@ -11,12 +12,15 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import edu.kit.scc.webreg.audit.AttributeSourceAuditor;
import edu.kit.scc.webreg.dao.GroupDao;
import edu.kit.scc.webreg.dao.as.ASUserAttrValueDao;
import edu.kit.scc.webreg.dao.as.AttributeSourceGroupDao;
import edu.kit.scc.webreg.entity.AuditStatus;
import edu.kit.scc.webreg.entity.UserEntity;
import edu.kit.scc.webreg.entity.as.ASUserAttrEntity;
import edu.kit.scc.webreg.entity.as.ASUserAttrValueEntity;
import edu.kit.scc.webreg.entity.as.ASUserAttrValueStringEntity;
import edu.kit.scc.webreg.entity.as.AttributeSourceEntity;
import edu.kit.scc.webreg.entity.as.AttributeSourceGroupEntity;
import edu.kit.scc.webreg.exc.PropertyReaderException;
import edu.kit.scc.webreg.exc.UserUpdateException;
......@@ -33,8 +37,22 @@ public abstract class AbstractAttributeSourceWorkflow implements AttributeSource
protected String groupKey;
protected String groupSeparator;
public void init(ASUserAttrEntity asUserAttr)
private ASUserAttrEntity asUserAttr;
private ASUserAttrValueDao asValueDao;
private GroupDao groupDao;
private AttributeSourceGroupDao attributeSourceGroupDao;
private AttributeSourceAuditor auditor;
public void init(ASUserAttrEntity asUserAttr, ASUserAttrValueDao asValueDao, GroupDao groupDao,
AttributeSourceAuditor auditor)
throws UserUpdateException {
this.asUserAttr = asUserAttr;
this.asValueDao = asValueDao;
this.groupDao = groupDao;
this.auditor = auditor;
attributeSourceGroupDao = groupDao.getAttributeSourceGroupDao();
try {
prop = new PropertyReader(asUserAttr.getAttributeSource().getAsProps());
......@@ -53,8 +71,7 @@ public abstract class AbstractAttributeSourceWorkflow implements AttributeSource
}
}
protected Boolean createOrUpdateValues(Map<String, Object> valueMap, ASUserAttrEntity asUserAttr,
ASUserAttrValueDao asValueDao, AttributeSourceGroupDao attributeSourceGroupDao, AttributeSourceAuditor auditor) {
protected Boolean createOrUpdateValues(Map<String, Object> valueMap) {
Boolean changed = false;
List<ASUserAttrValueEntity> valueList = asValueDao.findValues(asUserAttr);
......@@ -79,22 +96,21 @@ public abstract class AbstractAttributeSourceWorkflow implements AttributeSource
logger.debug("Marking {} for update, {} for add and {} for delete", keysToUpdate.size(), keysToAdd.size(), keysToDelete.size());
for (String key : keysToAdd) {
changed |= createValue(key, valueMap.get(key), asUserAttr, asValueDao, attributeSourceGroupDao, auditor);
changed |= createValue(key, valueMap.get(key));
}
for (Entry<String, ASUserAttrValueEntity> entry : keysToDelete.entrySet()) {
changed |= deleteValue(entry.getKey(), entry.getValue(), asUserAttr, asValueDao, attributeSourceGroupDao, auditor);
changed |= deleteValue(entry.getKey(), entry.getValue());
}
for (Entry<String, ASUserAttrValueEntity> entry : keysToUpdate.entrySet()) {
changed |= updateValue(entry.getKey(), entry.getValue(), valueMap.get(entry.getKey()), asUserAttr, asValueDao, attributeSourceGroupDao, auditor);
changed |= updateValue(entry.getKey(), entry.getValue(), valueMap.get(entry.getKey()));
}
return changed;
}
private Boolean createValue(String key, Object o, ASUserAttrEntity asUserAttr, ASUserAttrValueDao asValueDao,
AttributeSourceGroupDao attributeSourceGroupDao, AttributeSourceAuditor auditor) {
private Boolean createValue(String key, Object o) {
if (o == null) {
logger.warn("Cannot process null value");
return false;
......@@ -111,7 +127,7 @@ public abstract class AbstractAttributeSourceWorkflow implements AttributeSource
asValue = (ASUserAttrValueStringEntity) asValueDao.persist(asValue);
if (key.equals(groupKey)) {
processGroups(asValue, asUserAttr, asValueDao, attributeSourceGroupDao, auditor);
processGroups(asValue);
}
}
else {
......@@ -122,22 +138,20 @@ public abstract class AbstractAttributeSourceWorkflow implements AttributeSource
return true;
}
private Boolean deleteValue(String key, ASUserAttrValueEntity asValue, ASUserAttrEntity asUserAttr, ASUserAttrValueDao asValueDao,
AttributeSourceGroupDao attributeSourceGroupDao, AttributeSourceAuditor auditor) {
private Boolean deleteValue(String key, ASUserAttrValueEntity asValue) {
logger.debug("Deleting value for key {}", key);
auditor.logAction("as-workflow", "DELETE VALUE", key, "", AuditStatus.SUCCESS);
asValueDao.delete(asValue);
if (key.equals(groupKey)) {
processGroups(null, asUserAttr, asValueDao, attributeSourceGroupDao, auditor);
processGroups(null);
}
return true;
}
private Boolean updateValue(String key, ASUserAttrValueEntity asValue, Object o, ASUserAttrEntity asUserAttr, ASUserAttrValueDao asValueDao,
AttributeSourceGroupDao attributeSourceGroupDao, AttributeSourceAuditor auditor) {
private Boolean updateValue(String key, ASUserAttrValueEntity asValue, Object o) {
Boolean changed = false;
logger.debug("Updating value for key {}", key);
......@@ -154,7 +168,7 @@ public abstract class AbstractAttributeSourceWorkflow implements AttributeSource
}
if (key.equals(groupKey)) {
changed |= processGroups(asStringValue, asUserAttr, asValueDao, attributeSourceGroupDao, auditor);
changed |= processGroups(asStringValue);
}
}
else {
......@@ -164,20 +178,51 @@ public abstract class AbstractAttributeSourceWorkflow implements AttributeSource
return changed;
}
private Boolean processGroups(ASUserAttrValueStringEntity asValue, ASUserAttrEntity asUserAttr, ASUserAttrValueDao asValueDao,
AttributeSourceGroupDao attributeSourceGroupDao, AttributeSourceAuditor auditor) {
private Boolean processGroups(ASUserAttrValueStringEntity asValue) {
Boolean changed = false;
UserEntity user = asUserAttr.getUser();
AttributeSourceEntity attributeSource = asUserAttr.getAttributeSource();
List<AttributeSourceGroupEntity> oldGroupList = attributeSourceGroupDao.findByUserAndAS(asUserAttr.getUser(), attributeSource);
if (asValue == null || asValue.getValueString() == null || asValue.getValueString().equals("")) {
//delete all groups for this user
for (AttributeSourceGroupEntity group : oldGroupList) {
groupDao.removeUserGromGroup(user, group);
}
changed = true;
}
else {
String[] groupsString = asValue.getValueString().split(groupSeparator);
List<AttributeSourceGroupEntity> oldGroupList = attributeSourceGroupDao.findByUserAndAS(asUserAttr.getUser(), asUserAttr.getAttributeSource());
Set<String> newGroups = new HashSet<String>(Arrays.asList(groupsString));
Map<String, AttributeSourceGroupEntity> oldGroupsMap = new HashMap<String, AttributeSourceGroupEntity>();
for (AttributeSourceGroupEntity group : oldGroupList) {
oldGroupsMap.put(group.getName(), group);
}
Set<String> groupsToRemove = new HashSet<String>(oldGroupsMap.keySet());
groupsToRemove.removeAll(newGroups);
for(String s : groupsToRemove) {
logger.debug("Removeing {} grom group {}", user.getEppn(), s);
groupDao.removeUserGromGroup(user, oldGroupsMap.get(s));
}
Set<String> groupsToAdd = new HashSet<String>(newGroups);
groupsToAdd.removeAll(oldGroupsMap.keySet());
for (String s : groupsToAdd) {
AttributeSourceGroupEntity group = attributeSourceGroupDao.findByNameAndAS(s, attributeSource);
if (group == null) {
logger.debug("Creating group {}", s);
group = attributeSourceGroupDao.createNew();
group.setName(s);
group.setAttributeSource(attributeSource);
}
}
}
return changed;
......
......@@ -3,15 +3,15 @@ package edu.kit.scc.webreg.as;
import java.io.Serializable;
import edu.kit.scc.webreg.audit.AttributeSourceAuditor;
import edu.kit.scc.webreg.dao.GroupDao;
import edu.kit.scc.webreg.dao.as.ASUserAttrValueDao;
import edu.kit.scc.webreg.dao.as.AttributeSourceGroupDao;
import edu.kit.scc.webreg.entity.as.ASUserAttrEntity;
import edu.kit.scc.webreg.exc.UserUpdateException;
public interface AttributeSourceWorkflow extends Serializable {
Boolean pollUserAttributes(ASUserAttrEntity asUserAttr,
ASUserAttrValueDao asValueDao, AttributeSourceGroupDao attributeSourceGroupDao,
ASUserAttrValueDao asValueDao, GroupDao groupDao,
AttributeSourceAuditor auditor)
throws UserUpdateException;
}
......@@ -25,11 +25,10 @@ import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.kit.scc.webreg.audit.AttributeSourceAuditor;
import edu.kit.scc.webreg.dao.GroupDao;
import edu.kit.scc.webreg.dao.as.ASUserAttrValueDao;
import edu.kit.scc.webreg.dao.as.AttributeSourceGroupDao;
import edu.kit.scc.webreg.entity.UserEntity;
import edu.kit.scc.webreg.entity.as.ASUserAttrEntity;
import edu.kit.scc.webreg.entity.as.AttributeSourceEntity;
import edu.kit.scc.webreg.entity.as.AttributeSourceQueryStatus;
import edu.kit.scc.webreg.exc.PropertyReaderException;
import edu.kit.scc.webreg.exc.UserUpdateException;
......@@ -41,11 +40,11 @@ public class HttpUrlSingleAttributeSource extends
@Override
public Boolean pollUserAttributes(ASUserAttrEntity asUserAttr, ASUserAttrValueDao asValueDao,
AttributeSourceGroupDao attributeSourceGroupDao, AttributeSourceAuditor auditor) throws UserUpdateException {
GroupDao groupDao, AttributeSourceAuditor auditor) throws UserUpdateException {
Boolean changed = false;
init(asUserAttr);
init(asUserAttr, asValueDao, groupDao, auditor);
String urlTemplate;
try {
......@@ -55,7 +54,6 @@ public class HttpUrlSingleAttributeSource extends
}
UserEntity user = asUserAttr.getUser();
AttributeSourceEntity attributeSource = asUserAttr.getAttributeSource();
VelocityEngine engine = new VelocityEngine();
engine.setProperty("runtime.log.logsystem.log4j.logger", "root");
......@@ -119,7 +117,7 @@ public class HttpUrlSingleAttributeSource extends
Map<String, Object> map = om.readValue(r, Map.class);
logger.debug("Got {} values", map.size());
changed |= createOrUpdateValues(map, asUserAttr, asValueDao, attributeSourceGroupDao, auditor);
changed |= createOrUpdateValues(map);
asUserAttr.setQueryStatus(AttributeSourceQueryStatus.SUCCESS);
......
......@@ -12,11 +12,11 @@ import edu.kit.scc.webreg.audit.AttributeSourceAuditor;
import edu.kit.scc.webreg.bootstrap.ApplicationConfig;
import edu.kit.scc.webreg.dao.AuditDetailDao;
import edu.kit.scc.webreg.dao.AuditEntryDao;
import edu.kit.scc.webreg.dao.GroupDao;
import edu.kit.scc.webreg.dao.UserDao;
import edu.kit.scc.webreg.dao.as.ASUserAttrDao;
import edu.kit.scc.webreg.dao.as.ASUserAttrValueDao;
import edu.kit.scc.webreg.dao.as.AttributeSourceDao;
import edu.kit.scc.webreg.dao.as.AttributeSourceGroupDao;
import edu.kit.scc.webreg.entity.UserEntity;
import edu.kit.scc.webreg.entity.as.ASUserAttrEntity;
import edu.kit.scc.webreg.entity.as.AttributeSourceEntity;
......@@ -40,7 +40,7 @@ public class AttributeSourceQueryServiceImpl implements AttributeSourceQueryServ
private AttributeSourceDao attributeSourceDao;
@Inject
private AttributeSourceGroupDao attributeSourceGroupDao;
private GroupDao groupDao;
@Inject
private ASUserAttrDao asUserAttrDao;
......@@ -100,7 +100,7 @@ public class AttributeSourceQueryServiceImpl implements AttributeSourceQueryServ
auditor.setDetail("Updating attributes for user " + user.getEppn());
auditor.setAsUserAttr(asUserAttr);
changed = workflow.pollUserAttributes(asUserAttr, asValueDao, attributeSourceGroupDao, auditor);
changed = workflow.pollUserAttributes(asUserAttr, asValueDao, groupDao, auditor);
if (AttributeSourceQueryStatus.SUCCESS.equals(asUserAttr.getQueryStatus())) {
asUserAttr.setLastSuccessfulQuery(new Date());
......
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