view release on metacpan or search on metacpan
lib/App/ZofCMS/Plugin/FormChecker.pm view on Meta::CPAN
return $self->_fail( $name, 'max_error', $rule )
if defined $rule->{max}
and length($value) > $rule->{max};
if ( $rule->{must_match} ) {
return $self->_fail( $name, 'must_match_error', $rule )
if $value !~ /$rule->{must_match}/;
}
if ( $rule->{must_not_match} ) {
return $self->_fail( $name, 'must_not_match_error', $rule )
if $value =~ /$rule->{must_not_match}/;
}
if ( $rule->{code} ) {
return $self->_fail( $name, 'code_error', $rule )
unless $rule->{code}->( $value, map $self->$_, qw/template query config/ );
}
if ( my @values = @{ $rule->{valid_values} || [] } ) {
my %valid;
@valid{ @values} = (1) x @values;
lib/App/ZofCMS/Plugin/FormChecker.pm view on Meta::CPAN
return $rule->{ $err_name }
if exists $rule->{ $err_name };
my %errors = (
mandatory_error => "You must specify parameter $name",
num_error => "Parameter $name must contain digits only",
min_error => "Parameter $name must be at least $rule->{min} characters long",
max_error => "Parameter $name cannot be longer than $rule->{max} characters",
code_error => "Parameter $name contains incorrect data",
must_match_error => "Parameter $name contains incorrect data",
must_not_match_error => "Parameter $name contains incorrect data",
param_error => "Parameter $name does not match parameter $rule->{param}",
either_or_error => "Parameter $name must contain data if other parameters are not set",
valid_values_error
=> "Parameter $name must be " . do {
my $last = pop @{ $rule->{valid_values} || [''] };
join(', ', @{ $rule->{valid_values} || [] } ) . " or $last"
},
);
return $errors{ $err_name };
lib/App/ZofCMS/Plugin/FormChecker.pm view on Meta::CPAN
ok_code => sub { die "All ok!" },
fill_prefix => 'form_checker_',
rules => {
param1 => 'num',
param2 => qr/foo|bar/,
param3 => [ qw/optional num/ ],
param4 => {
optional => 1,
select => 1,
must_match => qr/foo|bar/,
must_not_match => qr/foos/,
must_match_error => 'Param4 must contain either foo or bar but not foos',
param => 'param2',
},
param5 => {
valid_values => [ qw/foo bar baz/ ],
valid_values_error => 'Param5 must be foo, bar or baz',
},
param6 => sub { time() % 2 }, # return true or false values
},
},
lib/App/ZofCMS/Plugin/FormChecker.pm view on Meta::CPAN
no_fill => 1,
fill_prefix => 'plug_form_q_',
rules => {
param1 => 'num',
param2 => qr/foo|bar/,
param3 => [ qw/optional num/ ],
param4 => {
optional => 1,
select => 1,
must_match => qr/foo|bar/,
must_not_match => qr/foos/,
must_match_error => 'Param4 must contain either foo or bar but not foos',
},
param5 => {
valid_values => [ qw/foo bar baz/ ],
valid_values_error => 'Param5 must be foo, bar or baz',
},
param6 => sub { time() % 2 }, # return true or false values
},
},
lib/App/ZofCMS/Plugin/FormChecker.pm view on Meta::CPAN
=head3 C<rules>
rules => {
param1 => 'num',
param2 => qr/foo|bar/,
param3 => [ qw/optional num/ ],
param4 => {
optional => 1,
select => 1,
must_match => qr/foo|bar/,
must_not_match => qr/foos/,
must_match_error => 'Param4 must contain either foo or bar but not foos',
},
param5 => {
valid_values => [ qw/foo bar baz/ ],
valid_values_error => 'Param5 must be foo, bar or baz',
},
param6 => sub { time() % 2 }, # return true or false values
},
This is the "heart" of the plugin, the place where you specify the rules for checking.
lib/App/ZofCMS/Plugin/FormChecker.pm view on Meta::CPAN
value defines diffent things or just indicates that the rule should be considered.
Here is the list of all valid ruleset keys:
rules => {
param => {
name => 'Parameter', # the name of this param to use in error messages
num => 1, # value must be numbers-only
optional => 1, # parameter is optional
either_or => [ qw/foo bar baz/ ], # param or foo or bar or baz must be set
must_match => qr/foo/, # value must match given regex
must_not_match => qr/bar/, # value must NOT match the given regex
max => 20, # value must not exceed 20 characters in length
min => 3, # value must be more than 3 characters in length
valid_values => [ qw/foo bar baz/ ], # value must be one from the given list
code => sub { time() %2 }, # return from the sub determines pass/fail
select => 1, # flag for "filling", see no_fill key above
param => 'param1',
num_error => 'Numbers only!', # custom error if num rule failed
mandatory_error => '', # same for if parameter is missing and not optional.
must_match_error => '', # same for must_match rule
must_not_match_error => '', # same for must_not_match_rule
max_error => '', # same for max rule
min_error => '', # same for min rule
code_error => '', # same for code rule
either_or_error => '', # same for either_or rule
valid_values_error => '', # same for valid_values rule
param_error => '', # same fore param rule
},
}
You can mix and match the rules for perfect tuning.
lib/App/ZofCMS/Plugin/FormChecker.pm view on Meta::CPAN
represents the name of a query parameter. In order for the rule to succeed B<either> one
of the parameters must be set. It's a bit messy, but you must use the C<optional> rule
as well as list the C<either_or> rule for every parameter that is tested for "either or" rule.
=head4 C<must_match>
must_match => qr/foo/,
Takes a regex (C<qr//>) as a value. The query parameter's value must match this regex.
=head4 C<must_not_match>
must_not_match => qr/bar/,
Takes a regex (C<qr//>) as a value. The query parameter's value must B<NOT> match this regex.
=head4 C<max>
max => 20,
Takes a positive integer as a value. Query parameter's value must not exceed C<max>
characters in length.
lib/App/ZofCMS/Plugin/FormChecker.pm view on Meta::CPAN
user did not specify the query parameter. I.e., "error to display for missing mandatory
parameters". B<Defaults to:> C<You must specify parameter $name>
=head4 C<must_match_error>
must_match_error => 'Must match me!',
This is the error for C<must_match> rule. B<Defaults to:>
C<Parameter $name contains incorrect data>
=head4 C<must_not_match_error>
must_not_match_error => 'Cannot has me!',
This is the error for C<must_not_match> rule. B<Defaults to:>
C<Parameter $name contains incorrect data>
=head4 C<max_error>
max_error => 'Too long!',
This is the error for C<max> rule. B<Defaults to:>
C<Parameter $name cannot be longer than $max characters> where C<$max> is the C<max> rule's
value.
lib/App/ZofCMS/Plugin/UserLogin.pm view on Meta::CPAN
}
sub is_page_restricted {
my ( $self, $page, $user_ref ) = @_;
my $opts = $self->opts;
if ( $page eq $opts->{login_page} ) {
return;
}
for ( @{ $opts->{not_restricted} || [] } ) {
if ( $self->page_matches( $page, $_ ) ) {
if ( ref eq 'HASH' ) {
if ( exists $_->{role} ) {
return if ref $_->{role} eq 'SCALAR';
return exists $user_ref->{role}{ $_->{role} } ? () : 1;
}
else {
return;
}
}
lib/App/ZofCMS/Plugin/UserLogin.pm view on Meta::CPAN
plug_login => {
dsn => "DBI:mysql:database=test;host=localhost",
user => 'test', # user,
pass => 'test', # pass
opt => { RaiseError => 1, AutoCommit => 0 },
table => 'users',
login_page => '/login',
redirect_on_restricted => '/login',
redirect_on_login => '/',
redirect_on_logout => '/',
not_restricted => [ qw(/ /index) ],
restricted => [ qr/^/ ],
smart_deny => 'login_redirect_page',
preserve_login => 'my_site_login',
login_button => '<input type="submit"
class="input_submit" value="Login">',
logout_button => '<input type="submit"
class="input_submit" value="Logout">',
},
In L<HTML::Template> template for C<'/login'> page:
lib/App/ZofCMS/Plugin/UserLogin.pm view on Meta::CPAN
opt => { RaiseError => 1, AutoCommit => 0 },
table => 'users',
user_ref => sub {
my ( $user_ref, $template ) = @_;
$template->{d}{plug_login_user} = $user_ref;
},
login_page => '/login',
redirect_on_restricted => '/login',
redirect_on_login => '/',
redirect_on_logout => '/',
not_restricted => [ qw(/ /index) ],
restricted => [ qr/^/ ],
smart_deny => 'login_redirect_page',
preserve_login => 'my_site_login',
login_button => '<input type="submit"
class="input_submit" value="Login">',
logout_button => '<input type="submit"
class="input_submit" value="Logout">',
},
These settings can be set via C<plug_login> first-level key in ZofCMS
lib/App/ZofCMS/Plugin/UserLogin.pm view on Meta::CPAN
Elements that are regexes (C<qr//>) will be matched against the page. If
the page matches the given regex access will be restricted to any user
who is not logged in.
=head3 a hashref
restricted => [
{ page => '/secret', role => \1 },
{ page => '/admin', role => 'customer' },
{ page => '/admin', role => 'not_customer' },
{ page => qr|^/customers/|, role => 'not_customer' },
],
Using hashrefs you can set specific roles that are restricted from a given
page. The hashref must contain two keys: the C<page> key and C<role> key.
The value of the C<page> key can be either a string or a regex which will
be matched against the current page the same way as described above. The
C<role> key must contain a role of users that B<are restricted> from
accessing the page specified by C<page> key or a scalarref
(meaning "any role"). B<Note> you can specify only
B<one> role per hashref. If you want to have several roles you need to
specify several hashrefs or use C<not_restricted> option described below.
In the example above only logged in users who are B<NOT> members of role
C<customer> or C<not_customer> can access C</admin> page and
only logged in users who are B<NOT> members of role C<not_customer>
can access pages that begin with C</customers/>. The page C</secret> is
restricted for B<everyone> (see note on scalarref below).
B<IMPORTANT NOTE:> the restrictions will be checked until the first one
matching the page criteria found. Therefore, make sure to place
the most restrictive restrictions first. In other words:
restricted => [
qr/^/,
{ page => '/foo', role => \1 },
lib/App/ZofCMS/Plugin/UserLogin.pm view on Meta::CPAN
B<Note:> the role can also be a I<scalarref>; if it is, it means "any role".
In other words:
restricted => [ qr/^/ ],
Means "all the pages are restricted for users who are not logged in". While:
restricted => [ { page => qr/^/, role \1 } ],
Means that "all pages are restricted for everyone" (in this case you'd use
C<not_restricted> option described below to ease the restrictions).
=head2 C<not_restricted>
not_restricted => [
qw(/foo /bar /baz),
qr|^/foo/|i,
{ page => '/garbage', role => \1 },
{ page => '/admin', role => 'admin' },
{ page => qr|^/customers/|, role => 'customer' },
],
B<Optional>. The value is the exact same format as for C<restricted> option
described above. B<By default> is not specified.
The purpose of C<not_restricted> is the reverse of
C<restricted> option. Note that pages that match anything in
C<not_restricted> option will not be checked against C<restricted>. In other
words you can construct rules such as this:
restricted => [
qr/^/,
{ page => qr|^/admin|, role => \1 },
],
not_restricted => [
qw(/ /index),
{ page => qr|^/admin|, role => 'admin' },
],
The example above will restrict access to every page on the site that is
not C</> or C</index> to any user who is not logged in. In addition, pages
that begin with C</admin> will be accessible only to users who are members
of role C<admin>.
=head2 C<limited_time>