<commit_before>(ns akvo.lumen.lib.auth
  (:require
   [akvo.commons.jwt :as jwt]
   [akvo.lumen.component.flow :as c.flow]
   [akvo.lumen.component.tenant-manager :as tenant-manager]
   [akvo.lumen.specs :as lumen.s]
   [akvo.lumen.specs.dataset :as dataset.s]
   [akvo.lumen.specs.visualisation :as visualisation.s]
   [akvo.lumen.specs.dashboard :as dashboard.s]
   [akvo.lumen.protocols :as p]
   [clojure.spec.alpha :as s]
   [clojure.string :as str]
   [clojure.tools.logging :as log]
   [clojure.set :as set]
   [hugsql.core :as hugsql]
   [integrant.core :as ig]
   [reitit.core :as rc]))

(hugsql/def-db-fns "akvo/lumen/lib/visualisation.sql")
(hugsql/def-db-fns "akvo/lumen/lib/dataset.sql")
(hugsql/def-db-fns "akvo/lumen/lib/dashboard.sql")

(declare ids)

(defrecord AuthServiceImpl [auth-datasets-set auth-visualisations-set auth-dashboards-set]
  p/AuthService
  (allow? [this d]
    (and (set/subset? (:visualisation-ids d) auth-visualisations-set)
         (set/subset? (:dataset-ids d) auth-datasets-set)
         (set/subset? (:dashboard-ids d) auth-dashboards-set)))
  (auth [this {:keys [dataset-ids visualisation-ids dashboard-ids]}]
    {:auth-datasets (set/intersection auth-datasets-set (set dataset-ids))
     :auth-visualisations (set/intersection auth-visualisations-set (set visualisation-ids))
     :auth-dashboards (set/intersection auth-dashboards-set (set dashboard-ids))})
  (auth [this type* uuid]
    (condp = type*
      :dataset (when (contains? auth-datasets-set uuid) uuid)
      :visualisation (when (contains? auth-visualisations-set uuid) uuid)
      :dashboard (when (contains? auth-dashboards-set uuid) uuid))))

(defn new-auth-service [{:keys [auth-datasets auth-visualisations auth-dashboards] :as auth-uuid-tree}]
  (AuthServiceImpl. (set auth-datasets) (set auth-visualisations) (set auth-dashboards)))

(defn match-by-jwt-family-name?<commit_msg>Refactor<commit_after>(ns akvo.lumen.lib.auth
  (:require
   [akvo.commons.jwt :as jwt]
   [akvo.lumen.component.flow :as c.flow]
   [akvo.lumen.component.tenant-manager :as tenant-manager]
   [akvo.lumen.specs :as lumen.s]
   [akvo.lumen.specs.dataset :as dataset.s]
   [akvo.lumen.specs.visualisation :as visualisation.s]
   [akvo.lumen.specs.dashboard :as dashboard.s]
   [akvo.lumen.protocols :as p]
   [clojure.spec.alpha :as s]
   [clojure.string :as str]
   [clojure.tools.logging :as log]
   [clojure.set :as set]
   [hugsql.core :as hugsql]
   [integrant.core :as ig]
   [reitit.core :as rc]))

(hugsql/def-db-fns "akvo/lumen/lib/visualisation.sql")
(hugsql/def-db-fns "akvo/lumen/lib/dataset.sql")
(hugsql/def-db-fns "akvo/lumen/lib/dashboard.sql")

(declare ids)

(defrecord AuthServiceImpl [auth-datasets-set auth-visualisations-set auth-dashboards-set]
  p/AuthService
  (allow? [this ids]
    (and (set/subset? (:visualisation-ids ids) auth-visualisations-set)
         (set/subset? (:dataset-ids ids) auth-datasets-set)
         (set/subset? (:dashboard-ids ids) auth-dashboards-set)))
  (auth [this {:keys [dataset-ids visualisation-ids dashboard-ids]}]
    {:auth-datasets (set/intersection auth-datasets-set (set dataset-ids))
     :auth-visualisations (set/intersection auth-visualisations-set (set visualisation-ids))
     :auth-dashboards (set/intersection auth-dashboards-set (set dashboard-ids))})
  (auth [this type* uuid]
    (condp = type*
      :dataset (when (contains? auth-datasets-set uuid) uuid)
      :visualisation (when (contains? auth-visualisations-set uuid) uuid)
      :dashboard (when (contains? auth-dashboards-set uuid) uuid))))

(defn new-auth-service [{:keys [auth-datasets auth-visualisations auth-dashboards] :as auth-uuid-tree}]
  (AuthServiceImpl. (set auth-datasets) (set auth-visualisations) (set auth-dashboards)))

(defn match-by-jwt-family-name?
{'fraction_non_alphanumeric': 0.1518586870551015, 'fraction_numerical': 0.0, 'mean_word_length': 3.9986824769433467, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4992961', 'n_tokens_mistral': 1446, 'n_tokens_neox': 1413, 'n_words': 279}