Permalink
Browse files

Reworked authentication so you can turn off/on

  • Loading branch information...
David Davis
David Davis committed Aug 11, 2012
1 parent 7da3f6f commit ead722e88b045733566551ef43d39f198e50327c
@@ -1,3 +1,10 @@
+#############################
+# Authentication
+#############################
+
+# can be ldap or none
+authentication = none
+
#############################
# LDAP Config
############################
@@ -1,40 +1,59 @@
(ns shared-resource.models.user
(:use shared-resource.datomic
+ [shared-resource.config]
[datomic.api :only (q db) :as d])
- (:require [shared-resource.models.authentication :as auth]))
+ (:require [shared-resource.datomic :as db]
+ [clojure.string :as string]
+ [shared-resource.models.authentication :as auth]))
+
+(def authentication (config-value "authentication"))
(defn create-user [username full-name email]
(let [conn (d/connect (uri datomic-config))]
(d/transact
conn
[{:db/id #db/id [:db.part/user]
- :user/username username
+ :user/username (string/lower-case username)
:user/name full-name
:user/email email}])))
(defn get-all-usernames []
(let [conn (d/connect (uri datomic-config))]
(q '[:find ?n :where [?c user/username ?n ]] (db conn))))
-(defn find-user [username]
- (first (first (execute `[:find ?c :where [?c user/username ~username]]))))
+(defn find-by-username [username]
+ (let [name (string/lower-case username)]
+ (first (first (execute `[:find ?c :where [?c user/username ~name]])))))
+
+(defn find-by-id [id]
+ (db/find-entity id))
(defn destroy-user [username]
(let [conn (d/connect (uri datomic-config))]
(d/transact
conn
- [[:db.fn/retractEntity (find-user username)]])))
+ [[:db.fn/retractEntity (find-by-username username)]])))
(defn find-or-create-user [{:keys [username full-name email]}]
- (if-let [user-record (find-user username)]
+ (if-let [user-record (find-by-username username)]
user-record
(create-user username full-name email)))
+(defn authenticate? [username password save-user]
+ (if-let [user-entry (auth/find-user username)]
+ (when (auth/authenticate? user-entry password)
+ (if save-user (find-or-create-user (auth/user-attributes user-entry)))
+ true)
+ false))
+
(defn login?
+ "Calls an authentication method based on auth config"
([username password] (login? username password false))
([username password save-user]
- (if-let [user-entry (auth/find-user username)]
- (when (auth/authenticate? user-entry password)
- (if save-user (find-or-create-user (auth/user-attributes user-entry)))
+ (if (= authentication "none")
+ ; just save the user if save-user and return true
+ (do
+ (if save-user (find-or-create-user {:username username :full-name "John Doe" :email "johndoe@mail.com"}))
true)
- false)))
+ ; actually authenticate the user
+ (authenticate? username password save-user))))
@@ -3,7 +3,8 @@
hiccup.core
hiccup.element
[hiccup.page :only [include-css include-js html5]])
- (:require [noir.session :as session]))
+ (:require [shared-resource.models.user :as user]
+ [noir.session :as session]))
(defpartial layout [& content]
(html5
@@ -13,8 +14,8 @@
(include-js "/js/main.js")]
[:body
[:div#session
- [:div#user-info (session/get :username)]
- (if (session/get :username) (link-to "/sessions/destroy" "Logout") "")]
+ [:div#user-info (:user/username (user/find-by-id (session/get :user)))]
+ (if (session/get :user) (link-to "/sessions/destroy" "Logout") "")]
[:div#flash.success (session/flash-get :success)]
[:div#flash.error (session/flash-get :error)]
[:div#wrapper
@@ -19,26 +19,26 @@
(submit-button "Login"))))
(defn logged-in? []
- (not (nil? (session/get :username))))
+ (not (nil? (session/get :user))))
;; Routes
(pre-route "/" {}
(when-not (logged-in?)
(resp/redirect "/sessions/new")))
-(pre-route "/resources/*" {}
+(pre-route "/resources*" {}
(when-not (logged-in?)
(resp/redirect "/sessions/new")))
(defpage "/sessions/new" []
- (if (session/get :username) (resp/redirect "/") (login-page "")))
+ (if (session/get :user) (resp/redirect "/") (login-page "")))
(defpage [:post "/sessions"] {:keys [username password]}
(if (user/login? username password true)
(do
(session/clear!)
- (session/put! :username username)
+ (session/put! :user (user/find-by-username username))
(session/flash-put! :success "Successfully logged in.")
(resp/redirect "/"))
(do

0 comments on commit ead722e

Please sign in to comment.