<commit_before>(ns oc.storage.api.access
  "Access control functions for storage API."
  (:require [if-let.core :refer (if-let*)]
            [defun.core :refer (defun)]
            [taoensso.timbre :as timbre]
            [oc.lib.schema :as lib-schema]
            [oc.lib.slugify :as slugify]
            [oc.storage.resources.org :as org-res]
            [oc.storage.resources.board :as board-res]))

;; ----- Validation -----

(defn malformed-user-id?
  "Read in the body param from the request and make sure it's a non-blank string
  that corresponds to a user-id. Otherwise just indicate it's malformed."
  [ctx]
  (try
    (if-let* [user-id (slurp (get-in ctx [:request :body]))
              valid? (lib-schema/unique-id? user-id)]
      [false {:data user-id}]
      true)
    (catch Exception e
      (timbre/warn "Request body not processable as a user-id: " e)
      true)))

(defn premium-org? [org user]
  ((set (:premium-teams user)) (:team-id org)))

;; ----- Authorization -----

(defun access-level-for
  "
  Given an org (or slug) and a user map, return the authorization level for the user on the org:
    :author
    :viewer
    :public
    false

  Or, given an org (or slug), board (or slug) and user map, return the authorization level for the user on the board:
    :author
    :viewer
    :public
    false
  "
  ;; Access to org
  ;; Invalid org slug
  ([_conn org-slug :guard #(and (string? %) (not (slugify/valid-slug? %))) _user]
    ;; Will fail existence checks later
    {:access-level :does-not-exist})

  ;; With org slug, check if it exists
  ([conn :guard lib-schema/conn? org-slug :guard slugify/valid-slug? user :guard #(or (map? %) (nil? %))]<commit_msg>Better docstring for access-level-for<commit_after>(ns oc.storage.api.access
  "Access control functions for storage API."
  (:require [if-let.core :refer (if-let*)]
            [defun.core :refer (defun)]
            [taoensso.timbre :as timbre]
            [oc.lib.schema :as lib-schema]
            [oc.lib.slugify :as slugify]
            [oc.storage.resources.org :as org-res]
            [oc.storage.resources.board :as board-res]))

;; ----- Validation -----

(defn malformed-user-id?
  "Read in the body param from the request and make sure it's a non-blank string
  that corresponds to a user-id. Otherwise just indicate it's malformed."
  [ctx]
  (try
    (if-let* [user-id (slurp (get-in ctx [:request :body]))
              valid? (lib-schema/unique-id? user-id)]
      [false {:data user-id}]
      true)
    (catch Exception e
      (timbre/warn "Request body not processable as a user-id: " e)
      true)))

(defn premium-org? [org user]
  ((set (:premium-teams user)) (:team-id org)))

;; ----- Authorization -----

(defun access-level-for
  "
  Given an org (or slug) and a user map, return a map describing the authorization level for the user on the org,
  or nil if the user has no access.
  If a board is specified (slug or map) the map will describe the access level of the user on it.

  Ie:
  {:access-level :author|:viewer|:public|:does-not-exist
   :role :admin|:member|:anonymous
   :premium? true|false}
  "
  ;; Access to org
  ;; Invalid org slug
  ([_conn org-slug :guard #(and (string? %) (not (slugify/valid-slug? %))) _user]
    ;; Will fail existence checks later
    {:access-level :does-not-exist})

  ;; With org slug, check if it exists
  ([conn :guard lib-schema/conn? org-slug :guard slugify/valid-slug? user :guard #(or (map? %) (nil? %))]
{'fraction_non_alphanumeric': 0.14240506329113925, 'fraction_numerical': 0.0, 'mean_word_length': 2.671594508975713, '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': '1529336', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1143, 'n_words': 385}