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

#
# This class is intended to be a base class for all session based commands.
#

use lib '/app/tools/common/lib';

use lib '/app/tools/rps/lib';
use RPS::DB::Item::UserAccess;

use lib '/app/tools/support/lib';
use Support::Client::ClientList;

use base 'RSApache::Command::SessionedCommand';

use constant kAccessDeniedTemplate => '/app/tools/support/templates/access_denied.xsl';
use constant kMsgAccessDenied      => 'access_denied';

# Overload if the command should check write permissions
sub write_command { undef }

# Overload if the command has special Keyword access
sub access_keyword { undef }

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

    my $response;

    # check session
    #
    $response = $self->_checkLogin();
    return $response if ($response);

    # !!! So, the command wants to seed the xml with some state.
    # !!! However, it isn't clear whether every subclass of this class
    # !!! will want the same state.  So, I'm going to make that process
    # !!! a method that we can override if necessary.
    #
    $self->_addToXML();

    # check permissions
    #
    unless ( $self->isRSUser() ) {
        return $self->accessDeniedResponse();
    }

    unless ( $self->canRead() ) {
        return $self->accessDeniedResponse();
    }

    if ( $self->write_command() && !$self->canWrite() ) {
        return $self->accessDeniedResponse();
    }

    # !!! Lets store our cookies
    Common::Preference::StoreCookies();

    # return nothing and let the derived Command execute
    #
    return undef;
}

sub canRead {
    my $self = shift;
    my $xml  = $self->{xml};

    my $module  = $xml->{Area} || $self->_area();
    my $command = $xml->{Cmd};
    my $keyword = $xml->{Keyword};

    Common::Log::Debug("Start: '$module' '$command'");
    return Support::Permission->CanRead( module => $module, page => $command, keyword => $keyword );
}

sub canWrite {
    my $self = shift;
    my $xml  = $self->{xml};

    my $module  = $xml->{Area} || $self->_area();
    my $command = $xml->{Cmd};
    my $keyword = $xml->{Keyword};

    return Support::Permission->CanWrite( module => $module, page => $command, keyword => $keyword );
}

# return true if there are ANY access records for the current user that are for RoyaltyShare
sub isRSUser {
    my $self    = shift;
    my $user_id = Common::RSApp::GetActiveUserID();

    my $userAccess = RPS::DB::Item::UserAccess->LookupRoyaltyShareAccess($user_id);

    return $userAccess ? $userAccess->hasNext() : undef;
}

sub accessDeniedResponse {
    my ($self) = @_;
    $self->addMessageXML( type => "error", code => kMsgAccessDenied );
    return RSApache::Response::XSLT->new( $self->{xml}, kAccessDeniedTemplate );
}

sub _checkLogin {
    my ($self) = @_;
    my $session = RSApache::RSWebApp::GetSession();

    if ( !$session->IsValid() ) {
        return $self->invalidSessionResponse();
    }

    return undef;
}

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

    $self->{xml}{User}       = $self->_getUser();
    $self->{xml}{Navigation} = $self->_getNavigation();
    $self->{xml}{ClientSite} = $self->_getSite();
    $self->{xml}{Clients}    = $self->_getClients();
    $self->{xml}{Keyword}    = $self->_getKeyword();
}

sub _getSite {
    my $site = "https://" . $ENV{SERVER_NAME};
    $site =~ s/admin.//;

    return $site;
}

sub _getNavigation {
    my $self = shift;

    return Support::Module::ModuleList->new( restrictedList => 1 );
}

sub _getClients {
    my $self = shift;

    return Support::Client::ClientList->new();
}

sub _getKeyword {
    my $self = shift;

    return $self->access_keyword();
}

sub _area {
    my $self = shift;

    my ( $path, $args ) = split( '\?', $ENV{REQUEST_URI} );
    my @bits = split( '\/', $path );
    return $bits[2] || 'DEFAULT';
}

###
1;    # Play nicely.
###
