Merge branch 'master' of git://factorcode.org/git/factor
[factor/jcg.git] / extra / webapps / user-admin / user-admin.factor
blob9d4e348596d14e9a0a7c26308b699786d45758d7
1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel sequences accessors namespaces combinators words
4 assocs db.tuples arrays splitting strings validators urls
5 html.forms
6 html.elements
7 html.components
8 furnace
9 furnace.boilerplate
10 furnace.auth.providers
11 furnace.auth.providers.db
12 furnace.auth.login
13 furnace.auth
14 furnace.actions
15 furnace.redirection
16 furnace.utilities
17 http.server
18 http.server.dispatchers ;
19 IN: webapps.user-admin
21 TUPLE: user-admin < dispatcher ;
23 : <user-list-action> ( -- action )
24     <page-action>
25         [ f <user> select-tuples "users" set-value ] >>init
26         { user-admin "user-list" } >>template ;
28 : init-capabilities ( -- )
29     capabilities get words>strings "capabilities" set-value ;
31 : validate-capabilities ( -- )
32     "capabilities" value
33     [ [ param empty? not ] keep set-value ] each ;
35 : selected-capabilities ( -- seq )
36     "capabilities" value [ value ] filter [ string>word ] map ;
38 : validate-user ( -- )
39     {
40         { "username" [ v-username ] }
41         { "realname" [ [ v-one-line ] v-optional ] }
42         { "email" [ [ v-email ] v-optional ] }
43     } validate-params ;
45 : <new-user-action> ( -- action )
46     <page-action>
47         [
48             "username" param <user> from-object
49             init-capabilities
50         ] >>init
52         { user-admin "new-user" } >>template
54         [
55             init-capabilities
56             validate-capabilities
58             validate-user
60             {
61                 { "new-password" [ v-password ] }
62                 { "verify-password" [ v-password ] }
63             } validate-params
65             same-password-twice
67             user new "username" value >>username select-tuple
68             [ user-exists ] when
69         ] >>validate
71         [
72             "username" value <user>
73                 "realname" value >>realname
74                 "email" value >>email
75                 "new-password" value >>encoded-password
76                 H{ } clone >>profile
77                 selected-capabilities >>capabilities
79             insert-tuple
81             URL" $user-admin" <redirect>
82         ] >>submit ;
84 : validate-username ( -- )
85     { { "username" [ v-username ] } } validate-params ;
87 : select-capabilities ( seq -- )
88     [ t swap word>string set-value ] each ;
90 : <edit-user-action> ( -- action )
91     <page-action>
92         [
93             validate-username
95             "username" value <user> select-tuple
96             [ from-object ] [ capabilities>> select-capabilities ] bi
98             init-capabilities
99         ] >>init
101         { user-admin "edit-user" } >>template
103         [
104             "username" value <user> select-tuple
105             [ from-object ] [ capabilities>> select-capabilities ] bi
107             init-capabilities
108             validate-capabilities
110             validate-user
112             {
113                 { "new-password" [ [ v-password ] v-optional ] }
114                 { "verify-password" [ [ v-password ] v-optional ] }
115             } validate-params
117             "new-password" "verify-password"
118             [ value empty? not ] either? [
119                 same-password-twice
120             ] when
121         ] >>validate
123         [
124             "username" value <user> select-tuple
125                 "realname" value >>realname
126                 "email" value >>email
127                 selected-capabilities >>capabilities
129             "new-password" value empty? [
130                 "new-password" value >>encoded-password
131             ] unless
133             update-tuple
135             URL" $user-admin" <redirect>
136         ] >>submit ;
138 : <delete-user-action> ( -- action )
139     <action>
140         [
141             validate-username
142             "username" value <user> delete-tuples
143             URL" $user-admin" <redirect>
144         ] >>submit ;
146 SYMBOL: can-administer-users?
148 can-administer-users? define-capability
150 : <user-admin> ( -- responder )
151     user-admin new-dispatcher
152         <user-list-action> "" add-responder
153         <new-user-action> "new" add-responder
154         <edit-user-action> "edit" add-responder
155         <delete-user-action> "delete" add-responder
156     <boilerplate>
157         { user-admin "user-admin" } >>template
158     <protected>
159         "administer users" >>description
160         { can-administer-users? } >>capabilities ;
162 : make-admin ( username -- )
163     <user>
164     select-tuple
165     [ can-administer-users? suffix ] change-capabilities
166     update-tuple ;