Search in sources :

Example 51 with AccessMethod

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);
            }
        }
    }
}
Also used : ShibbolethAutoAccessMethod(org.olat.shibboleth.manager.ShibbolethAutoAccessMethod) AccessMethod(org.olat.resource.accesscontrol.model.AccessMethod) FreeAccessMethod(org.olat.resource.accesscontrol.model.FreeAccessMethod) AbstractAccessMethod(org.olat.resource.accesscontrol.model.AbstractAccessMethod) TokenAccessMethod(org.olat.resource.accesscontrol.model.TokenAccessMethod) AbstractAccessMethod(org.olat.resource.accesscontrol.model.AbstractAccessMethod) Date(java.util.Date)

Example 52 with AccessMethod

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);
            }
        }
    }
}
Also used : ShibbolethAutoAccessMethod(org.olat.shibboleth.manager.ShibbolethAutoAccessMethod) AccessMethod(org.olat.resource.accesscontrol.model.AccessMethod) FreeAccessMethod(org.olat.resource.accesscontrol.model.FreeAccessMethod) AbstractAccessMethod(org.olat.resource.accesscontrol.model.AbstractAccessMethod) TokenAccessMethod(org.olat.resource.accesscontrol.model.TokenAccessMethod) AbstractAccessMethod(org.olat.resource.accesscontrol.model.AbstractAccessMethod) FreeAccessMethod(org.olat.resource.accesscontrol.model.FreeAccessMethod) Date(java.util.Date)

Example 53 with AccessMethod

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;
}
Also used : AccessMethod(org.olat.resource.accesscontrol.model.AccessMethod) AccessMethodSecurityCallback(org.olat.resource.accesscontrol.model.AccessMethodSecurityCallback) ArrayList(java.util.ArrayList) AccessMethodHandler(org.olat.resource.accesscontrol.method.AccessMethodHandler)

Example 54 with AccessMethod

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;
}
Also used : AccessMethod(org.olat.resource.accesscontrol.model.AccessMethod) Offer(org.olat.resource.accesscontrol.Offer) ArrayList(java.util.ArrayList)

Example 55 with AccessMethod

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();
        }
    }
}
Also used : OfferAccess(org.olat.resource.accesscontrol.OfferAccess) OLATResource(org.olat.resource.OLATResource) RepositoryEntry(org.olat.repository.RepositoryEntry) AccessMethod(org.olat.resource.accesscontrol.model.AccessMethod) FreeAccessMethod(org.olat.resource.accesscontrol.model.FreeAccessMethod) Offer(org.olat.resource.accesscontrol.Offer) Identity(org.olat.core.id.Identity) Test(org.junit.Test)

Aggregations

AccessMethod (org.olat.resource.accesscontrol.model.AccessMethod)92 TokenAccessMethod (org.olat.resource.accesscontrol.model.TokenAccessMethod)62 Test (org.junit.Test)56 FreeAccessMethod (org.olat.resource.accesscontrol.model.FreeAccessMethod)54 OLATResource (org.olat.resource.OLATResource)40 Identity (org.olat.core.id.Identity)22 ShibbolethAutoAccessMethod (org.olat.shibboleth.manager.ShibbolethAutoAccessMethod)22 BusinessGroup (org.olat.group.BusinessGroup)18 PaypalAccessMethod (org.olat.resource.accesscontrol.provider.paypal.model.PaypalAccessMethod)18 ArrayList (java.util.ArrayList)16 Date (java.util.Date)12 Offer (org.olat.resource.accesscontrol.Offer)12 FormLink (org.olat.core.gui.components.form.flexible.elements.FormLink)10 OfferAccess (org.olat.resource.accesscontrol.OfferAccess)10 AccessMethodHandler (org.olat.resource.accesscontrol.method.AccessMethodHandler)10 AbstractAccessMethod (org.olat.resource.accesscontrol.model.AbstractAccessMethod)10 HashSet (java.util.HashSet)8 FormLayoutContainer (org.olat.core.gui.components.form.flexible.impl.FormLayoutContainer)8 RepositoryEntry (org.olat.repository.RepositoryEntry)8 OLATResourceable (org.olat.core.id.OLATResourceable)6