-
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: disable edit and delete buttons for librarians
* Creates separate permission files for patron, library, location resources. * Disables location/library edit and delete buttons for librarians from external libraries. * Creates complete units tests for library and location permissions. * Removes duplicates imports. * Closes #488 Co-Authored-by: Aly Badr <aly.badr@rero.ch>
- Loading branch information
Aly Badr
committed
Oct 23, 2019
1 parent
2493ca6
commit bad0367
Showing
8 changed files
with
540 additions
and
85 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,74 @@ | ||
# -*- 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/>. | ||
|
||
"""Library permissions.""" | ||
|
||
|
||
from ...permissions import staffer_is_authenticated | ||
|
||
|
||
def can_update_library_factory(record, *args, **kwargs): | ||
"""Checks if logged user can update its organisation libraries. | ||
librarian must have librarian or system_librarian role. | ||
librarian can only update its affiliated library. | ||
sys_librarian can update any library of its organisation only. | ||
""" | ||
def can(self): | ||
patron = staffer_is_authenticated() | ||
if patron and patron.organisation_pid == record.organisation_pid: | ||
if not patron.is_system_librarian: | ||
if patron.library_pid and \ | ||
record.pid != patron.library_pid: | ||
return False | ||
return True | ||
return False | ||
return type('Check', (), {'can': can})() | ||
|
||
|
||
def can_delete_library_factory(record, *args, **kwargs): | ||
"""Checks if logged user can delete its organisation libraries. | ||
librarian must have system_librarian role. | ||
librarian can not delete any library. | ||
sys_librarian can delete any library of its organisation only. | ||
""" | ||
def can(self): | ||
patron = staffer_is_authenticated() | ||
if patron and patron.organisation_pid == record.organisation_pid: | ||
if patron.is_system_librarian: | ||
return True | ||
return False | ||
return type('Check', (), {'can': can})() | ||
|
||
|
||
def can_create_library_factory(record, *args, **kwargs): | ||
"""Checks if the logged user can create libraries of its organisation. | ||
user must have a system_librarian role. | ||
returns False if a librarian tries to create a library. | ||
returns False if a system_librarian tries to create a library in other org. | ||
""" | ||
def can(self): | ||
patron = staffer_is_authenticated() | ||
if patron and not record: | ||
return True | ||
if patron and patron.organisation_pid == record.organisation_pid: | ||
if patron.is_system_librarian: | ||
return True | ||
return False | ||
return type('Check', (), {'can': can})() |
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,61 @@ | ||
# -*- 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/>. | ||
|
||
"""Location permissions.""" | ||
|
||
|
||
from ...permissions import staffer_is_authenticated | ||
|
||
|
||
def can_update_delete_location_factory(record, *args, **kwargs): | ||
"""Checks if logged user can update or delete its organisation locations. | ||
user must have librarian or system_librarian role | ||
librarian can only update or delete its affiliated library locations. | ||
sys_librarian can update or delete any location of its organisation. | ||
""" | ||
def can(self): | ||
patron = staffer_is_authenticated() | ||
if patron and patron.organisation_pid == record.organisation_pid: | ||
if not patron.is_system_librarian: | ||
if patron.library_pid and \ | ||
record.library_pid != patron.library_pid: | ||
return False | ||
return True | ||
return False | ||
return type('Check', (), {'can': can})() | ||
|
||
|
||
def can_create_location_factory(record, *args, **kwargs): | ||
"""Checks if the logged user can create locations of its organisation. | ||
librarian can create locations for its library only. | ||
system_librarian can create locations at any library of its org. | ||
system_librarian or librarian can create locations at another org. | ||
""" | ||
def can(self): | ||
patron = staffer_is_authenticated() | ||
if patron and not record: | ||
return True | ||
if patron and patron.organisation_pid == record.organisation_pid: | ||
if patron.is_system_librarian: | ||
return True | ||
if patron.is_librarian and \ | ||
record.library_pid == patron.library_pid: | ||
return True | ||
return False | ||
return type('Check', (), {'can': can})() |
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,73 @@ | ||
# -*- 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/>. | ||
|
||
"""Patron permissions.""" | ||
|
||
from flask import request | ||
|
||
from ...permissions import staffer_is_authenticated | ||
|
||
|
||
def can_update_patron_factory(record, *args, **kwargs): | ||
"""Checks if the logged user can update its organisations patrons. | ||
user must have librarian or system_librarian role | ||
returns False if a librarian tries to update a system_librarian | ||
returns False if a librarian tries to add the system_librarian role. | ||
""" | ||
def can(self): | ||
incoming_record = request.get_json(silent=True) or {} | ||
patron = staffer_is_authenticated() | ||
if patron and patron.organisation_pid == record.organisation_pid: | ||
if not patron.is_system_librarian: | ||
if ( | ||
'system_librarian' in incoming_record.get( | ||
'roles', []) or | ||
'system_librarian' in record.get('roles', []) | ||
): | ||
return False | ||
if patron.library_pid and \ | ||
record.library_pid and \ | ||
record.library_pid != patron.library_pid: | ||
return False | ||
return True | ||
return False | ||
return type('Check', (), {'can': can})() | ||
|
||
|
||
def can_delete_patron_factory(record, *args, **kwargs): | ||
"""Checks if the logged user can delete records of its organisation. | ||
user must have librarian or system_librarian role | ||
returns False if a librarian tries to delete a system_librarian and if | ||
librarian tries to delete a librarian from another library. | ||
""" | ||
def can(self): | ||
patron = staffer_is_authenticated() | ||
if patron and patron.organisation_pid == record.organisation_pid: | ||
if patron.is_system_librarian: | ||
return True | ||
if patron.is_librarian: | ||
if 'system_librarian' in record.get('roles', []): | ||
return False | ||
if patron.library_pid and \ | ||
record.library_pid and \ | ||
record.library_pid != patron.library_pid: | ||
return False | ||
return True | ||
return False | ||
return type('Check', (), {'can': can})() |
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.