}
acl = provider.getProviderAclService().createAcl(oid);
}
- acl.insertAce(acl.getEntries().size(), permission, sid, grant);
- provider.getProviderAclService().updateAcl(acl);
+ // Need to see if there is already an entry, so we do not duplicate (e.g.,
+ // when we run our permission-roles init more than once.
+ List<AccessControlEntry> aceEntries = acl.getEntries();
+ if(aceListHasEntry(aceEntries, permission, sid, grant)) {
+ if (log.isDebugEnabled()) {
+ log.debug("addPermission: Pre-existing acl for oid=" + oid.toString()
+ + " perm=" + permission.toString()
+ + " sid=" + sid.toString()
+ + " grant=" + grant);
+ }
+
+ } else {
+ acl.insertAce(acl.getEntries().size(), permission, sid, grant);
+ provider.getProviderAclService().updateAcl(acl);
- if (log.isDebugEnabled()) {
- log.debug("addPermission: added acl for oid=" + oid.toString()
- + " perm=" + permission.toString()
- + " sid=" + sid.toString()
- + " grant=" + grant);
+ if (log.isDebugEnabled()) {
+ log.debug("addPermission: added acl for oid=" + oid.toString()
+ + " perm=" + permission.toString()
+ + " sid=" + sid.toString()
+ + " grant=" + grant);
+ }
}
}
+
+ private boolean aceListHasEntry(List<AccessControlEntry> aceEntries, Permission permission,
+ Sid sid, boolean grant) {
+ for(AccessControlEntry entry : aceEntries) {
+ if(permission.equals(entry.getPermission())
+ && sid.equals(entry.getSid())
+ && grant == entry.isGranting()) {
+ return true;
+ }
+ }
+ return false;
+ }
/**
* deletePermissions deletes given permission on given object id for given sid
import javax.persistence.EntityManagerFactory;
import org.collectionspace.services.authorization.Role;
+import org.collectionspace.services.authorization.PermissionRoleRel;
import org.collectionspace.services.authorization.perms.Permission;
import org.collectionspace.services.common.authorization_mgt.RoleStorageConstants;
import org.collectionspace.services.common.document.JaxbUtils;
}
+ static public PermissionRoleRel getPermRoleRel(EntityManager em, String permId, String roleId) {
+ PermissionRoleRel permRoleRel = null;
+
+ try {
+ permRoleRel = (PermissionRoleRel)JpaStorageUtils.getEntityByDualKeys(em,
+ PermissionRoleRel.class.getName(),
+ RoleStorageConstants.PERM_ROLE_REL_PERM_ID, permId,
+ RoleStorageConstants.PERM_ROLE_REL_ROLE_ID, roleId);
+ } catch (Throwable e) {
+ if (logger.isTraceEnabled()) {
+ logger.trace("Could not retrieve permissionRoleRel with permId =" + permId
+ +" and roleId="+roleId, e);
+ }
+ }
+
+ return permRoleRel;
+ }
+
+
static public Permission getPermission(Permission permission) {
Permission result = null;
//
boolean result = false;
try {
- String csid = (String)JaxbUtils.getValue(entity, "getCsid");
- Object existingEntity = em.find(entity.getClass(), csid);
- if (existingEntity != null) {
- result = true;
+ if(entity instanceof Role) {
+ // If find by name, exists
+ Role roleEntity = (Role)entity;
+ String roleName = roleEntity.getRoleName();
+ String tenantId = roleEntity.getTenantId();
+ if(getRoleByName(em, roleName, tenantId)!=null) {
+ result = true;
+ logger.trace("Role {} already exists in tenant {}.", roleName, tenantId);
+ } else {
+ logger.trace("Role {} does not exist in tenant {}.", roleName, tenantId);
+ }
+ } else if(entity instanceof PermissionRoleRel) {
+ // If find by name, exists
+ PermissionRoleRel permRoleEntity = (PermissionRoleRel)entity;
+ String roleId = permRoleEntity.getRoleId();
+ String permId = permRoleEntity.getPermissionId();
+ if(getPermRoleRel(em, permId, roleId)!=null) {
+ result = true;
+ logger.trace("PermRoleRel for {}, {} already exists.", permId, roleId);
+ } else {
+ logger.trace("PermRoleRel for {}, {} does not exist.", permId, roleId);
+ }
+ } else { // Default case; also best test for Permission
+ String csid = (String)JaxbUtils.getValue(entity, "getCsid");
+ Object existingEntity = em.find(entity.getClass(), csid);
+ if (existingEntity != null) {
+ result = true;
+ logger.trace("Entity with csid {} already exists.", csid);
+ } else {
+ logger.trace("Entity with csid {} does not exist.", csid);
+ }
}
} catch (Exception e) {
//NOTE: Not all entities have a CSID attribute
*/
public String store(EntityManager em, Object entity) throws Exception {
boolean entityExists = exists(em, entity);
+ /*
+ * Logging moved to exists, for better detail
if (entityExists == true) {
- logger.debug("Entity to persist already exists.");
+ logger.trace("Entity to persist already exists.");
}
+ */
if (JaxbUtils.getValue(entity, "getCreatedAt") == null) {
JaxbUtils.setValue(entity, "setCreatedAtItem", Date.class, new Date());
}
if (entityExists == true) {
//em.merge(entity); FIXME: Leave commented out until we address CSPACE-5031
+ // PLS: Question: why merge? what might be new to change, and is this really a good idea?
+ // Shouldn't we define them once and leave them alone?
} else {
em.persist(entity);
}
return result;
}
+ public static Object getEntityByDualKeys(EntityManager em, String entityName,
+ String key1, String value1,
+ String key2, String value2) {
+ return getEntityByDualKeys(em, entityName, key1, value1, key2, value2, null);
+ }
+
+ public static Object getEntityByDualKeys(EntityManager em, String entityName,
+ String key1, String value1,
+ String key2, String value2,
+ String tenantId) {
+ Object result = null;
+
+ if (entityName == null) {
+ throw new IllegalArgumentException("entityName is required");
+ }
+ if (key1 == null || key2 == null) {
+ throw new IllegalArgumentException("key names are required");
+ }
+
+ StringBuilder queryStrBldr = new StringBuilder("SELECT a FROM ");
+ queryStrBldr.append(entityName);
+ queryStrBldr.append(" a");
+ queryStrBldr.append(" WHERE " + key1 + " = :" + key1);
+ queryStrBldr.append(" AND " + key2 + " = :" + key2);
+ boolean csAdmin = SecurityUtils.isCSpaceAdmin();
+ if (!csAdmin && tenantId != null) {
+ queryStrBldr.append(" AND tenantId = :tenantId");
+ }
+ String queryStr = queryStrBldr.toString(); //for debugging
+ Query q = em.createQuery(queryStr);
+ q.setParameter(key1, value1);
+ q.setParameter(key2, value2);
+ if (!csAdmin) {
+ q.setParameter("tenantId", tenantId);
+ }
+ result = q.getSingleResult();
+
+ return result;
+ }
+
public static Object getEnityByKey(String entityName, String key, String value,
String tenantId) {
EntityManagerFactory emf = null;