Skip to content
Snippets Groups Projects
Commit 02fa1c72 authored by Cam Saul's avatar Cam Saul
Browse files

defannotation macro

parent 4afaf95b
Branches
Tags
No related merge requests found
......@@ -193,10 +193,24 @@
(defmethod arg-annotation-fn :default [annotation-kw arg-symbol]
(throw (Exception. (format "Don't know what to do with arg annotation '%s' on arg '%s'!" (name annotation-kw) (name arg-symbol)))))
(defmacro defannotation
"Convenience for defining a new `defendpoint` arg annotation.
BINDING is the actual symbol name of the arg being checked; `defannotation` returns form(s)
that will be included in the let binding for the annotated arg.
(defannotation required [param]
`(require-params ~param) ; quasiquoting needed to keep require-params from being evaluated at macroexpansion time
param)"
[annotation-name [binding] & body]
`(defmethod arg-annotation-fn ~(keyword annotation-name) [~'_ ~binding]
`(do ~~@body)))
;; `required` just calls require-params
(defmethod arg-annotation-fn :required [_ arg-symbol]
`(do (require-params ~arg-symbol)
~arg-symbol))
(defannotation required [param]
`(require-params ~param)
param)
;;; ### defendpoint
......
......@@ -21,14 +21,14 @@
(hydrate [:org_perms :organization])))
(defendpoint GET "/:id" [id]
(defendpoint GET "/:id" [id fish.required]
;; user must be getting their own details OR they must be a superuser to proceed
(check-403 (or (= id *current-user-id*) (:is_superuser @*current-user*)))
(check-404 (sel :one User :id id)))
(defmethod arg-annotation-fn :email [_ arg-symb]
`(do (check (is-email? ~arg-symb) [400 (format ~(str (name arg-symb) " '%s' is not a valid email.") ~arg-symb)])
~arg-symb))
(defannotation email [email]
`(check (is-email? ~email) [400 (format ~(str (name email) " '%s' is not a valid email.") ~email)])
email)
(defendpoint PUT "/:id" [id :as {{:keys [email.email] :as body} :body}]
;; user must be getting their own details OR they must be a superuser to proceed
......@@ -40,10 +40,12 @@
(mapply upd User id)))
(sel :one User :id id))
(defannotation complex-password [password]
`(check (password/is-complex? ~password) [400 "Insufficient password strength"])
password)
(defendpoint PUT "/:id/password" [id :as {{:keys [password.required old_password.required]} :body}]
(defendpoint PUT "/:id/password" [id :as {{:keys [password.required.complex-password old_password.required]} :body}]
(require-params password old_password)
(check (password/is-complex? password) [400 "Insufficient password strength"])
(check-403 (or (= id *current-user-id*)
(:is_superuser @*current-user*)))
(let-404 [user (sel :one [User :password_salt :password] :id id)]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment