use of org.olat.resource.accesscontrol.model.AccessMethod in project OpenOLAT by OpenOLAT.
the class ACMethodDAO method enableMethod.
public void enableMethod(Class<? extends AccessMethod> type, boolean enable) {
StringBuilder sb = new StringBuilder();
sb.append("select method from ").append(type.getName()).append(" method");
List<AccessMethod> methods = dbInstance.getCurrentEntityManager().createQuery(sb.toString(), AccessMethod.class).getResultList();
if (methods.isEmpty() && enable) {
try {
AccessMethod method = type.newInstance();
Date now = new Date();
((AbstractAccessMethod) method).setCreationDate(now);
((AbstractAccessMethod) method).setLastModified(now);
dbInstance.getCurrentEntityManager().persist(method);
} catch (InstantiationException e) {
log.error("Failed to instantiate an access method", e);
} catch (IllegalAccessException e) {
log.error("Failed to instantiate an access method", e);
}
} 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 OpenOLAT.
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 OpenOLAT.
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 OpenOLAT.
the class ACOfferDAO method findOfferByResource.
public List<Offer> findOfferByResource(OLATResource resource, boolean valid, Date atDate) {
StringBuilder sb = new StringBuilder();
sb.append("select offer, access.method from acofferaccess access ").append(" inner join access.offer offer").append(" left join offer.resource resource").append(" where resource.key=:resourceKey").append(" and offer.valid=").append(valid);
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<Object[]> query = dbInstance.getCurrentEntityManager().createQuery(sb.toString(), Object[].class).setParameter("resourceKey", resource.getKey());
if (atDate != null) {
query.setParameter("atDate", atDate, TemporalType.TIMESTAMP);
}
List<Object[]> loadedObjects = query.getResultList();
List<Offer> offers = new ArrayList<>();
for (Object[] objects : loadedObjects) {
Offer offer = (Offer) objects[0];
AccessMethod method = (AccessMethod) objects[1];
if (method.isVisibleInGui()) {
offers.add(offer);
}
}
return offers;
}
use of org.olat.resource.accesscontrol.model.AccessMethod in project OpenOLAT by OpenOLAT.
the class AccessTransactionTest method generateDatas.
@Test
public void generateDatas() {
// pick up a method
List<AccessMethod> methods = acMethodManager.getAvailableMethodsByType(FreeAccessMethod.class);
AccessMethod method = methods.get(0);
StringBuilder sb = new StringBuilder();
sb.append("select v from repositoryentry as v ").append(" inner join fetch v.olatResource as ores").append(" inner join fetch v.statistics as statistics").append(" left join fetch v.lifecycle as lifecycle").append(" where ores.resName='CourseModule' and v.access>0");
List<RepositoryEntry> courses = dbInstance.getCurrentEntityManager().createQuery(sb.toString(), RepositoryEntry.class).getResultList();
List<Identity> loadIdentities = securityManager.getVisibleIdentitiesByPowerSearch(null, null, false, null, null, null, null, null, 0, 40000);
for (RepositoryEntry course : courses) {
try {
List<Offer> offers = acService.findOfferByResource(course.getOlatResource(), true, null);
if (offers.isEmpty()) {
OLATResource randomOres = course.getOlatResource();
Offer offer = acService.createOffer(randomOres, "Free " + course.getDisplayname());
offer.setAutoBooking(true);
OfferAccess link = acService.createOfferAccess(offer, method);
offer = acService.save(offer);
acService.saveOfferAccess(link);
dbInstance.commit();
int fromIndex = (int) (Math.random() * loadIdentities.size() - 1);
int length = (int) (Math.random() * 200);
int toIndex = Math.min(loadIdentities.size() - 1, fromIndex + length);
List<Identity> identities = loadIdentities.subList(fromIndex, toIndex);
for (Identity identity : identities) {
acService.isAccessible(course, identity, false, true);
dbInstance.commit();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
dbInstance.commitAndCloseSession();
}
}
}
Aggregations