Search in sources :

Example 1 with UserModel

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

the class ConfigUserService method deleteUser.

/**
	 * Delete the user object with the specified username
	 *
	 * @param username
	 * @return true if successful
	 */
@Override
public synchronized boolean deleteUser(String username) {
    try {
        // Read realm file
        read();
        UserModel model = users.remove(username.toLowerCase());
        if (model == null) {
            // user does not exist
            return false;
        }
        // remove user from team
        for (TeamModel team : model.teams) {
            TeamModel t = teams.get(team.name);
            if (t == null) {
                // new team
                team.removeUser(username);
                teams.put(team.name.toLowerCase(), team);
            } else {
                // existing team
                t.removeUser(username);
            }
        }
        write();
        return true;
    } catch (Throwable t) {
        logger.error(MessageFormat.format("Failed to delete user {0}!", username), t);
    }
    return false;
}
Also used : UserModel(com.gitblit.models.UserModel) TeamModel(com.gitblit.models.TeamModel)

Example 2 with UserModel

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

the class ConfigUserService method updateUserModels.

/**
	 * Updates/writes all specified user objects.
	 *
	 * @param models a list of user models
	 * @return true if update is successful
	 * @since 1.2.0
	 */
@Override
public synchronized boolean updateUserModels(Collection<UserModel> models) {
    try {
        read();
        for (UserModel model : models) {
            UserModel originalUser = users.remove(model.username.toLowerCase());
            users.put(model.username.toLowerCase(), model);
            // can have a null teams object
            if (model.teams != null) {
                Set<TeamModel> userTeams = new HashSet<TeamModel>();
                for (TeamModel team : model.teams) {
                    TeamModel t = teams.get(team.name.toLowerCase());
                    if (t == null) {
                        // new team
                        t = team;
                        teams.put(team.name.toLowerCase(), t);
                    }
                    // do not clobber existing team definition
                    // maybe because this is a federated user
                    t.addUser(model.username);
                    userTeams.add(t);
                }
                // replace Team-Models in users by new ones.
                model.teams.clear();
                model.teams.addAll(userTeams);
                // check for implicit team removal
                if (originalUser != null) {
                    for (TeamModel team : originalUser.teams) {
                        if (!model.isTeamMember(team.name)) {
                            team.removeUser(model.username);
                        }
                    }
                }
            }
        }
        write();
        return true;
    } catch (Throwable t) {
        logger.error(MessageFormat.format("Failed to update user {0} models!", models.size()), t);
    }
    return false;
}
Also used : UserModel(com.gitblit.models.UserModel) TeamModel(com.gitblit.models.TeamModel) HashSet(java.util.HashSet)

Example 3 with UserModel

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

the class ConfigUserService method write.

/**
	 * Writes the properties file.
	 *
	 * @throws IOException
	 */
private synchronized void write() throws IOException {
    // Write a temporary copy of the users file
    File realmFileCopy = new File(realmFile.getAbsolutePath() + ".tmp");
    StoredConfig config = new FileBasedConfig(realmFileCopy, FS.detect());
    // write users
    for (UserModel model : users.values()) {
        if (!StringUtils.isEmpty(model.password)) {
            config.setString(USER, model.username, PASSWORD, model.password);
        }
        if (!StringUtils.isEmpty(model.cookie)) {
            config.setString(USER, model.username, COOKIE, model.cookie);
        }
        if (!StringUtils.isEmpty(model.displayName)) {
            config.setString(USER, model.username, DISPLAYNAME, model.displayName);
        }
        if (!StringUtils.isEmpty(model.emailAddress)) {
            config.setString(USER, model.username, EMAILADDRESS, model.emailAddress);
        }
        if (model.accountType != null) {
            config.setString(USER, model.username, ACCOUNTTYPE, model.accountType.name());
        }
        if (!StringUtils.isEmpty(model.organizationalUnit)) {
            config.setString(USER, model.username, ORGANIZATIONALUNIT, model.organizationalUnit);
        }
        if (!StringUtils.isEmpty(model.organization)) {
            config.setString(USER, model.username, ORGANIZATION, model.organization);
        }
        if (!StringUtils.isEmpty(model.locality)) {
            config.setString(USER, model.username, LOCALITY, model.locality);
        }
        if (!StringUtils.isEmpty(model.stateProvince)) {
            config.setString(USER, model.username, STATEPROVINCE, model.stateProvince);
        }
        if (!StringUtils.isEmpty(model.countryCode)) {
            config.setString(USER, model.username, COUNTRYCODE, model.countryCode);
        }
        if (model.disabled) {
            config.setBoolean(USER, model.username, DISABLED, true);
        }
        if (model.getPreferences() != null) {
            Locale locale = model.getPreferences().getLocale();
            if (locale != null) {
                String val;
                if (StringUtils.isEmpty(locale.getCountry())) {
                    val = locale.getLanguage();
                } else {
                    val = locale.getLanguage() + "_" + locale.getCountry();
                }
                config.setString(USER, model.username, LOCALE, val);
            }
            config.setBoolean(USER, model.username, EMAILONMYTICKETCHANGES, model.getPreferences().isEmailMeOnMyTicketChanges());
            if (model.getPreferences().getTransport() != null) {
                config.setString(USER, model.username, TRANSPORT, model.getPreferences().getTransport().name());
            }
        }
        // user roles
        List<String> roles = new ArrayList<String>();
        if (model.canAdmin) {
            roles.add(Role.ADMIN.getRole());
        }
        if (model.canFork) {
            roles.add(Role.FORK.getRole());
        }
        if (model.canCreate) {
            roles.add(Role.CREATE.getRole());
        }
        if (model.excludeFromFederation) {
            roles.add(Role.NOT_FEDERATED.getRole());
        }
        if (roles.size() == 0) {
            // we do this to ensure that user record with no password
            // is written.  otherwise, StoredConfig optimizes that account
            // away. :(
            roles.add(Role.NONE.getRole());
        }
        config.setStringList(USER, model.username, ROLE, roles);
        // discrete repository permissions
        if (model.permissions != null && !model.canAdmin) {
            List<String> permissions = new ArrayList<String>();
            for (Map.Entry<String, AccessPermission> entry : model.permissions.entrySet()) {
                if (entry.getValue().exceeds(AccessPermission.NONE)) {
                    permissions.add(entry.getValue().asRole(entry.getKey()));
                }
            }
            config.setStringList(USER, model.username, REPOSITORY, permissions);
        }
        // user preferences
        if (model.getPreferences() != null) {
            List<String> starred = model.getPreferences().getStarredRepositories();
            if (starred.size() > 0) {
                config.setStringList(USER, model.username, STARRED, starred);
            }
        }
    }
    // write teams
    for (TeamModel model : teams.values()) {
        // team roles
        List<String> roles = new ArrayList<String>();
        if (model.canAdmin) {
            roles.add(Role.ADMIN.getRole());
        }
        if (model.canFork) {
            roles.add(Role.FORK.getRole());
        }
        if (model.canCreate) {
            roles.add(Role.CREATE.getRole());
        }
        if (roles.size() == 0) {
            // we do this to ensure that team record is written.
            // Otherwise, StoredConfig might optimizes that record away.
            roles.add(Role.NONE.getRole());
        }
        config.setStringList(TEAM, model.name, ROLE, roles);
        if (model.accountType != null) {
            config.setString(TEAM, model.name, ACCOUNTTYPE, model.accountType.name());
        }
        if (!model.canAdmin) {
            // write team permission for non-admin teams
            if (model.permissions == null) {
                // can have a null repositories object
                if (!ArrayUtils.isEmpty(model.repositories)) {
                    config.setStringList(TEAM, model.name, REPOSITORY, new ArrayList<String>(model.repositories));
                }
            } else {
                // discrete repository permissions
                List<String> permissions = new ArrayList<String>();
                for (Map.Entry<String, AccessPermission> entry : model.permissions.entrySet()) {
                    if (entry.getValue().exceeds(AccessPermission.NONE)) {
                        // code:repository (e.g. RW+:~james/myrepo.git
                        permissions.add(entry.getValue().asRole(entry.getKey()));
                    }
                }
                config.setStringList(TEAM, model.name, REPOSITORY, permissions);
            }
        }
        // can have a null users object
        if (!ArrayUtils.isEmpty(model.users)) {
            config.setStringList(TEAM, model.name, USER, new ArrayList<String>(model.users));
        }
        // TeamModel can have a null users object
        if (!ArrayUtils.isEmpty(model.mailingLists)) {
            config.setStringList(TEAM, model.name, MAILINGLIST, new ArrayList<String>(model.mailingLists));
        }
        // TeamModel can have a null preReceiveScripts object
        if (!ArrayUtils.isEmpty(model.preReceiveScripts)) {
            config.setStringList(TEAM, model.name, PRERECEIVE, model.preReceiveScripts);
        }
        // TeamModel can have a null postReceiveScripts object
        if (!ArrayUtils.isEmpty(model.postReceiveScripts)) {
            config.setStringList(TEAM, model.name, POSTRECEIVE, model.postReceiveScripts);
        }
    }
    config.save();
    // manually set the forceReload flag because not all JVMs support real
    // millisecond resolution of lastModified. (issue-55)
    forceReload = true;
    // the temporary copy to the original filename.
    if (realmFileCopy.exists() && realmFileCopy.length() > 0) {
        if (realmFile.exists()) {
            if (!realmFile.delete()) {
                throw new IOException(MessageFormat.format("Failed to delete {0}!", realmFile.getAbsolutePath()));
            }
        }
        if (!realmFileCopy.renameTo(realmFile)) {
            throw new IOException(MessageFormat.format("Failed to rename {0} to {1}!", realmFileCopy.getAbsolutePath(), realmFile.getAbsolutePath()));
        }
    } else {
        throw new IOException(MessageFormat.format("Failed to save {0}!", realmFileCopy.getAbsolutePath()));
    }
}
Also used : Locale(java.util.Locale) ArrayList(java.util.ArrayList) AccessPermission(com.gitblit.Constants.AccessPermission) IOException(java.io.IOException) StoredConfig(org.eclipse.jgit.lib.StoredConfig) UserModel(com.gitblit.models.UserModel) TeamModel(com.gitblit.models.TeamModel) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) File(java.io.File) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 4 with UserModel

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

the class ConfigUserService method deleteRepositoryRole.

/**
	 * Removes a repository role from all users.
	 *
	 * @param role
	 * @return true if successful
	 */
@Override
public synchronized boolean deleteRepositoryRole(String role) {
    try {
        read();
        // identify users which require role rename
        for (UserModel user : users.values()) {
            user.removeRepositoryPermission(role);
        }
        // identify teams which require role rename
        for (TeamModel team : teams.values()) {
            team.removeRepositoryPermission(role);
        }
        // persist changes
        write();
        return true;
    } catch (Throwable t) {
        logger.error(MessageFormat.format("Failed to delete role {0}!", role), t);
    }
    return false;
}
Also used : UserModel(com.gitblit.models.UserModel) TeamModel(com.gitblit.models.TeamModel)

Example 5 with UserModel

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

the class ConfigUserService method updateUserModel.

/**
	 * Updates/writes and replaces a complete user object keyed by username.
	 * This method allows for renaming a user.
	 *
	 * @param username
	 *            the old username
	 * @param model
	 *            the user object to use for username
	 * @return true if update is successful
	 */
@Override
public synchronized boolean updateUserModel(String username, UserModel model) {
    UserModel originalUser = null;
    try {
        if (!model.isLocalAccount()) {
            // do not persist password
            model.password = Constants.EXTERNAL_ACCOUNT;
        }
        read();
        originalUser = users.remove(username.toLowerCase());
        if (originalUser != null) {
            cookies.remove(originalUser.cookie);
        }
        users.put(model.username.toLowerCase(), model);
        // can have a null teams object
        if (model.teams != null) {
            for (TeamModel team : model.teams) {
                TeamModel t = teams.get(team.name.toLowerCase());
                if (t == null) {
                    // new team
                    team.addUser(username);
                    teams.put(team.name.toLowerCase(), team);
                } else {
                    // do not clobber existing team definition
                    // maybe because this is a federated user
                    t.removeUser(username);
                    t.addUser(model.username);
                }
            }
            // check for implicit team removal
            if (originalUser != null) {
                for (TeamModel team : originalUser.teams) {
                    if (!model.isTeamMember(team.name)) {
                        team.removeUser(username);
                    }
                }
            }
        }
        write();
        return true;
    } catch (Throwable t) {
        if (originalUser != null) {
            // restore original user
            users.put(originalUser.username.toLowerCase(), originalUser);
        } else {
            // drop attempted add
            users.remove(model.username.toLowerCase());
        }
        logger.error(MessageFormat.format("Failed to update user model {0}!", model.username), t);
    }
    return false;
}
Also used : UserModel(com.gitblit.models.UserModel) TeamModel(com.gitblit.models.TeamModel)

Aggregations

UserModel (com.gitblit.models.UserModel)230 Test (org.junit.Test)127 RepositoryModel (com.gitblit.models.RepositoryModel)116 Date (java.util.Date)88 TeamModel (com.gitblit.models.TeamModel)58 ArrayList (java.util.ArrayList)24 IOException (java.io.IOException)16 File (java.io.File)15 HashMap (java.util.HashMap)11 Label (org.apache.wicket.markup.html.basic.Label)10 RevCommit (org.eclipse.jgit.revwalk.RevCommit)10 Repository (org.eclipse.jgit.lib.Repository)9 HashSet (java.util.HashSet)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 ProjectModel (com.gitblit.models.ProjectModel)7 Map (java.util.Map)7 GitBlitException (com.gitblit.GitBlitException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 List (java.util.List)6 Fragment (org.apache.wicket.markup.html.panel.Fragment)5