Search in sources :

Example 41 with TeamModel

use of com.gitblit.models.TeamModel in project gitblit by gitblit.

the class LdapAuthProvider method sync.

public synchronized void sync() {
    final boolean enabled = settings.getBoolean(Keys.realm.ldap.synchronize, false);
    if (enabled) {
        logger.info("Synchronizing with LDAP @ " + settings.getRequiredString(Keys.realm.ldap.server));
        final boolean deleteRemovedLdapUsers = settings.getBoolean(Keys.realm.ldap.removeDeletedUsers, true);
        LdapConnection ldapConnection = new LdapConnection(settings);
        if (ldapConnection.connect()) {
            if (ldapConnection.bind() == null) {
                ldapConnection.close();
                logger.error("Cannot synchronize with LDAP.");
                return;
            }
            try {
                String uidAttribute = settings.getString(Keys.realm.ldap.uid, "uid");
                String accountBase = ldapConnection.getAccountBase();
                String accountPattern = ldapConnection.getAccountPattern();
                accountPattern = StringUtils.replace(accountPattern, "${username}", "*");
                SearchResult result = doSearch(ldapConnection, accountBase, accountPattern);
                if (result != null && result.getEntryCount() > 0) {
                    final Map<String, UserModel> ldapUsers = new HashMap<String, UserModel>();
                    for (SearchResultEntry loggingInUser : result.getSearchEntries()) {
                        Attribute uid = loggingInUser.getAttribute(uidAttribute);
                        if (uid == null) {
                            logger.error("Can not synchronize with LDAP, missing \"{}\" attribute", uidAttribute);
                            continue;
                        }
                        final String username = uid.getValue();
                        logger.debug("LDAP synchronizing: " + username);
                        UserModel user = userManager.getUserModel(username);
                        if (user == null) {
                            user = new UserModel(username);
                        }
                        if (!supportsTeamMembershipChanges()) {
                            getTeamsFromLdap(ldapConnection, username, loggingInUser, user);
                        }
                        // Get User Attributes
                        setUserAttributes(user, loggingInUser);
                        // store in map
                        ldapUsers.put(username.toLowerCase(), user);
                    }
                    if (deleteRemovedLdapUsers) {
                        logger.debug("detecting removed LDAP users...");
                        for (UserModel userModel : userManager.getAllUsers()) {
                            if (AccountType.LDAP == userModel.accountType) {
                                if (!ldapUsers.containsKey(userModel.username)) {
                                    logger.info("deleting removed LDAP user " + userModel.username + " from user service");
                                    userManager.deleteUser(userModel.username);
                                }
                            }
                        }
                    }
                    userManager.updateUserModels(ldapUsers.values());
                    if (!supportsTeamMembershipChanges()) {
                        final Map<String, TeamModel> userTeams = new HashMap<String, TeamModel>();
                        for (UserModel user : ldapUsers.values()) {
                            for (TeamModel userTeam : user.teams) {
                                // Is this an administrative team?
                                setAdminAttribute(userTeam);
                                userTeams.put(userTeam.name, userTeam);
                            }
                        }
                        userManager.updateTeamModels(userTeams.values());
                    }
                }
                if (!supportsTeamMembershipChanges()) {
                    getEmptyTeamsFromLdap(ldapConnection);
                }
            } finally {
                ldapConnection.close();
            }
        }
    }
}
Also used : UserModel(com.gitblit.models.UserModel) TeamModel(com.gitblit.models.TeamModel) HashMap(java.util.HashMap) Attribute(com.unboundid.ldap.sdk.Attribute) SearchResult(com.unboundid.ldap.sdk.SearchResult) LdapConnection(com.gitblit.ldap.LdapConnection) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 42 with TeamModel

use of com.gitblit.models.TeamModel in project gitblit by gitblit.

the class LdapAuthProvider method createTeamFromLdap.

private TeamModel createTeamFromLdap(SearchResultEntry teamEntry) {
    TeamModel answer = new TeamModel(teamEntry.getAttributeValue("cn"));
    answer.accountType = getAccountType();
    return answer;
}
Also used : TeamModel(com.gitblit.models.TeamModel)

Example 43 with TeamModel

use of com.gitblit.models.TeamModel in project gitblit by gitblit.

the class UserServiceTest method testTeams.

protected void testTeams(IUserService service) {
    // confirm we have 1 team (admins)
    assertEquals(1, service.getAllTeamNames().size());
    assertEquals("admins", service.getAllTeamNames().get(0));
    RepositoryModel newrepo1 = new RepositoryModel("newrepo1", null, null, null);
    newrepo1.accessRestriction = AccessRestrictionType.VIEW;
    RepositoryModel NEWREPO1 = new RepositoryModel("NEWREPO1", null, null, null);
    NEWREPO1.accessRestriction = AccessRestrictionType.VIEW;
    // remove newrepo1 from test user
    // now test user has no repositories
    UserModel user = service.getUserModel("test");
    user.permissions.clear();
    service.updateUserModel(user);
    user = service.getUserModel("test");
    assertEquals(0, user.permissions.size());
    assertFalse(user.canView(newrepo1));
    assertFalse(user.canView(NEWREPO1));
    // create test team and add test user and newrepo1
    TeamModel team = new TeamModel("testteam");
    team.addUser("test");
    team.addRepositoryPermission(newrepo1.name);
    service.updateTeamModel(team);
    // confirm 1 user and 1 repo
    team = service.getTeamModel("testteam");
    assertEquals(1, team.permissions.size());
    assertEquals(1, team.users.size());
    // confirm team membership
    user = service.getUserModel("test");
    assertEquals(0, user.permissions.size());
    assertEquals(1, user.teams.size());
    // confirm team access
    assertTrue(team.hasRepositoryPermission(newrepo1.name));
    assertTrue(user.canView(newrepo1));
    assertTrue(team.hasRepositoryPermission(NEWREPO1.name));
    assertTrue(user.canView(NEWREPO1));
    // rename the team and add new repository
    RepositoryModel newrepo2 = new RepositoryModel("newrepo2", null, null, null);
    newrepo2.accessRestriction = AccessRestrictionType.VIEW;
    RepositoryModel NEWREPO2 = new RepositoryModel("NEWREPO2", null, null, null);
    NEWREPO2.accessRestriction = AccessRestrictionType.VIEW;
    team.addRepositoryPermission(newrepo2.name);
    team.name = "testteam2";
    service.updateTeamModel("testteam", team);
    team = service.getTeamModel("testteam2");
    user = service.getUserModel("test");
    // confirm user and team can access newrepo2
    assertEquals(2, team.permissions.size());
    assertTrue(team.hasRepositoryPermission(newrepo2.name));
    assertTrue(user.canView(newrepo2));
    assertTrue(team.hasRepositoryPermission(NEWREPO2.name));
    assertTrue(user.canView(NEWREPO2));
    // delete testteam2
    service.deleteTeam("testteam2");
    team = service.getTeamModel("testteam2");
    user = service.getUserModel("test");
    // confirm team does not exist and user can not access newrepo1 and 2
    assertEquals(null, team);
    assertFalse(user.canView(newrepo1));
    assertFalse(user.canView(newrepo2));
    // create new team and add it to user
    // this tests the inverse team creation/team addition
    team = new TeamModel("testteam");
    team.addRepositoryPermission(NEWREPO1.name);
    team.addRepositoryPermission(NEWREPO2.name);
    user.teams.add(team);
    service.updateUserModel(user);
    // confirm the inverted team addition
    user = service.getUserModel("test");
    team = service.getTeamModel("testteam");
    assertTrue(user.canView(newrepo1));
    assertTrue(user.canView(newrepo2));
    assertTrue(team.hasUser("test"));
    // drop testteam from user and add nextteam to user
    team = new TeamModel("nextteam");
    team.addRepositoryPermission(NEWREPO1.name);
    team.addRepositoryPermission(NEWREPO2.name);
    user.teams.clear();
    user.teams.add(team);
    service.updateUserModel(user);
    // confirm implicit drop
    user = service.getUserModel("test");
    team = service.getTeamModel("testteam");
    assertTrue(user.canView(newrepo1));
    assertTrue(user.canView(newrepo2));
    assertFalse(team.hasUser("test"));
    team = service.getTeamModel("nextteam");
    assertTrue(team.hasUser("test"));
    // delete the user and confirm team no longer has user
    service.deleteUser("test");
    team = service.getTeamModel("testteam");
    assertFalse(team.hasUser("test"));
    // delete both teams
    service.deleteTeam("testteam");
    service.deleteTeam("nextteam");
    // assert we still have the admins team
    assertEquals(1, service.getAllTeamNames().size());
    assertEquals("admins", service.getAllTeamNames().get(0));
    team = service.getTeamModel("admins");
    assertEquals(1, team.mailingLists.size());
    assertTrue(team.mailingLists.contains("admins@localhost.com"));
}
Also used : UserModel(com.gitblit.models.UserModel) TeamModel(com.gitblit.models.TeamModel) RepositoryModel(com.gitblit.models.RepositoryModel)

Example 44 with TeamModel

use of com.gitblit.models.TeamModel in project gitblit by gitblit.

the class UserServiceTest method testUsers.

protected void testUsers(IUserService service) {
    UserModel admin = service.getUserModel("admin");
    assertTrue(admin == null);
    // add admin and admins team
    TeamModel admins = new TeamModel("admins");
    admins.mailingLists.add("admins@localhost.com");
    admin = new UserModel("admin");
    admin.password = "password";
    admin.canAdmin = true;
    admin.excludeFromFederation = true;
    admin.teams.add(admins);
    service.updateUserModel(admin);
    admin = null;
    admins = null;
    // add new user
    UserModel newUser = new UserModel("test");
    newUser.password = "testPassword";
    newUser.addRepositoryPermission("repo1");
    newUser.addRepositoryPermission("repo2");
    newUser.addRepositoryPermission("sub/repo3");
    service.updateUserModel(newUser);
    // add one more new user and then test reload of first new user
    newUser = new UserModel("GARBAGE");
    newUser.password = "garbage";
    service.updateUserModel(newUser);
    // confirm all added users
    assertEquals(3, service.getAllUsernames().size());
    assertTrue(service.getUserModel("garbage") != null);
    assertTrue(service.getUserModel("GaRbAgE") != null);
    assertTrue(service.getUserModel("GARBAGE") != null);
    // confirm reloaded test user
    newUser = service.getUserModel("test");
    assertEquals("testPassword", newUser.password);
    assertEquals(3, newUser.permissions.size());
    assertTrue(newUser.hasRepositoryPermission("repo1"));
    assertTrue(newUser.hasRepositoryPermission("repo2"));
    assertTrue(newUser.hasRepositoryPermission("sub/repo3"));
    // delete a repository role and confirm role removal from test user
    service.deleteRepositoryRole("repo2");
    UserModel testUser = service.getUserModel("test");
    assertEquals(2, testUser.permissions.size());
    // delete garbage user and confirm user count
    service.deleteUser("garbage");
    assertEquals(2, service.getAllUsernames().size());
    // rename repository and confirm role change for test user
    service.renameRepositoryRole("repo1", "newrepo1");
    testUser = service.getUserModel("test");
    assertTrue(testUser.hasRepositoryPermission("newrepo1"));
}
Also used : UserModel(com.gitblit.models.UserModel) TeamModel(com.gitblit.models.TeamModel)

Example 45 with TeamModel

use of com.gitblit.models.TeamModel in project gitblit by gitblit.

the class PermissionsTest method testTeamMember_VIEW_CREATE.

/**
 * VIEW_CREATE = VIEW access restriction, CREATE access permission
 */
@Test
public void testTeamMember_VIEW_CREATE() throws Exception {
    RepositoryModel repository = new RepositoryModel("myrepo.git", null, null, new Date());
    repository.authorizationControl = AuthorizationControl.NAMED;
    repository.accessRestriction = AccessRestrictionType.VIEW;
    TeamModel team = new TeamModel("test");
    team.setRepositoryPermission(repository.name, AccessPermission.CREATE);
    UserModel user = new UserModel("test");
    user.teams.add(team);
    assertTrue("team member CAN NOT view!", user.canView(repository));
    assertTrue("team member CAN NOT clone!", user.canClone(repository));
    assertTrue("team member CAN NOT push!", user.canPush(repository));
    assertTrue("team member CAN NOT create ref!", user.canCreateRef(repository));
    assertFalse("team member CAN delete ref!", user.canDeleteRef(repository));
    assertFalse("team member CAN rewind ref!", user.canRewindRef(repository));
    assertEquals("team member has wrong permission!", AccessPermission.CREATE, user.getRepositoryPermission(repository).permission);
}
Also used : UserModel(com.gitblit.models.UserModel) TeamModel(com.gitblit.models.TeamModel) RepositoryModel(com.gitblit.models.RepositoryModel) Date(java.util.Date) Test(org.junit.Test)

Aggregations

TeamModel (com.gitblit.models.TeamModel)111 RepositoryModel (com.gitblit.models.RepositoryModel)68 Test (org.junit.Test)67 Date (java.util.Date)62 UserModel (com.gitblit.models.UserModel)58 ArrayList (java.util.ArrayList)18 HashSet (java.util.HashSet)8 RegistrantAccessPermission (com.gitblit.models.RegistrantAccessPermission)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 GitBlitException (com.gitblit.GitBlitException)4 SearchResult (com.unboundid.ldap.sdk.SearchResult)4 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)4 File (java.io.File)4 IOException (java.io.IOException)4 AccessPermission (com.gitblit.Constants.AccessPermission)3 Repository (org.eclipse.jgit.lib.Repository)3 IUserService (com.gitblit.IUserService)2 LdapConnection (com.gitblit.ldap.LdapConnection)2 RefModel (com.gitblit.models.RefModel)2