-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
permissions: update and delete permissions api for records
* Removes update and delete permissions from document serializer. * Removes unused codes. * Increases test code coverage. Co-Authored-by: Aly Badr <aly.badr@rero.ch>
- Loading branch information
Aly Badr
committed
Feb 17, 2020
1 parent
9897f0f
commit d1a0c05
Showing
14 changed files
with
12,665 additions
and
12,579 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# RERO ILS | ||
# Copyright (C) 2019 RERO | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, version 3 of the License. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""Permissions for all modules.""" | ||
|
||
|
||
from flask import jsonify | ||
|
||
from .utils import get_record_class_update_permission_from_route | ||
|
||
|
||
def jsonify_permission_api_response( | ||
can_update=False, can_delete=False, reasons={}): | ||
"""Jsonify api response.""" | ||
return jsonify({ | ||
'update': {'can': can_update}, | ||
'delete': {'can': can_delete, 'reasons': reasons} | ||
}) | ||
|
||
|
||
def record_update_delete_permissions(record_pid=None, route_name=None): | ||
"""Return record permissions.""" | ||
try: | ||
rec_class, update_permission = \ | ||
get_record_class_update_permission_from_route(route_name) | ||
record = rec_class.get_record_by_pid(record_pid) | ||
|
||
if not record: | ||
return jsonify({'status': 'error: Record not found.'}), 404 | ||
|
||
return jsonify_permission_api_response( | ||
can_update=update_permission(record).can(), | ||
can_delete=record.can_delete, | ||
reasons=record.reasons_not_to_delete(), | ||
) | ||
except Exception as error: | ||
return jsonify({'status': 'error: Bad request'}), 400 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# RERO ILS | ||
# Copyright (C) 2019 RERO | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, version 3 of the License. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""Blueprint used for loading templates for all modules.""" | ||
|
||
from __future__ import absolute_import, print_function | ||
|
||
from functools import wraps | ||
|
||
from flask import Blueprint, jsonify | ||
from flask_login import current_user | ||
|
||
from .permissions import record_update_delete_permissions | ||
from ..permissions import librarian_permission | ||
|
||
api_blueprint = Blueprint( | ||
'api_blueprint', | ||
__name__, | ||
url_prefix='' | ||
) | ||
|
||
|
||
def check_authentication(func): | ||
"""Decorator to check authentication for permissions HTTP API.""" | ||
@wraps(func) | ||
def decorated_view(*args, **kwargs): | ||
if not current_user.is_authenticated: | ||
return jsonify({'status': 'error: Unauthorized'}), 401 | ||
if not librarian_permission.require().can(): | ||
return jsonify({'status': 'error: Forbidden'}), 403 | ||
return func(*args, **kwargs) | ||
|
||
return decorated_view | ||
|
||
|
||
@api_blueprint.route( | ||
'/permissions/<route_name>/<record_pid>', methods=['GET']) | ||
@check_authentication | ||
def permissions(route_name, record_pid): | ||
"""HTTP GET request for record permissions. | ||
Required parameters: route_name, record_pid | ||
""" | ||
return record_update_delete_permissions( | ||
record_pid=record_pid, route_name=route_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.