MishkaDocument.Resources.Validations.MasterCommentReadOnly (Mishka Document v0.0.1)

Copy Markdown View Source

Stops a tenant changing an issue comment a master wrote.

What it is for

A master can write on any site's issue, and a tenant admin of that site can read what they wrote. Nothing in the policies stops the tenant editing or deleting it too — both are admins, and the comment sits on their own site — so a master's answer on an issue could be rewritten or removed by the site it was written to. A master's comment is theirs; the tenant only sees it.

Only a tenant actor (one with a site_id) is checked. A master is never refused, and neither is system code running without an actor, such as the seeds archiving comments. authorize?: false does not skip it: a tenant actor is a tenant actor either way.

Usage

update :update do
  validate MasterCommentReadOnly
end

Put it on the tenant :update, :destroy, :unarchive and :permanent_destroy, never on their master_* twins.

It reads the comment's CURRENT contributor_id — Ash.Changeset.get_data/2, not get_attribute/2 — because the question is who wrote the row, not what the changeset would like it to say. Who counts as a master is decided by MishkaDocument.Resources.Calculations.AuthoredByMaster.master_ids/1, the same read that tells the admin which comments to draw without Edit and Delete. When that read fails the change is refused with its error rather than let through.