oncall-engine/engine/apps/api/views/alert_group_table_settings.py

57 lines
2.7 KiB
Python
Raw Permalink Normal View History

import typing
from rest_framework.permissions import IsAuthenticated
from rest_framework.request import Request
from rest_framework.response import Response
from apps.api.alert_group_table_columns import alert_group_table_user_settings
from apps.api.permissions import RBACPermission
from apps.api.serializers.alert_group_table_settings import (
AlertGroupTableColumnsOrganizationSerializer,
AlertGroupTableColumnsUserSerializer,
)
from apps.api.views.labels import LabelsFeatureFlagViewSet
from apps.auth_token.auth import PluginAuthentication
Google OAuth2 flow + fetch Google Calendar OOO events (#4067) # What this PR does The following is deployed under a feature flag. **How it works** 1. The user clicks on the "Connect using your Google account" button in the user profile settings modal 2. The UI makes a call to `GET /api/internal/v1/login/google-oauth2`. The backend has now been configured to add `apps.social_auth.backends.GoogleOAuth2` as a "`social_auth` backend". 3. The backend will respond w/ a URL which points to the Google OAuth2 consent screen. The frontend then proceeds by sending the user to this page. This URL includes the following query parameters (amongst others): - `redirect_uri` - this will send the user back to `/api/internal/v1/complete/google-oauth2` (ie. make another API call to the OnCall backend to finalize the Google OAuth2 flow) - `state` - this represents an `apps.auth_token.models.GoogleOAuth2Token` token. This allows us to identify the OnCall user once they've linked their Google account. 4. Once redirected back to `/api/internal/v1/complete/google-oauth2`, this will complete the OAuth2 flow. At this point, the backend has access to several pieces of information about the Google user, including their `access_token` and `refresh_token`. We persist these (encrypted) for future use to fetch the user's out-of-office calendar events 5. The response from the API call in 4 above ☝️ is HTTP 302 (redirect) to `/a/grafana-oncall-app/users/me` (ie. open the user profile settings modal). At this point the user will see that their account has been connected and they can further configure the settings ![image](https://github.com/grafana/oncall/assets/9406895/c7673055-8485-4f9a-98df-b4f7347229ce) ## Which issue(s) this PR closes Closes https://github.com/grafana/oncall-private/issues/2584 ## Checklist - [x] Unit, integration, and e2e (if applicable) tests updated - [x] Documentation added (or `pr:no public docs` PR label added if not required) - will be done in https://github.com/grafana/oncall-private/issues/2591 - [x] Added the relevant release notes label (see labels prefixed w/ `release:`). These labels dictate how your PR will show up in the autogenerated release notes. - will be done in https://github.com/grafana/oncall-private/issues/2591 --------- Co-authored-by: Dominik <dominik.broj@grafana.com> Co-authored-by: Maxim Mordasov <maxim.mordasov@grafana.com>
2024-04-02 14:59:03 -04:00
from apps.user_management.constants import default_columns
from apps.user_management.types import AlertGroupTableColumn
class AlertGroupTableColumnsViewSet(LabelsFeatureFlagViewSet):
authentication_classes = (PluginAuthentication,)
permission_classes = (IsAuthenticated, RBACPermission)
rbac_permissions = {
"get_columns": [RBACPermission.Permissions.ALERT_GROUPS_READ],
"update_user_columns": [RBACPermission.Permissions.ALERT_GROUPS_READ],
"reset_user_columns": [RBACPermission.Permissions.ALERT_GROUPS_READ],
"update_organization_columns": [RBACPermission.Permissions.OTHER_SETTINGS_WRITE],
}
def get_columns(self, request: Request) -> Response:
return Response(alert_group_table_user_settings(request.user))
def update_organization_columns(self, request: Request) -> Response:
"""add/remove columns for organization"""
serializer = AlertGroupTableColumnsOrganizationSerializer(data=request.data, context={"request": request})
serializer.is_valid(raise_exception=True)
columns: typing.List[AlertGroupTableColumn] = serializer.validated_data.get(
"visible", []
) + serializer.validated_data.get("hidden", [])
request.auth.organization.update_alert_group_table_columns(columns)
return Response(alert_group_table_user_settings(request.user))
def update_user_columns(self, request: Request) -> Response:
"""select/hide/change order for user"""
user = request.user
serializer = AlertGroupTableColumnsUserSerializer(data=request.data, context={"request": request})
serializer.is_valid(raise_exception=True)
columns: typing.List[AlertGroupTableColumn] = serializer.validated_data.get("visible", [])
user.update_alert_group_table_selected_columns(columns)
return Response(alert_group_table_user_settings(user))
def reset_user_columns(self, request: Request) -> Response:
"""set default alert group table settings for user"""
user = request.user
user.update_alert_group_table_selected_columns(default_columns())
return Response(alert_group_table_user_settings(user))