Search in sources :

Example 1 with Grant

use of org.olat.basesecurity.Grant in project OpenOLAT by OpenOLAT.

the class GroupDAO method removeGrants.

public void removeGrants(Group group, String role, OLATResource resource) {
    StringBuilder sb = new StringBuilder();
    sb.append("select grant from bgrant as grant").append(" inner join fetch grant.group as baseGroup").append(" inner join fetch grant.resource as res").append(" where baseGroup=:group and res.key=:resourceKey and grant.role=:role");
    EntityManager em = dbInstance.getCurrentEntityManager();
    List<Grant> grantToDelete = em.createQuery(sb.toString(), Grant.class).setParameter("group", group).setParameter("resourceKey", resource.getKey()).setParameter("role", role).getResultList();
    for (Grant grant : grantToDelete) {
        em.remove(grant);
    }
}
Also used : Grant(org.olat.basesecurity.Grant) EntityManager(javax.persistence.EntityManager)

Example 2 with Grant

use of org.olat.basesecurity.Grant in project OpenOLAT by OpenOLAT.

the class BGRightManagerImpl method findBGRights.

@Override
public List<BGRights> findBGRights(List<Group> baseGroups, OLATResource resource) {
    List<Grant> grants = groupDao.getGrants(baseGroups, resource);
    List<BGRights> rights = new ArrayList<>();
    Map<BGRights, BGRights> rightsMap = new HashMap<>();
    for (Grant grant : grants) {
        String right = grant.getPermission();
        if (right.indexOf(BG_RIGHT_PREFIX) == 0 && grant.getResource().equals(resource)) {
            BGRightsRole role = null;
            if (GroupRoles.participant.name().equals(grant.getRole())) {
                role = BGRightsRole.participant;
            } else if (GroupRoles.coach.name().equals(grant.getRole())) {
                role = BGRightsRole.tutor;
            }
            BGRights wrapper = new BGRightsImpl(grant.getGroup(), role);
            wrapper = rightsMap.computeIfAbsent(wrapper, w -> w);
            wrapper.getRights().add(right);
            rights.add(wrapper);
        }
    }
    return rights;
}
Also used : Collection(java.util.Collection) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) IdentityRef(org.olat.basesecurity.IdentityRef) OLATResource(org.olat.resource.OLATResource) ArrayList(java.util.ArrayList) List(java.util.List) DB(org.olat.core.commons.persistence.DB) GroupRoles(org.olat.basesecurity.GroupRoles) GroupDAO(org.olat.basesecurity.manager.GroupDAO) Service(org.springframework.stereotype.Service) Map(java.util.Map) BusinessGroup(org.olat.group.BusinessGroup) Group(org.olat.basesecurity.Group) Collections(java.util.Collections) Grant(org.olat.basesecurity.Grant) Grant(org.olat.basesecurity.Grant) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 3 with Grant

use of org.olat.basesecurity.Grant in project openolat by klemens.

the class BGRightManagerTest method testBGRights.

/**
 * BGContextManagerImpl:deleteBGContext() *
 */
@Test
public void testBGRights() {
    RepositoryEntry c1 = JunitTestHelper.createAndPersistRepositoryEntry();
    RepositoryEntry c2 = JunitTestHelper.createAndPersistRepositoryEntry();
    BusinessGroup g1 = businessGroupService.createBusinessGroup(null, "g1", null, -1, -1, false, false, c1);
    BusinessGroup g2 = businessGroupService.createBusinessGroup(null, "g2", null, -1, -1, false, false, c1);
    BusinessGroup g3 = businessGroupService.createBusinessGroup(null, "g3", null, -1, -1, false, false, c2);
    businessGroupRelationDao.addRole(id1, g1, GroupRoles.participant.name());
    businessGroupRelationDao.addRole(id2, g1, GroupRoles.participant.name());
    businessGroupRelationDao.addRole(id1, g2, GroupRoles.participant.name());
    businessGroupRelationDao.addRole(id3, g3, GroupRoles.participant.name());
    rightManager.addBGRight(CourseRights.RIGHT_ARCHIVING, g1, BGRightsRole.participant);
    rightManager.addBGRight(CourseRights.RIGHT_COURSEEDITOR, g1, BGRightsRole.participant);
    rightManager.addBGRight(CourseRights.RIGHT_ARCHIVING, g2, BGRightsRole.participant);
    rightManager.addBGRight(CourseRights.RIGHT_COURSEEDITOR, g3, BGRightsRole.participant);
    // simulate user clicks
    DBFactory.getInstance().closeSession();
    List<Grant> grants = groupDao.getGrants(g1.getBaseGroup(), GroupRoles.participant.name());
    // read, parti, archiving, courseeditor
    Assert.assertEquals(2, grants.size());
    // simulate user clicks
    DBFactory.getInstance().closeSession();
    assertFalse(rightManager.hasBGRight(CourseRights.RIGHT_ARCHIVING, id1, c2.getOlatResource()));
    assertTrue(rightManager.hasBGRight(CourseRights.RIGHT_ARCHIVING, id1, c1.getOlatResource()));
    assertTrue(rightManager.hasBGRight(CourseRights.RIGHT_ARCHIVING, id2, c1.getOlatResource()));
    assertFalse(rightManager.hasBGRight(CourseRights.RIGHT_GROUPMANAGEMENT, id2, c1.getOlatResource()));
    assertFalse(rightManager.hasBGRight(CourseRights.RIGHT_ARCHIVING, id3, c2.getOlatResource()));
    assertTrue(rightManager.hasBGRight(CourseRights.RIGHT_COURSEEDITOR, id3, c2.getOlatResource()));
    assertFalse(rightManager.hasBGRight(CourseRights.RIGHT_COURSEEDITOR, id3, c1.getOlatResource()));
    Assert.assertEquals(2, rightManager.findBGRights(g1, BGRightsRole.participant).size());
    Assert.assertEquals(1, rightManager.findBGRights(g2, BGRightsRole.participant).size());
    // simulate user clicks
    DBFactory.getInstance().closeSession();
    rightManager.removeBGRight(CourseRights.RIGHT_ARCHIVING, g1.getBaseGroup(), c1.getOlatResource(), BGRightsRole.participant);
    rightManager.removeBGRight(CourseRights.RIGHT_COURSEEDITOR, g1.getBaseGroup(), c1.getOlatResource(), BGRightsRole.participant);
    rightManager.removeBGRight(CourseRights.RIGHT_ARCHIVING, g2.getBaseGroup(), c1.getOlatResource(), BGRightsRole.participant);
    rightManager.removeBGRight(CourseRights.RIGHT_COURSEEDITOR, g3.getBaseGroup(), c2.getOlatResource(), BGRightsRole.participant);
    // simulate user clicks
    DBFactory.getInstance().closeSession();
    assertFalse(rightManager.hasBGRight(CourseRights.RIGHT_ARCHIVING, id1, c1.getOlatResource()));
    assertFalse(rightManager.hasBGRight(CourseRights.RIGHT_ARCHIVING, id2, c1.getOlatResource()));
    assertFalse(rightManager.hasBGRight(CourseRights.RIGHT_COURSEEDITOR, id3, c2.getOlatResource()));
    Assert.assertEquals(0, rightManager.findBGRights(g1, BGRightsRole.participant).size());
    Assert.assertEquals(0, rightManager.findBGRights(g2, BGRightsRole.participant).size());
}
Also used : Grant(org.olat.basesecurity.Grant) BusinessGroup(org.olat.group.BusinessGroup) RepositoryEntry(org.olat.repository.RepositoryEntry) Test(org.junit.Test)

Example 4 with Grant

use of org.olat.basesecurity.Grant in project openolat by klemens.

the class GroupDAOTest method getGrants_withResource.

@Test
public void getGrants_withResource() {
    Group group = groupDao.createGroup();
    OLATResource resource = JunitTestHelper.createRandomResource();
    groupDao.addGrant(group, "getGrants-res", "getGrants-res-perm", resource);
    dbInstance.commitAndCloseSession();
    List<Group> groups = Collections.singletonList(group);
    List<Grant> grants = groupDao.getGrants(groups, resource);
    Assert.assertNotNull(grants);
    Assert.assertEquals(1, grants.size());
    Grant grant = grants.get(0);
    Assert.assertNotNull(grant);
    Assert.assertEquals(group, grant.getGroup());
    Assert.assertEquals(resource, grant.getResource());
    Assert.assertEquals("getGrants-res", grant.getRole());
    Assert.assertEquals("getGrants-res-perm", grant.getPermission());
}
Also used : Group(org.olat.basesecurity.Group) Grant(org.olat.basesecurity.Grant) OLATResource(org.olat.resource.OLATResource) Test(org.junit.Test)

Example 5 with Grant

use of org.olat.basesecurity.Grant in project openolat by klemens.

the class BGRightManagerImpl method findBGRights.

@Override
public List<BGRights> findBGRights(List<Group> baseGroups, OLATResource resource) {
    List<Grant> grants = groupDao.getGrants(baseGroups, resource);
    List<BGRights> rights = new ArrayList<>();
    Map<BGRights, BGRights> rightsMap = new HashMap<>();
    for (Grant grant : grants) {
        String right = grant.getPermission();
        if (right.indexOf(BG_RIGHT_PREFIX) == 0 && grant.getResource().equals(resource)) {
            BGRightsRole role = null;
            if (GroupRoles.participant.name().equals(grant.getRole())) {
                role = BGRightsRole.participant;
            } else if (GroupRoles.coach.name().equals(grant.getRole())) {
                role = BGRightsRole.tutor;
            }
            BGRights wrapper = new BGRightsImpl(grant.getGroup(), role);
            wrapper = rightsMap.computeIfAbsent(wrapper, w -> w);
            wrapper.getRights().add(right);
            rights.add(wrapper);
        }
    }
    return rights;
}
Also used : Collection(java.util.Collection) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) IdentityRef(org.olat.basesecurity.IdentityRef) OLATResource(org.olat.resource.OLATResource) ArrayList(java.util.ArrayList) List(java.util.List) DB(org.olat.core.commons.persistence.DB) GroupRoles(org.olat.basesecurity.GroupRoles) GroupDAO(org.olat.basesecurity.manager.GroupDAO) Service(org.springframework.stereotype.Service) Map(java.util.Map) BusinessGroup(org.olat.group.BusinessGroup) Group(org.olat.basesecurity.Group) Collections(java.util.Collections) Grant(org.olat.basesecurity.Grant) Grant(org.olat.basesecurity.Grant) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Aggregations

Grant (org.olat.basesecurity.Grant)16 Group (org.olat.basesecurity.Group)8 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)6 BusinessGroup (org.olat.group.BusinessGroup)6 OLATResource (org.olat.resource.OLATResource)6 EntityManager (javax.persistence.EntityManager)4 GroupRoles (org.olat.basesecurity.GroupRoles)4 Collection (java.util.Collection)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 IdentityRef (org.olat.basesecurity.IdentityRef)2 GroupDAO (org.olat.basesecurity.manager.GroupDAO)2 DB (org.olat.core.commons.persistence.DB)2 RepositoryEntry (org.olat.repository.RepositoryEntry)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Service (org.springframework.stereotype.Service)2