From ad48bd9eb8d530eae384dbb74fa9c805a2fa5a31 Mon Sep 17 00:00:00 2001 From: Bingen Galartza Iparragirre Date: Fri, 18 Oct 2024 17:12:22 +0200 Subject: [PATCH] Implement get-contexts-by-selectors method Add `get-contexts-by-selectors` to get a subset of the existing contexts. --- CHANGELOG.md | 4 ++++ src/dev/gethop/rbac/next.clj | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5802c9e..f1da9b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/dev/gethop/rbac/next.clj b/src/dev/gethop/rbac/next.clj index 3adef60..27212a1 100644 --- a/src/dev/gethop/rbac/next.clj +++ b/src/dev/gethop/rbac/next.clj @@ -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))