@ -20,6 +20,10 @@ function onRequest(context) {
var constants = require ( "/app/modules/constants.js" ) ;
var mdmProps = require ( "/app/modules/conf-reader/main.js" ) [ "conf" ] ;
var cloudProps = require ( "/app/modules/conf-reader/cloud.js" ) [ "conf" ] ;
var log = new Log ( "user-menu.js" ) ;
var sqlDateFormatter = new java . text . SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
var user = context . user ;
var isSuperTenant = false ;
if ( user . tenantId == - 1234 ) {
@ -32,6 +36,79 @@ function onRequest(context) {
viewModal . Main . Domain . isDomain = true ;
viewModal . Main . Support . isSupport = true ;
//get billing info
var type = { PAID : "PAID" , TRIAL : "TRIAL" , FREE : "FREE" } ;
var status = {
ACTIVE : "ACTIVE" ,
INACTIVE : "INACTIVE" ,
EXTENDED : "EXTENDED" ,
PENDING _DISABLED : "PENDING_DISABLED" ,
DISABLED : "DISABLED"
} ;
var BILLING _INFO _KEY = 'BILLING_INFO_' + context . user . domain ;
var BILLING _INFO _RETRY _COUNT _KEY = 'BILLING_INFO_RETRY_COUNT_' + context . user . domain ;
if ( viewModal . Main . Account . billingEnabled ) {
if ( ! session . get ( BILLING _INFO _KEY ) || daysAfterLastCheck ( session . get ( BILLING _INFO _KEY ) . lastChecked ) > 1 ) {
session . put ( BILLING _INFO _RETRY _COUNT _KEY , 0 ) ;
getBillingData ( getLoginCookie ( ) ) ;
}
var billingInfo = session . get ( BILLING _INFO _KEY ) ;
var isExpired = false ;
var isTrial = false ;
var trialPeriod = 14 ;
var cloudMgtIndexPage = viewModal . Main . Account . cloudMgtIndexPage ;
if ( ! billingInfo ) {
log . info ( "Access denied for tenant: " + context . user . domain
+ " with a NULL subscription. Redirected to CloudMgt" ) ;
response . sendRedirect ( cloudMgtIndexPage ) ;
exit ( 0 ) ;
} else if ( billingInfo . isPaidAccount && ( billingInfo . billingPlanStatus === status . ACTIVE
|| billingInfo . billingPlanStatus === status . PENDING _DISABLED ) ) {
isExpired = false ;
//change menu item name
delete viewModal . Main . Account . color ;
delete viewModal . Main . Account [ "Request Extension" ] ;
} else if ( ! billingInfo . isPaidAccount ) {
var accountContent = "Account" ;
if ( billingInfo . billingPlanStatus === status . ACTIVE || billingInfo . billingPlanStatus === status . EXTENDED ) {
var currDate = new java . util . Date ( ) ;
var endDate = sqlDateFormatter . parse ( billingInfo . endDate ) ;
var diff = endDate . getTime ( ) - currDate . getTime ( ) ;
isTrial = true ;
if ( diff > 0 ) {
noOfDays = java . util . concurrent . TimeUnit . MILLISECONDS . toDays ( diff ) ;
accountContent = "Trial - " + ( noOfDays + 1 ) + " days to upgrade" ;
} else {
isExpired = true ;
accountContent = "Trial Expired" ;
}
} else if ( billingInfo . billingPlanStatus === status . INACTIVE ) {
isExpired = false ;
isTrial = true ;
accountContent = "Trial " + ( trialPeriod ) + " days tade" ;
}
//change menu item name
viewModal . Main . Account . label = accountContent ;
viewModal . Main . Account . isDomain = false ;
viewModal . Main . Account . color = "red" ;
viewModal . Main . Account . isExpired = isExpired ;
} else if ( billingInfo . billingPlanStatus === status . DISABLED ) {
log . info (
"Access denied for tenant: " + context . user . domain
+ " with a DISABLED subscription. Redirected to CloudMgt" ) ;
response . sendRedirect ( cloudMgtIndexPage ) ;
exit ( 0 ) ;
}
} else {
// delete viewModal.Main.Account;
viewModal . Main . Account = { "billingEnabled" : false }
}
for ( var key in viewModal . Main ) {
var tempDropDownCheck = false ;
@ -80,4 +157,63 @@ function onRequest(context) {
viewModal . USER _SESSION _KEY = session . get ( constants [ "USER_SESSION_KEY" ] ) ;
viewModal . isCloud = mdmProps . isCloud ;
return viewModal ;
function getBillingData ( cookie , attempt ) {
serviceUrl = "https://cloudmgt.cloudstaging.wso2.com/cloudmgt/site/blocks/admin/admin.jag" ;
result = post ( serviceUrl ,
'action=getBillingStatusOfTenant&tenantDomain=' + context . user . domain + '&cloudType=device_cloud' ,
{ "Cookie" : cookie } ) ;
if ( result . data ) {
var billing = JSON . parse ( result . data ) . data ;
if ( ! billing . isPaidAccount && billing . billingPlanStatus === status . INACTIVE ) {
var rv = post ( serviceUrl ,
'action=informFirstLogin&tenantDomain=' + context . user . domain + '&cloudType=device_cloud' ,
{ "Cookie" : cookie } ) ;
if ( ! attempt ) attempt = 1 ;
var failStr = "First login capturing failed" ;
var successStr = "First login captured successfully" ;
if ( rv . data . substring ( 0 , failStr . length ) === failStr && attempt < 3 ) {
getBillingData ( cookie , ++ attempt ) ; //retry
} else if ( rv . data . substring ( 0 , successStr . length ) === successStr ) {
getBillingData ( cookie ) ; //get expiry details
}
} else {
session . put ( BILLING _INFO _KEY , JSON . parse ( result . data ) . data ) ;
session . put ( BILLING _INFO _RETRY _COUNT _KEY , 0 ) ;
}
}
}
function getLoginCookie ( ) {
var retryCount = session . get ( BILLING _INFO _RETRY _COUNT _KEY ) || 0 ;
if ( retryCount <= 3 ) {
retryCount ++ ;
session . put ( BILLING _INFO _RETRY _COUNT _KEY , retryCount ) ;
var username = viewModal . Main . Account . billingApi . username ;
var password = viewModal . Main . Account . billingApi . password ;
var serviceUrl = "https://cloudmgt.cloudstaging.wso2.com/cloudmgt/site/blocks/user/authenticate/ajax/login.jag" ;
var result = post ( serviceUrl , 'action=login&userName=' + username + '&password=' + password ,
{ "Content-Type" : "application/x-www-form-urlencoded" } ) ;
if ( result . data && result . data . trim ( ) === "true" ) {
var cookieHeader = result . xhr . getResponseHeader ( "Set-Cookie" ) ;
if ( cookieHeader ) {
var cookie = cookieHeader . split ( ";" ) [ 0 ] ;
return cookie ;
}
}
} else {
log . error ( "Billing info api failed after " + session . get ( BILLING _INFO _RETRY _COUNT _KEY ) + " attempts!" ) ;
}
}
function daysAfterLastCheck ( checkedDate ) {
if ( ! checkedDate ) return 0 ;
var currDate = new java . util . Date ( ) ;
var endDate = sqlDateFormatter . parse ( checkedDate ) ;
var diff = endDate . getTime ( ) - currDate . getTime ( ) ;
if ( diff > 0 ) {
return java . util . concurrent . TimeUnit . MILLISECONDS . toDays ( diff ) ;
}
return 0 ;
}
}