[cgiapp] How to integrate a module?

adam at spatialsystems.org adam at spatialsystems.org
Fri Jan 11 12:27:01 EST 2008


SUMMARY:
How should I integrate my OO module with CGI::Applcaiton?



Details:
Here is my first OO module ( Tools::Users ) that administers users on my system.

Create a user object populated with user info from DB:
my $user = Tools::User->new( $user_id );

Get info for user stored in db:
my $name = $user->name;

Store info into object and save to db:
$user->name( $new_name );

Check if a user exists:
Tools::User->exists( $user_id ) or die "User $user_id doesn't exist";

Create new user and return object for user:
my $user = Tools::User->insert({ id => 007, name => Bond });



QUESION:
How should I integrate this with CGI::Application and CGI::Application::Plugin::Authentication?



IMPLEMENTATION:
When a user logs in, Plugin::Authentication stores the User_Id in the session.  I'd like to take 
that User_Id and run it in Tools::User->exists( $user_id ) and if it comes back false, then 
redirect to a registration page where the user will get created on my system.

If the user already exists in my DB, then I want to load the info for them from my database and 
have my user object avail from $self->user in my CGI::Application.



I'd like functionality similar to:


package VzTools::CellAvail::WebApp;
use strict;

use base qw( CGI::Application);

use CGI::Application::Plugin::Session;
use CGI::Application::Plugin::Forward;
use CGI::Application::Plugin::Authentication;
use My::ActiveDirectory;
 
sub setup {
	my $self = shift;

	$self->start_mode('login');
	$self->mode_param('rm');
	$self->run_modes(
		'login' 	=> 'show_login',
		'main' 		=> 'show_main',
		'register'	=> 'show_register',
	);

	#  setup authentication
	$self->authen->config(
		DRIVER	=> [ 'Generic', \&login ],
		STORE			=> 'Session',
		LOGIN_RUNMODE	=> 'login',
		POST_LOGIN_RUNMODE => 'main',
		LOGOUT_RUNMODE	=> 'login',
	);	

	$self->authen->protected_runmodes( 'main' );

	$self->tmpl_path('templates');

	$self->add_callback( 'load_tmpl', \&load_template_callback );

	sub login {
	
		my ( $username, $password ) = @_; 
		my $ad_auth = My::ActiveDirectory->new('WIN');
	
		return 0 if not $ad_auth->login( $username, $password );
	
		$self->session->param( ad_auth_userinfo => $ad_auth->getUserInfo );

		
		#########################################################################	
		#                                                                 	#
		#  if we've never seen the user before, redirect them to register	#
		#								#
	  	#  if they are already in the DB, then load their data with 		#
		#  Tools::User object that will be accessable everywhere via		#
		#  $self->user							#
		#								#
		#########################################################################

		Tools::User->exists( %{ $self->session->param('ad_auth_userinfo') }->{user_id} } )
			or $self->forward('register');
		
		return $username;	
	
	}

    
}


sub show_main {

	my $self = shift;

	#########################################################################
	#								#
	#  retrieve user info, update user info, add user via class method	#
	#  from Tools::Users						#
	#								#
	#########################################################################


	#  retrieve user info
	my $name = $self->users->name


	#  update user info
	$self->user->name('New_Name');

	#  class method to see if user exists
	$self->user->exists( $user_id ) or die "user doesn't exist";


	#  create new user and get it's object, dont't know if this is necessary for me to do.
	my $new_user_object = $self->user->insert({ id => 007, name => Bond });

}


More information about the cgiapp mailing list