use of org.olat.resource.accesscontrol.model.AccessMethod in project openolat by klemens.
the class ACFrontendManager method getAvailableMethods.
@Override
public List<AccessMethod> getAvailableMethods(Identity identity, Roles roles) {
List<AccessMethod> methods = methodManager.getAvailableMethods();
List<AccessMethod> allowedMethods = new ArrayList<>();
for (AccessMethod method : methods) {
AccessMethodHandler handler = accessModule.getAccessMethodHandler(method.getType());
AccessMethodSecurityCallback secCallback = handler.getSecurityCallback(identity, roles);
if (secCallback.canUse()) {
allowedMethods.add(method);
}
}
return methods;
}
use of org.olat.resource.accesscontrol.model.AccessMethod in project openolat by klemens.
the class ACMethodDAO method activateFreeMethod.
protected void activateFreeMethod(boolean enable) {
StringBuilder sb = new StringBuilder();
sb.append("select method from ").append(AbstractAccessMethod.class.getName()).append(" method where type(method) =").append(FreeAccessMethod.class.getName());
TypedQuery<AccessMethod> query = dbInstance.getCurrentEntityManager().createQuery(sb.toString(), AccessMethod.class);
List<AccessMethod> methods = query.getResultList();
if (methods.isEmpty() && enable) {
FreeAccessMethod method = new FreeAccessMethod();
method.setCreationDate(new Date());
method.setLastModified(method.getCreationDate());
dbInstance.getCurrentEntityManager().persist(method);
} else {
for (AccessMethod method : methods) {
if (method.isEnabled() != enable) {
((AbstractAccessMethod) method).setEnabled(enable);
((AbstractAccessMethod) method).setLastModified(new Date());
dbInstance.getCurrentEntityManager().merge(method);
}
}
}
}
use of org.olat.resource.accesscontrol.model.AccessMethod in project openolat by klemens.
the class ACMethodDAO method activateTokenMethod.
/**
* Activate the token method if not already configured.
*/
protected void activateTokenMethod(boolean enable) {
StringBuilder sb = new StringBuilder();
sb.append("select method from actokenmethod method");
List<AccessMethod> methods = dbInstance.getCurrentEntityManager().createQuery(sb.toString(), AccessMethod.class).getResultList();
if (methods.isEmpty() && enable) {
TokenAccessMethod method = new TokenAccessMethod();
method.setCreationDate(new Date());
method.setLastModified(method.getCreationDate());
dbInstance.getCurrentEntityManager().persist(method);
} else {
for (AccessMethod method : methods) {
if (method.isEnabled() != enable) {
((AbstractAccessMethod) method).setEnabled(enable);
((AbstractAccessMethod) method).setLastModified(new Date());
dbInstance.getCurrentEntityManager().merge(method);
}
}
}
}
use of org.olat.resource.accesscontrol.model.AccessMethod in project openolat by klemens.
the class ACMethodDAO method isValidMethodAvailable.
public boolean isValidMethodAvailable(OLATResource resource, Date atDate) {
StringBuilder sb = new StringBuilder();
sb.append("select access.method from acofferaccess access ").append(" inner join access.offer offer").append(" inner join offer.resource oResource").append(" where access.valid=true").append(" and offer.valid=true").append(" and oResource.key=:resourceKey");
if (atDate != null) {
sb.append(" and (offer.validFrom is null or offer.validFrom<=:atDate)").append(" and (offer.validTo is null or offer.validTo>=:atDate)");
}
TypedQuery<AccessMethod> query = dbInstance.getCurrentEntityManager().createQuery(sb.toString(), AccessMethod.class);
query.setParameter("resourceKey", resource.getKey());
if (atDate != null) {
query.setParameter("atDate", atDate, TemporalType.TIMESTAMP);
}
List<AccessMethod> methods = query.getResultList();
List<AccessMethod> guiMethods = new ArrayList<>();
for (AccessMethod method : methods) {
if (method.isVisibleInGui()) {
guiMethods.add(method);
}
}
return !guiMethods.isEmpty();
}
use of org.olat.resource.accesscontrol.model.AccessMethod in project openolat by klemens.
the class ACFrontendManagerTest method testPaiedAccesToBusinessGroupWithWaitingList_enoughPlaceButAdmin.
/**
* Test paypal scenario where a user begin the process to pay an access
* to a group while an administrator is filling the group,
*/
@Test
public void testPaiedAccesToBusinessGroupWithWaitingList_enoughPlaceButAdmin() {
// enable paypal
boolean enabled = acModule.isPaypalEnabled();
if (!enabled) {
acModule.setPaypalEnabled(true);
}
// create a group with a free offer
Identity id1 = JunitTestHelper.createAndPersistIdentityAsUser("pay-1-" + UUID.randomUUID().toString());
Identity id2 = JunitTestHelper.createAndPersistIdentityAsUser("agp-" + UUID.randomUUID().toString());
Identity id3 = JunitTestHelper.createAndPersistIdentityAsUser("agp-" + UUID.randomUUID().toString());
BusinessGroup group = businessGroupService.createBusinessGroup(null, "Free group", "But you must wait", new Integer(0), new Integer(2), true, false, null);
Offer offer = acService.createOffer(group.getResource(), "Free group (waiting)");
offer = acService.save(offer);
List<AccessMethod> methods = acMethodManager.getAvailableMethodsByType(PaypalAccessMethod.class);
Assert.assertFalse(methods.isEmpty());
OfferAccess offerAccess = acService.createOfferAccess(offer, methods.get(0));
Assert.assertNotNull(offerAccess);
dbInstance.commitAndCloseSession();
// id1 start payment process
boolean reserved = acService.reserveAccessToResource(id1, offerAccess);
Assert.assertTrue(reserved);
dbInstance.commitAndCloseSession();
// admin fill the group
businessGroupRelationDao.addRole(id2, group, GroupRoles.participant.name());
businessGroupRelationDao.addRole(id3, group, GroupRoles.participant.name());
dbInstance.commitAndCloseSession();
// id1 finish the process
AccessResult result = acService.accessResource(id1, offerAccess, null);
Assert.assertNotNull(result);
Assert.assertTrue(result.isAccessible());
dbInstance.commitAndCloseSession();
// is id a waiting?
boolean participant = businessGroupRelationDao.hasRole(id1, group, GroupRoles.participant.name());
Assert.assertTrue(participant);
boolean waiting = businessGroupRelationDao.hasRole(id1, group, GroupRoles.waiting.name());
Assert.assertFalse(waiting);
if (!enabled) {
acModule.setPaypalEnabled(false);
}
}
Aggregations