Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Created a settings page with Gravatar information

  • Loading branch information...
commit 0a41d414a19a04438f30466c2a805090166f64e5 1 parent b446145
David Davis authored
View
4 src/foreclojure/core.clj
@@ -9,6 +9,7 @@
[foreclojure.datatable :only [datatable-routes]]
[foreclojure.problems :only [problems-routes]]
[foreclojure.login :only [login-routes]]
+ [foreclojure.settings :only [settings-routes]]
[foreclojure.register :only [register-routes]]
[foreclojure.golf :only [golf-routes]]
[foreclojure.ring :only [resources wrap-strip-trailing-slash wrap-url-as-file wrap-versioned-expiry split-hosts wrap-404 wrap-debug]]
@@ -46,7 +47,8 @@
graph-routes
api-routes
datatable-routes
- golf-routes)
+ golf-routes
+ settings-routes)
((if (:wrap-reload config)
#(wrap-reload % '(foreclojure.core))
identity))
View
91 src/foreclojure/login.clj
@@ -5,7 +5,6 @@
(:use [hiccup.form-helpers :only [form-to label text-field password-field check-box]]
[foreclojure.utils :only [from-mongo flash-error flash-msg with-user form-row assuming send-email login-url]]
[foreclojure.template :only [def-page content-page]]
- [foreclojure.users :only [disable-codebox? set-disable-codebox hide-solutions? set-hide-solutions]]
[compojure.core :only [defroutes GET POST]]
[useful.map :only [keyed]]
[clojail.core :only [thunk-timeout]]
@@ -50,84 +49,6 @@
(response/redirect (or location "/problems")))
(flash-error "/login" "Error logging in."))))
-(defn account-settings-box [user]
- [:table
- (form-to [:post "/login/update"]
- (map form-row
- [[text-field :new-username "Username" user]
- [password-field :old-pwd "Current password"]
- [password-field :pwd "New password"]
- [password-field :repeat-pwd "Repeat password"]])
- [:tr
- [:td [:button {:type "submit"} "Reset now"]]])])
-
-(defn js-settings-box [user-obj]
- (list
- [:p "Selecting this will disable the JavaScript code entry box and just give you plain text entry"]
- (form-to [:post "/users/set-disable-codebox"]
- (check-box :disable-codebox
- (disable-codebox? user-obj))
- [:label {:for "disable-codebox"}
- "Disable JavaScript in code entry box"]
- [:br]
- [:div#button-div
- [:button {:type "submit"} "Submit"]])))
-
-(defn hide-settings-box [user-obj]
- (list
- [:p "When you solve a problem, we allow any user who has solved a problem to view your solutions to that problem. Check this box to keep your solutions private."]
- (form-to [:post "/users/set-hide-solutions"]
- (check-box :hide-solutions
- (hide-solutions? user-obj))
- [:label {:for "hide-solutions"}
- "Hide my solutions"]
- [:br]
- [:div#button-div
- [:button {:type "submit"} "Submit"]])))
-
-(def-page update-credentials-page []
- (with-user [{:keys [user] :as user-obj}]
- {:title "Change password"
- :content
- (content-page
- {:main
- (list
- [:h2 "Change password for " user]
- [:div#account-settings (account-settings-box user)]
- [:hr]
- [:h2 "Disable JavaScript Code Box"]
- [:div#settings-codebox (js-settings-box user-obj)]
- [:hr]
- [:h2 "Hide My Solutions"]
- [:div#settings-follow (hide-settings-box user-obj)])})}))
-
-(defn do-update-credentials! [new-username old-pwd new-pwd repeat-pwd]
- (with-user [{:keys [user pwd]}]
- (let [encryptor (StrongPasswordEncryptor.)
- new-pwd-hash (.encryptPassword encryptor new-pwd)
- new-lower-user (.toLowerCase new-username)]
- (assuming [(or (= new-lower-user user) (nil? (fetch-one :users :where {:user new-lower-user})))
- "User already exists",
- (< 3 (.length new-lower-user) 14)
- "Username must be 4-13 characters long",
- (= new-lower-user
- (first (re-seq #"[A-Za-z0-9_]+" new-lower-user)))
- "Username must be alphanumeric"
- (or (empty? new-pwd) (< 6 (.length new-pwd)))
- "New password must be at least seven characters long",
- (= new-pwd repeat-pwd)
- "New password was not entered identically twice"
- (.checkPassword encryptor old-pwd pwd)
- "Old password incorrect"]
- (do
- (update! :users {:user user}
- {:$set {:pwd (if (not-empty new-pwd) new-pwd-hash pwd) :user new-lower-user}}
- :upsert false)
- (session/session-put! :user new-lower-user)
- (flash-msg "/problems"
- (str "Account for " new-lower-user " updated successfully")))
- (flash-error "/login/update" why)))))
-
(def-page reset-password-page []
{:title "Reset password"
:content
@@ -196,20 +117,10 @@
(POST "/login" {{:strs [user pwd]} :form-params}
(do-login user pwd))
- (GET "/login/update" [] (update-credentials-page))
- (POST "/login/update" {{:strs [new-username old-pwd pwd repeat-pwd]} :form-params}
- (do-update-credentials! new-username old-pwd pwd repeat-pwd))
-
(GET "/login/reset" [] (reset-password-page))
(POST "/login/reset" [email]
(do-reset-password! email))
(GET "/logout" []
(do (session/session-delete-key! :user)
- (response/redirect "/")))
-
- (POST "/users/set-disable-codebox" [disable-codebox]
- (set-disable-codebox disable-codebox))
-
- (POST "/users/set-hide-solutions" [hide-solutions]
- (set-hide-solutions hide-solutions)))
+ (response/redirect "/"))))
View
100 src/foreclojure/settings.clj
@@ -0,0 +1,100 @@
+(ns foreclojure.settings
+ (:require [sandbar.stateful-session :as session]
+ [ring.util.response :as response])
+ (:import [org.jasypt.util.password StrongPasswordEncryptor])
+ (:use [hiccup.form-helpers :only [form-to label text-field password-field check-box]]
+ [foreclojure.utils :only [from-mongo flash-error flash-msg with-user form-row assuming send-email login-url]]
+ [foreclojure.template :only [def-page content-page]]
+ [foreclojure.users :only [disable-codebox? hide-solutions? gravatar-img]]
+ [compojure.core :only [defroutes GET POST]]
+ [useful.map :only [keyed]]
+ [clojail.core :only [thunk-timeout]]
+ [clojure.stacktrace :only [print-cause-trace]]
+ [somnium.congomongo :only [update! fetch-one]]))
+
+(defn account-settings-box [user email]
+ (list
+ [:p "Leave new and reset password fields blank if you do not wish to change your password."]
+ [:table
+ (map form-row
+ [[text-field :new-username "Username" user]
+ [password-field :old-pwd "Current password"]
+ [password-field :pwd "New password"]
+ [password-field :repeat-pwd "Repeat password"]
+ [text-field :email "Email" email]])]))
+
+(defn js-settings-box [user-obj]
+ (list
+ [:p "Selecting this will disable the JavaScript code entry box and just give you plain text entry"]
+ (check-box :disable-codebox
+ (disable-codebox? user-obj))
+ [:label {:for "disable-codebox"}
+ "Disable JavaScript in code entry box"]
+ [:br]))
+
+(defn hide-settings-box [user-obj]
+ (list
+ [:p "When you solve a problem, we allow any user who has solved a problem to view your solutions to that problem. Check this box to keep your solutions private."]
+ (check-box :hide-solutions
+ (hide-solutions? user-obj))
+ [:label {:for "hide-solutions"}
+ "Hide my solutions"]
+ [:br]))
+
+(def-page settings-page []
+ (with-user [{:keys [user email] :as user-obj}]
+ {:title "Account settings"
+ :content
+ (content-page
+ {:main
+ (form-to [:post "/settings"]
+ (list
+ [:h2 "Change settings for " user]
+ [:div#account-settings (account-settings-box user email)]
+ [:hr]
+ [:h3 "Disable JavaScript Code Box"]
+ [:div#settings-codebox (js-settings-box user-obj)]
+ [:hr]
+ [:h3 "Hide My Solutions"]
+ [:div#settings-follow (hide-settings-box user-obj)]
+ [:hr]
+ [:h3 "Profile Image"]
+ [:div (gravatar-img {:email email :size 64})]
+ [:p "To change your profile image, visit <a href='http://gravatar.com' target='_blank'>Gravatar</a> and edit the image for '" email "'."]
+ [:div#button-div
+ [:button {:type "submit"} "Submit"]]))})}))
+
+(defn do-update-settings! [new-username old-pwd new-pwd repeat-pwd email disable-codebox hide-solutions]
+ (with-user [{:keys [user pwd]}]
+ (let [encryptor (StrongPasswordEncryptor.)
+ new-pwd-hash (.encryptPassword encryptor new-pwd)
+ new-lower-user (.toLowerCase new-username)]
+ (assuming [(or (= new-lower-user user) (nil? (fetch-one :users :where {:user new-lower-user})))
+ "User already exists",
+ (< 3 (.length new-lower-user) 14)
+ "Username must be 4-13 characters long",
+ (= new-lower-user
+ (first (re-seq #"[A-Za-z0-9_]+" new-lower-user)))
+ "Username must be alphanumeric"
+ (or (empty? new-pwd) (< 6 (.length new-pwd)))
+ "New password must be at least seven characters long",
+ (= new-pwd repeat-pwd)
+ "New password was not entered identically twice"
+ (.checkPassword encryptor old-pwd pwd)
+ "Current password incorrect"
+ (not (empty? email))
+ "Please enter a valid email address"]
+ (do
+ (update! :users {:user user}
+ {:$set {:pwd (if (not-empty new-pwd) new-pwd-hash pwd) :user new-lower-user :email email
+ :disable-code-box (boolean disable-codebox) :hide-solutions (boolean hide-solutions)}}
+ :upsert false)
+ (session/session-put! :user new-lower-user)
+ (flash-msg "/problems"
+ (str "Account for " new-lower-user " updated successfully")))
+ (flash-error "/settings" why)))))
+
+(defroutes settings-routes
+ (GET "/settings" [] (settings-page))
+ (POST "/settings" {{:strs [new-username old-pwd pwd repeat-pwd email disable-codebox hide-solutions]} :form-params}
+ (do-update-settings! new-username old-pwd pwd repeat-pwd email disable-codebox hide-solutions)))
View
2  src/foreclojure/template.clj
@@ -56,7 +56,7 @@
(when user
[:div#lower-menu
[:span
- (link-to "/login/update" "Account Settings")]
+ (link-to "/settings" "Account Settings")]
(when (:golfing-active config)
[:span ; deserves its own page, but just make it discoverable for now
(link-to "/league" "Leagues")])
View
14 src/foreclojure/users.clj
@@ -250,20 +250,6 @@
"next-action" (follow-url username (not follow?))
"next-label" (if follow? "Unfollow" "Follow")}))
-(defn set-disable-codebox [disable-flag]
- (with-user [{:keys [_id]}]
- (update! :users
- {:_id _id}
- {:$set {:disable-code-box (boolean disable-flag)}})
- (response/redirect "/problems")))
-
-(defn set-hide-solutions [hide-flag]
- (with-user [{:keys [_id]}]
- (update! :users
- {:_id _id}
- {:$set {:hide-solutions (boolean hide-flag)}})
- (response/redirect "/problems")))
-
(defn datatable-paging [start length users]
(take length (drop start users)))
Please sign in to comment.
Something went wrong with that request. Please try again.