oncall-engine/engine/apps/public_api/serializers/users.py

89 lines
2.9 KiB
Python
Raw Permalink Normal View History

from rest_framework import serializers
Add RBAC Support (#777) * Modify plugin.json to support RBAC role registration * defines 26 new custom roles in plugin.json. The main roles are: - Admin: read/write access to everything in OnCall - Reader: read access to everything in OnCall - OnCaller : read access to everything in OnCall + edit access to Alert Groups and Schedules - <object-type> Editor: read/write access to everything related to <object-type> - <object-type> Reader: read access for <object-type> - User Settings Admin: read/write access to all user's settings, not just own settings. This is in comparison to User Settings Editor which can only read/write own settings * update changelog and documentation (#686) * implement RBAC for OnCall backend This commit refactors backend authorization. It trys to use RBAC authorization if the org's grafana instance supports it, otherwise it falls back to basic role authorization. * update RBAC backend tests * add tests for RBAC changes - run backend tests as matrix where RBAC is enabled/disabled. When RBAC is enabled, the permissions granted are read from the role grants in the frontend's plugin.json file (instead of relying what we specify in RBACPermission.Permissions) - remove --reuse-db --nomigrations flags from engine/tox.ini - minor autoformatting changes to docker-compose-developer.yml * remove --ds=settings.ci-test from pytest CI command DJANGO_SETTINGS_MODULE is already specified as an env var so this is just unecessary duplication * update gitignore * update github action job name for "test" * RBAC frontend changes * refactors the use of basic roles (ex. Viewer, Editor, Admin) use RBAC permissions (when supported), or falling back to basic roles when RBAC is not supported. - updates the UserAction enum in grafana-plugin/src/state/userAction.ts. Previously this was hardcoded to a list of strings that were being returned by the OnCall API. Now the values here correspond to the permissions in plugin.json (plus a fallback role) * changes per Gabriel's comments: - get rid of group attribute in rbac roles - remove displayName role attribute - remove hidden role attribute - add back role to includes section * don't try to update user timezone if they don't have permission
2022-11-29 09:41:56 +01:00
from apps.api.permissions import LegacyAccessControlRole
from apps.slack.models import SlackUserIdentity
from apps.user_management.models import User
from common.api_helpers.mixins import EagerLoadingMixin
class SlackUserIdentitySerializer(serializers.ModelSerializer):
user_id: str = serializers.CharField(source="slack_id")
team_id: str = serializers.CharField(source="slack_team_identity.slack_id")
class Meta:
model = SlackUserIdentity
fields = (
"user_id",
"team_id",
)
class FastUserSerializer(serializers.ModelSerializer):
id: str = serializers.ReadOnlyField(read_only=True, source="public_primary_key")
grafana_id: int = serializers.IntegerField(read_only=True, source="user_id")
email: str = serializers.EmailField(read_only=True)
role: str = serializers.SerializerMethodField() # LEGACY, should be removed eventually
is_phone_number_verified: bool = serializers.SerializerMethodField()
class Meta:
model = User
fields = [
"id",
"grafana_id",
"email",
"username",
"role",
"is_phone_number_verified",
]
@staticmethod
def get_role(obj: User) -> str:
Add RBAC Support (#777) * Modify plugin.json to support RBAC role registration * defines 26 new custom roles in plugin.json. The main roles are: - Admin: read/write access to everything in OnCall - Reader: read access to everything in OnCall - OnCaller : read access to everything in OnCall + edit access to Alert Groups and Schedules - <object-type> Editor: read/write access to everything related to <object-type> - <object-type> Reader: read access for <object-type> - User Settings Admin: read/write access to all user's settings, not just own settings. This is in comparison to User Settings Editor which can only read/write own settings * update changelog and documentation (#686) * implement RBAC for OnCall backend This commit refactors backend authorization. It trys to use RBAC authorization if the org's grafana instance supports it, otherwise it falls back to basic role authorization. * update RBAC backend tests * add tests for RBAC changes - run backend tests as matrix where RBAC is enabled/disabled. When RBAC is enabled, the permissions granted are read from the role grants in the frontend's plugin.json file (instead of relying what we specify in RBACPermission.Permissions) - remove --reuse-db --nomigrations flags from engine/tox.ini - minor autoformatting changes to docker-compose-developer.yml * remove --ds=settings.ci-test from pytest CI command DJANGO_SETTINGS_MODULE is already specified as an env var so this is just unecessary duplication * update gitignore * update github action job name for "test" * RBAC frontend changes * refactors the use of basic roles (ex. Viewer, Editor, Admin) use RBAC permissions (when supported), or falling back to basic roles when RBAC is not supported. - updates the UserAction enum in grafana-plugin/src/state/userAction.ts. Previously this was hardcoded to a list of strings that were being returned by the OnCall API. Now the values here correspond to the permissions in plugin.json (plus a fallback role) * changes per Gabriel's comments: - get rid of group attribute in rbac roles - remove displayName role attribute - remove hidden role attribute - add back role to includes section * don't try to update user timezone if they don't have permission
2022-11-29 09:41:56 +01:00
"""
LEGACY, should be removed eventually
"""
return LegacyAccessControlRole(obj.role).name.lower()
def get_is_phone_number_verified(self, obj: User) -> bool:
return obj.verified_phone_number is not None
class UserSerializer(serializers.ModelSerializer, EagerLoadingMixin):
id: str = serializers.ReadOnlyField(read_only=True, source="public_primary_key")
grafana_id: int = serializers.IntegerField(read_only=True, source="user_id")
email: str = serializers.EmailField(read_only=True)
slack: SlackUserIdentity = SlackUserIdentitySerializer(read_only=True, source="slack_user_identity")
role: str = serializers.SerializerMethodField() # LEGACY, should be removed eventually
is_phone_number_verified: bool = serializers.SerializerMethodField()
teams: list[str] = serializers.SlugRelatedField(read_only=True, many=True, slug_field="public_primary_key")
SELECT_RELATED = [
"slack_user_identity",
"slack_user_identity__slack_team_identity",
]
PREFETCH_RELATED = ["teams"]
class Meta:
model = User
fields = [
"id",
"grafana_id",
"email",
"slack",
"username",
"role",
"is_phone_number_verified",
"timezone",
"teams",
]
read_only_fields = ["timezone"]
@staticmethod
def get_role(obj: User) -> str:
Add RBAC Support (#777) * Modify plugin.json to support RBAC role registration * defines 26 new custom roles in plugin.json. The main roles are: - Admin: read/write access to everything in OnCall - Reader: read access to everything in OnCall - OnCaller : read access to everything in OnCall + edit access to Alert Groups and Schedules - <object-type> Editor: read/write access to everything related to <object-type> - <object-type> Reader: read access for <object-type> - User Settings Admin: read/write access to all user's settings, not just own settings. This is in comparison to User Settings Editor which can only read/write own settings * update changelog and documentation (#686) * implement RBAC for OnCall backend This commit refactors backend authorization. It trys to use RBAC authorization if the org's grafana instance supports it, otherwise it falls back to basic role authorization. * update RBAC backend tests * add tests for RBAC changes - run backend tests as matrix where RBAC is enabled/disabled. When RBAC is enabled, the permissions granted are read from the role grants in the frontend's plugin.json file (instead of relying what we specify in RBACPermission.Permissions) - remove --reuse-db --nomigrations flags from engine/tox.ini - minor autoformatting changes to docker-compose-developer.yml * remove --ds=settings.ci-test from pytest CI command DJANGO_SETTINGS_MODULE is already specified as an env var so this is just unecessary duplication * update gitignore * update github action job name for "test" * RBAC frontend changes * refactors the use of basic roles (ex. Viewer, Editor, Admin) use RBAC permissions (when supported), or falling back to basic roles when RBAC is not supported. - updates the UserAction enum in grafana-plugin/src/state/userAction.ts. Previously this was hardcoded to a list of strings that were being returned by the OnCall API. Now the values here correspond to the permissions in plugin.json (plus a fallback role) * changes per Gabriel's comments: - get rid of group attribute in rbac roles - remove displayName role attribute - remove hidden role attribute - add back role to includes section * don't try to update user timezone if they don't have permission
2022-11-29 09:41:56 +01:00
"""
LEGACY, should be removed eventually
"""
return LegacyAccessControlRole(obj.role).name.lower()
def get_is_phone_number_verified(self, obj: User) -> bool:
return obj.verified_phone_number is not None