Commit 8ad4ccff authored by michael.simon's avatar michael.simon
Browse files

extend attribute sources

parent 9bafe700
......@@ -10,6 +10,8 @@
******************************************************************************/
package edu.kit.scc.webreg.dao.as;
import java.util.List;
import edu.kit.scc.webreg.dao.BaseDao;
import edu.kit.scc.webreg.entity.as.ASUserAttrEntity;
import edu.kit.scc.webreg.entity.as.ASUserAttrValueDateEntity;
......@@ -27,4 +29,6 @@ public interface ASUserAttrValueDao extends BaseDao<ASUserAttrValueEntity, Long>
ASUserAttrValueEntity findValueByKey(ASUserAttrEntity asUserAttr, String key);
List<ASUserAttrValueEntity> findValues(ASUserAttrEntity asUserAttr);
}
/*******************************************************************************
* 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.dao.as;
import java.util.List;
import edu.kit.scc.webreg.dao.BaseDao;
import edu.kit.scc.webreg.entity.UserEntity;
import edu.kit.scc.webreg.entity.as.AttributeSourceEntity;
import edu.kit.scc.webreg.entity.as.AttributeSourceGroupEntity;
public interface AttributeSourceGroupDao extends BaseDao<AttributeSourceGroupEntity, Long> {
List<AttributeSourceGroupEntity> findByUserAndAS(UserEntity user,
AttributeSourceEntity attributeSource);
}
......@@ -10,6 +10,8 @@
******************************************************************************/
package edu.kit.scc.webreg.dao.jpa.as;
import java.util.List;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
import javax.persistence.NoResultException;
......@@ -41,6 +43,14 @@ public class JpaASUserAttrValueDao extends JpaBaseDao<ASUserAttrValueEntity, Lon
return new ASUserAttrValueDateEntity();
}
@SuppressWarnings("unchecked")
@Override
public List<ASUserAttrValueEntity> findValues(ASUserAttrEntity asUserAttr) {
return (List<ASUserAttrValueEntity>) em.createQuery("select a from ASUserAttrValueEntity a where "
+ "a.asUserAttr = :asUserAttr")
.setParameter("asUserAttr", asUserAttr).getResultList();
}
@Override
public ASUserAttrValueEntity findValueByKey(ASUserAttrEntity asUserAttr, String key) {
try {
......
/*******************************************************************************
* 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.dao.jpa.as;
import java.util.List;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
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;
@Named
@ApplicationScoped
public class JpaAttributeSourceGroupDao extends JpaBaseDao<AttributeSourceGroupEntity, Long> implements AttributeSourceGroupDao {
@Override
public List<AttributeSourceGroupEntity> findByUserAndAS(UserEntity user, AttributeSourceEntity attributeSource) {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<AttributeSourceGroupEntity> criteria = builder.createQuery(AttributeSourceGroupEntity.class);
Root<UserEntity> userRoot = criteria.from(UserEntity.class);
Root<AttributeSourceGroupEntity> asgRoot = criteria.from(AttributeSourceGroupEntity.class);
criteria.where(
builder.and(
builder.equal(userRoot.get("id"), user.getId()),
builder.equal(asgRoot.get("id"), attributeSource.getId())
));
Join<UserEntity, AttributeSourceGroupEntity> users = userRoot.join("groups");
CriteriaQuery<AttributeSourceGroupEntity> cq = criteria.select(users);
TypedQuery<AttributeSourceGroupEntity> query = em.createQuery(cq);
return query.getResultList();
}
@Override
public Class<AttributeSourceGroupEntity> getEntityClass() {
return AttributeSourceGroupEntity.class;
}
}
/*******************************************************************************
* 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.entity.as;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import edu.kit.scc.webreg.entity.GlobalGroupEntity;
@Entity(name = "AttributeSourceGroupEntity")
@Table(name = "attribute_src_group")
public class AttributeSourceGroupEntity extends GlobalGroupEntity {
private static final long serialVersionUID = 1L;
@ManyToOne(targetEntity = AttributeSourceEntity.class)
private AttributeSourceEntity attributeSource;
public AttributeSourceEntity getAttributeSource() {
return attributeSource;
}
public void setAttributeSource(AttributeSourceEntity attributeSource) {
this.attributeSource = attributeSource;
}
}
package edu.kit.scc.webreg.as;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import edu.kit.scc.webreg.audit.AttributeSourceAuditor;
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.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.AttributeSourceGroupEntity;
import edu.kit.scc.webreg.exc.PropertyReaderException;
import edu.kit.scc.webreg.exc.UserUpdateException;
import edu.kit.scc.webreg.service.reg.ldap.PropertyReader;
......@@ -21,57 +30,156 @@ public abstract class AbstractAttributeSourceWorkflow implements AttributeSource
protected PropertyReader prop;
protected String groupKey;
protected String groupSeparator;
public void init(ASUserAttrEntity asUserAttr)
throws UserUpdateException {
try {
prop = new PropertyReader(asUserAttr.getAttributeSource().getAsProps());
if (prop.readPropOrNull("group_key") != null)
groupKey = prop.readPropOrNull("group_key");
else
groupKey = "group";
if (prop.readPropOrNull("group_separator") != null)
groupSeparator = prop.readPropOrNull("group_separator");
else
groupSeparator = ";";
} catch (PropertyReaderException e) {
throw new UserUpdateException(e);
}
}
protected Boolean createOrUpdateValue(String key, Object o, ASUserAttrEntity asUserAttr, ASUserAttrValueDao asValueDao, AttributeSourceAuditor auditor) {
protected Boolean createOrUpdateValues(Map<String, Object> valueMap, ASUserAttrEntity asUserAttr,
ASUserAttrValueDao asValueDao, AttributeSourceGroupDao attributeSourceGroupDao, AttributeSourceAuditor auditor) {
Boolean changed = false;
if (o == null || key == null) {
logger.warn("Null keys or values cannot be updated");
return changed;
List<ASUserAttrValueEntity> valueList = asValueDao.findValues(asUserAttr);
logger.debug("{} values already present for {}", valueList.size(), asUserAttr.getAttributeSource().getName());
Map<String, ASUserAttrValueEntity> keysToUpdate = new HashMap<String, ASUserAttrValueEntity>();
Map<String, ASUserAttrValueEntity> keysToDelete = new HashMap<String, ASUserAttrValueEntity>();
for (ASUserAttrValueEntity value : valueList) {
if (valueMap.containsKey(value.getKey())) {
keysToUpdate.put(value.getKey(), value);
}
else {
keysToDelete.put(value.getKey(), value);
}
}
Set<String> keysToAdd = new HashSet<String>(valueMap.keySet());
keysToAdd.removeAll(keysToUpdate.keySet());
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);
}
for (Entry<String, ASUserAttrValueEntity> entry : keysToDelete.entrySet()) {
changed |= deleteValue(entry.getKey(), entry.getValue(), asUserAttr, asValueDao, attributeSourceGroupDao, auditor);
}
for (Entry<String, ASUserAttrValueEntity> entry : keysToUpdate.entrySet()) {
changed |= updateValue(entry.getKey(), entry.getValue(), valueMap.get(entry.getKey()), asUserAttr, asValueDao, attributeSourceGroupDao, auditor);
}
return changed;
}
private Boolean createValue(String key, Object o, ASUserAttrEntity asUserAttr, ASUserAttrValueDao asValueDao,
AttributeSourceGroupDao attributeSourceGroupDao, AttributeSourceAuditor auditor) {
if (o == null) {
logger.warn("Cannot process null value");
return false;
}
logger.debug("Creating value for key {} of type {}", key, o.getClass().getName());
if (o instanceof String) {
String value = (String) o;
ASUserAttrValueEntity asValue = asValueDao.findValueByKey(asUserAttr, key);
ASUserAttrValueStringEntity asValue = asValueDao.createNewString();
asValue.setKey(key);
asValue.setAsUserAttr(asUserAttr);
asValue.setValueString((String) o);
auditor.logAction("as-workflow", "CREATE VALUE (String)", key, "", AuditStatus.SUCCESS);
asValue = (ASUserAttrValueStringEntity) asValueDao.persist(asValue);
if (asValue == null) {
asValue = asValueDao.createNewString();
asValue.setKey(key);
asValue.setAsUserAttr(asUserAttr);
auditor.logAction("as-workflow", "CREATE VALUE (String)", key, "", AuditStatus.SUCCESS);
changed = true;
}
else if (! (asValue instanceof ASUserAttrValueStringEntity)) {
asValueDao.delete(asValue);
auditor.logAction("as-workflow", "DELETE VALUE", key, "", AuditStatus.SUCCESS);
asValue = asValueDao.createNewString();
asValue.setKey(key);
asValue.setAsUserAttr(asUserAttr);
auditor.logAction("as-workflow", "CREATE VALUE (String)", key, "", AuditStatus.SUCCESS);
changed = true;
if (key.equals(groupKey)) {
processGroups(asValue, asUserAttr, asValueDao, attributeSourceGroupDao, auditor);
}
}
else {
logger.warn("Cannot process value of type {}", o.getClass());
return false;
}
return true;
}
private Boolean deleteValue(String key, ASUserAttrValueEntity asValue, ASUserAttrEntity asUserAttr, ASUserAttrValueDao asValueDao,
AttributeSourceGroupDao attributeSourceGroupDao, AttributeSourceAuditor auditor) {
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);
}
return true;
}
private Boolean updateValue(String key, ASUserAttrValueEntity asValue, Object o, ASUserAttrEntity asUserAttr, ASUserAttrValueDao asValueDao,
AttributeSourceGroupDao attributeSourceGroupDao, AttributeSourceAuditor auditor) {
Boolean changed = false;
logger.debug("Updating value for key {}", key);
if (o instanceof String && asValue instanceof ASUserAttrValueStringEntity) {
String s = (String) o;
ASUserAttrValueStringEntity asStringValue = (ASUserAttrValueStringEntity) asValue;
if (! value.equals(asStringValue.getValueString())) {
auditor.logAction("as-workflow", "UPDATE VALUE (String)", key, asStringValue.getValueString() + " -> " + value, AuditStatus.SUCCESS);
asStringValue.setValueString(value);
if (! s.equals(asStringValue.getValueString())) {
asStringValue.setValueString(s);
auditor.logAction("as-workflow", "UPDATE VALUE (String)", key, asStringValue.getValueString() + " -> " + s, AuditStatus.SUCCESS);
logger.debug("Updating value for key {}: {} -> {}", key, asStringValue.getValueString(), s);
changed = true;
}
asValueDao.persist(asValue);
if (key.equals(groupKey)) {
changed |= processGroups(asStringValue, asUserAttr, asValueDao, attributeSourceGroupDao, auditor);
}
}
else {
logger.warn("Cannot process value of type {}", o.getClass());
logger.warn("Value change for key {} from {} to ASUserAttrValueStringEntity not supported yet", key, o.getClass());
}
return changed;
}
private Boolean processGroups(ASUserAttrValueStringEntity asValue, ASUserAttrEntity asUserAttr, ASUserAttrValueDao asValueDao,
AttributeSourceGroupDao attributeSourceGroupDao, AttributeSourceAuditor auditor) {
Boolean changed = false;
if (asValue == null || asValue.getValueString() == null || asValue.getValueString().equals("")) {
//delete all groups for this user
changed = true;
}
else {
String[] groupsString = asValue.getValueString().split(groupSeparator);
List<AttributeSourceGroupEntity> oldGroupList = attributeSourceGroupDao.findByUserAndAS(asUserAttr.getUser(), asUserAttr.getAttributeSource());
}
return changed;
}
}
......@@ -4,12 +4,14 @@ import java.io.Serializable;
import edu.kit.scc.webreg.audit.AttributeSourceAuditor;
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, AttributeSourceAuditor auditor)
ASUserAttrValueDao asValueDao, AttributeSourceGroupDao attributeSourceGroupDao,
AttributeSourceAuditor auditor)
throws UserUpdateException;
}
......@@ -4,7 +4,6 @@ import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
......@@ -27,6 +26,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import edu.kit.scc.webreg.audit.AttributeSourceAuditor;
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;
......@@ -40,7 +40,8 @@ public class HttpUrlSingleAttributeSource extends
private static final long serialVersionUID = 1L;
@Override
public Boolean pollUserAttributes(ASUserAttrEntity asUserAttr, ASUserAttrValueDao asValueDao, AttributeSourceAuditor auditor) throws UserUpdateException {
public Boolean pollUserAttributes(ASUserAttrEntity asUserAttr, ASUserAttrValueDao asValueDao,
AttributeSourceGroupDao attributeSourceGroupDao, AttributeSourceAuditor auditor) throws UserUpdateException {
Boolean changed = false;
......@@ -118,12 +119,8 @@ 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);
for (Entry<String, Object> entry : map.entrySet()) {
logger.debug("Processing entry {}, value {}", entry.getKey(), entry.getValue());
changed |= createOrUpdateValue(entry.getKey(), entry.getValue(), asUserAttr, asValueDao, auditor);
}
asUserAttr.setQueryStatus(AttributeSourceQueryStatus.SUCCESS);
} catch (JsonMappingException e) {
......
......@@ -16,6 +16,7 @@ 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;
......@@ -37,6 +38,9 @@ public class AttributeSourceQueryServiceImpl implements AttributeSourceQueryServ
@Inject
private AttributeSourceDao attributeSourceDao;
@Inject
private AttributeSourceGroupDao attributeSourceGroupDao;
@Inject
private ASUserAttrDao asUserAttrDao;
......@@ -96,7 +100,7 @@ public class AttributeSourceQueryServiceImpl implements AttributeSourceQueryServ
auditor.setDetail("Updating attributes for user " + user.getEppn());
auditor.setAsUserAttr(asUserAttr);
changed = workflow.pollUserAttributes(asUserAttr, asValueDao, auditor);
changed = workflow.pollUserAttributes(asUserAttr, asValueDao, attributeSourceGroupDao, auditor);
if (AttributeSourceQueryStatus.SUCCESS.equals(asUserAttr.getQueryStatus())) {
asUserAttr.setLastSuccessfulQuery(new Date());
......
......@@ -24,21 +24,18 @@ public class UserController {
@Path(value = "/update-async/{eppn}")
@Produces({"application/json"})
@GET
public Map<String, String> updateUserAsync(@PathParam("eppn") String eppn, @Context HttpServletRequest request)
public void updateUserAsync(@PathParam("eppn") String eppn, @Context HttpServletRequest request)
throws IOException, RestInterfaceException, ServletException {
return userUpdateService.updateUser(eppn, request.getLocalName());
userUpdateService.updateUserAsync(eppn, request.getLocalName());
}
@Path(value = "/update/{eppn}")
@Produces({"application/json"})
@GET
public String updateUser(@PathParam("eppn") String eppn, @Context HttpServletRequest request)
public Map<String, String> updateUser(@PathParam("eppn") String eppn, @Context HttpServletRequest request)
throws IOException, RestInterfaceException, ServletException {
userUpdateService.updateUser(eppn, request.getLocalName());
return "";
}
return userUpdateService.updateUser(eppn, request.getLocalName());
}
}
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