예제 #1
0
def create_repository_admin_role(app, repository):
    """
    Create a new role with name-spaced name based on the repository name and its owner's public user
    name.  This will ensure that the tole name is unique.
    """
    sa_session = app.model.context.current
    name = get_repository_admin_role_name(str(repository.name), str(repository.user.username))
    description = 'A user or group member with this role can administer this repository.'
    role = app.model.Role(name=name, description=description, type=app.model.Role.types.SYSTEM)
    sa_session.add(role)
    sa_session.flush()
    # Associate the role with the repository owner.
    app.model.UserRoleAssociation(repository.user, role)
    # Associate the role with the repository.
    rra = app.model.RepositoryRoleAssociation(repository, role)
    sa_session.add(rra)
    sa_session.flush()
    return role
예제 #2
0
def update_repository(app, trans, id, **kwds):
    """Update an existing ToolShed repository"""
    message = None
    flush_needed = False
    sa_session = app.model.context.current
    repository = sa_session.query(app.model.Repository).get(
        app.security.decode_id(id))
    if repository is None:
        return None, "Unknown repository ID"

    if not (trans.user_is_admin
            or trans.app.security_agent.user_can_administer_repository(
                trans.user, repository)):
        message = "You are not the owner of this repository, so you cannot administer it."
        return None, message

    # Allowlist properties that can be changed via this method
    for key in ('type', 'description', 'long_description',
                'remote_repository_url', 'homepage_url'):
        # If that key is available, not None and different than what's in the model
        if key in kwds and kwds[key] is not None and kwds[key] != getattr(
                repository, key):
            setattr(repository, key, kwds[key])
            flush_needed = True

    if 'category_ids' in kwds and isinstance(kwds['category_ids'], list):
        # Get existing category associations
        category_associations = sa_session.query(app.model.RepositoryCategoryAssociation) \
                                          .filter(app.model.RepositoryCategoryAssociation.table.c.repository_id == app.security.decode_id(id))
        # Remove all of them
        for rca in category_associations:
            sa_session.delete(rca)

        # Then (re)create category associations
        for category_id in kwds['category_ids']:
            category = sa_session.query(app.model.Category) \
                                 .get(app.security.decode_id(category_id))
            if category:
                rca = app.model.RepositoryCategoryAssociation(
                    repository, category)
                sa_session.add(rca)
            else:
                pass
        flush_needed = True

    # However some properties are special, like 'name'
    if 'name' in kwds and kwds[
            'name'] is not None and repository.name != kwds['name']:
        if repository.times_downloaded != 0:
            message = "Repository names cannot be changed if the repository has been cloned."
        else:
            message = validate_repository_name(trans.app, kwds['name'],
                                               trans.user)
        if message:
            return None, message

        repo_dir = repository.repo_path(app)
        # Change the entry in the hgweb.config file for the repository.
        old_lhs = f"repos/{repository.user.username}/{repository.name}"
        new_lhs = "repos/{}/{}".format(repository.user.username, kwds['name'])
        trans.app.hgweb_config_manager.change_entry(old_lhs, new_lhs, repo_dir)

        # Change the entry in the repository's hgrc file.
        hgrc_file = get_hgrc_path(repo_dir)
        change_repository_name_in_hgrc_file(hgrc_file, kwds['name'])

        # Rename the repository's admin role to match the new repository name.
        repository_admin_role = repository.admin_role
        repository_admin_role.name = get_repository_admin_role_name(
            str(kwds['name']), str(repository.user.username))
        trans.sa_session.add(repository_admin_role)
        repository.name = kwds['name']
        flush_needed = True

    if flush_needed:
        trans.sa_session.add(repository)
        trans.sa_session.flush()
        message = "The repository information has been updated."
    else:
        message = None
    return repository, message