Search in sources :

Example 6 with GroupMembership

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

the class GroupDAOTest method hasRole.

@Test
public void hasRole() {
    Identity id = JunitTestHelper.createAndPersistIdentityAsRndUser("bgrp-2-");
    Identity id2 = JunitTestHelper.createAndPersistIdentityAsRndUser("bgrp-2b-");
    Group group = groupDao.createGroup();
    GroupMembership membership = groupDao.addMembershipTwoWay(group, id, "author");
    dbInstance.commit();
    Assert.assertNotNull(membership);
    dbInstance.commitAndCloseSession();
    boolean hasRole = groupDao.hasRole(group, id, "author");
    Assert.assertTrue(hasRole);
    // negative tests
    boolean hasNotRole = groupDao.hasRole(group, id, "pilot");
    Assert.assertFalse(hasNotRole);
    boolean id2_hasNotRole = groupDao.hasRole(group, id2, "author");
    Assert.assertFalse(id2_hasNotRole);
}
Also used : Group(org.olat.basesecurity.Group) GroupMembership(org.olat.basesecurity.GroupMembership) Identity(org.olat.core.id.Identity) Test(org.junit.Test)

Example 7 with GroupMembership

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

the class GroupDAOTest method removeMemberships_groupAndRole.

@Test
public void removeMemberships_groupAndRole() {
    Identity id1 = JunitTestHelper.createAndPersistIdentityAsRndUser("bgrp-12-");
    Identity id2 = JunitTestHelper.createAndPersistIdentityAsRndUser("bgrp-13-");
    Group group = groupDao.createGroup();
    GroupMembership membership1 = groupDao.addMembershipTwoWay(group, id1, "pilot");
    GroupMembership membership2 = groupDao.addMembershipTwoWay(group, id2, "copilot");
    Assert.assertNotNull(membership1);
    Assert.assertNotNull(membership2);
    dbInstance.commitAndCloseSession();
    // check
    int numOfMembers = groupDao.countMembers(group);
    Assert.assertEquals(2, numOfMembers);
    // remove
    int numOfDeletedRows = groupDao.removeMemberships(group, "pilot");
    dbInstance.commitAndCloseSession();
    Assert.assertEquals(1, numOfDeletedRows);
    // check
    List<GroupMembership> deletedMemberships = groupDao.getMemberships(group, "pilot");
    Assert.assertTrue(deletedMemberships.isEmpty());
    List<GroupMembership> lastMemberships = groupDao.getMemberships(group, "copilot");
    Assert.assertEquals(1, lastMemberships.size());
}
Also used : Group(org.olat.basesecurity.Group) GroupMembership(org.olat.basesecurity.GroupMembership) Identity(org.olat.core.id.Identity) Test(org.junit.Test)

Example 8 with GroupMembership

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

the class GroupDAOTest method removeMembership.

@Test
public void removeMembership() {
    Identity id1 = JunitTestHelper.createAndPersistIdentityAsRndUser("bgrp-7-");
    Identity id2 = JunitTestHelper.createAndPersistIdentityAsRndUser("bgrp-8-");
    Group group = groupDao.createGroup();
    GroupMembership membership1 = groupDao.addMembershipTwoWay(group, id1, "pilot");
    GroupMembership membership2 = groupDao.addMembershipTwoWay(group, id2, "pilot");
    Assert.assertNotNull(membership1);
    Assert.assertNotNull(membership2);
    dbInstance.commitAndCloseSession();
    // check
    List<GroupMembership> memberships = groupDao.getMemberships(group, "pilot");
    Assert.assertEquals(2, memberships.size());
    // remove
    groupDao.removeMembership(group, id1);
    dbInstance.commitAndCloseSession();
    // check
    List<GroupMembership> deletedMemberships = groupDao.getMemberships(group, "pilot");
    Assert.assertEquals(1, deletedMemberships.size());
    Identity stayingMember = deletedMemberships.get(0).getIdentity();
    Assert.assertNotNull(stayingMember);
    Assert.assertEquals(id2, stayingMember);
}
Also used : Group(org.olat.basesecurity.Group) GroupMembership(org.olat.basesecurity.GroupMembership) Identity(org.olat.core.id.Identity) Test(org.junit.Test)

Example 9 with GroupMembership

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

the class GroupDAOTest method countMembers.

@Test
public void countMembers() {
    Identity id1 = JunitTestHelper.createAndPersistIdentityAsRndUser("bgrp-4-");
    Identity id2 = JunitTestHelper.createAndPersistIdentityAsRndUser("bgrp-5-");
    Identity id3 = JunitTestHelper.createAndPersistIdentityAsRndUser("bgrp-6-");
    Group group = groupDao.createGroup();
    GroupMembership membership1 = groupDao.addMembershipTwoWay(group, id1, "pilot");
    GroupMembership membership2 = groupDao.addMembershipTwoWay(group, id2, "pilot");
    GroupMembership membership3 = groupDao.addMembershipTwoWay(group, id3, "copilot");
    dbInstance.commit();
    Assert.assertNotNull(membership1);
    Assert.assertNotNull(membership2);
    Assert.assertNotNull(membership3);
    dbInstance.commitAndCloseSession();
    int numOfMembers = groupDao.countMembers(group);
    Assert.assertEquals(3, numOfMembers);
}
Also used : Group(org.olat.basesecurity.Group) GroupMembership(org.olat.basesecurity.GroupMembership) Identity(org.olat.core.id.Identity) Test(org.junit.Test)

Example 10 with GroupMembership

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

the class OLATUpgrade_10_0_0 method processSecurityGroup.

private void processSecurityGroup(Group group, String role, SecurityGroup secGroup) {
    if (secGroup == null)
        return;
    List<SecurityGroupMembershipImpl> oldMemberships = getMembershipsOfSecurityGroup(secGroup);
    for (SecurityGroupMembershipImpl oldMembership : oldMemberships) {
        GroupMembershipImpl membership = new GroupMembershipImpl();
        membership.setCreationDate(oldMembership.getCreationDate());
        membership.setLastModified(oldMembership.getLastModified());
        membership.setGroup(group);
        membership.setIdentity(oldMembership.getIdentity());
        membership.setRole(role);
        dbInstance.getCurrentEntityManager().persist(membership);
        Set<GroupMembership> members = ((GroupImpl) group).getMembers();
        if (members == null) {
            members = new HashSet<>();
            ((GroupImpl) group).setMembers(members);
        }
        members.add(membership);
    }
}
Also used : SecurityGroupMembershipImpl(org.olat.basesecurity.SecurityGroupMembershipImpl) GroupImpl(org.olat.basesecurity.model.GroupImpl) GroupMembership(org.olat.basesecurity.GroupMembership) GroupMembershipImpl(org.olat.basesecurity.model.GroupMembershipImpl) SecurityGroupMembershipImpl(org.olat.basesecurity.SecurityGroupMembershipImpl)

Aggregations

GroupMembership (org.olat.basesecurity.GroupMembership)40 Identity (org.olat.core.id.Identity)30 Test (org.junit.Test)26 Group (org.olat.basesecurity.Group)26 GroupImpl (org.olat.basesecurity.model.GroupImpl)8 Date (java.util.Date)6 EntityManager (javax.persistence.EntityManager)6 GroupMembershipImpl (org.olat.basesecurity.model.GroupMembershipImpl)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 GroupMemberView (org.olat.admin.securitygroup.gui.GroupMemberView)2 IdentitiesOfGroupTableDataModel (org.olat.admin.securitygroup.gui.IdentitiesOfGroupTableDataModel)2 SecurityGroupMembershipImpl (org.olat.basesecurity.SecurityGroupMembershipImpl)2 UserPropertyHandler (org.olat.user.propertyhandlers.UserPropertyHandler)2