Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 87 additions & 31 deletions apis/enhance_api.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'enhance_response.php';

/**
Expand Down Expand Up @@ -123,8 +124,7 @@ public function apiRequest($route, array $body, $method)
// Return request response
return new EnhanceResponse([
'content' => $data[count($data) - 1],
'headers' => array_splice($data, 0, count($data) - 1)]
);
'headers' => array_splice($data, 0, count($data) - 1)]);
}

/**
Expand Down Expand Up @@ -481,24 +481,32 @@ public function getLogins()
*/
public function findCustomerByEmailWithLogging($email, $logCallback = null)
{
if ($logCallback) $logCallback("Starting login search for email: $email");
if ($logCallback) {
$logCallback("Starting login search for email: $email");
}

// Get all logins for the main organization
$response = $this->getLogins();

if ($response->errors()) {
if ($logCallback) $logCallback("ERROR: Failed to get logins - " . serialize($response->errors()));
if ($logCallback) {
$logCallback('ERROR: Failed to get logins - ' . serialize($response->errors()));
}
return null;
}

$logins = $response->response();
if (!isset($logins->items) || !is_array($logins->items)) {
if ($logCallback) $logCallback("ERROR: No login items in API response");
if ($logCallback) {
$logCallback('ERROR: No login items in API response');
}
return null;
}

$loginCount = count($logins->items);
if ($logCallback) $logCallback("Found $loginCount logins to check in main organization");
if ($logCallback) {
$logCallback("Found $loginCount logins to check in main organization");
}

// Search through logins to find matching email
for ($index = 0; $index < $loginCount; $index++) {
Expand All @@ -507,17 +515,23 @@ public function findCustomerByEmailWithLogging($email, $logCallback = null)
$loginId = $login->id ?? null;
$loginName = $login->name ?? 'Unknown';

if ($logCallback) $logCallback("Checking login $index: '$loginName' - email: '$loginEmail'");
if ($logCallback) {
$logCallback("Checking login $index: '$loginName' - email: '$loginEmail'");
}

// Compare emails (case-sensitive)
if ($loginEmail === $email) {
if ($logCallback) $logCallback("LOGIN MATCH FOUND! '$loginEmail' === '$email'");
if ($logCallback) {
$logCallback("LOGIN MATCH FOUND! '$loginEmail' === '$email'");
}

// We also try to find the customer record
$customerInfo = $this->findCustomerForLogin($loginId, $logCallback);

if ($customerInfo) {
if ($logCallback) $logCallback("Found customer record: " . serialize($customerInfo));
if ($logCallback) {
$logCallback('Found customer record: ' . serialize($customerInfo));
}

return [
'customer_id' => $customerInfo['id'],
Expand All @@ -527,7 +541,9 @@ public function findCustomerByEmailWithLogging($email, $logCallback = null)
'email' => $loginEmail
];
} else {
if ($logCallback) $logCallback("WARNING: Found login but no customer record. Using login info.");
if ($logCallback) {
$logCallback('WARNING: Found login but no customer record. Using login info.');
}

// Return login info with main org as fallback
return [
Expand All @@ -539,11 +555,15 @@ public function findCustomerByEmailWithLogging($email, $logCallback = null)
];
}
} else {
if ($logCallback) $logCallback("No match: '$loginEmail' !== '$email'");
if ($logCallback) {
$logCallback("No match: '$loginEmail' !== '$email'");
}
}
}

if ($logCallback) $logCallback("Search complete - no matching login found for email: $email");
if ($logCallback) {
$logCallback("Search complete - no matching login found for email: $email");
}
return null;
}

Expand All @@ -556,22 +576,30 @@ public function findCustomerByEmailWithLogging($email, $logCallback = null)
*/
private function findCustomerForLogin($loginId, $logCallback = null)
{
if ($logCallback) $logCallback("Searching for customer records under main org");
if ($logCallback) {
$logCallback('Searching for customer records under main org');
}

// Get all customers under the main organization
$response = $this->getCustomers();

if ($response->errors()) {
if ($logCallback) $logCallback("ERROR: Failed to get customers - " . serialize($response->errors()));
if ($logCallback) {
$logCallback('ERROR: Failed to get customers - ' . serialize($response->errors()));
}
return null;
}

$customers = $response->response();
if ($logCallback) $logCallback("DEBUG: Raw customers response structure: " . json_encode($customers));
if ($logCallback) {
$logCallback('DEBUG: Raw customers response structure: ' . json_encode($customers));
}

if (!isset($customers->data) || !is_array($customers->data)) {
if (!isset($customers->items) || !is_array($customers->items)) {
if ($logCallback) $logCallback("ERROR: No customer data in response. Available keys: " . implode(', ', array_keys((array)$customers)));
if ($logCallback) {
$logCallback('ERROR: No customer data in response. Available keys: ' . implode(', ', array_keys((array)$customers)));
}
return null;
}
// Try items array instead
Expand All @@ -580,21 +608,27 @@ private function findCustomerForLogin($loginId, $logCallback = null)
$customersList = $customers->data;
}

if ($logCallback) $logCallback("Found " . count($customersList) . " customers in main organization");
if ($logCallback) {
$logCallback('Found ' . count($customersList) . ' customers in main organization');
}

// For now, just return the first customer if any exist
// In a real implementation, we might match by name or other criteria
if (count($customersList) > 0) {
$customer = $customersList[0];
if ($logCallback) $logCallback("Returning first customer: " . ($customer->name ?? 'Unknown'));
if ($logCallback) {
$logCallback('Returning first customer: ' . ($customer->name ?? 'Unknown'));
}

return [
'id' => $customer->id ?? null,
'name' => $customer->name ?? 'Unknown Customer'
];
}

if ($logCallback) $logCallback("No customers found in main organization");
if ($logCallback) {
$logCallback('No customers found in main organization');
}
return null;
}

Expand All @@ -607,34 +641,46 @@ private function findCustomerForLogin($loginId, $logCallback = null)
*/
private function findCustomerOrgForLogin($loginId, $logCallback = null)
{
if ($logCallback) $logCallback("Searching for customer organization containing login ID: $loginId");
if ($logCallback) {
$logCallback("Searching for customer organization containing login ID: $loginId");
}

// Get all customer organizations
$response = $this->getCustomers();

if ($response->errors()) {
if ($logCallback) $logCallback("ERROR: Failed to get customers - " . serialize($response->errors()));
if ($logCallback) {
$logCallback('ERROR: Failed to get customers - ' . serialize($response->errors()));
}
return null;
}

$customers = $response->response();
if ($logCallback) $logCallback("DEBUG: Raw customers response structure: " . json_encode($customers));
if ($logCallback) {
$logCallback('DEBUG: Raw customers response structure: ' . json_encode($customers));
}

if (!isset($customers->data) || !is_array($customers->data)) {
if ($logCallback) $logCallback("ERROR: No customer data in response. Available keys: " . implode(', ', array_keys((array)$customers)));
if ($logCallback) {
$logCallback('ERROR: No customer data in response. Available keys: ' . implode(', ', array_keys((array)$customers)));
}
return null;
}

$customerCount = count($customers->data);
if ($logCallback) $logCallback("Checking $customerCount customer organizations");
if ($logCallback) {
$logCallback("Checking $customerCount customer organizations");
}

// Search through each customer organization
for ($index = 0; $index < $customerCount; $index++) {
$customer = $customers->data[$index];
$customer_org_id = $customer->id ?? null;
$customer_name = $customer->name ?? 'Unknown';

if ($logCallback) $logCallback("Checking customer $index: '$customer_name' (ID: $customer_org_id)");
if ($logCallback) {
$logCallback("Checking customer $index: '$customer_name' (ID: $customer_org_id)");
}

if (!$customer_org_id) {
continue;
Expand All @@ -644,24 +690,32 @@ private function findCustomerOrgForLogin($loginId, $logCallback = null)
$membersResponse = $this->apiRequest("orgs/{$customer_org_id}/members", [], 'GET');

if ($membersResponse->errors()) {
if ($logCallback) $logCallback("ERROR getting members for customer $index: " . serialize($membersResponse->errors()));
if ($logCallback) {
$logCallback("ERROR getting members for customer $index: " . serialize($membersResponse->errors()));
}
continue;
}

$members = $membersResponse->response();
if (!isset($members->data) || !is_array($members->data)) {
if ($logCallback) $logCallback("No members data for customer $index");
if ($logCallback) {
$logCallback("No members data for customer $index");
}
continue;
}

// Check each member to see if they have the matching login ID
foreach ($members->data as $memberIndex => $member) {
$memberLoginId = $member->login->id ?? null;

if ($logCallback) $logCallback("Member {$index}-{$memberIndex} has login ID: $memberLoginId");
if ($logCallback) {
$logCallback("Member {$index}-{$memberIndex} has login ID: $memberLoginId");
}

if ($memberLoginId === $loginId) {
if ($logCallback) $logCallback("FOUND! Customer organization '$customer_name' contains login ID $loginId");
if ($logCallback) {
$logCallback("FOUND! Customer organization '$customer_name' contains login ID $loginId");
}

return [
'customer_org_id' => $customer_org_id,
Expand All @@ -672,7 +726,9 @@ private function findCustomerOrgForLogin($loginId, $logCallback = null)
}
}

if ($logCallback) $logCallback("Login ID $loginId not found in any customer organization");
if ($logCallback) {
$logCallback("Login ID $loginId not found in any customer organization");
}
return null;
}

Expand Down Expand Up @@ -743,7 +799,7 @@ public function createCustomerLogin($customer_org_id, $email, $name, $password)
$endpoints = [
"orgs/{$customer_org_id}/logins",
"logins?orgId={$customer_org_id}",
"logins"
'logins'
];

$lastResponse = null;
Expand Down
26 changes: 20 additions & 6 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.0.1",
"version": "2.0.0",
"name": "Enhance.name",
"description": "Enhance.description",
"authors": [
Expand All @@ -21,8 +21,22 @@
"name_key": "domain"
},
"email_tags": {
"module": ["server_label", "hostname", "org_id", "api_token"],
"package": ["package"],
"service": ["domain", "username", "password", "website_id", "customer_email"]
}
}
"module": [
"server_label",
"hostname",
"org_id",
"api_token"
],
"package": [
"package"
],
"service": [
"domain",
"username",
"password",
"website_id",
"customer_email"
]
},
"icon": "bi-server"
}
2 changes: 1 addition & 1 deletion config/enhance.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

// Modules may define default content for package welcome emails here. Define both a text version and html
// version for each language you wish to include. For information on writing email templates see the docs
// at https://docs.blesta.com/display/user/Customizing+Emails
Expand Down Expand Up @@ -49,4 +50,3 @@
<p>Thank you for your business!</p>'
]
]);

Loading