Skip to content

Commit

Permalink
Implement get-contexts-by-selectors method
Browse files Browse the repository at this point in the history
Add `get-contexts-by-selectors` to get a subset of the existing
contexts.
  • Loading branch information
bgalartza committed Oct 18, 2024
1 parent 0fffca6 commit ad48bd9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add `get-contexts-by-selectors` to get a subset of the existing contexts.

## [0.1.0-alpha-6] - 2024.10.01

### Fixed
Expand Down
26 changes: 26 additions & 0 deletions src/dev/gethop/rbac/next.clj
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,32 @@
(let [return (get-* db-spec :rbac_context :contexts)]
(update return :contexts #(mapv db-context->context %))))

(s/def ::context-selector (s/keys :req-un [::context-type-name
::resource-id]))
(s/def ::context-selectors (s/coll-of ::context-selector))
(s/def ::get-contexts-by-selectors-args (s/cat :db-spec ::db-spec :context-selectors ::context-selectors))
(s/def ::get-contexts-by-selectors-ret (s/keys :req-un [::success?
::contexts]))
(s/fdef get-contexts-by-selectors
:args ::get-contexts-by-selectors-args
:ret ::get-contexts-by-selectors-ret)

(defn get-contexts-by-selectors
[db-spec context-selectors]
{:pre [(s/valid? ::db-spec db-spec)
(s/valid? ::context-selectors context-selectors)]}
(let [{:keys [success? values]}
(get-*-where-y db-spec :rbac-context
(reduce
(fn [condition {:keys [context-type-name resource-id]}]
(conj condition [:and
[:= :context-type-name (kw->str context-type-name)]
[:= :resource-id resource-id]]))
[:or]
context-selectors))]
{:success? success?
:contexts (map db-context->context values)}))

(s/def ::get-context-args (s/cat :db-spec ::db-spec
:context-type-name ::context-type-name
:resource-id ::resource-id))
Expand Down

0 comments on commit ad48bd9

Please sign in to comment.