Search in sources :

Example 1 with Transport

use of com.gitblit.Constants.Transport in project gitblit by gitblit.

the class UserPage method addPreferences.

private void addPreferences(UserModel user) {
    // add preferences
    Form<Void> prefs = new Form<Void>("prefsForm");
    List<Language> languages = getLanguages();
    Locale locale = user.getPreferences().getLocale();
    if (locale == null) {
        // user has not specified language preference
        // try server default preference
        String lc = app().settings().getString(Keys.web.forceDefaultLocale, null);
        if (StringUtils.isEmpty(lc)) {
            // server default language is not configured
            // try browser preference
            Locale sessionLocale = GitBlitWebSession.get().getLocale();
            if (sessionLocale != null) {
                locale = sessionLocale;
            }
        } else {
        }
    }
    Language preferredLanguage = null;
    if (locale != null) {
        String localeCode = locale.getLanguage();
        if (!StringUtils.isEmpty(locale.getCountry())) {
            localeCode += "_" + locale.getCountry();
        }
        for (Language language : languages) {
            if (language.code.equals(localeCode)) {
                // language_COUNTRY match
                preferredLanguage = language;
            } else if (preferredLanguage != null && language.code.startsWith(locale.getLanguage())) {
                // language match
                preferredLanguage = language;
            }
        }
    }
    final IModel<String> displayName = Model.of(user.getDisplayName());
    final IModel<String> emailAddress = Model.of(user.emailAddress == null ? "" : user.emailAddress);
    final IModel<Language> language = Model.of(preferredLanguage);
    final IModel<Boolean> emailMeOnMyTicketChanges = Model.of(user.getPreferences().isEmailMeOnMyTicketChanges());
    final IModel<Transport> transport = Model.of(user.getPreferences().getTransport());
    prefs.add(new TextOption("displayName", getString("gb.displayName"), getString("gb.displayNameDescription"), displayName).setVisible(app().authentication().supportsDisplayNameChanges(user)));
    prefs.add(new TextOption("emailAddress", getString("gb.emailAddress"), getString("gb.emailAddressDescription"), emailAddress).setVisible(app().authentication().supportsEmailAddressChanges(user)));
    prefs.add(new ChoiceOption<Language>("language", getString("gb.languagePreference"), getString("gb.languagePreferenceDescription"), language, languages));
    prefs.add(new BooleanOption("emailMeOnMyTicketChanges", getString("gb.emailMeOnMyTicketChanges"), getString("gb.emailMeOnMyTicketChangesDescription"), emailMeOnMyTicketChanges).setVisible(app().notifier().isSendingMail()));
    List<Transport> availableTransports = new ArrayList<>();
    if (app().services().isServingSSH()) {
        availableTransports.add(Transport.SSH);
    }
    if (app().services().isServingHTTP()) {
        availableTransports.add(Transport.HTTP);
    }
    if (app().services().isServingHTTPS()) {
        availableTransports.add(Transport.HTTPS);
    }
    if (app().services().isServingGIT()) {
        availableTransports.add(Transport.GIT);
    }
    prefs.add(new ChoiceOption<Transport>("transport", getString("gb.transportPreference"), getString("gb.transportPreferenceDescription"), transport, availableTransports));
    prefs.add(new AjaxButton("save") {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
            UserModel user = GitBlitWebSession.get().getUser();
            user.displayName = displayName.getObject();
            user.emailAddress = emailAddress.getObject();
            Language lang = language.getObject();
            if (lang != null) {
                user.getPreferences().setLocale(lang.code);
            }
            user.getPreferences().setEmailMeOnMyTicketChanges(emailMeOnMyTicketChanges.getObject());
            user.getPreferences().setTransport(transport.getObject());
            try {
                app().gitblit().reviseUser(user.username, user);
                setRedirect(true);
                setResponsePage(UserPage.class, WicketUtils.newUsernameParameter(user.username));
            } catch (GitBlitException e) {
            // logger.error("Failed to update user " + user.username, e);
            // error(getString("gb.failedToUpdateUser"), false);
            }
        }
    });
    // add the preferences tab
    add(new Fragment("preferencesLink", "preferencesLinkFragment", this).setRenderBodyOnly(true));
    Fragment fragment = new Fragment("preferencesTab", "preferencesTabFragment", this);
    fragment.add(prefs);
    add(fragment.setRenderBodyOnly(true));
}
Also used : Locale(java.util.Locale) Form(org.apache.wicket.markup.html.form.Form) ArrayList(java.util.ArrayList) TextOption(com.gitblit.wicket.panels.TextOption) GitBlitException(com.gitblit.GitBlitException) Fragment(org.apache.wicket.markup.html.panel.Fragment) UserModel(com.gitblit.models.UserModel) AjaxButton(org.apache.wicket.ajax.markup.html.form.AjaxButton) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) Transport(com.gitblit.Constants.Transport) BooleanOption(com.gitblit.wicket.panels.BooleanOption)

Example 2 with Transport

use of com.gitblit.Constants.Transport in project gitblit by gitblit.

the class GitblitReceivePackFactory method create.

@Override
public ReceivePack create(X req, Repository db) throws ServiceNotEnabledException, ServiceNotAuthorizedException {
    UserModel user = UserModel.ANONYMOUS;
    String repositoryName = "";
    String origin = "";
    String gitblitUrl = "";
    int timeout = 0;
    Transport transport = null;
    if (req instanceof HttpServletRequest) {
        // http/https request may or may not be authenticated
        HttpServletRequest client = (HttpServletRequest) req;
        repositoryName = client.getAttribute("gitblitRepositoryName").toString();
        origin = client.getRemoteHost();
        gitblitUrl = HttpUtils.getGitblitURL(client);
        // determine pushing user
        String username = client.getRemoteUser();
        if (!StringUtils.isEmpty(username)) {
            UserModel u = gitblit.getUserModel(username);
            if (u != null) {
                user = u;
            }
        }
        // determine the transport
        if ("http".equals(client.getScheme())) {
            transport = Transport.HTTP;
        } else if ("https".equals(client.getScheme())) {
            transport = Transport.HTTPS;
        }
    } else if (req instanceof GitDaemonClient) {
        // git daemon request is always anonymous
        GitDaemonClient client = (GitDaemonClient) req;
        repositoryName = client.getRepositoryName();
        origin = client.getRemoteAddress().getHostAddress();
        // set timeout from Git daemon
        timeout = client.getDaemon().getTimeout();
        transport = Transport.GIT;
    } else if (req instanceof SshDaemonClient) {
        // SSH request is always authenticated
        SshDaemonClient client = (SshDaemonClient) req;
        repositoryName = client.getRepositoryName();
        origin = client.getRemoteAddress().toString();
        user = client.getUser();
        transport = Transport.SSH;
    }
    if (!acceptPush(transport)) {
        throw new ServiceNotAuthorizedException();
    }
    boolean allowAnonymousPushes = settings.getBoolean(Keys.git.allowAnonymousPushes, false);
    if (!allowAnonymousPushes && UserModel.ANONYMOUS.equals(user)) {
        // prohibit anonymous pushes
        throw new ServiceNotEnabledException();
    }
    String url = settings.getString(Keys.web.canonicalUrl, null);
    if (StringUtils.isEmpty(url)) {
        url = gitblitUrl;
    }
    final RepositoryModel repository = gitblit.getRepositoryModel(repositoryName);
    // Determine which receive pack to use for pushes
    final GitblitReceivePack rp;
    if (gitblit.getTicketService().isAcceptingNewPatchsets(repository)) {
        rp = new PatchsetReceivePack(gitblit, db, repository, user);
    } else {
        rp = new GitblitReceivePack(gitblit, db, repository, user);
    }
    rp.setGitblitUrl(url);
    rp.setRefLogIdent(new PersonIdent(user.username, user.username + "@" + origin));
    rp.setTimeout(timeout);
    return rp;
}
Also used : SshDaemonClient(com.gitblit.transport.ssh.SshDaemonClient) RepositoryModel(com.gitblit.models.RepositoryModel) GitDaemonClient(com.gitblit.transport.git.GitDaemonClient) UserModel(com.gitblit.models.UserModel) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServiceNotEnabledException(org.eclipse.jgit.transport.resolver.ServiceNotEnabledException) PersonIdent(org.eclipse.jgit.lib.PersonIdent) Transport(com.gitblit.Constants.Transport) ServiceNotAuthorizedException(org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException)

Example 3 with Transport

use of com.gitblit.Constants.Transport in project gitblit by gitblit.

the class GitblitReceivePackFactory method acceptPush.

protected boolean acceptPush(Transport byTransport) {
    if (byTransport == null) {
        logger.info("Unknown transport, push rejected!");
        return false;
    }
    Set<Transport> transports = new HashSet<Transport>();
    for (String value : gitblit.getSettings().getStrings(Keys.git.acceptedPushTransports)) {
        Transport transport = Transport.fromString(value);
        if (transport == null) {
            logger.info(String.format("Ignoring unknown registered transport %s", value));
            continue;
        }
        transports.add(transport);
    }
    if (transports.isEmpty()) {
        // no transports are explicitly specified, all are acceptable
        return true;
    }
    // verify that the transport is permitted
    return transports.contains(byTransport);
}
Also used : Transport(com.gitblit.Constants.Transport) HashSet(java.util.HashSet)

Example 4 with Transport

use of com.gitblit.Constants.Transport in project gitblit by gitblit.

the class ServicesManager method acceptsPush.

@Override
public boolean acceptsPush(Transport byTransport) {
    if (byTransport == null) {
        logger.info("Unknown transport, push rejected!");
        return false;
    }
    Set<Transport> transports = new HashSet<Transport>();
    for (String value : settings.getStrings(Keys.git.acceptedPushTransports)) {
        Transport transport = Transport.fromString(value);
        if (transport == null) {
            logger.info(String.format("Ignoring unknown registered transport %s", value));
            continue;
        }
        transports.add(transport);
    }
    if (transports.isEmpty()) {
        // no transports are explicitly specified, all are acceptable
        return true;
    }
    // verify that the transport is permitted
    return transports.contains(byTransport);
}
Also used : Transport(com.gitblit.Constants.Transport) HashSet(java.util.HashSet)

Example 5 with Transport

use of com.gitblit.Constants.Transport in project gitblit by gitblit.

the class ServicesManager method getRepositoryUrls.

/**
	 * Returns a list of repository URLs and the user access permission.
	 *
	 * @param request
	 * @param user
	 * @param repository
	 * @return a list of repository urls
	 */
@Override
public List<RepositoryUrl> getRepositoryUrls(HttpServletRequest request, UserModel user, RepositoryModel repository) {
    if (user == null) {
        user = UserModel.ANONYMOUS;
    }
    String username = StringUtils.encodeUsername(UserModel.ANONYMOUS.equals(user) ? "" : user.username);
    List<RepositoryUrl> list = new ArrayList<RepositoryUrl>();
    // http/https url
    if (settings.getBoolean(Keys.git.enableGitServlet, true) && settings.getBoolean(Keys.web.showHttpServletUrls, true)) {
        AccessPermission permission = user.getRepositoryPermission(repository).permission;
        if (permission.exceeds(AccessPermission.NONE)) {
            String repoUrl = getRepositoryUrl(request, username, repository);
            Transport transport = Transport.fromUrl(repoUrl);
            if (permission.atLeast(AccessPermission.PUSH) && !acceptsPush(transport)) {
                // downgrade the repo permission for this transport
                // because it is not an acceptable PUSH transport
                permission = AccessPermission.CLONE;
            }
            list.add(new RepositoryUrl(repoUrl, permission));
        }
    }
    // ssh daemon url
    String sshDaemonUrl = getSshDaemonUrl(request, user, repository);
    if (!StringUtils.isEmpty(sshDaemonUrl) && settings.getBoolean(Keys.web.showSshDaemonUrls, true)) {
        AccessPermission permission = user.getRepositoryPermission(repository).permission;
        if (permission.exceeds(AccessPermission.NONE)) {
            if (permission.atLeast(AccessPermission.PUSH) && !acceptsPush(Transport.SSH)) {
                // downgrade the repo permission for this transport
                // because it is not an acceptable PUSH transport
                permission = AccessPermission.CLONE;
            }
            list.add(new RepositoryUrl(sshDaemonUrl, permission));
        }
    }
    // git daemon url
    String gitDaemonUrl = getGitDaemonUrl(request, user, repository);
    if (!StringUtils.isEmpty(gitDaemonUrl) && settings.getBoolean(Keys.web.showGitDaemonUrls, true)) {
        AccessPermission permission = getGitDaemonAccessPermission(user, repository);
        if (permission.exceeds(AccessPermission.NONE)) {
            if (permission.atLeast(AccessPermission.PUSH) && !acceptsPush(Transport.GIT)) {
                // downgrade the repo permission for this transport
                // because it is not an acceptable PUSH transport
                permission = AccessPermission.CLONE;
            }
            list.add(new RepositoryUrl(gitDaemonUrl, permission));
        }
    }
    // add all other urls
    // {0} = repository
    // {1} = username
    boolean advertisePermsForOther = settings.getBoolean(Keys.web.advertiseAccessPermissionForOtherUrls, false);
    for (String url : settings.getStrings(Keys.web.otherUrls)) {
        String externalUrl = null;
        if (url.contains("{1}")) {
            // external url requires username, only add url IF we have one
            if (StringUtils.isEmpty(username)) {
                continue;
            } else {
                externalUrl = MessageFormat.format(url, repository.name, username);
            }
        } else {
            // external url does not require username, just do repo name formatting
            externalUrl = MessageFormat.format(url, repository.name);
        }
        AccessPermission permission = null;
        if (advertisePermsForOther) {
            permission = user.getRepositoryPermission(repository).permission;
            if (permission.exceeds(AccessPermission.NONE)) {
                Transport transport = Transport.fromUrl(externalUrl);
                if (permission.atLeast(AccessPermission.PUSH) && !acceptsPush(transport)) {
                    // downgrade the repo permission for this transport
                    // because it is not an acceptable PUSH transport
                    permission = AccessPermission.CLONE;
                }
            }
        }
        list.add(new RepositoryUrl(externalUrl, permission));
    }
    // sort transports by highest permission and then by transport security
    Collections.sort(list, new Comparator<RepositoryUrl>() {

        @Override
        public int compare(RepositoryUrl o1, RepositoryUrl o2) {
            if (o1.hasPermission() && !o2.hasPermission()) {
                // prefer known permission items over unknown
                return -1;
            } else if (!o1.hasPermission() && o2.hasPermission()) {
                // prefer known permission items over unknown
                return 1;
            } else if (!o1.hasPermission() && !o2.hasPermission()) {
                // sort by Transport ordinal
                return o1.transport.compareTo(o2.transport);
            } else if (o1.permission.exceeds(o2.permission)) {
                // prefer highest permission
                return -1;
            } else if (o2.permission.exceeds(o1.permission)) {
                // prefer highest permission
                return 1;
            }
            // prefer more secure transports
            return o1.transport.compareTo(o2.transport);
        }
    });
    // consider the user's transport preference
    RepositoryUrl preferredUrl = null;
    Transport preferredTransport = user.getPreferences().getTransport();
    if (preferredTransport != null) {
        Iterator<RepositoryUrl> itr = list.iterator();
        while (itr.hasNext()) {
            RepositoryUrl url = itr.next();
            if (url.transport.equals(preferredTransport)) {
                itr.remove();
                preferredUrl = url;
                break;
            }
        }
    }
    if (preferredUrl != null) {
        list.add(0, preferredUrl);
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) AccessPermission(com.gitblit.Constants.AccessPermission) RepositoryUrl(com.gitblit.models.RepositoryUrl) Transport(com.gitblit.Constants.Transport)

Aggregations

Transport (com.gitblit.Constants.Transport)5 UserModel (com.gitblit.models.UserModel)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 AccessPermission (com.gitblit.Constants.AccessPermission)1 GitBlitException (com.gitblit.GitBlitException)1 RepositoryModel (com.gitblit.models.RepositoryModel)1 RepositoryUrl (com.gitblit.models.RepositoryUrl)1 GitDaemonClient (com.gitblit.transport.git.GitDaemonClient)1 SshDaemonClient (com.gitblit.transport.ssh.SshDaemonClient)1 BooleanOption (com.gitblit.wicket.panels.BooleanOption)1 TextOption (com.gitblit.wicket.panels.TextOption)1 Locale (java.util.Locale)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 AjaxRequestTarget (org.apache.wicket.ajax.AjaxRequestTarget)1 AjaxButton (org.apache.wicket.ajax.markup.html.form.AjaxButton)1 Form (org.apache.wicket.markup.html.form.Form)1 Fragment (org.apache.wicket.markup.html.panel.Fragment)1 PersonIdent (org.eclipse.jgit.lib.PersonIdent)1 ServiceNotAuthorizedException (org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException)1