#============================================================
# WebLinkObject2
#============================================================
package WebLinkObject2;
use Common;
@ISA = qw(Common);
#公開したいサブルーチン
#@EXPORT = qw(erfc tan);
@EXPORT_OK = qw();
use strict;
use Template;
#==========================================
# 大域変数
#==========================================
#============================================================
# コンストラクタ、デストラクタ
#============================================================
sub new
{
my ($module) = @_;
my $this = {};
bless $this;
# $this->SUPER::new(@_);
return $this;
}
sub DESTROY
{
my $this = shift;
$this->SUPER::DESTROY(@_);
}
sub SetApparatusMode { my ($this,$f)=@_; return $this->{ApparatusMode} = $f; };
#============================================================
# メンバー関数
#============================================================
sub ShowMenu
{
my ($this, $App, $pParams, $ProgramPath, $BaseOption, $ModuleName, $pMenuTitle) = @_;
my ($Option, $target) = $App->GetCGIParameters(
{
Action => "${ModuleName}::ShowPage",
PrevAction => $pParams->{Action},
},
[qw(+Action)]
);
my $lang = ($pParams->{Language})? $pParams->{Language} : 'en';
my $Title = $pMenuTitle->{$lang};
$App->PrintRawHTML("{ScriptPath}?$Option\" target=\"_self\">$Title ");
}
sub ShowPage
{
my ($this, $App, $pParams, $ProgramPath, $BaseOption, $ModuleName, $pMenuTitle, $TryDirectLink, %hash) = @_;
$TryDirectLink = 1 if(!defined $TryDirectLink);
my ($Option, $target) = $App->GetCGIParameters(
{
Action => "${ModuleName}::ShowPage",
PrevAction => $pParams->{Action},
},
[qw(+Action)]
);
# my ($ret, $link) = $this->BuildLink($App, $pParams);
my ($ret, $link, $url, $target2);
#$App->print("CG[$hash{ChangeGroups}]\n");
if($TryDirectLink) {
# ($ret, $link, $url, $target2) = $this->BuildLink($App, $pParams, 1) : (0, '');
#$App->print("1: $hash{CreateNewUser}, $hash{ChangePassword}, $hash{CryptPassword}, \n");
#$App->print("2: $hash{ChangePrivileges}, $hash{ChangeGroups}, \n");
($ret, $link, $url, $target2) = $this->BuildLink($App, $pParams, 1,
$hash{URLTemplate}, $hash{pLinkTitle}, $hash{pMenuTitle},
$hash{CreateNewUser}, $hash{ChangePassword}, $hash{CryptPassword},
$hash{ChangePrivileges}, $hash{ChangeGroups},
TargetGroup => $hash{TargetGroup},
);
}
else {
($ret, $link) = (0, '');
}
$App->print("ret=$ret [$url]\n");
#$App->print("link [$link] ret=$ret\n");
if($ret == 1 or $ret == 2) {
# if($ret == 1) {
$App->H2($App->mlText('en' => 'Click the following link',
'jp' => '下記のリンクをクリックしてください'));
# my $wait = 0;
# Utils::HTMLRedirectTo($url, $wait, '_self', $pParams->{WebCharSet});
$App->PrintRawHTML($link);
}
elsif($ret == -1) {
$App->H3("Error in ${ModuleName}::ShowPage: Your account [$pParams->{EMail}] is not registered in this Users DB.\n");
$App->PrintRawHTML($link);
}
elsif($ret == 3) {
$App->H3("Your account [$pParams->{EMail}] does not exist in the target User DB.\n");
$App->H3("This account [$pParams->{EMail}] has been added to the target User DB.\n");
$App->PrintRawHTML($link);
}
elsif($ret == -2) {
$App->H3("Your account [$pParams->{EMail}] does not exist in the target User DB.\n");
$App->PrintRawHTML($link);
}
elsif($ret == 3) {
$App->H3("Your password does not match with the target password.\n");
$App->H3("The target password has been changed.");
$App->PrintRawHTML($link);
}
elsif($ret == -3) {
$App->H3("Your password does not match with the target password.\n");
$App->PrintRawHTML($link);
}
elsif($ret == 4) {
$App->H3("Your record has been updated.\n");
$App->PrintRawHTML($link);
}
else {
$App->H3("Invalid return code: ret = $ret\n");
$App->PrintRawHTML($link);
}
}
sub BuildGroups
{
my ($this, $App, $pParams, $MyGroups, $TargetGroups, $DefaultGroups, $pOptionGroups) = @_;
my @MyGrs = Utils::Split("\\s*[\|,]\\s*", $MyGroups);
my @Total = Utils::Split("\\s*[\|,]\\s*", $TargetGroups);
my @a1 = Utils::Split("\\s*[\|,]\\s*", $DefaultGroups);
@Total = Utils::MergeList(@Total, @a1);
my $LabStr = "研究室";
my $Frontier = "フロンティア";
if($pParams->{SQLCharCode}) {
Jcode::convert(\$LabStr, $pParams->{SQLCharCode});
Jcode::convert(\$Frontier, $pParams->{SQLCharCode});
}
my %GroupKeys = (
$LabStr => 'HKLab',
'SORST' => 'Project',
$Frontier => 'Project',
);
foreach my $gr (@MyGrs) {
my $opt;
foreach my $key (keys %GroupKeys) {
if($gr =~ /$key/i) {
$opt = $pOptionGroups->{$GroupKeys{$key}};
last if($opt);
}
}
next if(!defined $opt);
my @a2 = Utils::Split("\\s*[\|,]\\s*", $opt);
@Total = Utils::MergeList(@Total, @a2);
}
my $ret = '';
foreach my $s (sort @Total) {
if($ret eq '') {
$ret = $s;
}
else {
$ret .= ",$s";
}
}
return $ret;
}
sub BuildPrivileges
{
my ($this, $App, $pParams, $MyPrivileges, $TargetPrivileges, $DefaultPrivileges, $pOptionPrivileges) = @_;
my @MyPrivs = Utils::Split("\\s*[\|,]\\s*", $MyPrivileges);
my @Total = Utils::Split("\\s*[\|,]\\s*", $TargetPrivileges);
my @a1 = Utils::Split("\\s*[\|,]\\s*", $DefaultPrivileges);
@Total = Utils::MergeList(@Total, @a1);
foreach my $pr (@MyPrivs) {
next if(!defined $pOptionPrivileges->{$pr});
my @a2 = Utils::Split("\\s*[\|,]\\s*", $pOptionPrivileges->{$pr});
@Total = Utils::MergeList(@Total, @a2);
}
my $ret = '';
foreach my $s (sort @Total) {
if($ret eq '') {
$ret = $s;
}
else {
$ret .= ",$s";
}
}
return $ret;
}
sub GetMyInformation
{
my ($this, $App, $pParams, $UsersTableName) = @_;
$App->DB()->Search($UsersTableName, "EMail='$pParams->{EMail}' order by sn", "");
my %Hit = $App->DB()->GetNextHit();
return %Hit;
}
sub GetDB
{
# my ($this, $App, $pParams, $AppModuleName, $DBConfigName) = @_;
my ($this, $App, $pParams, $pDBConfigArray, $DBConfigName) = @_;
return $App->MyCGIApplication::OpenDB($DBConfigName, $pDBConfigArray);
# my ($DB, $DBConfigName, $DBMName, $DBServer, $DBUser, $DBPassword, $DBName, @TableNames);
#eval("use $AppModuleName;" . 'my $ExtApp = new ' . "$AppModuleName;"
# .'($DB, $DBConfigName, $DBMName, $DBServer, $DBUser, $DBPassword, $DBName, @TableNames) = $ExtApp->OpenDB($DBConfigName);'
# ."no $AppModuleName");
# return ($DB, $DBConfigName, $DBMName, $DBServer, $DBUser, $DBPassword, $DBName, @TableNames);
}
sub BuildLink
{
my ($this, $App, $pParams, $ForMenu, $URLTemplate, $pLinkTitle, $pMenuTitle,
$CreateNewUser, $ChangePassword, $CryptPassword, $ChangePrivileges, $ChangeGroups, %hash) = @_;
$ChangeGroups = 0 if(!defined $ChangeGroups);
#$App->print("aa:$CreateNewUser, $ChangePassword, $CryptPassword, $ChangePrivileges, $ChangeGroups, ", join(':', %hash), "\n");
my %Hit = $this->GetMyInformation($App, $pParams);
if($Hit{Password} eq '') {
my $URL = Template->new()->ReplaceByHash($URLTemplate, $pParams, '{', '}', 0, 1, 0, 1);
return (-1, "Try this URL: " . $App->mlText(%$pLinkTitle) . "\n");
}
my ($DB, $DBConfigName, $DBMName, $DBServer, $DBUser, $DBPassword, $DBName, @TableNames) = $this->GetDB($App, $pParams);
return "no DB found" if(!defined $DB);
$App->print("Target DB: $DBName.$TableNames[0]/$DBServer TargetYear: $pParams->{TargetYear}\n");
my $ret = $DB->Search($TableNames[0], "EMail='$pParams->{EMail}' order by sn", "");
my $nHit = $DB->nHit();
$App->print("nHit=$nHit (ret=$ret)\n");
my %ExtHit;
for(my $i = 0 ; $i < $nHit ; $i++) {
my %Hit = $DB->GetNextHit();
%ExtHit = %Hit if($i == 0);
$App->print(" $i: $Hit{EMail}: Status=$Hit{Status}: sn=$Hit{sn}: FY=$Hit{FiscalYear}: Privilege=$Hit{Privilege}: App=$Hit{Apparatus}\n");
#$App->print(" PW=$Hit{Password}\\n");
}
my @DBArray = ();
#$App->print("ChangePrivileges=$ChangePrivileges, ChangeGroups=$ChangeGroups\n");
if($ChangePrivileges or $nHit == 0) {
my $pr = $this->BuildPrivileges($App, $pParams, $pParams->{LogonUserPrivilege}, $ExtHit{Privilege});
@DBArray = (Privilege => $pr);
}
#$App->print("TG:[$hash{TargetGroup}]: AppMode=$this->{ApparatusMode}: CG=$ChangeGroups\n");
if($this->{ApparatusMode} and ($ChangeGroups or $nHit == 0)) {
#$App->print("DB: $DBName\n");
#$App->print("Groups: [$Hit{Groups}] [$ExtHit{Apparatus}]\n");
if($hash{TargetGroup}) {
@DBArray = (@DBArray, Apparatus => $hash{TargetGroup});
}
else {
my $gr = $this->BuildGroups($App, $pParams, $Hit{Groups}, $ExtHit{Apparatus});
@DBArray = (@DBArray, Apparatus => $gr);
}
}
#$App->print("DBArray = ", join(',', @DBArray), "\n");
my $ret = 1;
if($nHit == 0) {
if($CreateNewUser) {
$App->print("Create new user [$Hit{EMail}] in $DBName.$TableNames[0].\n");
$ret = 2;
my $rret = $DB->InsertData($TableNames[0],
Name => $Hit{Name},
EMail => $Hit{EMail},
Password => $Hit{Password},
UpdateDate => time(),
@DBArray,
);
$App->print(" remote db ret=$rret\n");
}
else {
$ret = -2;
my $URL = Template->new()->ReplaceByHash($URLTemplate, $pParams, '{', '}', 0, 1, 0, 1);
return ($ret, "Try this URL: " . $App->mlText(%$pLinkTitle) . "\n");
}
}
else {
#$App->print("[$Hit{Password} ne $ExtHit{Password}]\n");
if($Hit{Password} ne $ExtHit{Password}) {
if($ChangePassword) {
$ret = 3;
$DB->UpdateData($TableNames[0], "sn=$ExtHit{sn}",
Password => $Hit{Password},
UpdateDate => time(),
@DBArray,
);
}
else {
$ret = -3;
my $URL = Template->new()->ReplaceByHash($URLTemplate, \%Hit, '{', '}', 0, 1, 0, 1);
return ($ret, "Try this URL: " . $App->mlText(%$pLinkTitle) . "\n");
}
}
if($ChangePrivileges or $ChangeGroups) {
$ret = 4;
$DB->UpdateData($TableNames[0], "sn=$ExtHit{sn}",
UpdateDate => time(),
@DBArray,
);
}
}
$Hit{Password} = $App->Crypt($Hit{Password}) if($CryptPassword);
my $URL = Template->new()->ReplaceByHash($URLTemplate, \%Hit, '{', '}', 0, 1, 0, 1);
if($ForMenu) {
return ($ret, "" . $App->mlText(%$pMenuTitle) . "\n");
}
return ($ret, "" . $App->mlText(%$pLinkTitle) . "\n", $URL, '_blank');
}
1;