Skip to content

Commit

Permalink
removes the admin link from the nav bar when the user is not admin; w…
Browse files Browse the repository at this point in the history
…orked around a problem caused by lib-noir's sessions middleware and lazy sequences produced by Enlive (see comment in noir-auth-app.views.common/navigation-menu for details)
  • Loading branch information
Xavi Caballe committed Mar 6, 2013
1 parent 59b2a59 commit e632151
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion resources/public/navs.html
Expand Up @@ -18,7 +18,7 @@
<div class="nav">

<ul id="logged-in-nav">
<li><a href="/admin">admin</a></li><!--
<li class="admin-nav-item"><a href="/admin">admin</a></li><!--
http://stackoverflow.com/q/12168194/974795
--><li><a href="/settings">settings</a></li><!--
--><li><a data-method="post" href="/logout">log out</a></li>
Expand Down
5 changes: 3 additions & 2 deletions src/noir_auth_app/views/admin.clj
Expand Up @@ -156,7 +156,8 @@
page-users
(if next-page-first-created-at (butlast users-batch) users-batch)]

(common/admin-layout
(doall ; see comment in common/navigation-menu
(common/admin-layout
{:title (i18n/translate :admin-page-title)
:nav (common/navigation-menu)
:content
Expand All @@ -167,7 +168,7 @@
:truncated-users-fields truncated-users-fields
:page-users page-users
:next-page-first-created-at
next-page-first-created-at})})))
next-page-first-created-at})}))))


(defroutes admin-routes
Expand Down
24 changes: 22 additions & 2 deletions src/noir_auth_app/views/common.clj
Expand Up @@ -46,10 +46,30 @@
; p. 551 of "Clojure Programming"
; https://github.com/swannodette/enlive-tutorial/blob/master/src/tutorial/template3.clj
(h/defsnippet not-logged-in-nav "public/navs.html" [:#not-logged-in-nav] [])
(h/defsnippet logged-in-nav "public/navs.html" [:#logged-in-nav] [])
(h/defsnippet logged-in-nav "public/navs.html" [:#logged-in-nav]
[]
[:.admin-nav-item] #(when (session/get :admin) %))

(defn navigation-menu []
(if (current-user) (logged-in-nav) (not-logged-in-nav)))
; Before using (doall) below there was this exception
; java.lang.ClassCastException: clojure.lang.Var$Unbound cannot be cast to clojure.lang.IDeref
; The reason is that logged-in-nav, like all defsnippet functions, yields a
; lazy sequence (the 'class' function can be used to check this) and by the
; time it's realized by Ring (which allows a sequence in :body, see
; https://github.com/ring-clojure/ring/wiki/Concepts) the lib-noir's
; session dynamic var is not bound anymore. Chas Emerick explains this and
; provides a general solution in the form of a middleware here
; https://groups.google.com/d/topic/enlive-clj/0WOG2CDqUDY/discussion
; For the moment though, I've decided to just solve this specific case
; using 'doall' to avoid the overhead that the general solution imposes on
; every request.
; On the other hand, like Christophe Grand says, this is something that
; should probably be solved in the wrap-noir-session middleware:
; "Given that per Ring's spec, :body may be lazy seqs, I would tend to say
; that it's up to the authors of middlewares which introduce dynamic
; binding to ensure that this case is covered."
; https://groups.google.com/d/topic/enlive-clj/CKuJHxaHkGY/discussion
(if (current-user) (doall (logged-in-nav)) (not-logged-in-nav)))


;; Layouts
Expand Down
2 changes: 1 addition & 1 deletion src/noir_auth_app/views/users.clj
Expand Up @@ -34,7 +34,7 @@


(defn save-user-info-in-session [{:keys [_id lowercase_username]}]
(when true ;(= lowercase_username "admin")
(when true ;(= lowercase_username "admin")
(session/put! :admin true))
(session/put! :user-id _id))

Expand Down

0 comments on commit e632151

Please sign in to comment.