Last active
April 3, 2021 00:02
-
-
Save cjsauer/9ab075ca995d7ee855040271c766db27 to your computer and use it in GitHub Desktop.
Fulcro RAD Authorization Idea
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns io.laef.model.account | |
(:require | |
[com.fulcrologic.rad.form-options :as fo] | |
[com.fulcrologic.rad.attributes :as attr :refer [defattr]] | |
[com.fulcrologic.rad.attributes-options :as ao] | |
[io.laef.authorization :as auth] | |
[io.laef.components.database-queries :as queries])) | |
(defattr id :account/id :uuid | |
{ao/identity? true | |
ao/schema :production | |
;; Anyone can "walk the edge" to an account, but only the account owner | |
;; can delete their account. | |
;; Admins are capable of creating new accounts. | |
::auth/auth (fn [env ident user-ident] | |
(cond-> #{:read} | |
(= ident user-ident) (conj :delete) | |
(queries/admin? env user-ident) (conj :create)))}) | |
(defattr email :account/email :string | |
{ao/identities #{:account/id} | |
ao/required? true | |
ao/schema :production | |
:com.fulcrologic.rad.database-adapters.datomic/attribute-schema | |
{:db/unique :db.unique/value} | |
;; Only account owner can read and modify their email address | |
::auth/auth (fn [_env ident user-ident] | |
(when (= ident user-ident) | |
#{:read :update}))}) | |
(defattr active? :account/active? :boolean | |
{ao/identities #{:account/id} | |
ao/schema :production | |
fo/default-value true | |
;; This attribute is publicly readable, but *nobody* can edit it | |
}) | |
(defattr password :password/hashed-value :string | |
{ao/required? true | |
ao/identities #{:account/id} | |
ao/schema :production | |
;; Account owner can update their own password, but *nobody* can read it | |
::auth/auth (fn [env ident user-ident] | |
(when (= ident user-ident) | |
#{:update}))}) | |
(defattr widget-id :widget/id :uuid | |
{ao/identity? true | |
ao/schema :production}) | |
(defattr widget-name :widget/name :string | |
{ao/identities #{:widget/id} | |
ao/required? true | |
ao/schema :production}) | |
(defattr widgets :account/widgets :ref | |
{ao/identities #{:account/id} | |
ao/schema :production | |
ao/cardinality :many | |
ao/target :widget/id | |
;; Only the account owner can "walk the edge" to their owned widgets | |
::auth/auth (fn [env ident user-ident] | |
(when (= ident user-ident) | |
#{:read}))}) | |
(def attributes [id email active? password | |
widgets widget-id widget-name | |
]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; ... | |
(def save-middleware | |
(-> | |
(datomic/wrap-datomic-save) | |
(blob/wrap-persist-images model/all-attributes) | |
(r.s.middleware/wrap-rewrite-values) | |
;; Auth must come last in threaded order (first in execution order) | |
(auth/save-middleware))) | |
(def delete-middleware | |
(-> (datomic/wrap-datomic-delete) | |
;; Auth must come last in threaded order (first in execution order) | |
(auth/delete-middleware))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; ... | |
(pathom/new-parser config | |
[(auth/pathom-plugin | |
;; Accepts a (fn [env]) as an argument and should return the authenticated user's ident | |
;; TODO: actually look at the ring reqest, hard-coded for demonstration | |
(constantly [:account/id (ids/new-uuid 1)])) | |
;; Other RAD plugins... | |
] | |
[automatic-resolvers | |
;; Other resolvers... | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment