Search in sources :

Example 16 with FileBasedConfig

use of org.eclipse.jgit.storage.file.FileBasedConfig in project gitblit by gitblit.

the class ConfigUserService method read.

/**
	 * Reads the realm file and rebuilds the in-memory lookup tables.
	 */
protected synchronized void read() {
    if (realmFile.exists() && (forceReload || (realmFile.lastModified() != lastModified))) {
        forceReload = false;
        lastModified = realmFile.lastModified();
        users.clear();
        cookies.clear();
        teams.clear();
        try {
            StoredConfig config = new FileBasedConfig(realmFile, FS.detect());
            config.load();
            Set<String> usernames = config.getSubsections(USER);
            for (String username : usernames) {
                UserModel user = new UserModel(username.toLowerCase());
                user.password = config.getString(USER, username, PASSWORD);
                user.displayName = config.getString(USER, username, DISPLAYNAME);
                user.emailAddress = config.getString(USER, username, EMAILADDRESS);
                user.accountType = AccountType.fromString(config.getString(USER, username, ACCOUNTTYPE));
                user.disabled = config.getBoolean(USER, username, DISABLED, false);
                user.organizationalUnit = config.getString(USER, username, ORGANIZATIONALUNIT);
                user.organization = config.getString(USER, username, ORGANIZATION);
                user.locality = config.getString(USER, username, LOCALITY);
                user.stateProvince = config.getString(USER, username, STATEPROVINCE);
                user.countryCode = config.getString(USER, username, COUNTRYCODE);
                user.cookie = config.getString(USER, username, COOKIE);
                if (StringUtils.isEmpty(user.cookie) && !StringUtils.isEmpty(user.password)) {
                    user.cookie = user.createCookie();
                }
                // preferences
                user.getPreferences().setLocale(config.getString(USER, username, LOCALE));
                user.getPreferences().setEmailMeOnMyTicketChanges(config.getBoolean(USER, username, EMAILONMYTICKETCHANGES, true));
                user.getPreferences().setTransport(Transport.fromString(config.getString(USER, username, TRANSPORT)));
                // user roles
                Set<String> roles = new HashSet<String>(Arrays.asList(config.getStringList(USER, username, ROLE)));
                user.canAdmin = roles.contains(Role.ADMIN.getRole());
                user.canFork = roles.contains(Role.FORK.getRole());
                user.canCreate = roles.contains(Role.CREATE.getRole());
                user.excludeFromFederation = roles.contains(Role.NOT_FEDERATED.getRole());
                // repository memberships
                if (!user.canAdmin) {
                    // non-admin, read permissions
                    Set<String> repositories = new HashSet<String>(Arrays.asList(config.getStringList(USER, username, REPOSITORY)));
                    for (String repository : repositories) {
                        user.addRepositoryPermission(repository);
                    }
                }
                // starred repositories
                Set<String> starred = new HashSet<String>(Arrays.asList(config.getStringList(USER, username, STARRED)));
                for (String repository : starred) {
                    UserRepositoryPreferences prefs = user.getPreferences().getRepositoryPreferences(repository);
                    prefs.starred = true;
                }
                // update cache
                users.put(user.username, user);
                if (!StringUtils.isEmpty(user.cookie)) {
                    cookies.put(user.cookie, user);
                }
            }
            // load the teams
            Set<String> teamnames = config.getSubsections(TEAM);
            for (String teamname : teamnames) {
                TeamModel team = new TeamModel(teamname);
                Set<String> roles = new HashSet<String>(Arrays.asList(config.getStringList(TEAM, teamname, ROLE)));
                team.canAdmin = roles.contains(Role.ADMIN.getRole());
                team.canFork = roles.contains(Role.FORK.getRole());
                team.canCreate = roles.contains(Role.CREATE.getRole());
                team.accountType = AccountType.fromString(config.getString(TEAM, teamname, ACCOUNTTYPE));
                if (!team.canAdmin) {
                    // non-admin team, read permissions
                    team.addRepositoryPermissions(Arrays.asList(config.getStringList(TEAM, teamname, REPOSITORY)));
                }
                team.addUsers(Arrays.asList(config.getStringList(TEAM, teamname, USER)));
                team.addMailingLists(Arrays.asList(config.getStringList(TEAM, teamname, MAILINGLIST)));
                team.preReceiveScripts.addAll(Arrays.asList(config.getStringList(TEAM, teamname, PRERECEIVE)));
                team.postReceiveScripts.addAll(Arrays.asList(config.getStringList(TEAM, teamname, POSTRECEIVE)));
                teams.put(team.name.toLowerCase(), team);
                // set the teams on the users
                for (String user : team.users) {
                    UserModel model = users.get(user);
                    if (model != null) {
                        model.teams.add(team);
                    }
                }
            }
        } catch (Exception e) {
            logger.error(MessageFormat.format("Failed to read {0}", realmFile), e);
        }
    }
}
Also used : StoredConfig(org.eclipse.jgit.lib.StoredConfig) UserModel(com.gitblit.models.UserModel) TeamModel(com.gitblit.models.TeamModel) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) UserRepositoryPreferences(com.gitblit.models.UserRepositoryPreferences) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 17 with FileBasedConfig

use of org.eclipse.jgit.storage.file.FileBasedConfig in project gitblit by gitblit.

the class GitblitAuthority method setMetadataDefaults.

private void setMetadataDefaults(X509Metadata metadata) {
    metadata.serverHostname = gitblitSettings.getString(Keys.web.siteName, Constants.NAME);
    if (StringUtils.isEmpty(metadata.serverHostname)) {
        metadata.serverHostname = Constants.NAME;
    }
    // set default values from config file
    File certificatesConfigFile = new File(folder, X509Utils.CA_CONFIG);
    FileBasedConfig config = new FileBasedConfig(certificatesConfigFile, FS.detect());
    if (certificatesConfigFile.exists()) {
        try {
            config.load();
        } catch (Exception e) {
            Utils.showException(GitblitAuthority.this, e);
        }
        NewCertificateConfig certificateConfig = NewCertificateConfig.KEY.parse(config);
        certificateConfig.update(metadata);
    }
}
Also used : FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) File(java.io.File) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) IOException(java.io.IOException)

Example 18 with FileBasedConfig

use of org.eclipse.jgit.storage.file.FileBasedConfig in project gitblit by gitblit.

the class GitblitAuthority method getConfig.

private StoredConfig getConfig() throws IOException, ConfigInvalidException {
    File configFile = new File(folder, X509Utils.CA_CONFIG);
    FileBasedConfig config = new FileBasedConfig(configFile, FS.detect());
    config.load();
    return config;
}
Also used : FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) File(java.io.File)

Example 19 with FileBasedConfig

use of org.eclipse.jgit.storage.file.FileBasedConfig in project gitblit by gitblit.

the class GitblitAuthority method load.

private void load(File folder) {
    this.folder = folder;
    this.userService = loadUsers(folder);
    System.out.println(Constants.baseFolder$ + " set to " + folder);
    if (userService == null) {
        JOptionPane.showMessageDialog(this, MessageFormat.format("Sorry, {0} doesn't look like a Gitblit GO installation.", folder));
    } else {
        // build empty certificate model for all users
        Map<String, UserCertificateModel> map = new HashMap<String, UserCertificateModel>();
        for (String user : userService.getAllUsernames()) {
            UserModel model = userService.getUserModel(user);
            UserCertificateModel ucm = new UserCertificateModel(model);
            map.put(user, ucm);
        }
        File certificatesConfigFile = new File(folder, X509Utils.CA_CONFIG);
        FileBasedConfig config = new FileBasedConfig(certificatesConfigFile, FS.detect());
        if (certificatesConfigFile.exists()) {
            try {
                config.load();
                // replace user certificate model with actual data
                List<UserCertificateModel> list = UserCertificateConfig.KEY.parse(config).list;
                for (UserCertificateModel ucm : list) {
                    ucm.user = userService.getUserModel(ucm.user.username);
                    map.put(ucm.user.username, ucm);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ConfigInvalidException e) {
                e.printStackTrace();
            }
        }
        tableModel.list = new ArrayList<UserCertificateModel>(map.values());
        Collections.sort(tableModel.list);
        tableModel.fireTableDataChanged();
        Utils.packColumns(table, Utils.MARGIN);
        File caKeystore = new File(folder, X509Utils.CA_KEY_STORE);
        if (!caKeystore.exists()) {
            if (!X509Utils.unlimitedStrength) {
                // prompt to confirm user understands JCE Standard Strength encryption
                int res = JOptionPane.showConfirmDialog(GitblitAuthority.this, Translation.get("gb.jceWarning"), Translation.get("gb.warning"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
                if (res != JOptionPane.YES_OPTION) {
                    if (Desktop.isDesktopSupported()) {
                        if (Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
                            try {
                                Desktop.getDesktop().browse(URI.create("http://www.oracle.com/technetwork/java/javase/downloads/index.html"));
                            } catch (IOException e) {
                            }
                        }
                    }
                    System.exit(1);
                }
            }
            // show certificate defaults dialog
            certificateDefaultsButton.doClick();
            // create "localhost" ssl certificate
            prepareX509Infrastructure();
        }
    }
}
Also used : UserModel(com.gitblit.models.UserModel) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) HashMap(java.util.HashMap) IOException(java.io.IOException) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) File(java.io.File) Point(java.awt.Point)

Example 20 with FileBasedConfig

use of org.eclipse.jgit.storage.file.FileBasedConfig in project gitblit by gitblit.

the class GitblitAuthority method updateAuthorityConfig.

private void updateAuthorityConfig(UserCertificateModel ucm) {
    File certificatesConfigFile = new File(folder, X509Utils.CA_CONFIG);
    FileBasedConfig config = new FileBasedConfig(certificatesConfigFile, FS.detect());
    if (certificatesConfigFile.exists()) {
        try {
            config.load();
        } catch (Exception e) {
            Utils.showException(GitblitAuthority.this, e);
        }
    }
    ucm.update(config);
    try {
        config.save();
    } catch (Exception e) {
        Utils.showException(GitblitAuthority.this, e);
    }
}
Also used : FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) File(java.io.File) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) IOException(java.io.IOException)

Aggregations

FileBasedConfig (org.eclipse.jgit.storage.file.FileBasedConfig)23 IOException (java.io.IOException)12 File (java.io.File)10 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)8 UserModel (com.gitblit.models.UserModel)4 HashMap (java.util.HashMap)4 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ObjectId (org.eclipse.jgit.lib.ObjectId)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 RefModel (com.gitblit.models.RefModel)2 TeamModel (com.gitblit.models.TeamModel)2 X509Metadata (com.gitblit.utils.X509Utils.X509Metadata)2 Date (java.util.Date)2 List (java.util.List)2 Map (java.util.Map)2 TreeSet (java.util.TreeSet)2 IndexWriter (org.apache.lucene.index.IndexWriter)2 Config (org.eclipse.jgit.lib.Config)2