Skip to content

Commit

Permalink
feat(distribution): allow custom permissions from the host app
Browse files Browse the repository at this point in the history
  • Loading branch information
anehx authored and czosel committed May 11, 2022
1 parent 2f45c98 commit 8a997bf
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 15 deletions.
3 changes: 3 additions & 0 deletions packages/distribution/addon/abilities/distribution.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default class DistributionAbility extends Ability {
get canSendInquiries() {
return (
!this.config.ui.readonly &&
(this.config.permissions.sendInquiry?.(null) ?? true) &&
this.distribution.controls.value?.send.edges.filter(
hasStatus("SUSPENDED")
).length > 0
Expand All @@ -22,6 +23,7 @@ export default class DistributionAbility extends Ability {
get canCreateInquiry() {
return (
!this.config.ui.readonly &&
(this.config.permissions.createInquiry?.() ?? true) &&
this.distribution.controls.value?.create.edges.filter(hasStatus("READY"))
.length > 0
);
Expand All @@ -30,6 +32,7 @@ export default class DistributionAbility extends Ability {
get canComplete() {
return (
!this.config.ui.readonly &&
(this.config.permissions.completeDistribution?.() ?? true) &&
this.distribution.controls.value?.complete.edges.filter(
hasStatus("READY")
).length > 0
Expand Down
17 changes: 17 additions & 0 deletions packages/distribution/addon/abilities/inquiry.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ export default class InquiryAbility extends Ability {
);
}

get canSend() {
return this.config.permissions.sendInquiry?.(this.model) ?? true;
}

get canWithdraw() {
return this.config.permissions.withdrawInquiry?.(this.model) ?? true;
}

get canAnswer() {
return (
!this.config.ui.readonly &&
Expand All @@ -39,4 +47,13 @@ export default class InquiryAbility extends Ability {
)
);
}

get canCompleteChildWorkItem() {
return (
this.config.permissions.completeInquiryChildWorkItem?.(
this.model,
this.task
) ?? true
);
}
}
13 changes: 13 additions & 0 deletions packages/distribution/addon/components/cd-inquiry-answer-form.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@
/>
</div>

{{#if
(and
(not this.buttons.length)
(can "edit answer form of inquiry" this.inquiry)
)
}}
<div class="uk-alert uk-alert-warning uk-flex uk-flex-middle">
<UkIcon @icon="warning" class="uk-margin-small-right" />
{{t "caluma.distribution.answer.complete-not-allowed"}}
{{t "caluma.distribution.not-allowed-hint"}}
</div>
{{/if}}

<content.form />

{{#each this.buttons as |buttonConfig|}}
Expand Down
28 changes: 19 additions & 9 deletions packages/distribution/addon/components/cd-inquiry-answer-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import inquiryAnswerQuery from "@projectcaluma/ember-distribution/gql/queries/in
export default class CdInquiryAnswerFormComponent extends Component {
@service intl;
@service router;
@service abilities;
@service notification;

@config config;
Expand All @@ -27,16 +28,25 @@ export default class CdInquiryAnswerFormComponent extends Component {
}

get buttons() {
return this.inquiry?.childCase.workItems.edges.map((edge) => {
const config = this.config.inquiry.answer.buttons[edge.node.task.slug];
return this.inquiry?.childCase.workItems.edges
.map((edge) => {
const config = this.config.inquiry.answer.buttons[edge.node.task.slug];

return {
workItemId: decodeId(edge.node.id),
color: config.color,
isFormButton: edge.node.task.__typename === "CompleteWorkflowFormTask",
label: this.intl.t(config.label),
};
});
return this.abilities.can(
"complete child work item of inquiry",
this.inquiry,
{ task: edge.node.task.slug }
)
? {
workItemId: decodeId(edge.node.id),
color: config.color,
isFormButton:
edge.node.task.__typename === "CompleteWorkflowFormTask",
label: this.intl.t(config.label),
}
: null;
})
.filter(Boolean);
}

@dropTask
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,17 @@
{{t "caluma.distribution.edit.link"}}
</LinkTo>
</li>
<li>
<a href="" {{on "click" (perform this.withdraw)}} data-test-withdraw>
{{t "caluma.distribution.withdraw.link"}}
</a>
</li>
{{#if (can "withdraw inquiry" @inquiry)}}
<li>
<a
href=""
{{on "click" (perform this.withdraw)}}
data-test-withdraw
>
{{t "caluma.distribution.withdraw.link"}}
</a>
</li>
{{/if}}
{{else if (can "answer inquiry" @inquiry)}}
<li>
<LinkTo
Expand Down
12 changes: 11 additions & 1 deletion packages/distribution/addon/components/cd-inquiry-edit-form.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@

<hr />

{{#if (cannot "send inquiry" this.inquiry)}}
<div class="uk-alert uk-alert-warning uk-flex uk-flex-middle">
<UkIcon @icon="warning" class="uk-margin-small-right" />
{{t "caluma.distribution.edit.send-not-allowed"}}
{{t "caluma.distribution.not-allowed-hint"}}
</div>
{{/if}}

<content.form />

{{#if (can "edit inquiry" this.inquiry)}}
{{#if
(and (can "edit inquiry" this.inquiry) (can "send inquiry" this.inquiry))
}}
<DocumentValidity @document={{content.document}} as |isValid validate|>
<UkButton
@type="submit"
Expand Down
1 change: 1 addition & 0 deletions packages/distribution/addon/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export default function config(target, property) {
},
},
},
permissions: {},
},
getOwner(this).lookup("service:calumaOptions")?.distribution ?? {}
);
Expand Down
16 changes: 16 additions & 0 deletions packages/distribution/tests/unit/abilities/distribution-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,20 @@ module("Unit | Ability | distribution", function (hooks) {

assert.false(ability.canComplete);
});

test("it respects configured custom permissions", async function (assert) {
const ability = this.owner.lookup("ability:distribution");

this.owner.lookup("service:caluma-options").distribution = {
permissions: {
completeDistribution: () => false,
createInquiry: () => false,
sendInquiry: () => false,
},
};

assert.false(ability.canComplete);
assert.false(ability.canCreateInquiry);
assert.false(ability.canSendInquiries);
});
});
35 changes: 35 additions & 0 deletions packages/distribution/tests/unit/abilities/inquiry-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { setupMirage } from "ember-cli-mirage/test-support";
import { setupTest } from "ember-qunit";
import { module, test } from "qunit";

module("Unit | Ability | inquiry", function (hooks) {
setupTest(hooks);
setupMirage(hooks);

hooks.beforeEach(async function () {
this.owner.lookup("service:caluma-options").currentGroupId = 1;
});

test("it respects configured custom permissions", async function (assert) {
assert.expect(4);

const ability = this.owner.lookup("ability:inquiry");

ability.task = "test-task";

this.owner.lookup("service:caluma-options").distribution = {
permissions: {
sendInquiry: () => false,
withdrawInquiry: () => false,
completeInquiryChildWorkItem: (_, task) => {
assert.strictEqual(task, "test-task");
return false;
},
},
};

assert.false(ability.canSend);
assert.false(ability.canWithdraw);
assert.false(ability.canCompleteChildWorkItem);
});
});
1 change: 1 addition & 0 deletions packages/distribution/tests/unit/config-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ module("Unit | config", function (hooks) {
},
},
},
permissions: {},
ui: { small: false, stack: false, readonly: false },
warningPeriod: 1,
});
Expand Down
5 changes: 5 additions & 0 deletions packages/distribution/translations/de.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ caluma:

last-modified: "Zuletzt bearbeitet von {user} am {date} um {time}"
details: "Details"
not-allowed-hint:
"Sie können jedoch das Formular bearbeiten und die Eingabedaten werden
automatisch gespeichert."

edit:
link: "Bearbeiten"
send: "Senden"
send-error: "Fehler beim Senden der Anfrage"
send-not-allowed: "Sie sind nicht berechtigt diese Anfrage zu versenden."

answer:
link: "Beantworten"
Expand All @@ -30,6 +34,7 @@ caluma:
revise: "Überarbeiten"

complete-error: "Fehler beim Abschliessen der Aufgabe"
complete-not-allowed: "Sie sind nicht berechtigt diese Antwort zu versenden."

new:
title: "Neue Anfrage"
Expand Down
5 changes: 5 additions & 0 deletions packages/distribution/translations/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ caluma:

last-modified: "Last modified by {user} on {date} at {time}"
details: "Details"
not-allowed-hint:
"However, you can edit the form and the input will be saved
automatically."

edit:
link: "Edit"
send: "Send"
send-error: "Error while sending the inquiry"
send-not-allowed: "You are not allowed to send this inquiry."

answer:
link: "Answer"
Expand All @@ -31,6 +35,7 @@ caluma:
revise: "Revise"

complete-error: "Error while completing the work item"
complete-not-allowed: "You are not allowed to send this inquiry answer."

new:
title: "New inquiry"
Expand Down
5 changes: 5 additions & 0 deletions packages/distribution/translations/fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ caluma:

last-modified: "Dernière modification par {user} le {date} à {time}"
details: "Détails"
not-allowed-hint:
"Vous pouvez toutefois modifier le formulaire et les données seront
automatiquement enregistrées."

edit:
link: "Modifier"
send: "Envoyer"
send-error: "Erreur lors de l'envoi de la demande"
send-not-allowed: "Vous n'êtes pas autorisé à envoyer cette demande."

answer:
link: "Répondre"
Expand All @@ -30,6 +34,7 @@ caluma:
revise: "Réviser"

complete-error: "Erreur lors de la clôture de la tâche"
complete-not-allowed: "Vous n'êtes pas autorisé à envoyer cette réponse."

new:
title: "Nouvelle demande"
Expand Down

0 comments on commit 8a997bf

Please sign in to comment.