Skip to content
Browse files

Clean up a few db functions.

  • Loading branch information...
1 parent 5b1ba2b commit 0daa231e7fd8a0f411c1790ee5256ad0c1af2fc1 @technomancy technomancy committed Sep 5, 2011
Showing with 48 additions and 47 deletions.
  1. +46 −46 src/clojars/db.clj
  2. +1 −0 src/clojars/maven.clj
  3. +1 −1 src/clojars/web/user.clj
View
92 src/clojars/db.clj
@@ -7,38 +7,41 @@
java.util.Date
java.io.File))
-(def ssh-options "no-agent-forwarding,no-port-forwarding,no-pty,no-X11-forwarding")
-
-(def *reserved-names*
- #{"clojure" "clojars" "clojar" "register" "login"
- "pages" "logout" "password" "username" "user"
- "repo" "repos" "jar" "jars" "about" "help" "doc"
- "docs" "images" "js" "css" "maven" "api"
- "download" "create" "new" "upload" "contact" "terms"
- "group" "groups" "browse" "status" "blog" "search"
- "email" "welcome" "devel" "development" "test" "testing"
- "prod" "production" "admin" "administrator" "root"
- "webmaster" "profile" "dashboard" "settings" "options"
- "index" "files" "releases" "snapshots"})
-
-(let [chars (map char
- (mapcat (fn [[x y]] (range (int x) (inc (int y))))
- [[\a \z] [\A \Z] [\0 \9]]))]
- (defn rand-string
- "Generates a random string of [A-z0-9] of length n."
- [n]
- (apply str (take n (map #(nth chars %)
- (repeatedly #(rand (count chars))))))))
+(def ^{:private true} ssh-options
+ "no-agent-forwarding,no-port-forwarding,no-pty,no-X11-forwarding")
+
+(def reserved-names
+ #{"clojure" "clojars" "clojar" "register" "login"
+ "pages" "logout" "password" "username" "user"
+ "repo" "repos" "jar" "jars" "about" "help" "doc"
+ "docs" "images" "js" "css" "maven" "api"
+ "download" "create" "new" "upload" "contact" "terms"
+ "group" "groups" "browse" "status" "blog" "search"
+ "email" "welcome" "devel" "development" "test" "testing"
+ "prod" "production" "admin" "administrator" "root"
+ "webmaster" "profile" "dashboard" "settings" "options"
+ "index" "files" "releases" "snapshots"})
+
+(def ^{:private true} constituent-chars
+ (->> [[\a \z] [\A \Z] [\0 \9]]
+ (mapcat (fn [[x y]] (range (int x) (inc (int y)))))
+ (map char)
+ vec))
+
+(defn rand-string
+ "Generates a random string of [A-z0-9] of length n."
+ [n]
+ (apply str (repeatedly n #(rand-nth constituent-chars))))
(defn write-key-file [path]
(locking (:key-file config)
(let [new-file (File. (str path ".new"))]
(with-query-results rs ["select user, ssh_key from users"]
(with-open [f (io/writer new-file)]
- (doseq [x rs]
- (.write f (str "command=\"ng --nailgun-port 8700 clojars.scp " (:user x)
- "\"," ssh-options " "
- (.replaceAll (.trim (:ssh_key x))
+ (doseq [{:keys [user ssh_key]} rs]
+ (.write f (str "command=\"ng --nailgun-port 8700 clojars.scp " user
+ "\"," ssh-options " "
+ (.replaceAll (.trim ssh_key)
"[\n\r\0]" "")
"\n")))))
(.renameTo new-file (File. path)))))
@@ -76,7 +79,7 @@
(doall (map :user rs))))
(defn auth-user [user pass]
- (with-query-results rs
+ (with-query-results rs
["select * from users where (user = ? or email = ?)" user user]
(first (filter #(= (:password %) (sha1 (:salt %) pass)) rs))))
@@ -92,20 +95,20 @@
(vec rs)))
(defn recent-jars []
- (with-query-results rs
+ (with-query-results rs
[(str "select * from jars group by group_name, jar_name "
"order by created desc limit 5")]
(vec rs)))
(defn find-canon-jar [jarname]
- (with-query-results rs
+ (with-query-results rs
[(str "select * from jars where "
"jar_name = ? and group_name = ? "
"order by created desc limit 1")
jarname jarname]
(first rs)))
-(defn find-jar
+(defn find-jar
([jarname]
(with-query-results rs [(str "select * from jars where "
"jar_name = ?") jarname]
@@ -116,25 +119,24 @@
"limit 1") group jarname]
(first rs))))
-
(defn add-user [email user password ssh-key]
(let [salt (rand-string 16)]
- (insert-values
+ (insert-values
:users
[:email :user :password :salt :ssh_key :created]
[email user (sha1 salt password) salt ssh-key (Date.)])
- (insert-values
+ (insert-values
:groups
[:name :user]
[(str "org.clojars." user) user])
(write-key-file (:key-file config))))
(defn update-user [account email user password ssh-key]
(let [salt (rand-string 16)]
- (update-values
+ (update-values
:users ["user=?" account]
- {:email email
- :user user
+ {:email email
+ :user user
:salt salt
:password (sha1 salt password)
:ssh_key ssh-key})
@@ -152,7 +154,7 @@
"and full-stops."))))
(let [members (group-members group)]
(if (empty? members)
- (if (*reserved-names* group)
+ (if (reserved-names group)
(throw (Exception. (str "The group name " group " is already taken.")))
(add-member group account))
(when-not (some #{account} members)
@@ -163,7 +165,7 @@
(when-not (re-matches #"^[a-z0-9-_.]+$" (:name jarmap))
(throw (Exception. (str "Jar names must consist solely of lowercase "
"letters, numbers, hyphens and underscores."))))
-
+
(with-connection (:db config)
(transaction
(when check-only (set-rollback-only))
@@ -199,12 +201,10 @@
;; TODO: do something less stupidly slow
(vec (map #(find-jar (:group_name %) (:jar_name %)) rs))))
-
(comment
- (with-db (add-jar "atotx" {:name "test3" :group "test3" :version "1.0"
- :description "An dog awesome and non-existent test jar."
- :homepage "http://clojars.org/"
- :authors ["Alex Osborne"
- "a little fish"]}))
- (with-db (find-user "atotx"))
-)
+ (with-db
+ (add-jar "atotx" {:name "test3" :group "test3" :version "1.0"
+ :description "An dog awesome and non-existent test jar."
+ :homepage "http://clojars.org/"
+ :authors ["Alex Osborne" "a little fish"]}))
+ (with-db (find-user "atotx")))
View
1 src/clojars/maven.clj
@@ -31,6 +31,7 @@
:description (.getDescription model)
:homepage (.getUrl model)
:authors (vec (map #(.getName %) (.getContributors model)))
+ ;; TODO: doesn't appear to be used anywhere?
:dependencies (vec (mapcat (fn [d] [(symbol (.getGroupId d)
(.getArtifactId d))
(.getVersion d)])
View
2 src/clojars/web/user.clj
@@ -48,7 +48,7 @@
(conj-when (blank? password) "Password can't be blank")
(conj-when (not= password confirm)
"Password and confirm password must match")
- (conj-when (or (*reserved-names* user) ; "I told them we already
+ (conj-when (or (reserved-names user) ; "I told them we already
(and (not= account user) ; got one!"
(find-user user))
(seq (group-members user)))

0 comments on commit 0daa231

Please sign in to comment.
Something went wrong with that request. Please try again.