App-ZofCMS

 view release on metacpan or  search on metacpan

lib/App/ZofCMS/Plugin/UserLogin.pm  view on Meta::CPAN


sub make_login_form {
    my ( $self, %args ) = @_;

    my $t = HTML::Template->new_scalar_ref( \ login_form_template() );

    $t->param(
        %args,
        smart_deny => (
            (
                defined $args{smart_deny_name}
                and length $args{smart_deny_name}
            ) ? 1 : 0
        ),
    );

    return $t->output;
}

sub opts {
    my $self = shift;
    if ( @_ ) {
        $self->{OPTS} = shift;
    }
    return $self->{OPTS};
}

sub login_error {
    my $self = shift;
    if ( @_ ) {
        $self->{LOGIN_ERROR} = shift;
    }
    return $self->{LOGIN_ERROR};
}

sub process_smart_deny_logon {
    my ( $opts, $q ) = @_;

    return $opts->{redirect_on_login}
        unless defined $opts->{smart_deny}
            and length $opts->{smart_deny}
            and defined $q->{ $opts->{smart_deny} }
            and length $q->{ $opts->{smart_deny} };

    return $q->{ $opts->{smart_deny} };
}

sub process_smart_deny {
    my ( $opts ) = @_;

    return ''
        unless defined $opts->{smart_deny}
            and length $opts->{smart_deny};

    use Data::Dumper;


    my $appended_value = $opts->{redirect_on_restricted} =~ /\?/
    ? '' : '?';

    $appended_value .= $opts->{smart_deny} . '=' . uri_escape( $ENV{REQUEST_URI} );

    return $appended_value;
}

sub login_form_template {
    return <<'END_TEMPLATE';
<form action="" method="POST" id="zofcms_plugin_login">
<div><tmpl_if name="error"><p class="error"><tmpl_var escape="html" name="error"></p></tmpl_if>
    <input type="hidden" name="page" value="<tmpl_var escape="html" name="page">">
    <input type="hidden" name="zofcms_plugin_login" value="login_user">
    <tmpl_if name="smart_deny">
        <input type="hidden" name="<tmpl_var escape="html" name="smart_deny_name">" value="<tmpl_var escape="html" name="smart_deny_value">">
    </tmpl_if>
    <ul>
        <li>
            <label for="zofcms_plugin_login_login">Login: </label
            ><input type="text" class="input_text" name="login" id="zofcms_plugin_login_login" value="<tmpl_var escape='html' name='cookie_login'>">
        </li>
        <li>
            <label for="zofcms_plugin_login_pass">Password: </label
            ><input type="password" class="input_password" name="pass" id="zofcms_plugin_login_pass">
        </li>
    </ul>
    <tmpl_var name='login_button'>
</div>
</form>
END_TEMPLATE
}

sub logout_form_template {
    return <<'END_TEMPLATE';
<form action="" method="POST" id="zofcms_plugin_login_logout">
<div><tmpl_if name="error"><p class="error"><tmpl_var escape="html" name="error"></p></tmpl_if>
    <input type="hidden" name="page" value="<tmpl_var escape="html" name="page">">
    <input type="hidden" name="zofcms_plugin_login" value="logout_user">
    <tmpl_var name='logout_button'>
</div>
</form>
END_TEMPLATE
}

sub cookie_l {
    my $self = shift;
    @_ and $self->{COOKIE_L} = shift;
    $self->{COOKIE_L};
}

sub cookie_s {
    my $self = shift;
    @_ and $self->{COOKIE_S} = shift;
    $self->{COOKIE_S};
}

1;
__END__

=encoding utf8

=head1 NAME

App::ZofCMS::Plugin::UserLogin - restrict access to pages based on user accounts

=head1 SYNOPSIS

In $your_database_of_choice that is supported by L<DBI> create a table.
You can have extra columns in it, but the first five must be named as appears
below. C<login_time> is the return of Perl's C<time()>. Password will be
C<md5_hex()>ed (with L<Digest::MD5>,
C<session_id> is C<rand() . rand() . rand()> and role depends
on what you set the roles to be:

    create TABLE users (
        login TEXT,
        password VARCHAR(32),
        login_time VARCHAR(10),
        session_id VARCHAR(55),
        role VARCHAR(20)
    );

Main config file:

    template_defaults => {
        plugins => [ { UserLogin => 10000 } ],
    },
    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      => '/',



( run in 0.631 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )