AutoForm/Session.pm untested

filter: Statementstmt 0.0% 0 / 117 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 218 CC 13 SCAR 16.8
CC
13
Cov
0%
next_form
12.0
export_form
12.0
new
6.0
CRAP 5.4
1 # Session.pm
2 #
3 # $Id: Session.pm,v 1.6 2005/01/27 21:32:49 rsandberg Exp $
4 #
5  
6 package CGI::AutoForm::Session;
7  
8 use strict;
9 use CGI::AutoForm;
10  
11  
12 =head1 NAME
13  
14 CGI::AutoForm::Session - Stateful CGI sessions
15  
16 =head1 SYNOPSIS
17  
18
19 use CGI::AutoForm::Session;
20  
21  
22 $session = new CGI::AutoForm::Session($query,[$user],[$dbh]);
23  
24 $form = $session->next_form();
25  
26 $form = $session->restart();
27  
28 $form = $session->jump_form();
29  
30 $form = $session->repeat_form();
31  
32 $form = $session->summary_form();
33  
34 $status = $session->status();
35  
36 $normalized_query = $session->query();
37  
38 $bool = $session->invalid_input();
39  
40  
41 =head2 Attribute Accessors/Modifiers
42  
43 Get the values of these READ-ONLY attributes.
44  
45 $query = $session->query();
46  
47 Get or set the values of these attributes.
48  
49 $dbh = $session->dbh();
50 $user = $session->user();
51  
52 =head1 DESCRIPTION
53  
54 A virtual base class to help implement a CGI session
55 across HTTP requests.
56  
57 The basic idea is to define a
58 set of forms using CGI::AutoForm, and cache them if desired
59 (e.g. if using mod_perl). You then define a sequence that
60 the forms will appear in the session depending on form names,
61 user input, etc. Your CGI program propagates the session, form by form (by calling $session->next_form())
62 and user input is automatically verified at each stage according to the constraints
63 you define with CGI::AutoForm. If a validation error occurs, the form is immediately presented again.
64 At the end of the session, you have the option
65 of presenting a summary screen where the user can confirm, edit
66 or cancel the session. Editing previous input is allowed by jumping back to
67 a previous form, yet all user input remains intact
68 throughout the session.
69  
70 Refer to CGI::AutoForm throughout this documentation.
71  
72  
73 =head1 DETAILS
74  
75 This class relies on HIDDEN HTML form elements for persistence;
76 why not something more sophisticated like IPC so that objects
77 don't have to be reinstantiated? (e.g. PHP)
78 Because with IPC you have to concern yourself with
79 memory contraints from a heavily loaded web site
80 or possible memory leaks from (perhaps rare) aberrant signals.
81 HIDDEN form elements don't add or remove security concerns;
82 a CGI session can only be as secure as
83 the HTTP protocol that underlies it. If security is an issue, use
84 HTTPS or equivalent and be consistent for the duration of the session.
85  
86 A disadvantage of this approach is that data passed back and forth
87 via HIDDEN fields need to be re-validated, but this should be done
88 with the final summary form in any case.
89  
90 Enough blabber!
91  
92 =head2 Subclassing
93  
94 You implement a session by subclassing this module.
95 The following methods are expected to be overridden:
96  
97 =over 4
98  
99 =item C<_init>
100  
101 $success = $session->_init();
102  
103 This is called during construction and allows any
104 class-specific initialization to occur (you can also override new() and chain up). With mod_perl
105 I like to use this method to create all my forms and cache
106 them when the very first object is created (of course, if any
107 definitions of the form change in the database, I have to restart apache).
108 Always give the form name to the contructor of form objects.
109  
110 Only the form structure should ever be cached; form
111 objects should be void of any user-specific data. By using/overriding
112 next_form() and/or fetch_form() you can effectively cache forms and alter their
113 structure or content during session runtime. An important distinction
114 is made here between form structure and session-specific forms which
115 may be customized according to user input or have user content. For security reasons, user input should
116 never be cached with this class.
117  
118 =item C<assign_forms>
119  
120 $success = $session->assign_forms();
121  
122 If you are caching forms, the {forms} attribute must be assigned in this
123 method. It will be a hashref of form_name => $form object
124 pairs. All cached forms must be assigned a name.
125 If you are not caching forms, or if your forms are created completely on-the-fly,
126 you must override fetch_form(). See _init for a discussion of form caching.
127  
128  
129 =item C<_next_form>
130  
131 $form = $session->_next_form();
132  
133 Not to be confused with next_form()
134 This is where you direct the session by returning a form object
135 according to a sequence of forms you define along with current and previous
136 session information in state machine fashion. A few conventions are available to help guide you.
137  
138 query() returns the web server response query string structured in a form acceptable to CGI::AutoForm::prepare()
139 which was passed in during construction.
140 See also CGI::AutoForm::format_query() for an even more useful structure.
141  
142 recent_form_name() - this will return the most recently submitted form name.
143  
144 start_form_name() - returns the name of the form that was first used in this
145 session. In order for this to work this method should be called when the
146 start form is first returned from _next_form() e.g., include this line somewhere
147 in your subclass:
148  
149 $self->start_form_name($this_is_the_first_form_of_this_session);
150  
151 Note that recent_form_name() and start_form_name() use the name
152 of your subclass as a unique identifier in the unlikely event that 2 or more sessions happen to
153 bleed into each other. It does this in a case-insensitive manner and may cause problems if 2 classes have the same
154 name that differ only by case.
155  
156 =item C<verify_callback>
157  
158 $sub = $session->verify_callback();
159  
160 This will be called during query validation (C<invalid_input>) and the
161 return value (if not undef()) must be a reference to a subroutine (callback)
162 that will be passed to CGI::AutoForm::validate_query().
163  
164 =head1 METHODS
165  
166  
167 =item C<new> (constructor)
168  
169 $session = new CGI::AutoForm::Session($query,[$user],[$dbh]);
170  
171 Create a new session object with a structured query string (see C<CGI::AutoForm::prepare> for the structure), and optional:
172 $user - a username for restricted forms and $dbh - a valid database handle.
173  
174 $query is immediately passed through CGI::AutoForm::normalize_query().
175 $user and $dbh may be convenient for subclasses.
176  
177 Return undef if error.
178  
179 =cut
180 sub new
181 {
182 my ($caller,$query,$user,$dbh) = @_;
183 my $class = ref($caller) || $caller;
184 my $nq = CGI::AutoForm->normalize_query($query) if $query;
185 my $self = bless({
186 dbh => $dbh,
187 query => $nq,
188 user => $user,
189 },$class);
190  
191 $self->_init() || (warn("Initialization failed"),return undef);
192 $self->assign_forms();
193 return $self;
194 }
195  
196 =pod
197  
198 =item C<status>
199  
200 $status = $session->status([$status]);
201  
202 To advise your CGI program (or mod_perl handler) the following default status indicators apply
203 to the form returned from next_form(). You can override them as appropriate.
204  
205 'CONTINUE' - The form is a continuation in a sequence of forms.
206  
207 'REPEAT' - The form is a repeat of the previous form (most likely
208 because it had user input validation errors).
209  
210 'JUMP' - See set by calling jump_form().
211  
212 'SUMMARY' - A summary of all previous forms in the session.
213  
214 =cut
215 sub status
216 {
217 my ($self) = @_;
218 return $self->{status};
219 }
220  
221 sub _init
222 {
223 return 1;
224 }
225  
226 sub _next_form
227 {
228 0;
229 }
230  
231 sub initialized
232 {
233 return 1;
234 }
235  
236 sub assign_forms
237 {
238 my ($self) = @_;
239 return $self->{forms} = {};
240 }
241  
242 sub verify_callback
243 {
244 my ($self) = @_;
245 return undef;
246 }
247  
248 =pod
249  
250 =item C<query>
251  
252 $normalized_query = $session->query();
253  
254 Return a normalized form of $query passed to the
255 constructor. (See CGI::AutoForm::normalize_query())
256  
257 =cut
258 sub query
259 {
260 my ($self) = @_;
261 return $self->{query};
262 }
263  
264 sub user
265 {
266 my ($self,$user) = @_;
267 if (defined($user))
268 {
269 $self->{user} = $user;
270 }
271 return $self->{user};
272 }
273  
274 sub dbh
275 {
276 my ($self,$dbh) = @_;
277 if (defined($dbh))
278 {
279 $self->{dbh} = $dbh;
280 }
281 return $self->{dbh};
282 }
283  
284 =pod
285  
286 =item C<invalid_input>
287  
288 $bool = $session->invalid_input();
289  
290 Validates user input via CGI::AutoForm::validate_query().
291 May return the same form if the input elements can't be verified with VALID_ERROR attrs set for each field in error.
292 Otherwise, return false if validation was successful.
293  
294 =cut
295 sub invalid_input
296 {
297 my ($self) = @_;
298 my $query = $self->query();
299 my $form = $self->fetch_form($self->recent_form_name()) || return 0; # No form to validate against
300 unless ($form->validate_query($query,$self->verify_callback()))
301 {
302 return $form;
303 }
304 return 0;
305 }
306  
307 =pod
308  
309 =item C<next_form>
310  
311 $form = $session->next_form();
312  
313 Validates current form submission using the query structure given in the constructor (if any).
314 May return the same form if the input elements can't be verified with VALID_ERROR attrs set for each field in error.
315 Otherwise the next form in the session is returned.
316  
317 Refer to CGI::AutoForm for usage of $form.
318  
319 =cut
320 sub next_form
321 {
322 my $self = shift;
323 my $form;
324 if ($form = $self->invalid_input())
325 {
326 ##at const,const,const
327 $self->{status} = 'REPEAT';
328 }
329 else
330 {
331 $form = $self->_next_form(@_) || return undef;
332 ##at const,const,const
333 $self->{status} = 'CONTINUE' unless defined($self->{status});
334 }
335 return $self->export_form($form);
336 }
337  
338 sub fen_prefix
339 {
340 my $self = shift;
341 my $class = uc(ref($self));
342 $class =~ s/\:\:/_/g;
343 ##at table/group names cannot be named __SSDAT this is a reserved group name!!
344 return "__SSDAT.$class";
345 }
346  
347 sub export_form
348 {
349 my ($self,$form) = @_;
350 my $query = $self->query();
351 my $i = 1;
352 my $form_exists;
353 my $name = $form->name();
354 my $fen_pre = $self->fen_prefix();
355 while (exists($query->{"$fen_pre.FORM$i"}))
356 {
357 $form_exists && (delete($query->{"$fen_pre.FORM$i"}),next);
358 $form_exists++ if $query->{"$fen_pre.FORM$i"} eq $name;
359 }
360 continue
361 {
362 $i++;
363 }
364 $query->{"$fen_pre.FORM$i"} = $name unless $form_exists;
365 $self->recent_form_name($form);
366 #$form->prepare($query);
367 $self->{form} = $form;
368 return $form;
369 }
370  
371 # have a property of fields be cacheable and cache a record only if ALL fields are cacheable
372 # actually, in general only search forms should never be cached
373 ##at user_masks should be unique
374  
375 =pod
376  
377 =item C<restart>
378  
379 $form = $session->restart();
380  
381 Return the very first form of this session with all user input intact.
382  
383 =cut
384 sub restart
385 {
386 my ($self) = @_;
387 my $form = $self->fetch_form($self->start_form_name());
388 $self->{status} = 'CONTINUE';
389 return $self->export_form($form);
390 }
391  
392 =pod
393  
394 =item C<jump_form>
395  
396 $form = $session->jump_form();
397  
398 Jump back to a form previously encountered in this session with all user input intact.
399  
400 =cut
401 sub jump_form
402 {
403 my ($self,$form_name) = @_;
404 my $form = $self->fetch_form($form_name);
405 ##at const,const,const
406 $self->{status} = 'JUMP';
407 return $self->export_form($form);
408 }
409  
410 =pod
411  
412 =item C<repeat_form>
413  
414 $form = $session->repeat_form();
415  
416 Return the most recently submitted form with all user input intact.
417  
418 =cut
419 sub repeat_form
420 {
421 my ($self) = @_;
422 my $form = $self->fetch_form($self->recent_form_name());
423 ##at const,const,const
424 $self->{status} = 'REPEAT';
425 return $self->export_form($form);
426 }
427  
428 =pod
429  
430 =item C<fetch_form>
431  
432 $form = $session->fetch_form($form_name);
433  
434 Return the $form object indentified with $form_name.
435  
436 =cut
437 sub fetch_form
438 {
439 my ($self,$form_name) = @_;
440 my $form = $self->{forms}{$form_name} || return undef;
441 return $form->clone($self->dbh(),$form_name);
442 }
443  
444 sub start_form_no
445 {
446 my ($self) = @_;
447 my $query = $self->query();
448 my $i = 1;
449 my $start_name = $self->start_form_name();
450 my $fen_pre = $self->fen_prefix();
451 while (exists($query->{"$fen_pre.FORM$i"}))
452 {
453 return $i if $query->{"$fen_pre.FORM$i"} eq $start_name;
454 $i++;
455 }
456 return undef;
457 }
458  
459 =pod
460  
461 =item C<prepare_form>
462  
463 $form = $session->prepare_form($form);
464  
465 Call the C<prepare> method on $form passing in
466 $session->{query}.
467  
468 =cut
469 sub prepare_form
470 {
471 my ($self,$form) = @_;
472 return $form->prepare($self->{query});
473 }
474  
475 sub start_form_name
476 {
477 my ($self,$form) = @_;
478 my $fen_pre = $self->fen_prefix();
479 my $form_key = "$fen_pre._SESSION_START";
480 if (defined($form))
481 {
482 return ($self->{query}{$form_key} = $form->name());
483 }
484 return $self->{query}{$form_key};
485 }
486  
487 sub recent_form_name
488 {
489 my ($self,$form) = @_;
490 my $fen_pre = $self->fen_prefix();
491 my $form_key = "$fen_pre._CURRENT_FORM";
492 if (defined($form))
493 {
494 return ($self->{query}{$form_key} = $form->name());
495 }
496 return $self->{query}{$form_key};
497 }
498  
499 =pod
500  
501 =item C<summary_form>
502  
503 $form = $session->summary_form();
504  
505 Return a form that contains all data groups of every form
506 submitted in this session. This could be lengthy so you may want to
507 customize the default layout of CGI::AutoForm.
508  
509 The form data will be readonly but all data will be available on submission
510 through hidden fields. Typically the user will have the chance to reject
511 the data and the session would continue at the beginning by calling restart().
512  
513 NOTE: Data from the summary form should be re-validated.
514  
515 =cut
516 sub summary_form
517 {
518 my ($self) = @_;
519 my $query = $self->query();
520 my $i = $self->start_form_no() || (warn("No start to this session?!?"),return undef);
521 my $form = new CGI::AutoForm(undef,'SUMMARY');
522 $form->readonly(1);
523 my $fen_pre = $self->fen_prefix();
524 while (exists($query->{"$fen_pre.FORM$i"}))
525 {
526 next if $query->{"$fen_pre.FORM$i"} eq 'SUMMARY';
527 my $sub_form = $self->fetch_form($query->{"$fen_pre.FORM$i"});
528 foreach my $gr (@{$sub_form->{group_list}})
529 {
530 $form->push_group($gr);
531 }
532 }
533 continue
534 {
535 $i++;
536 }
537 $self->{status} = 'SUMMARY';
538 return $form;
539 }
540  
541 1;
542  
543 __END__