| |
@@ -1,7 +1,7 @@
|
| |
# -*- coding: utf-8 -*-
|
| |
|
| |
"""
|
| |
- (c) 2015-2018 - Copyright Red Hat Inc
|
| |
+ (c) 2015-2019 - Copyright Red Hat Inc
|
| |
|
| |
Authors:
|
| |
Pierre-Yves Chibon <pingou@pingoured.fr>
|
| |
@@ -34,6 +34,7 @@
|
| |
get_page,
|
| |
get_per_page,
|
| |
)
|
| |
+ from pagure.api.utils import _get_repo, _check_token
|
| |
from pagure.config import config as pagure_config
|
| |
|
| |
|
| |
@@ -96,11 +97,7 @@
|
| |
flask.request.values.get("with_commits", False)
|
| |
)
|
| |
|
| |
- repo = get_authorized_api_project(
|
| |
- flask.g.session, repo, user=username, namespace=namespace
|
| |
- )
|
| |
- if repo is None:
|
| |
- raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOPROJECT)
|
| |
+ repo = _get_repo(repo, username, namespace)
|
| |
|
| |
tags = pagure.lib.git.get_git_tags(repo, with_commits=with_commits)
|
| |
|
| |
@@ -144,11 +141,7 @@
|
| |
}
|
| |
}
|
| |
"""
|
| |
- repo = get_authorized_api_project(
|
| |
- flask.g.session, repo, user=username, namespace=namespace
|
| |
- )
|
| |
- if repo is None:
|
| |
- raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOPROJECT)
|
| |
+ repo = _get_repo(repo, username, namespace)
|
| |
|
| |
implicit_watch_users = set([repo.user.username])
|
| |
for access_type in repo.access_users:
|
| |
@@ -235,13 +228,9 @@
|
| |
}
|
| |
}
|
| |
"""
|
| |
- repo = get_authorized_api_project(
|
| |
- flask.g.session, repo, user=username, namespace=namespace
|
| |
- )
|
| |
- if repo is None:
|
| |
- raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOPROJECT)
|
| |
- git_urls = {}
|
| |
+ repo = _get_repo(repo, username, namespace)
|
| |
|
| |
+ git_urls = {}
|
| |
git_url_ssh = pagure_config.get("GIT_URL_SSH")
|
| |
if pagure.utils.api_authenticated() and git_url_ssh:
|
| |
try:
|
| |
@@ -293,11 +282,7 @@
|
| |
}
|
| |
|
| |
"""
|
| |
- repo = get_authorized_api_project(
|
| |
- flask.g.session, repo, user=username, namespace=namespace
|
| |
- )
|
| |
- if repo is None:
|
| |
- raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOPROJECT)
|
| |
+ repo = _get_repo(repo, username, namespace)
|
| |
|
| |
branches = pagure.lib.git.get_git_branches(repo)
|
| |
|
| |
@@ -632,17 +617,12 @@
|
| |
}
|
| |
|
| |
"""
|
| |
- repo = get_authorized_api_project(
|
| |
- flask.g.session, repo, user=username, namespace=namespace
|
| |
- )
|
| |
+ repo = _get_repo(repo, username, namespace)
|
| |
|
| |
expand_group = pagure.utils.is_true(
|
| |
flask.request.values.get("expand_group", False)
|
| |
)
|
| |
|
| |
- if repo is None:
|
| |
- raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOPROJECT)
|
| |
-
|
| |
output = repo.to_json(api=True, public=True)
|
| |
|
| |
if expand_group:
|
| |
@@ -904,14 +884,8 @@
|
| |
}
|
| |
|
| |
"""
|
| |
- project = get_authorized_api_project(
|
| |
- flask.g.session, repo, namespace=namespace
|
| |
- )
|
| |
- if not project:
|
| |
- raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOPROJECT)
|
| |
-
|
| |
- if flask.g.token.project and project != flask.g.token.project:
|
| |
- raise pagure.exceptions.APIError(401, error_code=APIERROR.EINVALIDTOK)
|
| |
+ project = _get_repo(repo, namespace=namespace)
|
| |
+ _check_token(project, project_token=False)
|
| |
|
| |
is_site_admin = pagure.utils.is_admin()
|
| |
admins = [u.username for u in project.get_project_users("admin")]
|
| |
@@ -1145,14 +1119,8 @@
|
| |
}
|
| |
|
| |
"""
|
| |
- project = get_authorized_api_project(
|
| |
- flask.g.session, repo, namespace=namespace
|
| |
- )
|
| |
- if not project:
|
| |
- raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOPROJECT)
|
| |
-
|
| |
- if flask.g.token.project and project != flask.g.token.project:
|
| |
- raise pagure.exceptions.APIError(401, error_code=APIERROR.EINVALIDTOK)
|
| |
+ project = _get_repo(repo, username, namespace)
|
| |
+ _check_token(project, project_token=False)
|
| |
|
| |
# Check if it's JSON or form data
|
| |
if flask.request.headers.get("Content-Type") == "application/json":
|
| |
@@ -1230,14 +1198,8 @@
|
| |
}
|
| |
|
| |
"""
|
| |
- project = get_authorized_api_project(
|
| |
- flask.g.session, repo, namespace=namespace
|
| |
- )
|
| |
- if not project:
|
| |
- raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOPROJECT)
|
| |
-
|
| |
- if flask.g.token.project and project != flask.g.token.project:
|
| |
- raise pagure.exceptions.APIError(401, error_code=APIERROR.EINVALIDTOK)
|
| |
+ project = _get_repo(repo, username, namespace)
|
| |
+ _check_token(project, project_token=False)
|
| |
|
| |
# Check if it's JSON or form data
|
| |
if flask.request.headers.get("Content-Type") == "application/json":
|
| |
@@ -1341,11 +1303,7 @@
|
| |
}
|
| |
|
| |
"""
|
| |
- repo = get_authorized_api_project(
|
| |
- flask.g.session, repo, user=username, namespace=namespace
|
| |
- )
|
| |
- if repo is None:
|
| |
- raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOPROJECT)
|
| |
+ repo = _get_repo(repo, username, namespace)
|
| |
|
| |
reponame = pagure.utils.get_repo_path(repo)
|
| |
repo_obj = Repository(reponame)
|
| |
@@ -1476,18 +1434,11 @@
|
| |
|
| |
""" # noqa
|
| |
|
| |
- repo = get_authorized_api_project(
|
| |
- flask.g.session, repo, user=username, namespace=namespace
|
| |
- )
|
| |
+ repo = _get_repo(repo, username, namespace)
|
| |
+ _check_token(repo, project_token=False)
|
| |
|
| |
output = {}
|
| |
|
| |
- if repo is None:
|
| |
- raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOPROJECT)
|
| |
-
|
| |
- if flask.g.token.project and repo != flask.g.token.project:
|
| |
- raise pagure.exceptions.APIError(401, error_code=APIERROR.EINVALIDTOK)
|
| |
-
|
| |
reponame = pagure.utils.get_repo_path(repo)
|
| |
repo_obj = Repository(reponame)
|
| |
try:
|
| |
@@ -1618,14 +1569,8 @@
|
| |
}
|
| |
"""
|
| |
|
| |
- project = get_authorized_api_project(
|
| |
- flask.g.session, repo, namespace=namespace
|
| |
- )
|
| |
- if not project:
|
| |
- raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOPROJECT)
|
| |
-
|
| |
- if flask.g.token.project and project != flask.g.token.project:
|
| |
- raise pagure.exceptions.APIError(401, error_code=APIERROR.EINVALIDTOK)
|
| |
+ project = _get_repo(repo, username, namespace)
|
| |
+ _check_token(project)
|
| |
|
| |
# Get the input submitted
|
| |
data = get_request_data()
|
| |
@@ -1773,14 +1718,9 @@
|
| |
|
| |
"""
|
| |
output = {}
|
| |
- project = get_authorized_api_project(
|
| |
- flask.g.session, repo, namespace=namespace
|
| |
- )
|
| |
- if not project:
|
| |
- raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOPROJECT)
|
| |
|
| |
- if flask.g.token.project and project != flask.g.token.project:
|
| |
- raise pagure.exceptions.APIError(401, error_code=APIERROR.EINVALIDTOK)
|
| |
+ project = _get_repo(repo, username, namespace)
|
| |
+ _check_token(project, project_token=False)
|
| |
|
| |
form = pagure.forms.ModifyACLForm(csrf_enabled=False)
|
| |
if form.validate_on_submit():
|
| |
@@ -1956,14 +1896,8 @@
|
| |
}
|
| |
|
| |
"""
|
| |
- project = get_authorized_api_project(
|
| |
- flask.g.session, repo, namespace=namespace
|
| |
- )
|
| |
- if not project:
|
| |
- raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOPROJECT)
|
| |
-
|
| |
- if flask.g.token.project and project != flask.g.token.project:
|
| |
- raise pagure.exceptions.APIError(401, error_code=APIERROR.EINVALIDTOK)
|
| |
+ project = _get_repo(repo, username, namespace)
|
| |
+ _check_token(project, project_token=False)
|
| |
|
| |
return flask.jsonify({"settings": project.settings, "status": "ok"})
|
| |
|
| |
@@ -2015,14 +1949,8 @@
|
| |
}
|
| |
|
| |
"""
|
| |
- project = get_authorized_api_project(
|
| |
- flask.g.session, repo, namespace=namespace
|
| |
- )
|
| |
- if not project:
|
| |
- raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOPROJECT)
|
| |
-
|
| |
- if flask.g.token.project and project != flask.g.token.project:
|
| |
- raise pagure.exceptions.APIError(401, error_code=APIERROR.EINVALIDTOK)
|
| |
+ project = _get_repo(repo, username, namespace)
|
| |
+ _check_token(project, project_token=False)
|
| |
|
| |
authorized_users = [project.user.username]
|
| |
authorized_users.extend(
|
| |
@@ -2105,14 +2033,8 @@
|
| |
}
|
| |
|
| |
"""
|
| |
- project = get_authorized_api_project(
|
| |
- flask.g.session, repo, namespace=namespace
|
| |
- )
|
| |
- if not project:
|
| |
- raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOPROJECT)
|
| |
-
|
| |
- if flask.g.token.project and project != flask.g.token.project:
|
| |
- raise pagure.exceptions.APIError(401, error_code=APIERROR.EINVALIDTOK)
|
| |
+ project = _get_repo(repo, username, namespace)
|
| |
+ _check_token(project, project_token=False)
|
| |
|
| |
settings = {}
|
| |
for key in flask.request.form:
|
| |
@@ -2194,14 +2116,9 @@
|
| |
|
| |
"""
|
| |
output = {}
|
| |
- project = get_authorized_api_project(
|
| |
- flask.g.session, repo, namespace=namespace
|
| |
- )
|
| |
- if not project:
|
| |
- raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOPROJECT)
|
| |
|
| |
- if flask.g.token.project and project != flask.g.token.project:
|
| |
- raise pagure.exceptions.APIError(401, error_code=APIERROR.EINVALIDTOK)
|
| |
+ project = _get_repo(repo, username, namespace)
|
| |
+ _check_token(project, project_token=False)
|
| |
|
| |
authorized_users = [project.user.username]
|
| |
authorized_users.extend(
|
| |
@@ -2229,3 +2146,91 @@
|
| |
|
| |
jsonout = flask.jsonify(output)
|
| |
return jsonout
|
| |
+
|
| |
+
|
| |
+ @API.route("/<repo>/blockuser", methods=["POST"])
|
| |
+ @API.route("/<namespace>/<repo>/blockuser", methods=["POST"])
|
| |
+ @API.route("/fork/<username>/<repo>/blockuser", methods=["POST"])
|
| |
+ @API.route("/fork/<username>/<namespace>/<repo>/blockuser", methods=["POST"])
|
| |
+ @api_login_required(acls=["modify_project"])
|
| |
+ @api_method
|
| |
+ def api_project_block_user(repo, namespace=None, username=None):
|
| |
+ """
|
| |
+ Block an user from a project
|
| |
+ ----------------------------
|
| |
+ Block an user from interacting with the project
|
| |
+
|
| |
+ This is restricted to project admins.
|
| |
+
|
| |
+ ::
|
| |
+
|
| |
+ POST /api/0/<repo>/blockuser
|
| |
+ POST /api/0/<namespace>/<repo>/blockuser
|
| |
+
|
| |
+ ::
|
| |
+
|
| |
+ POST /api/0/fork/<username>/<repo>/blockuser
|
| |
+ POST /api/0/fork/<username>/<namespace>/<repo>/blockuser
|
| |
+
|
| |
+
|
| |
+ Input
|
| |
+ ^^^^^
|
| |
+
|
| |
+ +------------------+---------+---------------+---------------------------+
|
| |
+ | Key | Type | Optionality | Description |
|
| |
+ +==================+=========+===============+===========================+
|
| |
+ | ``username`` | String | optional | The username of the user |
|
| |
+ | | | | to block on this project |
|
| |
+ +------------------+---------+---------------+---------------------------+
|
| |
+
|
| |
+ Beware that this API endpoint updates **all** the users blocked in the
|
| |
+ project, so if you are updating this list, do not submit just one username,
|
| |
+ submit the updated list.
|
| |
+
|
| |
+
|
| |
+ Sample response
|
| |
+ ^^^^^^^^^^^^^^^
|
| |
+
|
| |
+ ::
|
| |
+
|
| |
+ {"message": "User(s) blocked"}
|
| |
+
|
| |
+ """
|
| |
+ output = {}
|
| |
+
|
| |
+ project = _get_repo(repo, username, namespace)
|
| |
+ _check_token(project)
|
| |
+
|
| |
+ authorized_users = [project.user.username]
|
| |
+ authorized_users.extend(
|
| |
+ [user.user for user in project.access_users["admin"]]
|
| |
+ )
|
| |
+ if flask.g.fas_user.username not in authorized_users:
|
| |
+ raise pagure.exceptions.APIError(
|
| |
+ 401, error_code=APIERROR.ENOTHIGHENOUGH
|
| |
+ )
|
| |
+
|
| |
+ usernames = flask.request.form.getlist("username")
|
| |
+
|
| |
+ try:
|
| |
+ users = set()
|
| |
+ for user in usernames:
|
| |
+ user = user.strip()
|
| |
+ if user:
|
| |
+ pagure.lib.query.get_user(flask.g.session, user)
|
| |
+ users.add(user)
|
| |
+ project.block_users = list(users)
|
| |
+ flask.g.session.add(project)
|
| |
+ flask.g.session.commit()
|
| |
+ output = {"message": "User(s) blocked"}
|
| |
+ except pagure.exceptions.PagureException as err:
|
| |
+ raise pagure.exceptions.APIError(
|
| |
+ 400, error_code=APIERROR.ENOCODE, error=str(err)
|
| |
+ )
|
| |
+ except SQLAlchemyError as err: # pragma: no cover
|
| |
+ flask.g.session.rollback()
|
| |
+ _log.exception(err)
|
| |
+ raise pagure.exceptions.APIError(400, error_code=APIERROR.EDBERROR)
|
| |
+
|
| |
+ jsonout = flask.jsonify(output)
|
| |
+ return jsonout
|
| |
Fixes https://pagure.io/pagure/issue/4255