|
entity
|
Type : Entity
|
|
|
The entity being edited (PublicFormConfig)
|
Methods
|
Async
addPermissionAutomatically
|
addPermissionAutomatically()
|
|
|
Automatically add the missing public create permission
Returns : Promise<void>
|
|
canAddPermissions
|
Type : unknown
|
Default value : signal<boolean>(false)
|
|
|
Whether the current user can add permissions (has admin_app role)
|
|
Readonly
entityType
|
Type : unknown
|
Default value : computed(() => {
const formConfig = this.entity() as PublicFormConfig | undefined;
if (!formConfig) {
return "";
}
const multiFormEntityType = formConfig.forms?.[0]?.entity;
if (multiFormEntityType) {
return multiFormEntityType;
}
const legacyEntityType = (formConfig as { entity?: string }).entity;
return legacyEntityType ?? "";
})
|
|
|
|
Readonly
missingPermissionWarning
|
Type : unknown
|
Default value : computed(() =>
this.permissionService.missingPublicPermissionWarning(this.entityType()),
)
|
|
|
Shared with the save confirmation dialog, see PublicFormPermissionService.
|
|
Readonly
permissionCheck
|
Type : unknown
|
Default value : resource({
params: () => ({ entityType: this.entityType() }),
loader: async ({ params: { entityType } }) => {
if (!entityType) return true;
try {
return await this.permissionService.hasPublicCreatePermission(
entityType,
);
} catch (error) {
Logging.error("Failed to check public permissions:", error);
return false;
}
},
})
|
|
|
import {
Component,
computed,
resource,
input,
inject,
signal,
ChangeDetectionStrategy,
} from "@angular/core";
import { AlertService } from "app/core/alerts/alert.service";
import { Entity } from "app/core/entity/model/entity";
import { PublicFormPermissionService } from "../public-form-permission.service";
import { PublicFormConfig } from "../public-form-config";
import { HintBoxComponent } from "app/core/common-components/hint-box/hint-box.component";
import { MatButtonModule } from "@angular/material/button";
import { Logging } from "#src/app/core/logging/logging.service";
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: "app-public-form-permission-warning",
templateUrl: "./public-form-permission-warning.component.html",
imports: [HintBoxComponent, MatButtonModule],
})
export class PublicFormPermissionWarningComponent {
private readonly permissionService = inject(PublicFormPermissionService);
private readonly alertService = inject(AlertService);
/**
* The entity being edited (PublicFormConfig)
*/
entity = input<Entity>();
/**
* Whether the current user can add permissions (has admin_app role)
*/
canAddPermissions = signal<boolean>(false);
readonly entityType = computed(() => {
const formConfig = this.entity() as PublicFormConfig | undefined;
if (!formConfig) {
return "";
}
const multiFormEntityType = formConfig.forms?.[0]?.entity;
if (multiFormEntityType) {
return multiFormEntityType;
}
const legacyEntityType = (formConfig as { entity?: string }).entity;
return legacyEntityType ?? "";
});
/** Shared with the save confirmation dialog, see PublicFormPermissionService. */
readonly missingPermissionWarning = computed(() =>
this.permissionService.missingPublicPermissionWarning(this.entityType()),
);
readonly permissionCheck = resource({
params: () => ({ entityType: this.entityType() }),
loader: async ({ params: { entityType } }) => {
if (!entityType) return true;
try {
return await this.permissionService.hasPublicCreatePermission(
entityType,
);
} catch (error) {
Logging.error("Failed to check public permissions:", error);
return false;
}
},
});
constructor() {
this.canAddPermissions.set(this.permissionService.hasAdminPermission());
}
/**
* Automatically add the missing public create permission
*/
async addPermissionAutomatically(): Promise<void> {
const entityType = this.entityType();
if (!entityType) return;
try {
await this.permissionService.addPublicCreatePermission(entityType);
this.alertService.addInfo(
$localize`Permission added successfully! The public form should now work correctly.`,
);
this.permissionCheck.reload();
} catch (error) {
Logging.error("Failed to add permission:", error);
this.alertService.addDanger(
$localize`Failed to add permission automatically. Please contact an administrator or check the permissions configuration manually.`,
);
}
}
}
@if (permissionCheck.value() === false) {
<app-hint-box type="warning" class="margin-bottom-regular">
<div>
<strong i18n>⚠️ Missing Public Permission</strong>
<p>{{ missingPermissionWarning() }}</p>
<p>
<strong i18n>Required:</strong>
<span i18n>
The "public" role needs "create" permission for "{{ entityType() }}"
entities.
</span>
</p>
<div class="flex-row gap-small margin-top-small">
@if (canAddPermissions()) {
<button
mat-raised-button
color="accent"
(click)="addPermissionAutomatically()"
i18n
>
Update Permission & Save Form
</button>
} @else {
<p class="text-secondary" i18n>
You need admin permissions to add the required permission
automatically.
</p>
}
</div>
</div>
</app-hint-box>
}
Legend
Html element with directive