Search in sources :

Example 6 with Grant

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

the class GroupDAOTest method getGrants_withResource_withRole.

@Test
public void getGrants_withResource_withRole() {
    Group group = groupDao.createGroup();
    OLATResource resource = JunitTestHelper.createRandomResource();
    groupDao.addGrant(group, "getGrants-role-1", "getGrants-role-1-perm", resource);
    groupDao.addGrant(group, "getGrants-role-2", "getGrants-role-2-perm", resource);
    dbInstance.commitAndCloseSession();
    List<Grant> grants = groupDao.getGrants(group, "getGrants-role-2", 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-role-2", grant.getRole());
    Assert.assertEquals("getGrants-role-2-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 7 with Grant

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

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 8 with Grant

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

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 9 with Grant

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

the class GroupDAO method removeGrant.

public void removeGrant(Group group, String role, String permission, 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.permission=:permission 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).setParameter("permission", permission).getResultList();
    for (Grant grant : grantToDelete) {
        em.remove(grant);
    }
}
Also used : Grant(org.olat.basesecurity.Grant) EntityManager(javax.persistence.EntityManager)

Example 10 with Grant

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

the class BGRightManagerImpl method hasBGRight.

@Override
public boolean hasBGRight(List<BusinessGroup> groups) {
    if (groups == null || groups.isEmpty())
        return false;
    List<Group> secGroups = new ArrayList<>(groups.size());
    for (BusinessGroup group : groups) {
        secGroups.add(group.getBaseGroup());
    }
    List<Grant> grants = groupDao.getGrants(secGroups);
    // start with bgr.
    for (Grant grant : grants) {
        String right = grant.getPermission();
        if (right.indexOf(BG_RIGHT_PREFIX) == 0) {
            return true;
        }
    }
    return false;
}
Also used : BusinessGroup(org.olat.group.BusinessGroup) Group(org.olat.basesecurity.Group) Grant(org.olat.basesecurity.Grant) BusinessGroup(org.olat.group.BusinessGroup) 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