Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sources: prevent deletion of built-in source #12914

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions authentik/core/api/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from drf_spectacular.utils import OpenApiResponse, extend_schema
from rest_framework import mixins
from rest_framework.decorators import action
from rest_framework.exceptions import ValidationError
from rest_framework.fields import CharField, ReadOnlyField, SerializerMethodField
from rest_framework.parsers import MultiPartParser
from rest_framework.request import Request
Expand Down Expand Up @@ -154,6 +155,17 @@
matching_sources.append(source_settings.validated_data)
return Response(matching_sources)

def destroy(self, request: Request, *args, **kwargs):
"""Prevent deletion of built-in sources"""
instance = self.get_object()

Check warning on line 160 in authentik/core/api/sources.py

View check run for this annotation

Codecov / codecov/patch

authentik/core/api/sources.py#L160

Added line #L160 was not covered by tests

if instance.slug.startswith("authentik-built-in"):
raise ValidationError(

Check warning on line 163 in authentik/core/api/sources.py

View check run for this annotation

Codecov / codecov/patch

authentik/core/api/sources.py#L162-L163

Added lines #L162 - L163 were not covered by tests
{"detail": "Built-in sources cannot be deleted"}, code="protected"
)

return super().destroy(request, *args, **kwargs)

Check warning on line 167 in authentik/core/api/sources.py

View check run for this annotation

Codecov / codecov/patch

authentik/core/api/sources.py#L167

Added line #L167 was not covered by tests


class UserSourceConnectionSerializer(SourceSerializer):
"""User source connection"""
Expand Down
7 changes: 5 additions & 2 deletions web/src/admin/sources/SourceListPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ export class SourceListPage extends TablePage<Source> {
}

renderToolbarSelected(): TemplateResult {
const disabled = this.selectedElements.length < 1;
const disabled =
this.selectedElements.length < 1 ||
this.selectedElements.some((item) => item.component === "");
const nonBuiltInSources = this.selectedElements.filter((item) => item.component !== "");
return html`<ak-forms-delete-bulk
objectLabel=${msg("Source(s)")}
.objects=${this.selectedElements}
.objects=${nonBuiltInSources}
.usedBy=${(item: Source) => {
return new SourcesApi(DEFAULT_CONFIG).sourcesAllUsedByList({
slug: item.slug,
Expand Down
Loading