Merge a few cleanups inspired by bpt's redesign
[hcoop/portal.git] / app / app.sml
index fde065e..8a734f2 100644 (file)
@@ -2,7 +2,7 @@ structure App :> APP =
 struct
 
 val baseUrl = "https://join.hcoop.net/join/"
-val portalUrl = "https://members2.hcoop.net/portal/"
+val portalUrl = "https://members.hcoop.net/portal/"
 
 open Sql
 
@@ -14,7 +14,7 @@ val rnd = ref (Random.rand (0, 0))
 
 fun init () = 
     let
-       val c = C.conn "dbname='hcoop_hcoop'"
+       val c = C.conn "dbname='hcoop_hcoop' host='postgres'"
     in
        db := SOME c;
        C.dml c "BEGIN";
@@ -98,14 +98,14 @@ type application = { name : string, rname : string, gname : string option, email
 
 fun randomPassword () =
     let
-       val proc = Unix.execute ("/usr/bin/apg", ["/usr/bin/apg", "-n", "1", "-m", "10"])
+       val proc = Unix.execute ("/usr/bin/pwgen", ["-cCnB", "8", "1"])
     in
        case TextIO.inputLine (Unix.textInstreamOf proc) of
-           NONE => raise Fail "Couldn't execute apg"
+           NONE => raise Fail "Couldn't execute pwgen"
          | SOME line =>
            case String.tokens Char.isSpace line of
                [s] => s
-             | _ => raise Fail "Couldn't parse output of apg"
+             | _ => raise Fail "Couldn't parse output of pwgen"
     end
 
 val allLower = CharVector.map Char.toLower
@@ -159,10 +159,11 @@ fun validHost s =
 fun validDomain s =
     size s > 0 andalso size s < 100 andalso List.all validHost (String.fields (fn ch => ch = #".") s)
 
-fun validUser s =
-    size s > 0 andalso size s < 50 andalso List.all
-                                              (fn ch => isIdent ch orelse ch = #"." orelse ch = #"_" orelse ch = #"-" orelse ch = #"+")
-                                              (String.explode s)
+fun validUsername name =
+    size name <= 12
+    andalso size name > 0
+    andalso Char.isLower (String.sub (name, 0))
+    andalso CharVector.all Char.isAlphaNum name
 
 fun validEmailUser s =
     size s > 0 andalso size s < 50 andalso List.all
@@ -175,7 +176,9 @@ fun validEmail s =
        | _ => false)
 
 fun userExists name =
-    (Posix.SysDB.getpwnam name; true) handle OS.SysErr _ => false
+    case C.oneOrNoRows (getDb ()) ($`SELECT id FROM WebUser WHERE name = ^(C.stringToSql name)`) of
+       SOME _ => true
+      | NONE => (Posix.SysDB.getpwnam name; true) handle OS.SysErr _ => false
 
 fun confirm (id, passwd) =
     let