| | |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | |
| | | import com.gitblit.GitBlit; |
| | | import com.gitblit.IStoredSettings; |
| | | import com.gitblit.Keys; |
| | | import com.gitblit.manager.IGitblit; |
| | | import com.gitblit.models.RepositoryModel; |
| | | import com.gitblit.models.UserModel; |
| | | import com.gitblit.transport.git.GitDaemonClient; |
| | | import com.gitblit.transport.ssh.SshDaemonClient; |
| | | import com.gitblit.utils.HttpUtils; |
| | | import com.gitblit.utils.StringUtils; |
| | | |
| | |
| | | |
| | | protected final Logger logger = LoggerFactory.getLogger(GitblitReceivePackFactory.class); |
| | | |
| | | private final IStoredSettings settings; |
| | | |
| | | private final IGitblit gitblit; |
| | | |
| | | public GitblitReceivePackFactory(IGitblit gitblit) { |
| | | super(); |
| | | this.settings = gitblit.getSettings(); |
| | | this.gitblit = gitblit; |
| | | } |
| | | |
| | | @Override |
| | | public ReceivePack create(X req, Repository db) |
| | | throws ServiceNotEnabledException, ServiceNotAuthorizedException { |
| | |
| | | String repositoryName = ""; |
| | | String origin = ""; |
| | | String gitblitUrl = ""; |
| | | String repositoryUrl = ""; |
| | | int timeout = 0; |
| | | |
| | | if (req instanceof HttpServletRequest) { |
| | | // http/https request may or may not be authenticated |
| | | HttpServletRequest request = (HttpServletRequest) req; |
| | | repositoryName = request.getAttribute("gitblitRepositoryName").toString(); |
| | | origin = request.getRemoteHost(); |
| | | gitblitUrl = HttpUtils.getGitblitURL(request); |
| | | repositoryUrl = request.getRequestURI(); |
| | | HttpServletRequest client = (HttpServletRequest) req; |
| | | repositoryName = client.getAttribute("gitblitRepositoryName").toString(); |
| | | origin = client.getRemoteHost(); |
| | | gitblitUrl = HttpUtils.getGitblitURL(client); |
| | | |
| | | // determine pushing user |
| | | String username = request.getRemoteUser(); |
| | | String username = client.getRemoteUser(); |
| | | if (!StringUtils.isEmpty(username)) { |
| | | UserModel u = GitBlit.self().getUserModel(username); |
| | | UserModel u = gitblit.getUserModel(username); |
| | | if (u != null) { |
| | | user = u; |
| | | } |
| | |
| | | |
| | | // set timeout from Git daemon |
| | | timeout = client.getDaemon().getTimeout(); |
| | | } else if (req instanceof SshDaemonClient) { |
| | | // SSH request is always authenticated |
| | | SshDaemonClient client = (SshDaemonClient) req; |
| | | repositoryName = client.getRepositoryName(); |
| | | origin = client.getRemoteAddress().toString(); |
| | | user = client.getUser(); |
| | | } |
| | | |
| | | // TODO make this a setting |
| | | boolean allowAnonymousPushes = true; |
| | | boolean allowAnonymousPushes = settings.getBoolean(Keys.git.allowAnonymousPushes, false); |
| | | if (!allowAnonymousPushes && UserModel.ANONYMOUS.equals(user)) { |
| | | // prohibit anonymous pushes |
| | | throw new ServiceNotEnabledException(); |
| | | } |
| | | |
| | | final RepositoryModel repository = GitBlit.self().getRepositoryModel(repositoryName); |
| | | String url = settings.getString(Keys.web.canonicalUrl, null); |
| | | if (StringUtils.isEmpty(url)) { |
| | | url = gitblitUrl; |
| | | } |
| | | |
| | | final GitblitReceivePack rp = new GitblitReceivePack(db, repository, user); |
| | | rp.setGitblitUrl(gitblitUrl); |
| | | rp.setRepositoryUrl(repositoryUrl); |
| | | 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); |
| | | |