-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes to show all survey results to superusers (#2078)
* Changes to show all survey results to superusers * fixing link * removing hard coded values * fixing linting * splitting to seperate end points * fixing auth check * fixing linting * merging method in service
- Loading branch information
1 parent
4a6c238
commit 45257ff
Showing
20 changed files
with
306 additions
and
35 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
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,58 @@ | ||
# 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.""" | ||
|
||
PUBLIC_USER = 'public_user' | ||
ANONYMOUS_USER = 'anonymous_user' | ||
|
||
# STAFF Based roles | ||
CREATE_TENANT = 'create_tenant' | ||
VIEW_TENANT = 'view_tenant' | ||
VIEW_USERS = 'view_users' | ||
TOGGLE_USER_STATUS = 'toggle_user_status' | ||
CREATE_ADMIN_USER = 'create_admin_user' | ||
CREATE_TEAM = 'create_team' | ||
CREATE_ENGAGEMENT = 'create_engagement' | ||
VIEW_SURVEYS = 'view_surveys' | ||
CREATE_SURVEY = 'create_survey' | ||
EDIT_SURVEY = 'edit_survey' | ||
CLONE_SURVEY = 'clone_survey' | ||
PUBLISH_ENGAGEMENT = 'publish_engagement' | ||
VIEW_ENGAGEMENT = 'view_engagement' | ||
VIEW_ASSIGNED_ENGAGEMENTS = 'view_assigned_engagements' | ||
VIEW_PRIVATE_ENGAGEMENTS = 'view_private_engagements' | ||
EDIT_ENGAGEMENT = 'edit_engagement' | ||
REVIEW_COMMENTS = 'review_comments' | ||
REVIEW_ALL_COMMENTS = 'review_all_comments' | ||
ACCESS_DASHBOARD = 'access_dashboard' | ||
VIEW_MEMBERS = 'view_members' | ||
EDIT_MEMBERS = 'edit_members' | ||
VIEW_ALL_SURVEYS = 'view_all_surveys' # Super user can view all kind of surveys including hidden | ||
EDIT_ALL_SURVEYS = 'edit_all_surveys' | ||
EDIT_DRAFT_ENGAGEMENT = 'edit_draft_engagement' | ||
EDIT_SCHEDULED_ENGAGEMENT = 'edit_scheduled_engagement' | ||
EDIT_UPCOMING_ENGAGEMENT = 'edit_upcoming_engagement' | ||
EDIT_OPEN_ENGAGEMENT = 'edit_open_engagement' | ||
EDIT_CLOSED_ENGAGEMENT = 'edit_closed_engagement' | ||
VIEW_APPROVED_COMMENTS = 'view_approved_comments' # used just in the front end to show the comment page | ||
VIEW_FEEDBACKS = 'view_feedbacks' | ||
VIEW_ALL_ENGAGEMENTS = 'view_all_engagements' # Allows user access to all engagements including draft | ||
SHOW_ALL_COMMENT_STATUS = 'show_all_comment_status' # Allows user to see all comment status | ||
EXPORT_TO_CSV = 'export_to_csv' # Allows users to export comments to csv | ||
VIEW_ALL_SURVEY_RESULTS = 'view_all_survey_results' # Allows users to view results to all questions |
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,116 @@ | ||
# 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. | ||
"""User Context to hold request scoped variables.""" | ||
|
||
import functools | ||
from typing import Dict | ||
|
||
from flask import g, request | ||
|
||
from analytics_api.utils.roles import Role | ||
|
||
|
||
def _get_context(): | ||
"""Return User context.""" | ||
return UserContext() | ||
|
||
|
||
class UserContext: # pylint: disable=too-many-instance-attributes | ||
"""Object to hold request scoped user context.""" | ||
|
||
def __init__(self): | ||
"""Return a User Context object.""" | ||
token_info: Dict = _get_token_info() or {} | ||
self._token_info = token_info | ||
self._user_name: str = token_info.get('username', token_info.get('preferred_username', None)) | ||
self._first_name: str = token_info.get('firstname', None) | ||
self._last_name: str = token_info.get('lastname', None) | ||
self._bearer_token: str = _get_token() | ||
self._roles: list = token_info.get('realm_access', None).get('roles', []) if 'realm_access' in token_info \ | ||
else [] | ||
self._sub: str = token_info.get('sub', None) | ||
self._name: str = f"{token_info.get('firstname', None)} {token_info.get('lastname', None)}" | ||
|
||
@property | ||
def user_name(self) -> str: | ||
"""Return the user_name.""" | ||
return self._user_name if self._user_name else None | ||
|
||
@property | ||
def first_name(self) -> str: | ||
"""Return the user_first_name.""" | ||
return self._first_name | ||
|
||
@property | ||
def last_name(self) -> str: | ||
"""Return the user_last_name.""" | ||
return self._last_name | ||
|
||
@property | ||
def bearer_token(self) -> str: | ||
"""Return the bearer_token.""" | ||
return self._bearer_token | ||
|
||
@property | ||
def roles(self) -> list: | ||
"""Return the roles.""" | ||
return self._roles | ||
|
||
@property | ||
def sub(self) -> str: | ||
"""Return the subject.""" | ||
return self._sub | ||
|
||
def has_role(self, role_name: str) -> bool: | ||
"""Return True if the user has the role.""" | ||
return role_name in self._roles | ||
|
||
def is_staff_admin(self) -> bool: | ||
"""Return True if the user is staff user.""" | ||
return Role.CREATE_ENGAGEMENT.value in self._roles if self._roles else False | ||
|
||
def is_system(self) -> bool: | ||
"""Return True if the user is system user.Helps to idenitfy connections from EPIC.""" | ||
return Role.SYSTEM.value in self._roles if self._roles else False | ||
|
||
@property | ||
def name(self) -> str: | ||
"""Return the name.""" | ||
return self._name | ||
|
||
@property | ||
def token_info(self) -> Dict: | ||
"""Return the name.""" | ||
return self._token_info | ||
|
||
|
||
def user_context(function): | ||
"""Add user context object as an argument to function.""" | ||
|
||
@functools.wraps(function) | ||
def wrapper(*func_args, **func_kwargs): | ||
context = _get_context() | ||
func_kwargs['user_context'] = context | ||
return function(*func_args, **func_kwargs) | ||
|
||
return wrapper | ||
|
||
|
||
def _get_token_info() -> Dict: | ||
return g.jwt_oidc_token_info if g and 'jwt_oidc_token_info' in g else {} | ||
|
||
|
||
def _get_token() -> str: | ||
token: str = request.headers['Authorization'] if request and 'Authorization' in request.headers else None | ||
return token.replace('Bearer ', '') if token else None |
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
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
Oops, something went wrong.