fetch_merchant on primary API request

This commit is contained in:
Rainer Gümpelein 2022-01-05 11:08:12 +01:00
parent 67dda28bb6
commit 20048865c0
3 changed files with 119 additions and 57 deletions

View file

@ -52,87 +52,61 @@ my $dbh = "";
#fetch merchant_id by request or hostname
sub fetch_merchant {
my $self = shift;
my $q = shift;
my $varenv = shift;
my $req_sessionid = shift || "";
my $req_authcookie = shift || "";
my $req_coo = shift || "";
my $req_merchant_id = shift || "";
my $response = {
user_id => "",
authcookie => "",
new_authcoo => "0",
clearing_cache => "0",
agb_checked => "0",
user_group => [],
user_tour => [],
response_state => "OK, nothing todo",
privacy_html => "site/privacy_1.html",
agb_html => "site/agb.html",
impress_html => "site/impress.html",
tariff_info_html => "site/tariff_info_1.html",
bike_info_html => "site/bike_info.html",
lang => "DE",
last_used_operator => {
operator_name => "sharee.bike | TeilRad GmbH",
operator_color => "#009699",
operator_email => "hotline\@sharee.bike",
operator_phone => "+49 761 45370097",
operator_hours => "Bürozeiten: Montag, Mittwoch, Freitag 9-12 Uhr",
},
init_map => {
center => { latitude => "", longitude => "" },
radius => ""
}
};
my $return = { merchant_id => "" };
my $merchanized = 0;
my $aowner = 0;
my $merchant_id = "";
while ((my $merchant_conf, my $value) = each %{ $dbt->{merchant_ids}}) {
if($merchant_conf && (($req_sessionid && $req_sessionid =~ /$merchant_conf$/) || ($req_authcookie && $req_authcookie =~ /$merchant_conf$/) || ($req_merchant_id && $req_merchant_id eq $merchant_conf))){
$bw->log("---> trying merchant select by session-cookie OR authcookie OR merchant_id: if($merchant_conf && (($req_coo && $req_coo =~ /$merchant_conf/) || ($req_merchant_id && $req_merchant_id eq $merchant_conf))){",$merchant_conf,"");
if($merchant_conf && (($req_coo && $req_coo =~ /$merchant_conf$/) || ($req_merchant_id && $req_merchant_id eq $merchant_conf))){
$aowner = $value->{id};
$merchant_id = $merchant_conf;
$merchanized = 1;
$return->{merchant_id} = $merchant_conf;
my $lat = "";
my $lng = "";
$value->{initMap} =~ s/\s//g;
($lat,$lng) = split(/,/,$value->{initMap}) if($value->{initMap} && $value->{initMap} =~ /\d+,\d+/);
if($lat && $lng){
$response->{init_map}->{center}->{latitude} = $lat;
$response->{init_map}->{center}->{longitude} = $lng;
$response->{init_map}->{radius} = "2.9";
$return->{init_map}->{center}->{latitude} = $lat;
$return->{init_map}->{center}->{longitude} = $lng;
$return->{init_map}->{radius} = "2.9";
}
$bw->log("merchant select by session-cookie OR authcookie OR merchant_id: if($merchant_conf && (($req_sessionid && $req_sessionid =~ /$merchant_conf$/) || ($req_authcookie && $req_authcookie =~ /$merchant_conf$/) || ($req_merchant_id && $req_merchant_id eq $merchant_conf))){",$merchant_conf,"");
last;
$bw->log("===> merchant select by session-cookie OR authcookie OR merchant_id: if($merchant_conf && (($req_coo && $req_coo =~ /$merchant_conf/) || ($req_merchant_id && $req_merchant_id eq $merchant_conf))){",$return->{merchant_id},"");
#last;
}
}
if(!$merchanized){
if($varenv->{syshost} eq "shareeapp-primary" || $varenv->{syshost} eq "shareedms-primary"){
$aowner = $dbt->{primary}->{sharee_primary}->{owner};
$merchant_id = $dbt->{primary}->{sharee_primary}->{merchant_id};
$return->{merchant_id} = $dbt->{primary}->{sharee_primary}->{merchant_id};
$merchanized = 1;
$bw->log("merchant select by primary dbname: sharee_primary:",$merchant_id,"");
$bw->log("merchant select by primary dbname: sharee_primary:",$return->{merchant_id},"");
}
elsif($varenv->{syshost} =~ /shareeapp-(\w+)/ || $varenv->{syshost} =~ /shareedms-(\w+)/){
$aowner = $dbt->{operator}->{$varenv->{dbname}}->{owner};
$merchant_id = $dbt->{operator}->{$varenv->{dbname}}->{merchant_id};
$return->{merchant_id} = $dbt->{operator}->{$varenv->{dbname}}->{merchant_id};
$merchanized = 1;
$bw->log("merchant select by operator dbname=$varenv->{dbname}:",$merchant_id,"");
$bw->log("merchant select by operator dbname=$varenv->{dbname}:",$return->{merchant_id},"");
}
elsif($varenv->{syshost} =~ /shareeweb-/){
$aowner = $dbt->{website}->{$varenv->{syshost}}->{owner};
$merchant_id = $dbt->{website}->{$varenv->{syshost}}->{merchant_id};
$return->{merchant_id} = $dbt->{website}->{$varenv->{syshost}}->{merchant_id};
$merchanized = 1;
$bw->log("merchant select by website syshost=$varenv->{syshost}:",$merchant_id,"");
$bw->log("===> merchant select by website syshost=$varenv->{syshost}:",$return->{merchant_id},"");
}
}
return ($aowner,$merchant_id,$response);
return ($aowner,$return);
}
#helper to get template
sub get_node_meta {
my $self = shift;

View file

@ -35,7 +35,7 @@ sub handler {
my ($r) = @_;
my $q = new CGI;
my $netloc = $q->url(-base=>1);
#$q->import_names('R');
$q->import_names('R');
my $json = JSON->new->allow_nonref;
my $cf = new Config;
my $dbt = new DBtank;
@ -60,17 +60,44 @@ print $q->header(-type => "application/json", -charset => "utf-8", -'Access-Cont
my $respreq = $q->param('request') || "";
my $apiserver = $q->url(-base=>1) || "";
(my $aowner,$varenv{merchant_id},my $response) = $apif->fetch_merchant(\%varenv,"",$R::authcookie,$R::merchant_id);
my $response = {
apiserver => "$apiserver",
response => "$respreq",
uri_primary => "$dbt->{primary}->{sharee_primary}->{primaryApp}",
copri_version => "4.1.9.6",
user_id => "",
authcookie => "",
new_authcoo => "0",
clearing_cache => "0",
agb_checked => "0",
user_group => [],
user_tour => [],
response_state => "OK, nothing todo",
privacy_html => "site/privacy_1.html",
agb_html => "site/agb.html",
impress_html => "site/impress.html",
tariff_info_html => "site/tariff_info_1.html",
bike_info_html => "site/bike_info.html",
lang => "DE",
last_used_operator => {
operator_name => "sharee.bike | TeilRad GmbH",
operator_color => "#009699",
operator_email => "hotline\@sharee.bike",
operator_phone => "+49 761 45370097",
operator_hours => "Bürozeiten: Montag, Mittwoch, Freitag 9-12 Uhr",
},
init_map => {
center => { latitude => "", longitude => "" },
radius => ""
}
};
$response->{apiserver} = "$apiserver";
$response->{response} = "$respreq";
$response->{uri_primary} = "$varenv{uri_primary}";
$response->{copri_version} = "4.1.9.40";
if(!$aowner && ($R::authcookie || $R::merchant_id)){
my $aowner = 0;
my $coo = $q->param('authcookie') || $q->param('sessionid') || "";
if(!$coo && !$q->param('merchant_id')){
$response->{response_state} = "Failure 9900: no authcookie or merchant_id defined";
$response->{response_text} = "Authentifizierung fehlgeschlagen.";
$bw->log("NO authcookie or merchant_id defined",$R::merchant_id,"");
$bw->log("Failure 9900: NO authcookie or merchant_id defined",$q,"");
my $jrout = $json->pretty->encode({shareejson => $response});
print $jrout;
return Apache2::Const::OK;
@ -81,6 +108,7 @@ if(!$aowner && ($R::authcookie || $R::merchant_id)){
foreach(@keywords){
if(length($_) > 40 || length($q->param($_)) > 400){
$response->{response_state} = "Failure 9000: amount of characters in $_ exceeds";
$bw->log("Failure 9000: amount of characters in $_ exceeds",$q,"");
my $jrout = $json->pretty->encode({shareejson => $response});
print $jrout;
return Apache2::Const::OK;
@ -100,6 +128,10 @@ if($q->param('user_device_manufaturer') || $q->param('user_device_model') || $q-
#just auth_verify
if($q->param('request') eq "auth_verify"){
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
my ($auth,$authraw) = $apif->auth_verify($q);
$response = { %$response, %$auth };
if(ref($auth) eq "HASH" && $auth->{authcookie}){
@ -112,6 +144,10 @@ if($q->param('request') eq "auth_verify"){
#authout
elsif($q->param('request') eq "authout"){
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
my ($auth,$authraw) = $apif->authout($q);
$response = { %$response, %$auth };
if(ref($auth) eq "HASH" && $auth->{authcookie}){
@ -125,6 +161,10 @@ elsif($q->param('request') eq "authout"){
}
#authorization
elsif($q->param('request') eq "authorization"){
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
my ($auth,$authraw) = $apif->authorization($q,"","",$aowner);
if(ref($auth) eq "HASH" && $auth->{authcookie}){
$response = { %$response, %$auth };
@ -137,6 +177,10 @@ elsif($q->param('request') eq "authorization"){
#booking request
elsif($q->param('request') eq "booking_request"){
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
my ($auth,$authraw) = $apif->auth_verify($q);
$response = { %$response, %$auth };
if(ref($auth) eq "HASH" && $auth->{authcookie}){
@ -218,6 +262,10 @@ elsif($q->param('request') eq "booking_cancel" || $q->param('request') eq "booki
$q->param(-name=>'request',-value=>"booking_update");
$q->param(-name=>'state',-value=>"canceled");
}
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
my ($auth,$authraw) = $apif->auth_verify($q);
my $rows = 0;
$response = { %$response, %$auth };
@ -312,6 +360,10 @@ elsif($q->param('request') eq "booking_cancel" || $q->param('request') eq "booki
#user_rental_history
elsif($q->param('request') eq "user_rentals_history"){
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
my ($auth,$authraw) = $apif->auth_verify($q);
$response = { %$response, %$auth };
@ -327,6 +379,9 @@ elsif($q->param('request') eq "user_rentals_history"){
#user_bikes_occupied
elsif($q->param('request') eq "user_bikes_occupied"){
if($varenv{syshost} eq "shareeapp-primary"){
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
my ($auth,$authraw) = $apif->auth_verify($q);
if(ref($auth) eq "HASH" && $auth->{authcookie}){
@ -356,6 +411,10 @@ elsif($q->param('request') eq "user_bikes_occupied"){
elsif($q->param('request') eq "bikes_available"){
#use Time::HiRes qw/gettimeofday/;
if($varenv{syshost} eq "shareeapp-primary"){
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
my ($auth,$authraw) = $apif->auth_verify($q);
$response = { %$response, %$auth };
($response->{bikes},$response->{uri_operator_array},$response->{user_group},$response->{user_tour}) = $jsc->loop_sharees($q,$auth,$aowner);
@ -377,6 +436,10 @@ elsif($q->param('request') eq "bikes_available"){
#cronjob for maintanance update runs at ~ 7:00
elsif($q->param('request') eq "bikes_all"){
if($varenv{syshost} eq "shareeapp-primary"){
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
my ($auth,$authraw) = $apif->auth_verify($q);
$response = { %$response, %$auth };
($response->{bikes},$response->{uri_operator_array},$response->{user_group},$response->{user_tour}) = $jsc->loop_sharees($q,$auth,$aowner);
@ -521,6 +584,10 @@ elsif($q->param('request') eq "bikes_all"){
#stations_all
elsif($q->param('request') eq "stations_all"){
if($varenv{syshost} eq "shareeapp-primary"){
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
my ($auth,$authraw) = $apif->auth_verify($q);
$response = { %$response, %$auth };
($response->{stations},$response->{uri_operator_array},$response->{user_group},$response->{user_tour}) = $jsc->loop_sharees($q,$auth,$aowner);
@ -535,6 +602,10 @@ elsif($q->param('request') eq "stations_all"){
#stations_available
elsif($q->param('request') eq "stations_available"){
if($varenv{syshost} eq "shareeapp-primary"){
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
my ($auth,$authraw) = $apif->auth_verify($q);
#Mein konrad App
#if($dbt->{merchant_ids}->{$varenv{merchant_id}}->{id} eq "176"){
@ -552,6 +623,10 @@ elsif($q->param('request') eq "stations_available"){
#user_feedback / user_minianswer of user_miniquery
elsif($q->param('request') eq "user_feedback" || $q->param('request') eq "user_minianswer"){
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
my ($auth,$authraw) = $apif->auth_verify($q);
$response = { %$response, %$auth };
$response->{uri_operator} = "$varenv{wwwhost}";
@ -588,6 +663,10 @@ elsif($q->param('request') eq "user_feedback" || $q->param('request') eq "user_m
#service_done
#insert and/or update
elsif($q->param('request') eq "service_done"){
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
my ($auth,$authraw) = $apif->auth_verify($q);
my $station_id = $1 if($q->param('station') =~ /(\d+)/);
my $bike_id = $1 if($q->param('bike') =~ /(\d+)/);
@ -720,6 +799,10 @@ elsif($q->param('request') eq "service_done"){
#service_work
#service_work. select last service by bike-id
elsif($q->param('request') eq "service_work"){
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
my ($auth,$authraw) = $apif->auth_verify($q);
my $station_id = "";
my $bike_id = "";
@ -797,6 +880,10 @@ elsif($q->param('request') eq "service_work"){
#last if request not defined
else{
($aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$q->param('merchant_id'));
$varenv{merchant_id} = $return_merchant->{merchant_id};
$response = { %$response, %$return_merchant };
$response->{'response_state'} = "Failure: request not defined";
}

View file

@ -109,7 +109,8 @@ sub handler {
print $q->header(-charset=>"$html_charset", -cookie=>$cookie);
}
(my $aowner,$varenv{merchant_id},my $response) = $apif->fetch_merchant(\%varenv,$coo,"","");
(my $aowner,my $return_merchant) = $apif->fetch_merchant($q,\%varenv,$coo,$R::merchant_id);
$varenv{merchant_id} = $return_merchant->{merchant_id};
$bw->log("Indexsharee merchant select used with access_owner $aowner",$varenv{merchant_id},"");
($api_return,$users_sharee) = $apif->auth_verify($q,$coo,"");