public EditRepositoryPage() {
    // create constructor
    super();
    isCreate = true;
    RepositoryModel model = new RepositoryModel();
    String restriction = GitBlit.getString(Keys.git.defaultAccessRestriction, null);
    model.accessRestriction = AccessRestrictionType.fromName(restriction);
    String authorization = GitBlit.getString(Keys.git.defaultAuthorizationControl, null);
    model.authorizationControl = AuthorizationControl.fromName(authorization);

    GitBlitWebSession session = GitBlitWebSession.get();
    UserModel user = session.getUser();
    if (user != null && user.canCreate() && !user.canAdmin()) {
      // personal create permissions, inject personal repository path
      model.name = user.getPersonalPath() + "/";
      model.projectPath = user.getPersonalPath();
      model.addOwner(user.username);
      // personal repositories are private by default
      model.accessRestriction = AccessRestrictionType.VIEW;
      model.authorizationControl = AuthorizationControl.NAMED;
    }

    setupPage(model);
    setStatelessHint(false);
    setOutputMarkupId(true);
  }
Exemple #2
0
  @Test
  public void testBogusLoginClone() throws Exception {
    // restrict repository access
    RepositoryModel model = GitBlit.self().getRepositoryModel("ticgit.git");
    model.accessRestriction = AccessRestrictionType.CLONE;
    GitBlit.self().updateRepositoryModel(model.name, model, false);

    // delete any existing working folder
    boolean cloned = false;
    try {
      CloneCommand clone = Git.cloneRepository();
      clone.setURI(MessageFormat.format("{0}/git/ticgit.git", url));
      clone.setDirectory(ticgit2Folder);
      clone.setBare(false);
      clone.setCloneAllBranches(true);
      clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider("bogus", "bogus"));
      close(clone.call());
      cloned = true;
    } catch (Exception e) {
      // swallow the exception which we expect
    }

    // restore anonymous repository access
    model.accessRestriction = AccessRestrictionType.NONE;
    GitBlit.self().updateRepositoryModel(model.name, model, false);

    assertFalse("Bogus login cloned a repository?!", cloned);
  }
Exemple #3
0
  @Test
  public void testUnauthorizedLoginClone() throws Exception {
    // restrict repository access
    RepositoryModel model = GitBlit.self().getRepositoryModel("ticgit.git");
    model.accessRestriction = AccessRestrictionType.CLONE;
    model.authorizationControl = AuthorizationControl.NAMED;
    UserModel user = new UserModel("james");
    user.password = "******";
    GitBlit.self().updateUserModel(user.username, user, true);
    GitBlit.self().updateRepositoryModel(model.name, model, false);

    FileUtils.delete(ticgit2Folder, FileUtils.RECURSIVE);

    // delete any existing working folder
    boolean cloned = false;
    try {
      CloneCommand clone = Git.cloneRepository();
      clone.setURI(MessageFormat.format("{0}/git/ticgit.git", url));
      clone.setDirectory(ticgit2Folder);
      clone.setBare(false);
      clone.setCloneAllBranches(true);
      clone.setCredentialsProvider(
          new UsernamePasswordCredentialsProvider(user.username, user.password));
      close(clone.call());
      cloned = true;
    } catch (Exception e) {
      // swallow the exception which we expect
    }

    assertFalse("Unauthorized login cloned a repository?!", cloned);

    FileUtils.delete(ticgit2Folder, FileUtils.RECURSIVE);

    // switch to authenticated
    model.authorizationControl = AuthorizationControl.AUTHENTICATED;
    GitBlit.self().updateRepositoryModel(model.name, model, false);

    // try clone again
    cloned = false;
    CloneCommand clone = Git.cloneRepository();
    clone.setURI(MessageFormat.format("{0}/git/ticgit.git", url));
    clone.setDirectory(ticgit2Folder);
    clone.setBare(false);
    clone.setCloneAllBranches(true);
    clone.setCredentialsProvider(
        new UsernamePasswordCredentialsProvider(user.username, user.password));
    close(clone.call());
    cloned = true;

    assertTrue("Authenticated login could not clone!", cloned);

    FileUtils.delete(ticgit2Folder, FileUtils.RECURSIVE);

    // restore anonymous repository access
    model.accessRestriction = AccessRestrictionType.NONE;
    model.authorizationControl = AuthorizationControl.NAMED;
    GitBlit.self().updateRepositoryModel(model.name, model, false);
    GitBlit.self().deleteUser(user.username);
  }
  private boolean validateFields() {
    String rname = nameField.getText();
    if (StringUtils.isEmpty(rname)) {
      error("Please enter a repository name!");
      return false;
    }

    // automatically convert backslashes to forward slashes
    rname = rname.replace('\\', '/');
    // Automatically replace // with /
    rname = rname.replace("//", "/");

    // prohibit folder paths
    if (rname.startsWith("/")) {
      error("Leading root folder references (/) are prohibited.");
      return false;
    }
    if (rname.startsWith("../")) {
      error("Relative folder references (../) are prohibited.");
      return false;
    }
    if (rname.contains("/../")) {
      error("Relative folder references (../) are prohibited.");
      return false;
    }

    // confirm valid characters in repository name
    Character c = StringUtils.findInvalidCharacter(rname);
    if (c != null) {
      error(MessageFormat.format("Illegal character ''{0}'' in repository name!", c));
      return false;
    }

    // verify repository name uniqueness on create
    if (isCreate) {
      // force repo names to lowercase
      // this means that repository name checking for rpc creation
      // is case-insensitive, regardless of the Gitblit server's
      // filesystem
      if (repositoryNames.contains(rname.toLowerCase())) {
        error(
            MessageFormat.format(
                "Can not create repository ''{0}'' because it already exists.", rname));
        return false;
      }
    } else {
      // check rename collision
      if (!repositoryName.equalsIgnoreCase(rname)) {
        if (repositoryNames.contains(rname.toLowerCase())) {
          error(
              MessageFormat.format(
                  "Failed to rename ''{0}'' because ''{1}'' already exists.",
                  repositoryName, rname));
          return false;
        }
      }
    }

    if (accessRestriction.getSelectedItem() == null) {
      error("Please select access restriction!");
      return false;
    }

    if (federationStrategy.getSelectedItem() == null) {
      error("Please select federation strategy!");
      return false;
    }

    repository.name = rname;
    repository.description = descriptionField.getText();
    repository.owner =
        ownerField.getSelectedItem() == null ? null : ownerField.getSelectedItem().toString();
    repository.HEAD =
        headRefField.getSelectedItem() == null ? null : headRefField.getSelectedItem().toString();
    repository.useTickets = useTickets.isSelected();
    repository.useDocs = useDocs.isSelected();
    repository.showRemoteBranches = showRemoteBranches.isSelected();
    repository.showReadme = showReadme.isSelected();
    repository.skipSizeCalculation = skipSizeCalculation.isSelected();
    repository.skipSummaryMetrics = skipSummaryMetrics.isSelected();
    repository.isFrozen = isFrozen.isSelected();

    String ml = mailingListsField.getText();
    if (!StringUtils.isEmpty(ml)) {
      Set<String> list = new HashSet<String>();
      for (String address : ml.split("(,|\\s)")) {
        if (StringUtils.isEmpty(address)) {
          continue;
        }
        list.add(address.toLowerCase());
      }
      repository.mailingLists = new ArrayList<String>(list);
    }

    repository.accessRestriction = (AccessRestrictionType) accessRestriction.getSelectedItem();
    repository.federationStrategy = (FederationStrategy) federationStrategy.getSelectedItem();

    if (repository.federationStrategy.exceeds(FederationStrategy.EXCLUDE)) {
      repository.federationSets = setsPalette.getSelections();
    }

    repository.indexedBranches = indexedBranchesPalette.getSelections();
    repository.preReceiveScripts = preReceivePalette.getSelections();
    repository.postReceiveScripts = postReceivePalette.getSelections();

    // Custom Fields
    repository.customFields = new LinkedHashMap<String, String>();
    if (customTextfields != null) {
      for (JTextField field : customTextfields) {
        String key = field.getName();
        String value = field.getText();
        repository.customFields.put(key, value);
      }
    }
    return true;
  }