-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a25ef7
commit 5955031
Showing
7 changed files
with
121 additions
and
3 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
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 @@ | ||
"""The Authorization service. | ||
This module is to handle authorization related queries. | ||
""" | ||
|
||
from flask_restx import abort | ||
|
||
from api.utils import TokenInfo | ||
from api.utils.roles import Membership | ||
from api.models import Staff as StaffModel | ||
from api.models import StaffWorkRole as StaffWorkRoleModel | ||
|
||
|
||
# pylint: disable=unused-argument,inconsistent-return-statements | ||
def check_auth(**kwargs): | ||
"""Check if user is authorized to perform action on the service.""" | ||
token_roles = set(TokenInfo.get_roles()) | ||
permitted_roles = set(kwargs.get('one_of_roles', [])) | ||
has_valid_roles = token_roles & permitted_roles | ||
if has_valid_roles: | ||
return | ||
|
||
matching_memberships = {membership.name for membership in Membership} & permitted_roles | ||
|
||
if matching_memberships and _has_team_membership(kwargs, matching_memberships): | ||
return True | ||
|
||
abort(403) | ||
|
||
|
||
def _has_team_membership(kwargs, team_permitted_roles) -> bool: | ||
work_id = kwargs.get('work_id') | ||
|
||
if not work_id: | ||
return False | ||
|
||
email = TokenInfo.get_user_data()['email_id'] | ||
staff_model: StaffModel = StaffModel.find_by_email(email) | ||
|
||
work_roles = StaffWorkRoleModel.find_by_params( | ||
{"work_id": work_id, "staff_id": staff_model.id} | ||
) | ||
if not work_roles: | ||
return False | ||
|
||
if Membership.TEAM_MEMBER in team_permitted_roles: | ||
return bool(work_roles) | ||
|
||
membership_ids = {membership.value for membership in Membership} | ||
|
||
return any(role.id in membership_ids for role in work_roles) |
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,34 @@ | ||
# Copyright © 2019 Province of British Columbia | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the 'License'); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an 'AS IS' BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Role definitions.""" | ||
from enum import Enum | ||
|
||
|
||
class Role(Enum): | ||
"""User Role.""" | ||
|
||
# Keycloak Based roles | ||
CREATE = 'create' | ||
EDIT = 'edit' | ||
|
||
|
||
class Membership(Enum): | ||
"""User Position in EAO""" | ||
|
||
EPD = 1 | ||
LEAD = 2 | ||
OTHER = 3 | ||
FNCAIRT = 4 | ||
ANALYST = 5 | ||
TEAM_MEMBER = 'TEAM_MEMBER' |
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