Search in sources :

Example 6 with SecurityGroup

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

the class LDAPLoginTest method testCronSync.

@Test
@Ignore
public void testCronSync() throws Exception {
    Assume.assumeTrue(ldapLoginModule.isLDAPEnabled());
    LdapContext ctx;
    List<LDAPUser> ldapUserList;
    List<Attributes> newLdapUserList;
    Map<Identity, Map<String, String>> changedMapIdenityMap;
    List<Identity> deletedUserList;
    LDAPError errors = new LDAPError();
    // find user changed after 2010,01,09,00,00
    ctx = ldapManager.bindSystem();
    Calendar cal = Calendar.getInstance();
    cal.set(2010, 0, 10, 0, 0, 0);
    Date syncDate = cal.getTime();
    ldapUserList = ldapDao.getUserAttributesModifiedSince(syncDate, ctx);
    assertEquals(1, ldapUserList.size());
    // find all users
    syncDate = null;
    ldapUserList = ldapDao.getUserAttributesModifiedSince(syncDate, ctx);
    assertEquals(6, ldapUserList.size());
    // prepare create- and sync-Lists for each user from defined syncTime
    Identity idenity;
    Map<String, String> changedAttrMap;
    newLdapUserList = new LinkedList<Attributes>();
    changedMapIdenityMap = new HashMap<Identity, Map<String, String>>();
    for (int i = 0; i < ldapUserList.size(); i++) {
        Attributes userAttrs = ldapUserList.get(i).getAttributes();
        String user = getAttributeValue(userAttrs.get(syncConfiguration.getOlatPropertyToLdapAttribute("userID")));
        idenity = ldapManager.findIdentityByLdapAuthentication(userAttrs, errors);
        if (idenity != null) {
            changedAttrMap = ldapManager.prepareUserPropertyForSync(userAttrs, idenity);
            if (changedAttrMap != null)
                changedMapIdenityMap.put(idenity, changedAttrMap);
        } else {
            if (errors.isEmpty()) {
                String[] reqAttrs = syncConfiguration.checkRequestAttributes(userAttrs);
                if (reqAttrs == null)
                    newLdapUserList.add(userAttrs);
                else
                    System.out.println("Cannot create User " + user + " required Attributes are missing");
            } else
                System.out.println(errors.get());
        }
    }
    // create Users in LDAP Group only existing in OLAT
    User user1 = UserManager.getInstance().createUser("hansi", "hürlima", "hansi@hansli.com");
    Identity identity1 = securityManager.createAndPersistIdentityAndUser("hansi", null, user1, "LDAP", "hansi");
    SecurityGroup secGroup1 = securityManager.findSecurityGroupByName(LDAPConstants.SECURITY_GROUP_LDAP);
    securityManager.addIdentityToSecurityGroup(identity1, secGroup1);
    user1 = UserManager.getInstance().createUser("chaspi", "meier", "chaspi@hansli.com");
    identity1 = securityManager.createAndPersistIdentityAndUser("chaspi", null, user1, "LDAP", "chaspi");
    securityManager.addIdentityToSecurityGroup(identity1, secGroup1);
    // create User to Delete List
    deletedUserList = ldapManager.getIdentitysDeletedInLdap(ctx);
    assertEquals(4, (deletedUserList.size()));
    // sync users
    Iterator<Identity> itrIdent = changedMapIdenityMap.keySet().iterator();
    while (itrIdent.hasNext()) {
        Identity ident = itrIdent.next();
        ldapManager.syncUser(changedMapIdenityMap.get(ident), ident);
    }
    // create all users
    for (int i = 0; i < newLdapUserList.size(); i++) {
        ldapManager.createAndPersistUser(newLdapUserList.get(i));
    }
    // delete all users
    ldapManager.deletIdentities(deletedUserList);
    // check if users are deleted
    deletedUserList = ldapManager.getIdentitysDeletedInLdap(ctx);
    assertEquals(0, (deletedUserList.size()));
}
Also used : LDAPUser(org.olat.ldap.model.LDAPUser) User(org.olat.core.id.User) Calendar(java.util.Calendar) Attributes(javax.naming.directory.Attributes) LDAPUser(org.olat.ldap.model.LDAPUser) SecurityGroup(org.olat.basesecurity.SecurityGroup) Date(java.util.Date) Identity(org.olat.core.id.Identity) HashMap(java.util.HashMap) Map(java.util.Map) LdapContext(javax.naming.ldap.LdapContext) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 7 with SecurityGroup

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

the class LDAPLoginTest method testIdentityDeletedInLDAP.

@Test
@Ignore
public void testIdentityDeletedInLDAP() {
    Assume.assumeTrue(ldapLoginModule.isLDAPEnabled());
    List<Identity> deletList;
    // should be empty
    LdapContext ctx = ldapManager.bindSystem();
    deletList = ldapManager.getIdentitysDeletedInLdap(ctx);
    assertEquals(0, (deletList.size()));
    // simulate closed session (user adding from startup job)
    DBFactory.getInstance().intermediateCommit();
    // create some users in LDAPSecurityGroup
    User user = UserManager.getInstance().createUser("grollia", "wa", "gorrila@olat.org");
    Identity identity = securityManager.createAndPersistIdentityAndUser("gorilla", null, user, "LDAP", "gorrila");
    SecurityGroup secGroup1 = securityManager.findSecurityGroupByName(LDAPConstants.SECURITY_GROUP_LDAP);
    securityManager.addIdentityToSecurityGroup(identity, secGroup1);
    user = UserManager.getInstance().createUser("wer", "immer", "immer@olat.org");
    identity = securityManager.createAndPersistIdentityAndUser("der", null, user, "LDAP", "der");
    securityManager.addIdentityToSecurityGroup(identity, secGroup1);
    user = UserManager.getInstance().createUser("die", "da", "chaspi@olat.org");
    identity = securityManager.createAndPersistIdentityAndUser("das", null, user, "LDAP", "das");
    securityManager.addIdentityToSecurityGroup(identity, secGroup1);
    // simulate closed session
    DBFactory.getInstance().intermediateCommit();
    // 3 members in LDAP group but not existing in OLAT
    deletList = ldapManager.getIdentitysDeletedInLdap(ctx);
    assertEquals(3, (deletList.size()));
    // delete user in OLAT
    securityManager.removeIdentityFromSecurityGroup(identity, secGroup1);
    UserDeletionManager.getInstance().deleteIdentity(identity);
    // simulate closed session
    DBFactory.getInstance().intermediateCommit();
    // 2 members in LDAP group but not existing in OLAT
    deletList = ldapManager.getIdentitysDeletedInLdap(ctx);
    assertEquals(2, (deletList.size()));
}
Also used : LDAPUser(org.olat.ldap.model.LDAPUser) User(org.olat.core.id.User) Identity(org.olat.core.id.Identity) SecurityGroup(org.olat.basesecurity.SecurityGroup) LdapContext(javax.naming.ldap.LdapContext) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 8 with SecurityGroup

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

the class OAuthRegistrationController method formOK.

@Override
protected void formOK(UserRequest ureq) {
    String lang = langEl.getSelectedKey();
    String username = usernameEl.getValue();
    OAuthUser oauthUser = registration.getOauthUser();
    User newUser = userManager.createUser(null, null, null);
    for (UserPropertyHandler userPropertyHandler : userPropertyHandlers) {
        FormItem propertyItem = this.flc.getFormComponent(userPropertyHandler.getName());
        userPropertyHandler.updateUserFromFormItem(newUser, propertyItem);
    }
    // Init preferences
    newUser.getPreferences().setLanguage(lang);
    newUser.getPreferences().setInformSessionTimeout(true);
    String id;
    if (StringHelper.containsNonWhitespace(oauthUser.getId())) {
        id = oauthUser.getId();
    } else if (StringHelper.containsNonWhitespace(oauthUser.getEmail())) {
        id = oauthUser.getEmail();
    } else {
        id = username;
    }
    authenticatedIdentity = securityManager.createAndPersistIdentityAndUser(username, null, newUser, registration.getAuthProvider(), id, null);
    // Add user to system users group
    SecurityGroup olatuserGroup = securityManager.findSecurityGroupByName(Constants.GROUP_OLATUSERS);
    securityManager.addIdentityToSecurityGroup(authenticatedIdentity, olatuserGroup);
    // open disclaimer
    removeAsListenerAndDispose(disclaimerController);
    disclaimerController = new DisclaimerController(ureq, getWindowControl());
    listenTo(disclaimerController);
    cmc = new CloseableModalController(getWindowControl(), translate("close"), disclaimerController.getInitialComponent(), true, translate("disclaimer.title"));
    cmc.activate();
    listenTo(cmc);
}
Also used : OAuthUser(org.olat.login.oauth.model.OAuthUser) User(org.olat.core.id.User) DisclaimerController(org.olat.registration.DisclaimerController) OAuthUser(org.olat.login.oauth.model.OAuthUser) FormItem(org.olat.core.gui.components.form.flexible.FormItem) CloseableModalController(org.olat.core.gui.control.generic.closablewrapper.CloseableModalController) SecurityGroup(org.olat.basesecurity.SecurityGroup) UserPropertyHandler(org.olat.user.propertyhandlers.UserPropertyHandler)

Example 9 with SecurityGroup

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

the class PLockTest method testSync.

@Test
public void testSync() {
    log.info("testing enrollment");
    // ------------------ now check with lock -------------------
    // create a group
    // create users
    final List<Identity> identities = new ArrayList<Identity>();
    for (int i = 0; i < MAX_COUNT + MAX_USERS_MORE; i++) {
        Identity id = JunitTestHelper.createAndPersistIdentityAsUser("u-" + i + "-" + UUID.randomUUID().toString());
        identities.add(id);
        log.info("testSync: Identity=" + id.getName() + " created");
    }
    dbInstance.closeSession();
    final SecurityGroup group2 = BaseSecurityManager.getInstance().createAndPersistSecurityGroup();
    // make sure the lock has been written to the disk (tests for createOrFind see other methods)
    dbInstance.closeSession();
    // prepare threads
    int numOfThreads = MAX_COUNT + MAX_USERS_MORE;
    final CountDownLatch finishCount = new CountDownLatch(numOfThreads);
    // try to enrol all in the same group
    for (int i = 0; i < numOfThreads; i++) {
        final int j = i;
        new Thread(new Runnable() {

            public void run() {
                try {
                    log.info("testSync: thread started j=" + j);
                    Identity id = identities.get(j);
                    // 
                    PLock p2 = pessimisticLockManager.findOrPersistPLock("befinsert");
                    assertNotNull(p2);
                    doNoLockingEnrol(id, group2);
                    dbInstance.commit();
                    dbInstance.closeSession();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    finishCount.countDown();
                }
            }
        }).start();
    }
    try {
        finishCount.await(120, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        log.error("", e);
    }
    // now count
    dbInstance.closeSession();
    int cnt2 = BaseSecurityManager.getInstance().countIdentitiesOfSecurityGroup(group2);
    assertTrue("cnt should be smaller or eq than allowed since synced with select for update. cnt:" + cnt2 + ", max " + MAX_COUNT, cnt2 <= MAX_COUNT);
    assertTrue("cnt should be eq to allowed since synced with select for update. cnt:" + cnt2 + ", max " + MAX_COUNT, cnt2 == MAX_COUNT);
    log.info("cnt lock " + cnt2);
}
Also used : PLock(org.olat.core.commons.services.lock.pessimistic.PLock) ArrayList(java.util.ArrayList) Identity(org.olat.core.id.Identity) SecurityGroup(org.olat.basesecurity.SecurityGroup) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 10 with SecurityGroup

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

the class EPStructureManagerTest method testGetStructureElementsForUser.

@Test
public void testGetStructureElementsForUser() {
    Identity user = JunitTestHelper.createAndPersistIdentityAsRndUser("EP-1-");
    PortfolioStructure el = epFrontendManager.createAndPersistPortfolioDefaultMap(user, "users-test-map", "a-map-to-test-get-afterwards");
    Assert.assertNotNull(el);
    dbInstance.commitAndCloseSession();
    List<SecurityGroup> secGroups = securityManager.getSecurityGroupsForIdentity(user);
    Assert.assertNotNull(secGroups);
    Assert.assertTrue(secGroups.size() >= 1);
    List<PortfolioStructure> elRes = epStructureManager.getStructureElementsForUser(user, ElementType.DEFAULT_MAP);
    Assert.assertNotNull(elRes);
    Assert.assertEquals(1, elRes.size());
    Assert.assertEquals("users-test-map", elRes.get(0).getTitle());
    // get another map
    PortfolioStructure el2 = epFrontendManager.createAndPersistPortfolioDefaultMap(user, "users-test-map-2", "2-a-map-to-test-get-afterwards");
    Assert.assertNotNull(el2);
    dbInstance.commitAndCloseSession();
    List<PortfolioStructure> elRes2 = epStructureManager.getStructureElementsForUser(user);
    Assert.assertNotNull(elRes2);
    Assert.assertEquals(2, elRes2.size());
}
Also used : PortfolioStructure(org.olat.portfolio.model.structel.PortfolioStructure) Identity(org.olat.core.id.Identity) SecurityGroup(org.olat.basesecurity.SecurityGroup) Test(org.junit.Test)

Aggregations

SecurityGroup (org.olat.basesecurity.SecurityGroup)142 Identity (org.olat.core.id.Identity)104 ArrayList (java.util.ArrayList)36 Test (org.junit.Test)24 BaseSecurity (org.olat.basesecurity.BaseSecurity)20 User (org.olat.core.id.User)20 CatalogEntry (org.olat.repository.CatalogEntry)18 RepositoryEntry (org.olat.repository.RepositoryEntry)16 Path (javax.ws.rs.Path)14 Date (java.util.Date)12 UserVO (org.olat.user.restapi.UserVO)10 URI (java.net.URI)8 Calendar (java.util.Calendar)8 HashMap (java.util.HashMap)8 HttpResponse (org.apache.http.HttpResponse)8 IdentitiesAddEvent (org.olat.admin.securitygroup.gui.IdentitiesAddEvent)8 UserPropertyHandler (org.olat.user.propertyhandlers.UserPropertyHandler)8 LDAPUser (org.olat.ldap.model.LDAPUser)7 HashSet (java.util.HashSet)6 NamingException (javax.naming.NamingException)6