DEV Community

Cover image for Clojure Is Awesome!!! [PART 4]
André Borba
André Borba

Posted on

1

Clojure Is Awesome!!! [PART 4]

(ns monostate)

(def ^:private session-state
  (atom {:user-id nil
         :permissions #{}
         :last-access nil}))

(defn start-session
  "Starts a new user session with ID and permissions."
  [user-id permissions]
  (reset! session-state {:user-id user-id
                         :permissions permissions
                         :last-access (java.time.Instant/now)}))

(defn end-session
  "Ends the session, resetting the state to default values."
  []
  (reset! session-state {:user-id nil
                         :permissions #{}
                         :last-access nil}))

(defn update-last-access
  "Updates the last access timestamp to the current time."
  []
  (swap! session-state assoc :last-access (java.time.Instant/now)))

(defn get-session
  "Retrieves the complete state of the current session."
  []
  @session-state)

(defn has-permission?
  "Checks if the user has a specific permission."
  [permission]
  (contains? (:permissions @session-state) permission))

(comment
  (start-session "user-123" #{"read" "write"})
  ;; => {:user-id "user-123", :permissions #{"read" "write"}, :last-access <timestamp>}

  (has-permission? "read") ;; => true
  (has-permission? "delete") ;; => false

  (update-last-access)

  (get-session)
  ;; => {:user-id "user-123", :permissions #{"read" "write"}, :last-access <new-timestamp>}

  (end-session)
  ;; => {:user-id nil, :permissions #{}, :last-access nil}
)
Enter fullscreen mode Exit fullscreen mode

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more