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
10 furnace.auth.providers
11 furnace.auth.providers.db
18 http.server.dispatchers ;
19 IN: webapps.user-admin
21 TUPLE: user-admin < dispatcher ;
23 : <user-list-action> ( -- 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 ( -- )
33 [ [ param empty? not ] keep set-value ] each ;
35 : selected-capabilities ( -- seq )
36 "capabilities" value [ value ] filter [ string>word ] map ;
38 : validate-user ( -- )
40 { "username" [ v-username ] }
41 { "realname" [ [ v-one-line ] v-optional ] }
42 { "email" [ [ v-email ] v-optional ] }
45 : <new-user-action> ( -- action )
48 "username" param <user> from-object
52 { user-admin "new-user" } >>template
61 { "new-password" [ v-password ] }
62 { "verify-password" [ v-password ] }
67 user new "username" value >>username select-tuple
72 "username" value <user>
73 "realname" value >>realname
75 "new-password" value >>encoded-password
77 selected-capabilities >>capabilities
81 URL" $user-admin" <redirect>
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 )
95 "username" value <user> select-tuple
96 [ from-object ] [ capabilities>> select-capabilities ] bi
101 { user-admin "edit-user" } >>template
104 "username" value <user> select-tuple
105 [ from-object ] [ capabilities>> select-capabilities ] bi
108 validate-capabilities
113 { "new-password" [ [ v-password ] v-optional ] }
114 { "verify-password" [ [ v-password ] v-optional ] }
117 "new-password" "verify-password"
118 [ value empty? not ] either? [
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
135 URL" $user-admin" <redirect>
138 : <delete-user-action> ( -- action )
142 "username" value <user> delete-tuples
143 URL" $user-admin" <redirect>
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
157 { user-admin "user-admin" } >>template
159 "administer users" >>description
160 { can-administer-users? } >>capabilities ;
162 : make-admin ( username -- )
165 [ can-administer-users? suffix ] change-capabilities