-- maing add-user only operate on variables passed into it
[bkell-clj.git] / src / commands / add.clj
blob13dff767d78ff86fcaa5379525e0d645779462a1
1 (defn add-user [db-base-URL db-system-DIR working-USER] 
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 working-USER))        ;; stringing together lookup URL leaf 
10                                                                 "/" 
11                                                                 (str 
12                                                                         (name (:tag working-USER)) 
13                                                                         "." 
14                                                                         (:id (:attrs working-USER)))
15                                                                 "/"
16                                                                 (str                    ;; repeating user name as leaf document 
17                                                                         (name (:tag working-USER)) 
18                                                                         "." 
19                                                                         (:id (:attrs working-USER))))
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[" working-USER "]")  
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 working-USER)) 
42                                                                                                 "." 
43                                                                                                 (:id (:attrs working-USER)))] 
44                                                                                 
45                                                                                 (let [db-group  (assoc aauth-group 
46                                                                                                                                                                                 :attrs  {       
47                                                                                                                                                                                                                         :id local-id , 
48                                                                                                                                                                                                                         :name local-id , 
49                                                                                                                                                                                                                         :owner (:id (:attrs working-USER)) 
50                                                                                                                                                                                                                 }, 
51                                                                                                                                                                                 :content        [ 
52                                                                                                                                                                                                                                 { 
53                                                                                                                                                                                                                                         :tag (keyword "user"), 
54                                                                                                                                                                                                                                         :attrs  { 
55                                                                                                                                                                                                                                                                                 :xmlns "com/interrupt/bookkeeping/users", 
56                                                                                                                                                                                                                                                                                 :id (:id (:attrs working-USER)) 
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 working-USER)) 
69                                                                                                                                                                                                                                                         "/" "group." (:id (:attrs working-USER)))
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 [" working-USER "] / XML[" (with-out-str (clojure.xml/emit working-USER)) "]" )
81                                                                                                 (execute-http-call              
82                                                                                                                                                                                         (str db-base-URL db-system-DIR (working-dir-lookup :user)
83                                                                                                                                                                                                                                                         "/" "user." (:id (:attrs working-USER)) 
84                                                                                                                                                                                                                                                         "/" "user." (:id (:attrs working-USER)))
85                                                                                                                                                                                         "PUT" 
86                                                                                                                                                                                         {       "Content-Type" "text/xml"
87                                                                                                                                                                                                 "Authorization" "Basic YWRtaW46"}
88                                                                                                                                                                                         (with-out-str (clojure.xml/emit working-USER))
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 working-USER)) 
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         )
110 (defn add-generic [db-base-URL db-system-DIR working-ITEM]
111                 
112                 
113                 ;; ... TODO - logic to build XQuery to use to insert 
114                 
115                 ;; PUT to eXist 
116                 (println "CREATing [" working-ITEM "] / XML[" (with-out-str (clojure.xml/emit working-ITEM)) "]" )
117                 (execute-http-call              
118                                                                                                         (str db-base-URL db-system-DIR (working-dir-lookup :bookkeeping)
119                                                                                                                                                                         "/" "group." (:id (:attrs working-ITEM)) 
120                                                                                                                                                                         "/bookkeeping.main.bookkeeping/bookkeeping.main.bookkeeping" 
121                                                                                                                                                                         "?_wrap=no&_query="
122                                                                                                                                                                         )
123                                                                                                         "POST" 
124                                                                                                         {       "Content-Type" "text/xml"
125                                                                                                                 "Authorization" "Basic YWRtaW46" }
126                                                                                                         (slurp "etc/xml/default.bookkeeping.xml" )
127                 )