Search in sources :

Example 41 with SecurityGroup

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

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 42 with SecurityGroup

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

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 43 with SecurityGroup

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

the class LDAPLoginTest method testCreateChangedAttrMap.

@Test
@Ignore
public void testCreateChangedAttrMap() {
    Assume.assumeTrue(ldapLoginModule.isLDAPEnabled());
    // simulate closed session (user adding from startup job)
    DBFactory.getInstance().intermediateCommit();
    String uid = "kmeier";
    String pwd = "olat";
    LDAPError errors = new LDAPError();
    boolean usersSyncedAtStartup = ldapLoginModule.isLdapSyncOnStartup();
    if (usersSyncedAtStartup) {
        try {
            // create user but with different attributes - must fail since user already exists
            User user = UserManager.getInstance().createUser("klaus", "Meier", "klaus@meier.ch");
            Identity identity = securityManager.createAndPersistIdentityAndUser("kmeier", null, user, "LDAP", "kmeier");
            SecurityGroup secGroup = securityManager.findSecurityGroupByName(LDAPConstants.SECURITY_GROUP_LDAP);
            securityManager.addIdentityToSecurityGroup(identity, secGroup);
            // simulate closed session (user adding from startup job)
            DBFactory.getInstance().intermediateCommit();
            fail("Expected constrant violation becaus of doupliate entry");
        } catch (Exception e) {
        // success, this is what we expected
        }
        // changedAttrMap empty since already synchronized
        Attributes attrs = ldapManager.bindUser(uid, pwd, errors);
        Identity identitys = securityManager.findIdentityByName(uid);
        Map<String, String> changedAttrMap = ldapManager.prepareUserPropertyForSync(attrs, identitys);
        // map is empty - no attributes to sync
        assertNull(changedAttrMap);
    } else {
        // create user but with different attributes - must fail since user already exists
        User user = UserManager.getInstance().createUser("klaus", "Meier", "klaus@meier.ch");
        Identity identity = securityManager.createAndPersistIdentityAndUser("kmeier", null, user, "LDAP", "kmeier");
        SecurityGroup secGroup = securityManager.findSecurityGroupByName(LDAPConstants.SECURITY_GROUP_LDAP);
        securityManager.addIdentityToSecurityGroup(identity, secGroup);
        // simulate closed session (user adding from startup job)
        DBFactory.getInstance().intermediateCommit();
        // changedAttrMap has 2 changes and uid as entrys (Klaus!=klaus, klaus@olat.org!=klaus@meier.ch)
        Attributes attrs = ldapManager.bindUser(uid, pwd, errors);
        Identity identitys = securityManager.findIdentityByName(uid);
        Map<String, String> changedAttrMap = ldapManager.prepareUserPropertyForSync(attrs, identitys);
        // result must be 3: 2 changed plus the user ID which is always in the map
        assertEquals(3, changedAttrMap.keySet().size());
    }
    // nothing to change for this user
    uid = "mrohrer";
    Attributes attrs = ldapManager.bindUser(uid, pwd, errors);
    Identity identitys = securityManager.findIdentityByName(uid);
    Map<String, String> changedAttrMap = ldapManager.prepareUserPropertyForSync(attrs, identitys);
    assertEquals(true, (changedAttrMap == null));
}
Also used : LDAPUser(org.olat.ldap.model.LDAPUser) User(org.olat.core.id.User) Attributes(javax.naming.directory.Attributes) Identity(org.olat.core.id.Identity) SecurityGroup(org.olat.basesecurity.SecurityGroup) NamingException(javax.naming.NamingException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 44 with SecurityGroup

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

the class CourseTest method getAuthors.

@Test
public void getAuthors() throws IOException, URISyntaxException {
    // make auth1 and auth2 authors
    SecurityGroup authorGroup = securityManager.findSecurityGroupByName(Constants.GROUP_AUTHORS);
    if (!securityManager.isIdentityInSecurityGroup(auth1, authorGroup)) {
        securityManager.addIdentityToSecurityGroup(auth1, authorGroup);
    }
    if (!securityManager.isIdentityInSecurityGroup(auth2, authorGroup)) {
        securityManager.addIdentityToSecurityGroup(auth2, authorGroup);
    }
    dbInstance.intermediateCommit();
    // make auth1 and auth2 owner
    RepositoryEntry repositoryEntry = repositoryManager.lookupRepositoryEntry(course1, true);
    List<Identity> authors = new ArrayList<Identity>();
    authors.add(auth1);
    authors.add(auth2);
    IdentitiesAddEvent identitiesAddedEvent = new IdentitiesAddEvent(authors);
    repositoryManager.addOwners(admin, identitiesAddedEvent, repositoryEntry, null);
    dbInstance.intermediateCommit();
    // get them
    assertTrue(conn.login("administrator", "openolat"));
    URI uri = UriBuilder.fromUri(getContextURI()).path("/repo/courses/" + course1.getResourceableId() + "/authors").build();
    HttpGet method = conn.createGet(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    InputStream body = response.getEntity().getContent();
    assertNotNull(body);
    List<UserVO> authorVOs = parseUserArray(body);
    assertNotNull(authorVOs);
}
Also used : UserVO(org.olat.user.restapi.UserVO) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) IdentitiesAddEvent(org.olat.admin.securitygroup.gui.IdentitiesAddEvent) HttpResponse(org.apache.http.HttpResponse) RepositoryEntry(org.olat.repository.RepositoryEntry) SecurityGroup(org.olat.basesecurity.SecurityGroup) Identity(org.olat.core.id.Identity) URI(java.net.URI) Test(org.junit.Test)

Example 45 with SecurityGroup

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

the class CourseTest method addAuthor.

@Test
public void addAuthor() throws IOException, URISyntaxException {
    Assert.assertTrue(conn.login("administrator", "openolat"));
    URI request = UriBuilder.fromUri(getContextURI()).path("/repo/courses/" + course1.getResourceableId() + "/authors/" + auth0.getKey()).build();
    HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    EntityUtils.consume(response.getEntity());
    // is auth0 author
    SecurityGroup authorGroup = securityManager.findSecurityGroupByName(Constants.GROUP_AUTHORS);
    boolean isAuthor = securityManager.isIdentityInSecurityGroup(auth0, authorGroup);
    dbInstance.intermediateCommit();
    Assert.assertTrue(isAuthor);
    // is auth0 owner
    RepositoryEntry repositoryEntry = repositoryManager.lookupRepositoryEntry(course1, true);
    boolean isOwner = repositoryService.hasRole(auth0, repositoryEntry, GroupRoles.owner.name());
    dbInstance.intermediateCommit();
    Assert.assertTrue(isOwner);
}
Also used : HttpResponse(org.apache.http.HttpResponse) RepositoryEntry(org.olat.repository.RepositoryEntry) SecurityGroup(org.olat.basesecurity.SecurityGroup) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut) 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