Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
122 lines (114 sloc) 5.81 KB
(ns foreclojure.settings
(:require [sandbar.stateful-session :as session]
[ring.util.response :as response]
[foreclojure.login :as login])
(: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 plausible-email?]]
[foreclojure.template :only [def-page content-page]]
[foreclojure.users :only [disable-codebox? hide-solutions? gravatar-img]]
[foreclojure.messages :only [err-msg]]
[compojure.core :only [defroutes GET POST]]
[ :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]
[:p "Leave new and reset password fields blank if you do not wish to change your password."]
(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]
[: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"]
(defn hide-settings-box [user-obj]
[: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"]
(defn assoc-openid-box [openid]
[:p "Associate an OpenID with your account and you can log in with that in the future. Other settings will not change if this field is changed."]
[text-field :openid "OpenID" openid])))
(def-page settings-page []
(with-user [{:keys [user email openid] :as user-obj}]
{:title "Account settings"
(form-to [:post "/settings"]
[:h2 "Change settings for " user]
[:div#account-settings (account-settings-box user email)]
[:h3 "Disable JavaScript Code Box"]
[:div#settings-codebox (js-settings-box user-obj)]
[:h3 "Hide My Solutions"]
[:div#settings-follow (hide-settings-box user-obj)]
[:h3 "Associate an OpenID with your account"]
[:div#assoc-openid (assoc-openid-box openid)]
[:h3 "Profile Image"]
[:div (gravatar-img {:email email :size 64})]
[:p "To change your profile image, visit <a href='' target='_blank'>Gravatar</a> and edit the image for '" email "'."]
[:button {:type "submit"} "Submit"]]))})}))
(defn do-update-settings! [new-username old-pwd new-pwd repeat-pwd email disable-codebox hide-solutions new-openid cookie-val]
(with-user [{:keys [user pwd openid]}]
(if (not= openid new-openid)
(session/session-put! :login-to "/settings")
(login/do-openid-login new-openid cookie-val))
(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})))
(err-msg "settings.user-exists"),
(< 3 (.length new-lower-user) 14)
(err-msg "settings.uname-size"),
(= new-lower-user
(first (re-seq #"[A-Za-z0-9_]+" new-lower-user)))
(err-msg "settings.uname-alphanum")
(or (empty? new-pwd) (< 6 (.length new-pwd)))
(err-msg "settings.npwd-size"),
(= new-pwd repeat-pwd)
(err-msg "settings.npwd-match")
(or (empty? new-pwd)
(.checkPassword encryptor old-pwd pwd))
(err-msg "settings.pwd-incorrect")
(plausible-email? email)
(err-msg "")
(nil? (fetch-one :users :where {:email email :user {:$ne user}}))
(err-msg "")]
(update! :users {:user user}
{:$set {:pwd (if (seq 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 openid]} :form-params {:strs [ring-session]} :cookies}
(do-update-settings! new-username old-pwd pwd repeat-pwd email disable-codebox hide-solutions openid (:value ring-session))))
Jump to Line
Something went wrong with that request. Please try again.