Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
65a8c23d
Commit
65a8c23d
authored
10 years ago
by
Cam Saul
Browse files
Options
Downloads
Plain Diff
Fix accidental detached head wonky git shennanigans
parents
e18fa58e
1bd68a4e
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/metabase/api/common.clj
+13
-13
13 additions, 13 deletions
src/metabase/api/common.clj
src/metabase/api/user.clj
+12
-10
12 additions, 10 deletions
src/metabase/api/user.clj
test/metabase/api/common_test.clj
+2
-2
2 additions, 2 deletions
test/metabase/api/common_test.clj
with
27 additions
and
25 deletions
src/metabase/api/common.clj
+
13
−
13
View file @
65a8c23d
...
...
@@ -180,11 +180,13 @@
Dispatches on the arg annotation as a keyword, and is also passed the symbol
of the argument that should be checked.
(defendpoint GET ... [id
.
required
]
)
(defendpoint GET ... [id
] {id
required
}
)
-> (let [id ~(arg-annotation-fn :required id)] ...)
-> (let [id ~(arg-annotation-fn :required id)]
...)
-> (let [id (do (require-params id) id)] ...)"
-> (let [id (do (require-params id) id)]
...)"
(
fn
[
annotation-kw
arg-symb
]
{
:pre
[(
keyword?
annotation-kw
)
(
symbol?
arg-symb
)]}
...
...
@@ -194,32 +196,28 @@
(
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
)))))
;; ### defannotation
(
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
r
equired [param]
(defannotation
R
equired [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
(
defannotation
required
[
param
]
`
(
require-params
~
param
)
param
)
;; ### common annotation definitions
;; `req
` is an alias for `required`
(
defannotation
re
q
[
param
]
;; `req
uired` just calls require-params
(
defannotation
Requi
re
d
[
param
]
`
(
require-params
~
param
)
param
)
(
defannotation
int
[
param
]
`
(
Integer/parseInt
~
param
))
;;; ### defendpoint
...
...
@@ -229,10 +227,12 @@
- calls `auto-parse` to automatically parse certain args. e.g. `id` is converted from `String` to `Integer` via `Integer/parseInt`
- converts ROUTE from a simple form like `\"/:id\"` to a typed one like `[\"/:id\" :id #\"[0-9]+\"]`
- sequentially applies specified annotation functions on args to validate or cast them.
- executes BODY inside a `try-catch` block that handles `ApiExceptions`
- automatically calls `wrap-response-if-needed` on the result of BODY
- tags function's metadata in a way that subsequent calls to `define-routes` (see below)
will automatically include the function in the generated `defroutes` form."
{
:arglists
'
([
method
route
args
annotations-map?
&
body
])}
[
method
route
args
&
more
]
{
:pre
[(
or
(
string?
route
)
(
vector?
route
))
...
...
This diff is collapsed.
Click to expand it.
src/metabase/api/user.clj
+
12
−
10
View file @
65a8c23d
...
...
@@ -9,6 +9,14 @@
[
metabase.util
:refer
[
is-email?
select-non-nil-keys
]]
[
metabase.util.password
:as
password
]))
(
defannotation
Email
[
email
]
`
(
check
(
is-email?
~
email
)
[
400
(
format
~
(
str
(
name
email
)
" '%s' is not a valid email."
)
~
email
)])
email
)
(
defannotation
ComplexPassword
[
password
]
`
(
check
(
password/is-complex?
~
password
)
[
400
"Insufficient password strength"
])
password
)
(
defendpoint
GET
"/"
[]
;; user must be a superuser to proceed
...
...
@@ -26,13 +34,9 @@
(
check-403
(
or
(
=
id
*current-user-id*
)
(
:is_superuser
@
*current-user*
)))
(
check-404
(
sel
:one
User
:id
id
)))
(
defannotation
email
[
email
]
`
(
require-params
~
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
]
:as
body
}
:body
}]
{
email
e
mail
}
{
email
[
Required
E
mail
]
}
;; 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*
)))
;; can't change email if it's already taken BY ANOTHER ACCOUNT
...
...
@@ -42,13 +46,10 @@
(
mapply
upd
User
id
)))
(
sel
:one
User
:id
id
))
(
defannotation
complex-pw
[
password
]
`
(
check
(
password/is-complex?
~
password
)
[
400
"Insufficient password strength"
])
password
)
(
defendpoint
PUT
"/:id/password"
[
id
:as
{{
:keys
[
password
old_password
]}
:body
}]
{
password
[
req
complex-pw
]
old_password
re
q
}
{
password
[
Required
ComplexPassword
]
old_password
Requi
re
d
}
(
check-403
(
or
(
=
id
*current-user-id*
)
(
:is_superuser
@
*current-user*
)))
(
let-404
[
user
(
sel
:one
[
User
:password_salt
:password
]
:id
id
)]
...
...
@@ -56,4 +57,5 @@
(
set-user-password
id
password
)
(
sel
:one
User
:id
id
))
(
define-routes
)
This diff is collapsed.
Click to expand it.
test/metabase/api/common_test.clj
+
2
−
2
View file @
65a8c23d
...
...
@@ -176,10 +176,10 @@
(
metabase.api.common.internal/auto-parse
[
id
]
(
metabase.api.common.internal/catch-api-exceptions
(
metabase.api.common.internal/let-annotated-args
{
id
r
equired
}
{
id
R
equired
}
(
clojure.core/->
(
do
(
->404
(
sel
:one
Card
:id
id
)))
metabase.api.common.internal/wrap-response-if-needed
))))))
(
clojure.core/alter-meta!
#
'GET_
:id
clojure.core/assoc
:is-endpoint?
true
))
(
defendpoint
GET
"/:id"
[
id
]
{
id
r
equired
}
{
id
R
equired
}
(
->404
(
sel
:one
Card
:id
id
)))))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment