#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package BookPub::Command;

#
# This class is intended to be a base class for all the commands in BookPub.
#

use strict;
use warnings;

use lib '/app/tools/common/lib';
use RSApache::Response::Redirect;
use base 'RSApache::Command::SessionedCommand';

use lib '/app/tools/bookpub/lib';
use BookPub::Message;

sub app      { return 'bookpub'; }
sub pageName { return 'Catalog Home'; }

sub _init {
    my ($self) = @_;

    $self->SUPER::_init();

    # In this application, we'll have each command specify explicitly the page name (or title).
    #
    $self->{xml}{PageName} = $self->pageName();
    return $self;
}

sub execute {
    my ($self) = @_;

    my $response = $self->SUPER::execute();
    return $response if $response;


    my $userID = Common::RSApp::GetActiveUserID();
    my $user   = AppUser::DB::Item::User->Lookup( user_id => $userID );
    if ( $user->must_change ) {

        # The must_change flag means that only the change password page (/rps/login?c=changepw)
        # is valid.
        # The flag will be cleared once the user successfully updates their password.
        # If the user tries to access any other area or command then kill the session and force
        # the user back to the login page.
        # Since all bookpub routes start with /bookpub, we'll just reject any bookpub
        # commands if must_change is set.
        #
        my $session = RSApache::RSWebApp::GetSession();
        if ( $session->IsValid() ) {
            if ( $session->End() ) {
                print STDERR "BookPub::Command -- session ended\n";
            } else {
                print STDERR "BookPub::Command -- session end failed: ".$session->errstr."\n";
            }
        }

        $response = RSApache::Response::Redirect->new('/');
        return $response;

    }
    return undef;
}
###
1;    # Play nicely.
###

