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

fix: permissions are correctly applied in popup form #1387

Merged
merged 5 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,31 @@ export class ActivitiesOverviewComponent implements OnInitDynamicComponent {
entity: Entity;
records: RecurringActivity[] = [];
listConfig: EntityListConfig;
activityConstructor = RecurringActivity;

constructor(private entityMapper: EntityMapperService) {}

async onInitFromDynamicConfig(config: any) {
onInitFromDynamicConfig(config: any) {
if (config?.config?.columns) {
this.columns = config.config.columns;
}

this.entity = config.entity;
this.records = (
await this.entityMapper.loadType<RecurringActivity>(RecurringActivity)
).filter((activity) => activity.linkedGroups.includes(this.entity.getId()));
this.initializeRelatedActivities();
this.entityMapper
.receiveUpdates(RecurringActivity)
.subscribe((updateEntity) => {
if (updateEntity.type === "update") {
this.records = this.records.filter((activity) =>
activity.linkedGroups.includes(this.entity.getId())
);
this.initializeRelatedActivities();
TheSlimvReal marked this conversation as resolved.
Show resolved Hide resolved
}
});
}

private async initializeRelatedActivities() {
this.records = (
await this.entityMapper.loadType<RecurringActivity>(RecurringActivity)
).filter((activity) => activity.linkedGroups.includes(this.entity.getId()));
}

generateNewRecordFactory(): () => RecurringActivity {
return () => {
const newRecurringActivity = new RecurringActivity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,14 @@
<button
mat-raised-button
(click)="save()"
*appDisabledEntityOperation="{
operation: 'update',
entity: data.entity
}"
[disabled]="form.disabled"
i18n="Save button for forms"
>
Save
</button>

<button
*ngIf="editMode === 'update'"
mat-icon-button
[matMenuTriggerFor]="additional"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { EntitySubrecordModule } from "../entity-subrecord.module";
import { FontAwesomeTestingModule } from "@fortawesome/angular-fontawesome/testing";
import { EntityFormService } from "../../entity-form/entity-form.service";
import { AlertService } from "../../../alerts/alert.service";
import { EntityAbility } from "../../../permissions/ability/entity-ability";

describe("RowDetailsComponent", () => {
let component: RowDetailsComponent<any>;
Expand All @@ -37,6 +38,7 @@ describe("RowDetailsComponent", () => {
{ provide: MatDialogRef, useValue: {} },
],
}).compileComponents();
spyOn(TestBed.inject(EntityAbility), "cannot").and.returnValue(true);
});

beforeEach(() => {
Expand Down Expand Up @@ -77,4 +79,8 @@ describe("RowDetailsComponent", () => {
expect(alertSpy).toHaveBeenCalledWith(message);
expect(closeSpy).not.toHaveBeenCalled();
}));

it("should not disable the form when creating a new entity", () => {
expect(component.form.disabled).toBeFalsy();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
RemoveResult,
} from "../../../entity/entity-remove.service";
import { AlertService } from "../../../alerts/alert.service";
import { EntityAction } from "../../../permissions/permission-types";

/**
* Data interface that must be given when opening the dialog
Expand All @@ -38,6 +39,7 @@ export class RowDetailsComponent<E extends Entity> {

viewOnlyColumns: FormFieldConfig[];
tempEntity: Entity;
editMode: EntityAction = "update";

constructor(
@Inject(MAT_DIALOG_DATA) public data: DetailsComponentData<E>,
Expand All @@ -48,7 +50,13 @@ export class RowDetailsComponent<E extends Entity> {
private alertService: AlertService
) {
this.form = this.formService.createFormGroup(data.columns, data.entity);
if (this.ability.cannot("update", data.entity)) {
if (!this.data.entity._rev) {
this.editMode = "create";
}
if (
this.editMode === "update" &&
this.ability.cannot("update", data.entity)
) {
this.form.disable();
}
this.tempEntity = this.data.entity;
Expand Down