-- basic adding of user (along with group) now in code
[bkell-clj.git] / src / commands / add.clj
blob24d90205cecf8109cdfa0c753f3085764a0f6bb0
1 (defn add-user [db-base-URL db-system-DIR] 
2                 
3                 ;; 1. check that there's not an existing user 
4                 (let [check-user
5                                                 (execute-http-call      ;; TODO - put in 404 check 
6                                                         (str 
7                                                                 db-base-URL 
8                                                                 db-system-DIR 
9                                                                 (working-dir-lookup (:tag (:previous @bkell/shell )))   ;; stringing together lookup URL leaf 
10                                                                 "/" 
11                                                                 (str 
12                                                                         (name (:tag (:previous @bkell/shell ))) 
13                                                                         "." 
14                                                                         (:id (:attrs (:previous @bkell/shell ))))
15                                                                 "/"
16                                                                 (str                    ;; repeating user name as leaf document 
17                                                                         (name (:tag (:previous @bkell/shell ))) 
18                                                                         "." 
19                                                                         (:id (:attrs (:previous @bkell/shell )))))
20                                                                 "GET" 
21                                                                 {"Content-Type" "text/xml"} 
22                                                                 nil
23                                                 )
24                                  ]
25                         
26                         (println "check-user[" check-user "]")  ;; TODO - if <error/>, ADD user; user exists otherwise 
27                         
28                         (if (not (= (:msg check-user) "Error"))
29                                         
30                                         (println "user ALREADY exists") ;; TODO - throw error to user
31                                         
32                                         (do 
33                                                 
34                                                 (println "ADDING user[" (:previous @bkell/shell ) "]")  
35                                                 
36                                                 ;; 2. add to aauth.groups and corresponding default group to the new user
37                                                 (let [aauth-group (clojure.xml/parse "etc/xml/add.group.xml")] 
38                                                         
39                                                         (println "...loading add.group.xml[" aauth-group "]")
40                                                         (let [local-id  (str 
41                                                                                                 (name (:tag (:previous @bkell/shell ))) 
42                                                                                                 "." 
43                                                                                                 (:id (:attrs (:previous @bkell/shell ))))] 
44                                                                                 
45                                                                                 (let [db-group  (assoc aauth-group 
46                                                                                                                                                                                 :attrs  {       
47                                                                                                                                                                                                                         :id local-id , 
48                                                                                                                                                                                                                         :name local-id , 
49                                                                                                                                                                                                                         :owner (:id (:attrs (:previous @bkell/shell ))) 
50                                                                                                                                                                                                                 }, 
51                                                                                                                                                                                 :content        [ 
52                                                                                                                                                                                                                                 { 
53                                                                                                                                                                                                                                         :tag (keyword "user"), 
54                                                                                                                                                                                                                                         :attrs  { 
55                                                                                                                                                                                                                                                                                 :xmlns "com/interrupt/bookkeeping/users", 
56                                                                                                                                                                                                                                                                                 :id (:id (:attrs (:previous @bkell/shell ))) 
57                                                                                                                                                                                                                                                                         }
58                                                                                                                                                                                                                                 } 
59                                                                                                                                                                                                                         ] 
60                                                                                                                                                 )
61                                                                                                                         ]
62                                                                                                                         
63                                                                                                 
64                                                                                                 ;; PUT to eXist 
65                                                                                                 (println "CREATing group [" db-group "] / XML[" (with-out-str (clojure.xml/emit db-group)) "]" )
66                                                                                                 (execute-http-call              
67                                                                                                                                                                                         (str db-base-URL db-system-DIR (working-dir-lookup :group)
68                                                                                                                                                                                                                                                         "/" "group." (:id (:attrs (:previous @bkell/shell ))) 
69                                                                                                                                                                                                                                                         "/" "group." (:id (:attrs (:previous @bkell/shell ))))
70                                                                                                                                                                                         "PUT" 
71                                                                                                                                                                                         {       "Content-Type" "text/xml"
72                                                                                                                                                                                                 "Authorization" "Basic YWRtaW46"}
73                                                                                                                                                                                         (with-out-str (clojure.xml/emit db-group))
74                                                                                                                                                                                 
75                                                                                                         
76                                                                                                 )
77                                                                                                 
78                                                                                                 ;; 3. add to aauth.users ... PUT to eXist 
79                                                                                                 ;; 4. profile Details ... PUT to eXist  ... TODO 
80                                                                                                 (println "CREATing user [" (:previous @bkell/shell ) "] / XML[" (with-out-str (clojure.xml/emit (:previous @bkell/shell ))) "]" )
81                                                                                                 (execute-http-call              
82                                                                                                                                                                                         (str db-base-URL db-system-DIR (working-dir-lookup :user)
83                                                                                                                                                                                                                                                         "/" "user." (:id (:attrs (:previous @bkell/shell ))) 
84                                                                                                                                                                                                                                                         "/" "user." (:id (:attrs (:previous @bkell/shell ))))
85                                                                                                                                                                                         "PUT" 
86                                                                                                                                                                                         {       "Content-Type" "text/xml"
87                                                                                                                                                                                                 "Authorization" "Basic YWRtaW46"}
88                                                                                                                                                                                         (with-out-str (clojure.xml/emit (:previous @bkell/shell )))
89                                                                                                 )
90                                                                                                 
91                                                                                                 ;; 5. add associated Bookkeeping to Group ... PUT to eXist 
92                                                                                                 (execute-http-call              
93                                                                                                                                                                                         (str db-base-URL db-system-DIR (working-dir-lookup :bookkeeping)
94                                                                                                                                                                                                                                                         "/" "group." (:id (:attrs (:previous @bkell/shell ))) 
95                                                                                                                                                                                                                                                         "/bookkeeping.main.bookkeeping/bookkeeping.main.bookkeeping" )
96                                                                                                                                                                                         "PUT" 
97                                                                                                                                                                                         {       "Content-Type" "text/xml"
98                                                                                                                                                                                                 "Authorization" "Basic YWRtaW46"}
99                                                                                                                                                                                         (slurp "etc/xml/default.bookkeeping.xml" )
100                                                                                                 )
101                                                                                 )
102                                                                 )
103                                                 )
104                                                 
105                         )
106                         )
107         )