public void loadPasswordCredentialUser(final PasswordCredential credential) { if (credential.getUser() == null) { getPersistenceBrokerTemplate().execute( new PersistenceBrokerCallback() { public Object doInPersistenceBroker(PersistenceBroker pb) throws PersistenceBrokerException { pb.retrieveReference(credential, "user"); return null; } } ); } }
public JetspeedPrincipal getPrincipal(Long id) { // check cache Object principal = jspmCache.getPrincipal(id); if (principal != null) { return ((principal != JSPMCache.CACHE_NULL) ? (JetspeedPrincipal) principal : null); } // perform query try { principal = getPersistenceBrokerTemplate().getObjectById(PersistentJetspeedPrincipal.class, id); } catch (ObjectRetrievalFailureException orfe) { } // put result in cache jspmCache.putPrincipal(id, ((principal != null) ? principal : JSPMCache.CACHE_NULL)); // return result return (JetspeedPrincipal) principal; }
public SecurityDomain getDomain(Long domainId) { // check cache Object domain = jspmCache.getDomain(domainId); if (domain != null) { return ((domain != JSPMCache.CACHE_NULL) ? (SecurityDomain) domain : null); } // perform query try { domain = getPersistenceBrokerTemplate().getObjectById(SecurityDomainImpl.class, domainId); } catch (ObjectRetrievalFailureException orfe) { } // put result in cache jspmCache.putDomain(domainId, ((domain != null) ? domain : JSPMCache.CACHE_NULL)); // return result return (SecurityDomain) domain; }
@SuppressWarnings("unchecked") public Collection<SecurityDomain> getAllDomains() { // check cache String cacheKey = "getDomains:[]"; List<SecurityDomain> domains = (List<SecurityDomain>) jspmCache.getDomainQuery(cacheKey); if (domains != null) { return new ArrayList<SecurityDomain>(domains); } // perform query QueryByCriteria query = QueryFactory.newQuery(SecurityDomainImpl.class, new Criteria()); query.addOrderByAscending("name"); domains = (List<SecurityDomain>)getPersistenceBrokerTemplate().execute(new ManagedListByQueryCallback(query)); // put result in cache jspmCache.putDomainQuery(cacheKey, JSPMCache.ANY_ID, new ArrayList<SecurityDomain>(domains)); // return result return domains; }
@SuppressWarnings("unchecked") public List<PersistentJetspeedPermission> getPermissions() { // check cache String cacheKey = "getPermissions:[]"; List<PersistentJetspeedPermission> permissions = (List<PersistentJetspeedPermission>) jspmCache.getPermissionQuery(cacheKey); if (permissions != null) { return new ArrayList<PersistentJetspeedPermission>(permissions); } // perform query QueryByCriteria query = QueryFactory.newQuery(PersistentJetspeedPermissionImpl.class, new Criteria()); query.addOrderByAscending("type"); query.addOrderByAscending("name"); permissions = (List<PersistentJetspeedPermission>)getPersistenceBrokerTemplate().execute(new ManagedListByQueryCallback(query)); // put result in cache jspmCache.putPermissionQuery(cacheKey, null, null, JSPMCache.ANY_ID, null, new ArrayList<PersistentJetspeedPermission>(permissions)); // return result return permissions; }
@SuppressWarnings("unchecked") public Collection<SecurityDomain> getDomainsOwnedBy(Long ownerDomainId) { Criteria criteria = new Criteria(); criteria.addEqualTo("ownerDomainId", ownerDomainId); // check cache String cacheKey = "getDomainsOwnedBy:"+criteria; List<SecurityDomain> domains = (List<SecurityDomain>) jspmCache.getDomainQuery(cacheKey); if (domains != null) { return new ArrayList<SecurityDomain>(domains); } // perform query QueryByCriteria query = QueryFactory.newQuery(SecurityDomainImpl.class, criteria); query.addOrderByAscending("name"); domains = (List<SecurityDomain>)getPersistenceBrokerTemplate().execute(new ManagedListByQueryCallback(query)); // put result in cache jspmCache.putDomainQuery(cacheKey, JSPMCache.ANY_ID, new ArrayList<SecurityDomain>(domains)); // return result return domains; }
@SuppressWarnings("unchecked") public List<PersistentJetspeedPermission> getPermissions(String type, String nameFilter) { Criteria criteria = new Criteria(); criteria.addEqualTo("type", type); if (nameFilter != null && nameFilter.length() > 0) { criteria.addLike("name", nameFilter+"%"); } // check cache String cacheKey = "getPermissions:"+criteria; List<PersistentJetspeedPermission> permissions = (List<PersistentJetspeedPermission>) jspmCache.getPermissionQuery(cacheKey); if (permissions != null) { return new ArrayList<PersistentJetspeedPermission>(permissions); } // perform query QueryByCriteria query = QueryFactory.newQuery(PersistentJetspeedPermissionImpl.class, criteria); query.addOrderByAscending("name"); permissions = (List<PersistentJetspeedPermission>)getPersistenceBrokerTemplate().execute(new ManagedListByQueryCallback(query)); // put result in cache jspmCache.putPermissionQuery(cacheKey, null, null, JSPMCache.ANY_ID, null, new ArrayList<PersistentJetspeedPermission>(permissions)); // return result return permissions; }
@SuppressWarnings("unchecked") public List<JetspeedPrincipal> getPrincipals(String nameFilter, JetspeedPrincipalType type, Long securityDomain) { Criteria criteria = new Criteria(); if (nameFilter != null && nameFilter.length() > 0) { criteria.addLike("name", nameFilter+"%"); } criteria.addEqualTo("type", type.getName()); criteria.addEqualTo("domainId", securityDomain); // check cache String cacheKey = "getPrincipals:"+criteria; List<JetspeedPrincipal> principals = (List<JetspeedPrincipal>)jspmCache.getPrincipalQuery(cacheKey); if (principals != null) { return new ArrayList<JetspeedPrincipal>(principals); } // perform query Query query = QueryFactory.newQuery(PersistentJetspeedPrincipal.class,criteria); principals = (List<JetspeedPrincipal>) getPersistenceBrokerTemplate().execute(new ManagedListByQueryCallback(query)); // put result in cache jspmCache.putPrincipalQuery(cacheKey, JSPMCache.ANY_ID, null, securityDomain, new ArrayList<JetspeedPrincipal>(principals)); // return result return principals; }
public void removeAssociation(JetspeedPrincipal from, JetspeedPrincipal to, String associationName) throws SecurityException { Criteria criteria = new Criteria(); criteria.addEqualTo("fromPrincipalId", from.getId()); criteria.addEqualTo("toPrincipalId", to.getId()); criteria.addEqualTo("associationName", associationName); Query query = QueryFactory.newQuery(JetspeedPrincipalAssociation.class,criteria); if (getPersistenceBrokerTemplate().getCount(query) != 0) { try { getPersistenceBrokerTemplate().delete(new JetspeedPrincipalAssociation(from, to, associationName)); // evict principals from cache to notify jspmCache.evictPrincipal(from.getId()); jspmCache.evictPrincipal(to.getId()); } catch (Exception pbe) { KeyedMessage msg = SecurityException.UNEXPECTED.create("JetspeedSecurityPersistenceManager", "removeAssociation", pbe.getMessage()); logger.error(msg, pbe); throw new SecurityException(msg, pbe); } } }
@SuppressWarnings("unchecked") public List<PersistentJetspeedPermission> getPermissions(PersistentJetspeedPrincipal principal) { Criteria criteria = new Criteria(); criteria.addEqualTo("principals.principalId", principal.getId()); // check cache String cacheKey = "getPermissions:"+criteria; List<PersistentJetspeedPermission> permissions = (List<PersistentJetspeedPermission>) jspmCache.getPermissionQuery(cacheKey); if (permissions != null) { return new ArrayList<PersistentJetspeedPermission>(permissions); } // perform query QueryByCriteria query = QueryFactory.newQuery(PersistentJetspeedPermissionImpl.class, criteria); query.addOrderByAscending("type"); query.addOrderByAscending("name"); permissions = (List<PersistentJetspeedPermission>) getPersistenceBrokerTemplate().execute(new ManagedListByQueryCallback(query)); // put result in cache jspmCache.putPermissionQuery(cacheKey, principal.getId(), null, JSPMCache.ANY_ID, null, new ArrayList<PersistentJetspeedPermission>(permissions)); // return result return permissions; }
public void addDomain(SecurityDomain domain) throws SecurityException { if (domainExists(domain)) { throw new SecurityException(SecurityException.SECURITY_DOMAIN_EXISTS.create(domain.getName())); } try { getPersistenceBrokerTemplate().store(domain); // evict from and put in cache to notify jspmCache.evictDomain(domain.getDomainId()); jspmCache.putDomain(domain.getDomainId(), domain); } catch (Exception pbe) { KeyedMessage msg = SecurityException.UNEXPECTED.create("JetspeedSecurityPersistenceManager", "addDomain", pbe.getMessage()); logger.error(msg, pbe); throw new SecurityException(msg, pbe); } }
@SuppressWarnings("unchecked") public List<JetspeedPrincipal> getPrincipalsByAttribute(String attributeName, String attributeValue, JetspeedPrincipalType type, Long securityDomain) { Criteria criteria = new Criteria(); criteria.addEqualTo("attributes.name", attributeName); criteria.addEqualTo("attributes.value", attributeValue); criteria.addEqualTo("type", type.getName()); criteria.addEqualTo("domainId", securityDomain); // check cache String cacheKey = "getPrincipalsByAttribute:"+criteria; List<JetspeedPrincipal> principals = (List<JetspeedPrincipal>)jspmCache.getPrincipalQuery(cacheKey); if (principals != null) { return new ArrayList<JetspeedPrincipal>(principals); } // perform query Query query = QueryFactory.newQuery(PersistentJetspeedPrincipal.class,criteria); principals = (List<JetspeedPrincipal>) getPersistenceBrokerTemplate().execute(new ManagedListByQueryCallback(query)); // put result in cache jspmCache.putPrincipalQuery(cacheKey, JSPMCache.ANY_ID, null, securityDomain, new ArrayList<JetspeedPrincipal>(principals)); // return result return principals; }
public void addPermission(PersistentJetspeedPermission permission) throws SecurityException { if (permission.getId() != null || permissionExists(permission)) { throw new SecurityException(SecurityException.PERMISSION_ALREADY_EXISTS.create(permission.getName())); } try { getPersistenceBrokerTemplate().store(permission); // evict from and put in cache to notify jspmCache.evictPermission(permission.getId()); jspmCache.putPermission(permission.getId(), permission); } catch (Exception pbe) { KeyedMessage msg = SecurityException.UNEXPECTED.create("JetspeedSecurityPersistenceManager", "addPermission", pbe.getMessage()); logger.error(msg, pbe); throw new SecurityException(msg, pbe); } }
public void updatePrincipal(JetspeedPrincipal principal) throws SecurityException { if (!principalExists(principal)) { throw new SecurityException(SecurityException.PRINCIPAL_DOES_NOT_EXIST.createScoped(principal.getType().getName(), principal.getName())); } try { getPersistenceBrokerTemplate().store(principal); // evict from and put in cache to notify jspmCache.evictPrincipal(principal.getId()); jspmCache.putPrincipal(principal.getId(), principal); } catch (Exception pbe) { if (pbe instanceof DataIntegrityViolationException) { logger.error(pbe.getMessage(), pbe); throw new SecurityException(SecurityException.PRINCIPAL_UPDATE_FAILURE.createScoped(principal.getType().getName(), principal.getName())); } KeyedMessage msg = SecurityException.UNEXPECTED.create("JetspeedSecurityPersistenceManager", "removePrincipal", pbe.getMessage()); logger.error(msg, pbe); throw new SecurityException(msg, pbe); } }
@SuppressWarnings("unchecked") public List<JetspeedPrincipal> getAssociatedFrom(Long principalFromId, JetspeedPrincipalType from, JetspeedPrincipalType to, String associationName, Long fromSecurityDomain, Long toSecurityDomain) { Criteria criteria = new Criteria(); criteria.addEqualTo("associationsTo.associationName", associationName); criteria.addEqualTo("associationsTo.from.id", principalFromId); criteria.addEqualTo("associationsTo.from.type", from.getName()); criteria.addEqualTo("type", to.getName()); criteria.addEqualTo("associationsTo.from.domainId", fromSecurityDomain); criteria.addEqualTo("domainId", toSecurityDomain); // check cache String cacheKey = "getAssociatedFrom:"+criteria; List<JetspeedPrincipal> associatedFrom = (List<JetspeedPrincipal>)jspmCache.getAssociationQuery(cacheKey); if (associatedFrom != null) { return new ArrayList<JetspeedPrincipal>(associatedFrom); } // perform query Query query = QueryFactory.newQuery(PersistentJetspeedPrincipal.class, criteria); associatedFrom = (List<JetspeedPrincipal>) getPersistenceBrokerTemplate().execute(new ManagedListByQueryCallback(query)); // put result in cache jspmCache.putAssociationQuery(cacheKey, principalFromId, extractPrincipalIds(associatedFrom), fromSecurityDomain, toSecurityDomain, new ArrayList<JetspeedPrincipal>(associatedFrom)); // return result return associatedFrom; }
@SuppressWarnings("unchecked") public List<JetspeedPrincipal> getAssociatedTo(Long principalToId, JetspeedPrincipalType from, JetspeedPrincipalType to, String associationName, Long fromSecurityDomain, Long toSecurityDomain) { Criteria criteria = new Criteria(); criteria.addEqualTo("associationsFrom.associationName", associationName); criteria.addEqualTo("associationsFrom.to.id", principalToId); criteria.addEqualTo("type", from.getName()); criteria.addEqualTo("associationsFrom.to.type", to.getName()); criteria.addEqualTo("associationsFrom.to.domainId", toSecurityDomain); criteria.addEqualTo("domainId", fromSecurityDomain); // check cache String cacheKey = "getAssociatedTo:"+criteria; List<JetspeedPrincipal> associatedTo = (List<JetspeedPrincipal>)jspmCache.getAssociationQuery(cacheKey); if (associatedTo != null) { return new ArrayList<JetspeedPrincipal>(associatedTo); } // perform query Query query = QueryFactory.newQuery(PersistentJetspeedPrincipal.class, criteria); associatedTo = (List<JetspeedPrincipal>) getPersistenceBrokerTemplate().execute(new ManagedListByQueryCallback(query)); // put result in cache jspmCache.putAssociationQuery(cacheKey, principalToId, extractPrincipalIds(associatedTo), fromSecurityDomain, toSecurityDomain, new ArrayList<JetspeedPrincipal>(associatedTo)); // return result return associatedTo; }
public void storePasswordCredential(PasswordCredential credential) throws SecurityException { if (credential.getUser() == null) { loadPasswordCredentialUser(credential); } if (credential.isNewPasswordSet()) { if (credential.getNewPassword() != null) { credential.setPassword(credential.getNewPassword(), false); } } getPersistenceBrokerTemplate().store(credential); // evict user principal from cache to notify jspmCache.evictPrincipal(credential.getUser().getId()); }
public void addPrincipal(JetspeedPrincipal principal, Set<JetspeedPrincipalAssociationReference> associations) throws SecurityException { if (principal.getDomainId() == null && principal instanceof TransientJetspeedPrincipal) { ((TransientJetspeedPrincipal)principal).setDomainId(getDefaultSecurityDomainId()); } if (principalExists(principal)) { throw new SecurityException(SecurityException.PRINCIPAL_ALREADY_EXISTS.createScoped(principal.getType().getName(), principal.getName())); } try { getPersistenceBrokerTemplate().store(principal); // evict from and put in cache to notify jspmCache.evictPrincipal(principal.getId()); jspmCache.putPrincipal(principal.getId(), principal); } catch (Exception pbe) { KeyedMessage msg = SecurityException.UNEXPECTED.create("JetspeedSecurityPersistenceManager", "addPrincipal", pbe.getMessage()); logger.error(msg, pbe); throw new SecurityException(msg, pbe); } // Note: the (optional) required associations are expected to be stored by the calling JetspeedPrincipalManager }
public boolean permissionExists(JetspeedPermission permission) { Criteria criteria = new Criteria(); criteria.addEqualTo("type", permission.getType()); criteria.addEqualTo("name", permission.getName()); criteria.addEqualTo("actions", permission.getActions()); // check cache String cacheKey = "permissionExists:"+criteria; Boolean permissionExists = (Boolean)jspmCache.getPermissionQuery(cacheKey); if (permissionExists != null) { return permissionExists; } // perform query Query query = QueryFactory.newQuery(PersistentJetspeedPermissionImpl.class, criteria); permissionExists = (getPersistenceBrokerTemplate().getCount(query) == 1); // put result in cache jspmCache.putPermissionQuery(cacheKey, null, null, JSPMCache.ANY_ID, null, permissionExists); // return result return permissionExists; }
public void grantPermission(PersistentJetspeedPermission permission, JetspeedPrincipal principal) throws SecurityException { if (permission.getId() == null) { Criteria criteria = new Criteria(); criteria.addEqualTo("type", permission.getType()); criteria.addEqualTo("name", permission.getName()); Query query = QueryFactory.newQuery(PersistentJetspeedPermissionImpl.class, criteria); PersistentJetspeedPermission p = (PersistentJetspeedPermission)getPersistenceBrokerTemplate().getObjectByQuery(query); if (p == null) { throw new SecurityException(SecurityException.PERMISSION_DOES_NOT_EXIST.create(permission.getName())); } permission = p; } grantPermission(permission, principal, true); }