2022-06-03 08:09:47 -06:00
|
|
|
from contextlib import suppress
|
|
|
|
|
|
|
|
|
|
from rest_framework import status
|
|
|
|
|
from rest_framework.permissions import IsAuthenticated
|
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
|
|
2022-11-29 09:41:56 +01:00
|
|
|
from apps.api.permissions import RBACPermission
|
2024-04-11 11:51:56 -03:00
|
|
|
from apps.api.serializers.organization import CurrentOrganizationConfigChecksSerializer, CurrentOrganizationSerializer
|
2022-06-03 08:09:47 -06:00
|
|
|
from apps.auth_token.auth import PluginAuthentication
|
|
|
|
|
from apps.base.messaging import get_messaging_backend_from_id
|
2023-07-21 17:38:58 +02:00
|
|
|
from apps.mobile_app.auth import MobileAppAuthTokenAuthentication
|
2022-06-03 08:09:47 -06:00
|
|
|
from apps.telegram.client import TelegramClient
|
2022-08-24 12:04:44 +05:00
|
|
|
from common.insight_log import EntityEvent, write_resource_insight_log
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class CurrentOrganizationView(APIView):
|
2023-07-21 17:38:58 +02:00
|
|
|
authentication_classes = (
|
|
|
|
|
MobileAppAuthTokenAuthentication,
|
|
|
|
|
PluginAuthentication,
|
|
|
|
|
)
|
2022-11-29 09:41:56 +01:00
|
|
|
permission_classes = (IsAuthenticated, RBACPermission)
|
2022-06-03 08:09:47 -06:00
|
|
|
|
2022-11-29 09:41:56 +01:00
|
|
|
rbac_permissions = {
|
2023-10-19 14:39:08 -03:00
|
|
|
"get": [RBACPermission.Permissions.OTHER_SETTINGS_READ],
|
2022-11-29 09:41:56 +01:00
|
|
|
"put": [RBACPermission.Permissions.OTHER_SETTINGS_WRITE],
|
|
|
|
|
}
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
|
organization = request.auth.organization
|
|
|
|
|
serializer = CurrentOrganizationSerializer(organization, context={"request": request})
|
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
|
|
|
|
def put(self, request):
|
|
|
|
|
organization = self.request.auth.organization
|
2022-08-24 12:04:44 +05:00
|
|
|
prev_state = organization.insight_logs_serialized
|
2022-06-03 08:09:47 -06:00
|
|
|
serializer = CurrentOrganizationSerializer(
|
|
|
|
|
instance=organization, data=request.data, context={"request": request}
|
|
|
|
|
)
|
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
serializer.save()
|
2022-08-24 12:04:44 +05:00
|
|
|
new_state = serializer.instance.insight_logs_serialized
|
|
|
|
|
write_resource_insight_log(
|
|
|
|
|
instance=serializer.instance,
|
|
|
|
|
author=self.request.user,
|
|
|
|
|
event=EntityEvent.UPDATED,
|
|
|
|
|
prev_state=prev_state,
|
|
|
|
|
new_state=new_state,
|
2022-06-03 08:09:47 -06:00
|
|
|
)
|
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
|
|
|
|
|
2024-04-11 11:51:56 -03:00
|
|
|
class OrganizationConfigChecksView(APIView):
|
|
|
|
|
authentication_classes = (PluginAuthentication,)
|
|
|
|
|
permission_classes = (IsAuthenticated, RBACPermission)
|
|
|
|
|
|
|
|
|
|
rbac_permissions = {
|
|
|
|
|
"get": [RBACPermission.Permissions.OTHER_SETTINGS_READ],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
|
organization = request.auth.organization
|
|
|
|
|
serializer = CurrentOrganizationConfigChecksSerializer(organization)
|
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
class GetTelegramVerificationCode(APIView):
|
|
|
|
|
authentication_classes = (PluginAuthentication,)
|
2022-11-29 09:41:56 +01:00
|
|
|
permission_classes = (IsAuthenticated, RBACPermission)
|
|
|
|
|
|
|
|
|
|
rbac_permissions = {
|
|
|
|
|
"get": [RBACPermission.Permissions.INTEGRATIONS_WRITE],
|
|
|
|
|
}
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
|
organization = request.auth.organization
|
|
|
|
|
user = request.user
|
2023-07-25 10:43:23 +01:00
|
|
|
from apps.telegram.models import TelegramChannelVerificationCode
|
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
with suppress(TelegramChannelVerificationCode.DoesNotExist):
|
|
|
|
|
existing_verification_code = organization.telegram_verification_code
|
|
|
|
|
existing_verification_code.delete()
|
|
|
|
|
new_code = TelegramChannelVerificationCode.objects.create(organization=organization, author=user)
|
|
|
|
|
telegram_client = TelegramClient()
|
|
|
|
|
bot_username = telegram_client.api_client.username
|
|
|
|
|
bot_link = f"https://t.me/{bot_username}"
|
2022-10-25 14:53:07 +08:00
|
|
|
return Response(
|
2022-12-06 22:42:58 +08:00
|
|
|
{"telegram_code": str(new_code.uuid_with_org_uuid), "bot_link": bot_link}, status=status.HTTP_200_OK
|
2022-10-25 14:53:07 +08:00
|
|
|
)
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class GetChannelVerificationCode(APIView):
|
|
|
|
|
authentication_classes = (PluginAuthentication,)
|
2022-11-29 09:41:56 +01:00
|
|
|
permission_classes = (IsAuthenticated, RBACPermission)
|
|
|
|
|
|
|
|
|
|
rbac_permissions = {
|
|
|
|
|
"get": [RBACPermission.Permissions.INTEGRATIONS_WRITE],
|
|
|
|
|
}
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
|
organization = request.auth.organization
|
|
|
|
|
backend_id = request.query_params.get("backend")
|
|
|
|
|
backend = get_messaging_backend_from_id(backend_id)
|
|
|
|
|
if backend is None:
|
|
|
|
|
return Response(status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
|
|
|
|
code = backend.generate_channel_verification_code(organization)
|
|
|
|
|
return Response(code)
|
|
|
|
|
|
|
|
|
|
|
feat: convert `organization.general_log_channel_id` to `organization.default_slack_channel` (#5191)
# What this PR does
Related to https://github.com/grafana/oncall-private/issues/2947
Right now `general_log_channel_id` is just a string value representing
the Slack Channel ID (ex. `C043HQ70QMB`). This PR migrates this instead
to be a foreign key relationship on the `slack_slackchannel` table and
updates all references to `general_log_channel_id`.
Tested migrations locally:
```bash
Operations to perform:
Apply all migrations: [redacted secret grafana-admin-creds:admin-user], alerts, auth, auth_token, base, contenttypes, email, exotel, fcm_django, google, heartbeat, labels, mobile_app, oss_installation, phone_notifications, schedules, sessions, slack, social_django, telegram, twilioapp, user_management, webhooks, zvonok
Running migrations:
Applying user_management.0024_organization_general_log_slack_channel... OK
source=engine:app google_trace_id=none logger=apps.user_management.migrations.0025_auto_20241017_1919 Starting migration to populate general_log_slack_channel field.
source=engine:app google_trace_id=none logger=apps.user_management.migrations.0025_auto_20241017_1919 Total organizations to process: 1
source=engine:app google_trace_id=none logger=apps.user_management.migrations.0025_auto_20241017_1919 Organization 1 updated with SlackChannel 2 (slack_id: C043LL6RTS7).
source=engine:app google_trace_id=none logger=apps.user_management.migrations.0025_auto_20241017_1919 Finished migration. Total organizations processed: 1. Organizations updated: 1. Missing SlackChannels: 0.
Applying user_management.0025_auto_20241017_1919... OK
```
## Future incoming PRs
- Drop `Organization.general_log_channel_id` column
- Migrate `ChannelFilter.slack_channel_id` and
`ResolutionNoteSlackMessage.slack_channel_id` to use foreign key
relationships
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [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.
2024-11-01 06:41:38 +01:00
|
|
|
class SetDefaultSlackChannel(APIView):
|
2022-06-03 08:09:47 -06:00
|
|
|
authentication_classes = (PluginAuthentication,)
|
2022-11-29 09:41:56 +01:00
|
|
|
permission_classes = (IsAuthenticated, RBACPermission)
|
|
|
|
|
|
|
|
|
|
rbac_permissions = {
|
|
|
|
|
"post": [RBACPermission.Permissions.CHATOPS_UPDATE_SETTINGS],
|
|
|
|
|
}
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
def post(self, request):
|
2023-07-25 10:43:23 +01:00
|
|
|
from apps.slack.models import SlackChannel
|
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
organization = request.auth.organization
|
|
|
|
|
slack_team_identity = organization.slack_team_identity
|
|
|
|
|
slack_channel_id = request.data["id"]
|
|
|
|
|
|
|
|
|
|
slack_channel = SlackChannel.objects.get(
|
|
|
|
|
public_primary_key=slack_channel_id, slack_team_identity=slack_team_identity
|
|
|
|
|
)
|
|
|
|
|
|
feat: convert `organization.general_log_channel_id` to `organization.default_slack_channel` (#5191)
# What this PR does
Related to https://github.com/grafana/oncall-private/issues/2947
Right now `general_log_channel_id` is just a string value representing
the Slack Channel ID (ex. `C043HQ70QMB`). This PR migrates this instead
to be a foreign key relationship on the `slack_slackchannel` table and
updates all references to `general_log_channel_id`.
Tested migrations locally:
```bash
Operations to perform:
Apply all migrations: [redacted secret grafana-admin-creds:admin-user], alerts, auth, auth_token, base, contenttypes, email, exotel, fcm_django, google, heartbeat, labels, mobile_app, oss_installation, phone_notifications, schedules, sessions, slack, social_django, telegram, twilioapp, user_management, webhooks, zvonok
Running migrations:
Applying user_management.0024_organization_general_log_slack_channel... OK
source=engine:app google_trace_id=none logger=apps.user_management.migrations.0025_auto_20241017_1919 Starting migration to populate general_log_slack_channel field.
source=engine:app google_trace_id=none logger=apps.user_management.migrations.0025_auto_20241017_1919 Total organizations to process: 1
source=engine:app google_trace_id=none logger=apps.user_management.migrations.0025_auto_20241017_1919 Organization 1 updated with SlackChannel 2 (slack_id: C043LL6RTS7).
source=engine:app google_trace_id=none logger=apps.user_management.migrations.0025_auto_20241017_1919 Finished migration. Total organizations processed: 1. Organizations updated: 1. Missing SlackChannels: 0.
Applying user_management.0025_auto_20241017_1919... OK
```
## Future incoming PRs
- Drop `Organization.general_log_channel_id` column
- Migrate `ChannelFilter.slack_channel_id` and
`ResolutionNoteSlackMessage.slack_channel_id` to use foreign key
relationships
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [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.
2024-11-01 06:41:38 +01:00
|
|
|
organization.set_default_slack_channel(slack_channel, request.user)
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
return Response(status=200)
|