1#!%TCLSH%
2
3#
4# Manage realms
5#
6# Called by: pgaindex
7#
8# Parameters (form or url):
9#   - display realm page
10#	- action : "list"
11#   - display add page
12#	- action : "add"
13#	- realm : (none)
14#   - process new realm addition
15#	- action : "add"
16#	- realm : realm name
17#	- descr : realm description
18#   - display selection page for modification
19#	- action : "mod"
20#   - edit realm members (modification)
21#	- buttons: store, reset, left, right
22#	- if store
23#		- realm : selected realm name
24#		- descr : modified realm description
25#		- truemembers : select members
26#		- users : not used
27#		- members : not used
28#	- if right
29#		- realm : selected realm name
30#		- descr : modified realm description
31#		- truemembers : select members
32#		- users : users to add to the realm (i.e. to truemembers)
33#		- members : not used
34#	- if left
35#		- realm : selected realm name
36#		- descr : modified realm description
37#		- truemembers : select members
38#		- users : not used
39#		- members : users to remove from the realm (i.e. from truemembers)
40#   - display selection page for removal
41#	- action : "del"
42#   - process realm removal
43#	- action : "del"
44#	- realm : realm name
45#
46#
47# History
48#   2003/05/30 : pda/jean : design
49#   2003/06/27 : pda      : use cgi-exec
50#   2007/12/04 : pda/jean : integration in netmagis
51#   2010/12/27 : pda      : i18n and netmagis merge
52#
53
54#
55# Template pages used by this script
56#
57
58set conf(page-index)	pgarlm-index.html
59set conf(page-list)	pgarlm-list.html
60set conf(page-add)	pgarlm-add.html
61set conf(page-modsel)	pgarlm-modsel.html
62set conf(page-modedit)	pgarlm-modedit.html
63set conf(page-del)	pgarlm-del.html
64set conf(page-ok)	pga-ok.html
65
66#
67# Next actions
68#
69
70set conf(next)		"pgarealm"
71
72# listbox size for realm edition
73set conf(size)		20
74
75#
76# Realm list
77#
78
79set conf(tabrlm) {
80    global {
81	chars {10 normal}
82	align {left}
83	botbar {yes}
84	columns {20 70 10}
85    }
86    pattern Realm {
87	vbar {yes}
88	column {
89	    chars {bold}
90	}
91	vbar {yes}
92	column {
93	    multicolumn {2}
94	}
95	vbar {yes}
96    }
97    pattern RealmAdmin {
98	vbar {yes}
99	column {
100	    chars {bold}
101	}
102	vbar {yes}
103	column { }
104	vbar {yes}
105	column { }
106	vbar {yes}
107    }
108    pattern Users {
109	vbar {yes}
110	column {
111	    multicolumn {3}
112	}
113	vbar {yes}
114    }
115}
116
117
118#
119# Netmagis general library
120#
121
122source %LIBNETMAGIS%
123
124# ::webapp::cgidebug ; exit
125
126##############################################################################
127# Display realm index page
128##############################################################################
129
130d cgi-register {
131    action {}
132} {} {
133    global conf
134
135    foreach action {list add mod del} {
136	set up [string toupper $action]
137	d urlset "%URL$up%" $conf(next) [list [list "action" $action]]
138    }
139
140    #
141    # End of script: output page and close database
142    #
143
144    d result $conf(page-index) [list \
145			    ]
146}
147
148##############################################################################
149# Display realm list page
150##############################################################################
151
152d cgi-register {
153    action list
154} {} {
155    global conf
156
157    #
158    # Get realm list
159    #
160
161    pgauth-lsrealm $dbfd tabrlm
162
163    #
164    # Display realm list
165    #
166
167    set lines {}
168    foreach r [lsort [array names tabrlm]] {
169	lassign $tabrlm($r) descr members admin
170	if {$admin} then {
171	    set admin [mc "Admin"]
172	} else {
173	    set admin ""
174	}
175	lappend lines [list RealmAdmin $r $descr $admin]
176	lappend lines [list Users [join [lsort $members] " "]]
177    }
178
179    set realms [::arrgen::output "html" $conf(tabrlm) $lines]
180
181    #
182    # End of script: output page and close database
183    #
184
185    d result $conf(page-list) [list \
186				[list %REALMS% $realms] \
187			    ]
188}
189
190##############################################################################
191# Display realm add page
192##############################################################################
193
194d cgi-register {
195    action add
196    realm {}
197} {} {
198    global conf
199
200    #
201    # Get realm list
202    #
203
204    pgauth-lsrealm $dbfd tabrlm
205
206    #
207    # Display realm list
208    #
209
210    set lines {}
211    foreach r [lsort [array names tabrlm]] {
212	set descr [lindex $tabrlm($r) 0]
213	lappend lines [list Realm $r $descr]
214    }
215
216    set realms [::arrgen::output "html" $conf(tabrlm) $lines]
217
218    #
219    # Admin Yes/No
220    #
221
222    set yes [mc "yes"]
223    set no [mc "no"]
224    set fmt "%1\$s $yes   %2\$s $no"
225    set admin [::webapp::form-yesno "admin" 0 $fmt]
226
227    #
228    # End of script: output page and close database
229    #
230
231    d urlset "%URLFORM%" $conf(next)
232
233    d result $conf(page-add) [list \
234				[list %REALMS% $realms] \
235				[list %ADMIN% $admin] \
236			    ]
237}
238
239##############################################################################
240# Store new realm
241##############################################################################
242
243d cgi-register {
244    action add
245    realm .+
246} {
247    {descr	1 1}
248    {admin	1 1}
249} {
250    global conf
251
252    #
253    # Check admin form value
254    #
255
256    if {$admin ne "0" && $admin ne "1"} then {
257	d error [mc "Invalid input"]
258    }
259
260    #
261    # Add realm in database
262    #
263
264    if {! [pgauth-addrealm $dbfd $realm $descr $admin msg]} then {
265	d error $msg
266    }
267
268    #
269    # End of script: output page and close database
270    #
271
272    set title [mc {Realm '%s' insertion} $realm]
273    d result $conf(page-ok) [list \
274				[list %TITLEACTION% $title] \
275				[list %COMPLEMENT% ""] \
276			    ]
277}
278
279##############################################################################
280# Display realm mod page
281##############################################################################
282
283d cgi-register {
284    action mod
285    realm {}
286} {} {
287    global conf
288
289    #
290    # Get realm selection menu
291    #
292
293    set menurealm [pgauth-htmlrealmmenu $dbfd "realm" 0 {}]
294    if {$menurealm eq ""} then {
295	d error [mc "Unable to get realm list"]
296    }
297
298    #
299    # End of script: output page and close database
300    #
301
302    d urlset "%URLFORM%" $conf(next)
303
304    d result $conf(page-modsel) [list \
305				[list %MENUREALM% $menurealm] \
306			    ]
307}
308
309##############################################################################
310# Update modified realm
311##############################################################################
312
313d cgi-register {
314    action mod-edit.*
315} {
316    {realm	1 1}
317    {descr	0 1}
318    {admin	0 1}
319    {truemembers 0 1}
320    {users	0 99999}
321    {members	0 99999}
322    {store	0 1}
323    {reset	0 1}
324    {left	0 1}
325    {right	0 1}
326} {
327    global conf
328
329    #
330    # Check realm information
331    #
332
333    pgauth-lsrealm $dbfd tabrlm
334    if {! [info exists tabrlm($realm)]} then {
335	d error [mc "Realm '%s' not found" $realm]
336    }
337
338    #
339    # Is this the first time we enter this cgi?
340    #
341
342    if {$action eq "mod-editfirst"} then {
343	set reset "yes"
344    }
345
346    if {$reset ne ""} then {
347	#
348	# Reset : use values from database
349	#
350	lassign $tabrlm($realm) descr truemembers admin
351    }
352
353    #
354    # Admin
355    #
356
357    if {$admin eq ""} then {
358	set admin 0
359    }
360    if {$admin ne "0" && $admin ne "1"} then {
361	d error [mc "Invalid input"]
362    }
363
364    #
365    # Button values:
366    #   - store : store current values
367    #   - left : add users to realm
368    #   - right : remove users from realm
369    #   - reset : welcome in the loop
370    #
371
372    if {$store ne ""} then {
373	#
374	# Store realm modification
375	#
376
377	if {! [pgauth-setrealm $dbfd $realm $descr $admin $truemembers msg]} then {
378	    d error $msg
379	}
380
381	#
382	# End of script: output page and close database
383	#
384
385	set title [mc {Realm '%s' modification} $realm]
386	d result $conf(page-ok) [list \
387				    [list %TITLEACTION% $title] \
388				    [list %COMPLEMENT% ""] \
389				]
390	return
391    } elseif {$right ne ""} then {
392	set truemembers [mod-members $truemembers $users {}]
393    } elseif {$left ne ""} then {
394	set truemembers [mod-members $truemembers {} $members]
395    }
396
397    #
398    # Admin menu
399    #
400
401    set yes [mc "yes"]
402    set no [mc "no"]
403    set fmt "%1\$s $yes   %2\$s $no"
404    set admin [::webapp::form-yesno "admin" $admin $fmt]
405
406    #
407    # Listbox for members
408    #
409
410    set l {}
411    foreach u [lsort $truemembers] {
412	set tab($u) {}
413	lappend l [list $u $u]
414    }
415    set members [::webapp::form-menu members $conf(size) 1 $l {}]
416
417    #
418    # Listbox for all other users
419    #
420
421    set l {}
422    foreach u [pgauth-searchuser $dbfd nonexistentarray {+login}] {
423	if {! [info exists tab($u)]} then {
424	    lappend l [list $u $u]
425	}
426    }
427    set users [::webapp::form-menu users $conf(size) 1 $l {}]
428
429    #
430    # End of script: output page and close database
431    #
432
433    set qdescr [::webapp::html-string $descr]
434    d urlset "%URLFORM%" $conf(next)
435    d result $conf(page-modedit) [list \
436				[list %REALM%       $realm] \
437				[list %DESCR%       $qdescr] \
438				[list %ADMIN%       $admin] \
439				[list %TRUEMEMBERS% $truemembers] \
440				[list %ALL%         $users] \
441				[list %MEMBERLIST%  $members] \
442			    ]
443}
444
445proc mod-members {truemembers more less} {
446    #
447    # Initial set
448    #
449
450    foreach m $truemembers {
451	set tab($m) {}
452    }
453
454    #
455    # Add "more" members
456    #
457
458    foreach m $more {
459	set tab($m) {}
460    }
461
462    #
463    # Remove "less" members
464    #
465
466    foreach m $less {
467	catch {unset tab($m)}
468    }
469
470    #
471    # return result back
472    #
473
474    return [array names tab]
475}
476
477##############################################################################
478# Display realm removal page
479##############################################################################
480
481d cgi-register {
482    action del
483    realm {}
484} {} {
485    global conf
486
487    #
488    # Get realm selection menu
489    #
490
491    set menurealm [pgauth-htmlrealmmenu $dbfd "realm" 0 {}]
492    if {$menurealm eq ""} then {
493	d error [mc "Unable to get realm list"]
494    }
495
496    #
497    # End of script: output page and close database
498    #
499
500    d urlset "%URLFORM%" $conf(next)
501    d result $conf(page-del) [list \
502				[list %MENUREALM% $menurealm] \
503			    ]
504}
505
506##############################################################################
507# Remove realm
508##############################################################################
509
510d cgi-register {
511    action del
512    realm .+
513} {} {
514    global conf
515
516    #
517    # Check realm information
518    #
519
520    pgauth-lsrealm $dbfd tabrlm
521    if {! [info exists tabrlm($realm)]} then {
522	d error [mc "Realm '%s' not found" $realm]
523    }
524
525    set nusers [llength [lindex $tabrlm($realm) 1]]
526    if {$nusers > 0} then {
527	d error [mc {Realm '%1$s' holds %2$s member(s). Delete them first} $realm $nusers]
528    }
529
530    #
531    # Remove the realm
532    #
533
534    if {! [pgauth-delrealm $dbfd $realm msg]} then {
535	d error $msg
536    }
537
538    #
539    # End of script: output page and close database
540    #
541
542    set title [mc {Realm '%s' removal} $realm]
543    d result $conf(page-ok) [list \
544				[list %TITLEACTION% $title] \
545				[list %COMPLEMENT% ""] \
546			    ]
547}
548
549##############################################################################
550# Main procedure
551##############################################################################
552
553d cgi-dispatch "pgauth" "authadmin"
554