CGI-Application-Framework

 view release on metacpan or  search on metacpan

caf_project/Example/common-modules/Example.pm  view on Meta::CPAN


sub template_pre_process {
    my $self       = shift;
    my ($template) = @_;

    # Change the internal template parameters by reference
    my $template_params = $template->get_param_hash;

    my $config = $self->conf->context;

    foreach (keys %$config) {
        unless (exists $template_params->{$_}) {
            $template_params->{$_} = $config->{$_};
        }
    }
    return $self->SUPER::template_pre_process(@_);
}

########################################################################
########################################################################
#####
##### The following subroutines are all needed by Framework.pm.
##### Your application will, at some point in time or another, crash
##### if you do not provide the subs such that they do what they're
##### supposed to do.  Probably, the crash will happen very soon
##### indeed.
#####
########################################################################
########################################################################

sub _login_authenticate {

    my $self = shift;

    # ===============================================================
    # Framework.pm expects this subroutine to return a list
    # with 2 values:
    #
    #    1st value -- 0 or 1, 0 being failure to login-authenticate
    #                 a user, 1 being success
    #                     --> 1 means that both the username and
    #                         password matched
    #                     --> 0 means that they didn't
    #
    #    2nd value -- undef, or a $user object
    #                     --> undef means that no such user could
    #                         be found
    #                     --> $user object means that a user with
    #                         the given username was found
    #
    # The combination that these return values create is interpreted
    # by Framework.pm as follows:
    #
    #   (0, undef) --> Unknown user
    #   (0, $user) --> user was found, incorrect password given
    #   (1, $user) --> user was found, password given correct
    #
    #   ** (1, undef) --> not possible!  this will never happen **
    #
    # Note that you are responsible for creating a login.html
    # HTML::Template file (or just plain HTML file I guess
    # but that's a dumb idea) that is on the template search path
    # that will provide something logically equivalent to username
    # and password form fields, that you will use here.  You can
    # name them what you want to here;  Framework.pm makes
    # no assumptions regarding what they should be called.
    #
    # Technically, you don't even need username and password fields.
    # If you can creatively figure out a way to authenticate without
    # these concepts then that's up to you.  Framework.pm
    # doesn't depend on username and password concepts.
    # ===============================================================

    # ---------------------------------------------------------------
    # Note that picking up the query param 'username' comes from a
    # web login attempt.
    # ---------------------------------------------------------------
    my $user     = undef;
    my $username = $self->query->param('username');

    ($user) = CDBI::Example::example::Users->search(
						    username => $username
						    )
	if length($username);

    # ---------------------------------------------------------------

    # Note that, in this example, _password_authenticate_user generates
    # the 2-element list that is finally returned to Framework.pm

    return $self->_password_authenticate_user($user);
}

sub _relogin_authenticate {

    my $self = shift;

    # ===============================================================
    # Framework.pm expects this subroutine to return a list
    # with 2 values:
    #
    #    1st value -- 0 or 1, 0 being failure to login-authenticate
    #                 a user, 1 being success
    #                     --> 1 means that both the username and
    #                         password matched
    #                     --> 0 means that they didn't
    #
    #    2nd value -- undef, or a $user object
    #                     --> undef means that no such user could
    #                         be found
    #                     --> $user object means that a user with
    #                         the given username was found
    #
    # The combination that these return values create is interpreted
    # by Framework.pm as follows:
    #
    #   (0, undef) --> Unknown user
    #   (0, $user) --> user was found, incorrect password given
    #   (1, $user) --> user was found, password given correct
    #
    #   ** (1, undef) --> not possible!  this will never happen **
    #
    # Note that you are responsible for creating a relogin.html
    # HTML::Template file (or just plain HTML file I guess
    # but that's a dumb idea) that is on the template search path
    # that will provide something logically equivalent to a password
    # form field, that you will use here.  You can name them what
    # you want to here;  Framework.pm makes no assumptions
    # regarding what they should be called.
    #
    # Technically, you don't even need a password field. If you can
    # creatively figure out a way to authenticate without these
    # concepts then that's up to you.  Framework.pm doesn't
    # depend on username and password concepts.
    # ===============================================================

    # ---------------------------------------------------------------
    # Since we are reauthenticating from within the application we
    # have the username (and uid) stuck within the session, so we
    # retrieve it from there.
    # ---------------------------------------------------------------
    my $user = undef;
    $user = CDBI::Example::example::Users->retrieve
	(
	 $self->session->{uid}
	 );
    # ---------------------------------------------------------------

    # Note that, in this example, _password_authenticate_user generates
    # the 2-element list that is finally returned to Framework.pm

    return $self->_password_authenticate_user($user);
}

sub _login_profile {

    # --------------------------------------------------
    # This is a Data::FormValidate definition, needed by
    # CGI::Application::Plugin::ValidateRM
    #
    # It is invoked from Framework.pm.  The
    # specifics of this should match the needs of your
    # login.html form-displaying HTML::Template.
    # --------------------------------------------------

    return {
	required => [ qw ( username password ) ],
	msgs     => {
	    any_errors => 'some_errors', # just want to set a true value here
	    prefix     => 'err_',
	},
    };
    # --------------------------------------------------
}

sub _relogin_profile {

    # --------------------------------------------------
    # This is a Data::FormValidate definition, needed by
    # CGI::Application::Plugin::ValidateRM
    #
    # It is invoked from Framework.pm.  The
    # specifics of this should match the needs of your
    # relogin.html form-displaying HTML::Template.



( run in 1.864 second using v1.01-cache-2.11-cpan-39bf76dae61 )