Пример #1
0
def api_acl_update():
    '''
    Update package ACL
    ------------------
    Update the ACL for a given package.

    ::

        /api/package/acl/

    Accepts POST queries only.

    :arg pkgname: String of the package name.
    :arg branches: List of strings with the name of the branches to change,
        update.
    :arg acl: List of strings of the ACL to change/update. Possible acl
        are: 'commit', 'build', 'watchbugzilla', 'watchcommits',
        'approveacls', 'checkout'.
    :arg acl_status: String of the type of action required. Possible status
        are: 'Approved', 'Awaiting Review', 'Denied', 'Obsolete', 'Removed'.
    :kwarg user: the name of the user that is the target of this ACL
        change/update. This will only work if: 1) you are an admin,
        2) you are changing one of your package.
    :kwarg namespace: The namespace of the packages (defaults to ``rpms``).

    Sample response:

    ::

        {
          "output": "ok",
          "messages": ["user: $USER set acl: $ACL of package: $PACKAGE "
                       "from: $PREVIOUS_STATUS to $NEW_STATUS on branch: "
                       "$BRANCH"]
        }

        {
          "output": "notok",
          "error": ["You are not allowed to update ACLs of someone else."]
        }

    '''
    httpcode = 200
    output = {}

    status = pkgdblib.get_status(
        SESSION, ['pkg_acl', 'acl_status', 'namespaces'])
    collections = pkgdblib.search_collection(
        SESSION, '*', 'Under Development')
    collections.extend(pkgdblib.search_collection(SESSION, '*', 'Active'))

    form = forms.SetAclPackageForm(
        csrf_enabled=False,
        collections=[col.branchname for col in collections],
        pkg_acl=status['pkg_acl'],
        acl_status=status['acl_status'],
        namespaces=status['namespaces'],
    )

    if str(form.namespace.data) in ['None', '']:
        form.namespace.data = 'rpms'

    if form.validate_on_submit():
        namespace = form.namespace.data
        pkg_name = form.pkgname.data
        pkg_branch = form.branches.data
        pkg_acl = form.acl.data
        acl_status = form.acl_status.data
        pkg_user = form.user.data

        try:
            messages = []
            for (branch, acl) in itertools.product(pkg_branch, pkg_acl):

                acl_status2 = acl_status

                if acl_status2 == 'Awaiting Review' and \
                        acl in APP.config['AUTO_APPROVE']:
                    acl_status2 = 'Approved'

                message = pkgdblib.set_acl_package(
                    SESSION,
                    namespace=namespace,
                    pkg_name=pkg_name,
                    pkg_branch=branch,
                    acl=acl,
                    status=acl_status2,
                    pkg_user=pkg_user,
                    user=flask.g.fas_user,
                )
                if message:
                    messages.append(message)
                else:
                    messages.append(
                        'Nothing to update on branch: %s for acl: %s' %
                        (branch, acl))
            SESSION.commit()
            output['output'] = 'ok'
            output['messages'] = messages
        except PkgdbException as err:
            SESSION.rollback()
            output['output'] = 'notok'
            output['error'] = str(err)
            httpcode = 500
    else:
        output['output'] = 'notok'
        output['error'] = 'Invalid input submitted'
        if form.errors:
            detail = []
            for error in form.errors:
                detail.append('%s: %s' % (error,
                              '; '.join(form.errors[error])))
            output['error_detail'] = detail
        httpcode = 500

    jsonout = flask.jsonify(output)
    jsonout.status_code = httpcode
    return jsonout
Пример #2
0
def api_acl_update():
    '''
Update package ACL
------------------
    Update the ACL for a given package.

    ::

        /api/package/acl/

    Accept POST queries only.

    :arg pkg_name: String of the package name.
    :arg pkg_branch: List of strings with the name of the branches to change,
        update.
    :arg pkg_acl: List of strings of the ACL to change/update. Possible acl
        are: 'commit', 'build', 'watchbugzilla', 'watchcommits',
        'approveacls', 'checkout'.
    :arg acl_status: String of the type of action required. Possible status
        are: 'Approved', 'Awaiting Review', 'Denied', 'Obsolete', 'Removed'.
    :kwarg pkg_user: the name of the user that is the target of this ACL
        change/update. This will only work if: 1) you are an admin,
        2) you are changing one of your package.

    Sample response:

    ::

        {
          "output": "ok",
          "messages": ["user: $USER set acl: $ACL of package: $PACKAGE "
                       "from: $PREVIOUS_STATUS to $NEW_STATUS on branch: "
                       "$BRANCH"]
        }

        {
          "output": "notok",
          "error": ["You are not allowed to update ACLs of someone else."]
        }

     '''
    httpcode = 200
    output = {}

    status = pkgdblib.get_status(SESSION, ['pkg_acl', 'acl_status'])

    form = forms.SetAclPackageForm(
        csrf_enabled=False,
        pkg_acl=status['pkg_acl'],
        acl_status=status['acl_status'],
    )
    if form.validate_on_submit():
        pkg_name = form.pkg_name.data
        pkg_branch = form.pkg_branch.data.split(',')
        pkg_acl = form.pkg_acl.data.split(',')
        acl_status = form.acl_status.data
        pkg_user = form.pkg_user.data

        try:
            messages = []
            for (acl, branch) in itertools.product(pkg_acl, pkg_branch):
                message = pkgdblib.set_acl_package(
                    SESSION,
                    pkg_name=pkg_name,
                    pkg_branch=branch,
                    acl=acl,
                    status=acl_status,
                    pkg_user=pkg_user,
                    user=flask.g.fas_user,
                )
                messages.append(message)
            SESSION.commit()
            output['output'] = 'ok'
            output['messages'] = messages
        except pkgdblib.PkgdbException, err:
            SESSION.rollback()
            output['output'] = 'notok'
            output['error'] = str(err)
            httpcode = 500