Search in sources :

Example 76 with TeamModel

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

the class AuthenticationProvider method updateTeam.

protected void updateTeam(TeamModel teamModel) {
    final TeamModel teamLocalDB = userManager.getTeamModel(teamModel.name);
    // Establish the checksum of the current version of the team
    final BigInteger teamCurrentCheck = DeepCopier.checksum(teamModel);
    // Establish the checksum of the stored version of the team
    final BigInteger teamLocalDBcheck = DeepCopier.checksum(teamLocalDB);
    // Compare the checksums
    if (!teamCurrentCheck.equals(teamLocalDBcheck)) {
        // If mismatch, save the new instance.
        userManager.updateTeamModel(teamModel);
    }
}
Also used : TeamModel(com.gitblit.models.TeamModel) BigInteger(java.math.BigInteger)

Example 77 with TeamModel

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

the class HttpHeaderAuthProvider method authenticate.

@Override
public UserModel authenticate(HttpServletRequest httpRequest) {
    // Try to authenticate using custom HTTP header if user header is defined
    if (!StringUtils.isEmpty(userHeaderName)) {
        String headerUserName = httpRequest.getHeader(userHeaderName);
        if (!StringUtils.isEmpty(headerUserName) && !userManager.isInternalAccount(headerUserName)) {
            // We have a user, try to load team names as well
            Set<TeamModel> userTeams = new HashSet<>();
            if (!StringUtils.isEmpty(teamHeaderName)) {
                String headerTeamValue = httpRequest.getHeader(teamHeaderName);
                if (!StringUtils.isEmpty(headerTeamValue)) {
                    String[] headerTeamNames = headerTeamValue.split(teamHeaderSeparator);
                    for (String teamName : headerTeamNames) {
                        teamName = teamName.trim();
                        if (!StringUtils.isEmpty(teamName)) {
                            TeamModel team = userManager.getTeamModel(teamName);
                            if (null == team) {
                                // Create teams here so they can marked with the correct AccountType
                                team = new TeamModel(teamName);
                                team.accountType = AccountType.HTTPHEADER;
                                updateTeam(team);
                            }
                            userTeams.add(team);
                        }
                    }
                }
            }
            UserModel user = userManager.getUserModel(headerUserName);
            if (user != null) {
                // If team header is provided in request, reset all team memberships, even if resetting to empty set
                if (!StringUtils.isEmpty(teamHeaderName)) {
                    user.teams.clear();
                    user.teams.addAll(userTeams);
                }
                updateUser(user);
                return user;
            } else if (settings.getBoolean(Keys.realm.httpheader.autoCreateAccounts, false)) {
                // auto-create user from HTTP header
                user = new UserModel(headerUserName.toLowerCase());
                user.displayName = headerUserName;
                user.password = Constants.EXTERNAL_ACCOUNT;
                user.accountType = AccountType.HTTPHEADER;
                user.teams.addAll(userTeams);
                updateUser(user);
                return user;
            }
        }
    }
    return null;
}
Also used : UserModel(com.gitblit.models.UserModel) TeamModel(com.gitblit.models.TeamModel) HashSet(java.util.HashSet)

Example 78 with TeamModel

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

the class LdapAuthProvider method authenticate.

@Override
public UserModel authenticate(String username, char[] password) {
    String simpleUsername = getSimpleUsername(username);
    LdapConnection ldapConnection = new LdapConnection(settings);
    if (ldapConnection.connect()) {
        // Try to bind either to the "manager" account,
        // or directly to the DN of the user logging in, if realm.ldap.bindpattern is configured.
        String passwd = new String(password);
        BindResult bindResult = null;
        String bindPattern = settings.getString(Keys.realm.ldap.bindpattern, "");
        if (!StringUtils.isEmpty(bindPattern)) {
            bindResult = ldapConnection.bind(bindPattern, simpleUsername, passwd);
        } else {
            bindResult = ldapConnection.bind();
        }
        if (bindResult == null) {
            ldapConnection.close();
            return null;
        }
        try {
            // Find the logging in user's DN
            SearchResult result = ldapConnection.searchUser(simpleUsername);
            if (result != null && result.getEntryCount() == 1) {
                SearchResultEntry loggingInUser = result.getSearchEntries().get(0);
                String loggingInUserDN = loggingInUser.getDN();
                if (ldapConnection.isAuthenticated(loggingInUserDN, passwd)) {
                    logger.debug("LDAP authenticated: " + username);
                    UserModel user = null;
                    synchronized (this) {
                        user = userManager.getUserModel(simpleUsername);
                        if (user == null) {
                            // create user object for new authenticated user
                            user = new UserModel(simpleUsername);
                        }
                        // create a user cookie
                        setCookie(user);
                        if (!supportsTeamMembershipChanges()) {
                            getTeamsFromLdap(ldapConnection, simpleUsername, loggingInUser, user);
                        }
                        // Get User Attributes
                        setUserAttributes(user, loggingInUser);
                        // Push the ldap looked up values to backing file
                        updateUser(user);
                        if (!supportsTeamMembershipChanges()) {
                            for (TeamModel userTeam : user.teams) {
                                // Is this an administrative team?
                                setAdminAttribute(userTeam);
                                updateTeam(userTeam);
                            }
                        }
                    }
                    return user;
                }
            }
        } finally {
            ldapConnection.close();
        }
    }
    return null;
}
Also used : UserModel(com.gitblit.models.UserModel) TeamModel(com.gitblit.models.TeamModel) BindResult(com.unboundid.ldap.sdk.BindResult) SearchResult(com.unboundid.ldap.sdk.SearchResult) LdapConnection(com.gitblit.ldap.LdapConnection) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 79 with TeamModel

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

the class LdapAuthProvider method getTeamsFromLdap.

private void getTeamsFromLdap(LdapConnection ldapConnection, String simpleUsername, SearchResultEntry loggingInUser, UserModel user) {
    String loggingInUserDN = loggingInUser.getDN();
    // Clear the users team memberships - we're going to get them from LDAP
    user.teams.clear();
    String groupBase = settings.getString(Keys.realm.ldap.groupBase, "");
    String groupMemberPattern = settings.getString(Keys.realm.ldap.groupMemberPattern, "(&(objectClass=group)(member=${dn}))");
    groupMemberPattern = StringUtils.replace(groupMemberPattern, "${dn}", LdapConnection.escapeLDAPSearchFilter(loggingInUserDN));
    groupMemberPattern = StringUtils.replace(groupMemberPattern, "${username}", LdapConnection.escapeLDAPSearchFilter(simpleUsername));
    // Fill in attributes into groupMemberPattern
    for (Attribute userAttribute : loggingInUser.getAttributes()) {
        groupMemberPattern = StringUtils.replace(groupMemberPattern, "${" + userAttribute.getName() + "}", LdapConnection.escapeLDAPSearchFilter(userAttribute.getValue()));
    }
    SearchResult teamMembershipResult = searchTeamsInLdap(ldapConnection, groupBase, true, groupMemberPattern, Arrays.asList("cn"));
    if (teamMembershipResult != null && teamMembershipResult.getEntryCount() > 0) {
        for (int i = 0; i < teamMembershipResult.getEntryCount(); i++) {
            SearchResultEntry teamEntry = teamMembershipResult.getSearchEntries().get(i);
            String teamName = teamEntry.getAttribute("cn").getValue();
            TeamModel teamModel = userManager.getTeamModel(teamName);
            if (teamModel == null) {
                teamModel = createTeamFromLdap(teamEntry);
            }
            user.teams.add(teamModel);
            teamModel.addUser(user.getName());
        }
    }
}
Also used : TeamModel(com.gitblit.models.TeamModel) Attribute(com.unboundid.ldap.sdk.Attribute) SearchResult(com.unboundid.ldap.sdk.SearchResult) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 80 with TeamModel

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

the class LdapAuthProvider method getEmptyTeamsFromLdap.

private void getEmptyTeamsFromLdap(LdapConnection ldapConnection) {
    logger.info("Start fetching empty teams from ldap.");
    String groupBase = settings.getString(Keys.realm.ldap.groupBase, "");
    String groupMemberPattern = settings.getString(Keys.realm.ldap.groupEmptyMemberPattern, "(&(objectClass=group)(!(member=*)))");
    SearchResult teamMembershipResult = searchTeamsInLdap(ldapConnection, groupBase, true, groupMemberPattern, null);
    if (teamMembershipResult != null && teamMembershipResult.getEntryCount() > 0) {
        for (int i = 0; i < teamMembershipResult.getEntryCount(); i++) {
            SearchResultEntry teamEntry = teamMembershipResult.getSearchEntries().get(i);
            if (!teamEntry.hasAttribute("member")) {
                String teamName = teamEntry.getAttribute("cn").getValue();
                TeamModel teamModel = userManager.getTeamModel(teamName);
                if (teamModel == null) {
                    teamModel = createTeamFromLdap(teamEntry);
                    setAdminAttribute(teamModel);
                    userManager.updateTeamModel(teamModel);
                }
            }
        }
    }
    logger.info("Finished fetching empty teams from ldap.");
}
Also used : TeamModel(com.gitblit.models.TeamModel) SearchResult(com.unboundid.ldap.sdk.SearchResult) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Aggregations

TeamModel (com.gitblit.models.TeamModel)109 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)6 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 List (java.util.List)3 Repository (org.eclipse.jgit.lib.Repository)3 StoredConfig (org.eclipse.jgit.lib.StoredConfig)3 IUserService (com.gitblit.IUserService)2