Skip to content

Commit

Permalink
feature(initialversion-single-lifetime-license-support)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonvandebroek committed Oct 17, 2023
1 parent 9bc5931 commit dba2943
Show file tree
Hide file tree
Showing 12 changed files with 478 additions and 33 deletions.
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<name>Sendent</name>
<summary lang="en">Sendent allows you to securely exchange files and emails</summary>
<description lang="en">Sendent allows you to securely exchange files and emails. Sendent is linked to Microsoft Outlook, so you can continue to work from your trusted email program while you mail more easily and securely. Very useful, for example, to share privacy-sensitive documents or content or to send attachments that are normally too large to email. All files are uploaded to your personal Nextcloud environment from which you determine who has access to them.</description>
<version>2.0.4</version>
<version>2.0.5</version>
<licence>agpl</licence>
<author mail="[email protected]" homepage="https://www.sendent.nl">Sendent B.V.</author>
<namespace>Sendent</namespace>
Expand Down
2 changes: 2 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@
'requirements' => ['path' => '.+']
],
['name' => 'license_api#show', 'url' => '/api/1.0/licensestatus', 'verb' => 'GET'],
['name' => 'license_api#showInternal', 'url' => '/api/1.0/licensestatusinternal', 'verb' => 'GET'],
['name' => 'license_api#showForNCGroup', 'url' => '/api/1.0/licensestatusForNCGroup', 'verb' => 'GET'],
['name' => 'license_api#showForNCGroupInternal', 'url' => '/api/1.0/licensestatusForNCGroupinternal', 'verb' => 'GET'],
['name' => 'license_api#delete', 'url' => '/api/1.0/license', 'verb' => 'DELETE'],
['name' => 'license_api#create', 'url' => '/api/1.0/license', 'verb' => 'POST'],
['name' => 'license_api#validate', 'url' => '/api/1.0/licensevalidation', 'verb' => 'GET'],
Expand Down
223 changes: 209 additions & 14 deletions lib/Controller/LicenseApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,43 @@ public function show(): DataResponse {
}

}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* Returns license status for current user
*
* @return DataResponse
*/
public function showInternal(): DataResponse {

$this->logger->info('Getting license information for user ' . $this->userId);

// Gets groups for which specific settings and/or license are defined
// Groups are ordered from highest priority to lowest
$sendentGroups = $this->appConfig->getAppValue('sendentGroups', '');
$sendentGroups = $sendentGroups !== '' ? json_decode($sendentGroups) : [];

// Gets user groups
$user = $this->userManager->get($this->userId);
$userGroups = $this->groupManager->getUserGroups($user);
$userGroups = array_map(function ($group) {
return $group->getGid();
}, $userGroups);

// Gets user groups that are sendentGroups
$userSendentGroups = array_intersect($sendentGroups, $userGroups);

// Returns settings for 1st matching group
if (count($userSendentGroups)) {
$this->logger->info('First matching group of user ' . $this->userId . ' is ' . $userSendentGroups[array_keys($userSendentGroups)[0]]);
return $this->showForNCGroup($userSendentGroups[array_keys($userSendentGroups)[0]]);
} else {
$this->logger->info('User ' . $this->userId . ' is not member of any sendent group');
return $this->showForNCGroup('');
}

}
/**
* @NoAdminRequired
* @NoCSRFRequired
Expand Down Expand Up @@ -146,16 +182,17 @@ public function showForNCGroup(string $ncgroup = ''): DataResponse {
if (is_array($result) && count($result) > 0
&& $result[0]->getLevel() != "Error_clear" && $result[0]->getLevel() != "Error_incomplete") {
} else {
throw new Exception();
//throw new Exception();
}
}
} catch (Exception $e) {
$this->logger->error('Error while renewing license ' . $result[0]->getId());
$this->logger->error('Error while renewing license ' . $result[0]->getId() . '\r\n' . $e);
}

// Reports license status
$email = $result[0]->getEmail();
$licensekey = $result[0]->getLicensekey();
$licensekey = $result[0]->getLicensekeytoken();
$this->logger->info('License key returned is: ' . $licensekey);
$dateExpiration = $result[0]->getDatelicenseend();
$dateLastCheck = $result[0]->getDatelastchecked();
$level = $result[0]->getLevel();
Expand All @@ -166,58 +203,216 @@ public function showForNCGroup(string $ncgroup = ''): DataResponse {
if ($result[0]->isCleared()) {
$status = $this->l->t("No license configured");
$statusKind = "nolicense";
} elseif ($result[0]->isIncomplete()) {
}
elseif ($result[0]->isIncomplete()) {
$status = $this->l->t("Missing email address or license key.");
$statusKind = "error_incomplete";
} elseif ($result[0]->isCheckNeeded()) {
}
elseif ($result[0]->isCheckNeeded()) {
$status = $this->l->t("Revalidation of your license is required");
$statusKind = "check";
} elseif ($result[0]->isLicenseExpired()) {
}
elseif ($result[0]->isLicenseRenewedOrSwitched()) {
$status = $this->l->t("Current license has been changed.") .
"</br>" .
$this->l->t('%1$sContact support%2$s if you experience issues with configuring your license key.', ["<a href='mailto:[email protected]' style='color:blue'>", "</a>"]);
$statusKind = "check";
}
elseif ($result[0]->isLicenseInactive()) {
$status = $this->l->t("Current license has been deactivated by Sendent.") .
"</br>" .
$this->l->t('%1$sContact support%2$s for more information.', ["<a href='mailto:[email protected]' style='color:blue'>", "</a>"]);
$statusKind = "check";
}
elseif ($result[0]->isLicenseSuspended()) {
$status = $this->l->t("Current license has been suspended by Sendent.") .
"</br>" .
$this->l->t('%1$sContact support%2$s for more information.', ["<a href='mailto:[email protected]' style='color:blue'>", "</a>"]);
$statusKind = "check";
}
elseif ($result[0]->isLicenseExpired()) {
$status = $this->l->t("Current license has expired.") .
"</br>" .
$this->l->t('%1$sContact support%2$s if you experience issues with configuring your license key.', ["<a href='mailto:[email protected]' style='color:blue'>", "</a>"]);
$statusKind = "expired";
} elseif (!$result[0]->isCheckNeeded() && !$result[0]->isLicenseExpired()) {
}
elseif (!$result[0]->isCheckNeeded() && !$result[0]->isLicenseExpired()) {
$status = $this->l->t("Current license is valid");
$statusKind = "valid";
} elseif (!$this->licensemanager->isWithinUserCount($result[0]) && $this->licensemanager->isWithinGraceUserCount($result[0])) {
}
elseif (!$this->licensemanager->isWithinUserCount($result[0]) && $this->licensemanager->isWithinGraceUserCount($result[0])) {
$status = $this->l->t("Current amount of active users exceeds licensed amount. Some users might not be able to use Sendent.");
$statusKind = "userlimit";
} elseif (!$this->licensemanager->isWithinUserCount($result[0]) && !$this->licensemanager->isWithinGraceUserCount($result[0])) {
}
elseif (!$this->licensemanager->isWithinUserCount($result[0]) && !$this->licensemanager->isWithinGraceUserCount($result[0])) {
$status = $this->l->t("Current amount of active users exceeds licensed amount. Additional users trying to use Sendent will be prevented from doing so.");
$statusKind = "userlimit";
}
return new DataResponse(new LicenseStatus($status, $statusKind, $level,$licensekey, $dateExpiration, $dateLastCheck, $email, $group));
} elseif (count($result) > 0 && $result[0]->getLevel() == "Error_incomplete") {
}
elseif (count($result) > 0 && $result[0]->getLevel() == "Error_incomplete") {
$email = $result[0]->getEmail();
$licensekey = $result[0]->getLicensekey();
$group = $result[0]->getNcgroup();
$status = $this->l->t('Missing (or incorrect) email address or license key. %1$sContact support%2$s to get your correct license information.', ["<a href='mailto:[email protected]' style='color:blue'>", "</a>"]);
return new DataResponse(new LicenseStatus($status, "error_incomplete" ,"-", $licensekey, "-", "-", $email, $group));
} elseif (count($result) > 0 && $result[0]->getLevel() == License::ERROR_VALIDATING) {
}
elseif (count($result) > 0 && $result[0]->getLevel() == License::ERROR_VALIDATING) {
$email = $result[0]->getEmail();
$licensekey = $result[0]->getLicensekey();
$group = $result[0]->getNcgroup();
return new DataResponse(new LicenseStatus($this->l->t("Cannot verify your license. Please make sure your licensekey and email address are correct before you try to 'Activate license'."), "error_validating","-", $licensekey, "-", "-", $email, $group));
} else {
}
else {
return new DataResponse(new LicenseStatus($this->l->t("No license configured"), "nolicense" ,"-", "-", "-", "-", "-"));
}
} else {
}
else {
return new DataResponse(new LicenseStatus($this->l->t("No license configured"), "nolicense" ,"-", "-", "-", "-", "-"));
}
} catch (Exception $e) {
$this->logger->error('Cannot verify license');
return new DataResponse(new LicenseStatus($this->l->t("Cannot verify your license. Please make sure your licensekey and email address are correct before you try to 'Activate license'."), "fatal" ,"-", "-", "-", "-", "-"));
}
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* Returns license status for group $ncgroup
*
* @param string $ncgroup
* @return DataResponse
*/
public function showForNCGroupInternal(string $ncgroup = ''): DataResponse {

if ($ncgroup === "") {
$this->logger->info('Getting license information for default group');
} else {
$this->logger->info('Getting license information for group ' . $ncgroup);
}

try {
// Gets license for group $ncgroup
$result = $this->service->findByGroup($ncgroup);
if (isset($result) && $result !== null && $result !== false && is_array($result) && count($result) === 0) {
// No license for group $ncgroup, getting default license
$result = $this->service->findByGroup('');
}

if (isset($result) && $result !== null && $result !== false) {
if (is_array($result) && count($result) > 0
&& $result[0]->getLevel() != "Error_clear" && $result[0]->getLevel() != "Error_incomplete") {


$this->logger->info('Check needed for license ' . $result[0]->getId());
try {
$this->licensemanager->renewLicense($result[0]);
$result = $this->service->findByGroup($result[0]->getNcgroup());
if (isset($result) && $result !== null && $result !== false) {
if (is_array($result) && count($result) > 0
&& $result[0]->getLevel() != "Error_clear" && $result[0]->getLevel() != "Error_incomplete") {
} else {
//throw new Exception();
}
}
} catch (Exception $e) {
$this->logger->error('Error while renewing license ' . $result[0]->getId() . '\r\n' . $e);
}

// Reports license status
$email = $result[0]->getEmail();
$licensekey = $result[0]->getLicensekey();
$this->logger->info('License key returned is: ' . $licensekey);
$dateExpiration = $result[0]->getDatelicenseend();
$dateLastCheck = $result[0]->getDatelastchecked();
$level = $result[0]->getLevel();
$group = $result[0]->getNcgroup();
$statusKind = "";
$status = "";

if ($result[0]->isCleared()) {
$status = $this->l->t("No license configured");
$statusKind = "nolicense";
}
elseif ($result[0]->isIncomplete()) {
$status = $this->l->t("Missing email address or license key.");
$statusKind = "error_incomplete";
}
elseif ($result[0]->isCheckNeeded()) {
$status = $this->l->t("Revalidation of your license is required");
$statusKind = "check";
}
elseif ($result[0]->isLicenseRenewedOrSwitched()) {
$status = $this->l->t("Current license has been changed.") .
"</br>" .
$this->l->t('%1$sContact support%2$s if you experience issues with configuring your license key.', ["<a href='mailto:[email protected]' style='color:blue'>", "</a>"]);
$statusKind = "check";
}
elseif ($result[0]->isLicenseInactive()) {
$status = $this->l->t("Current license has been deactivated by Sendent.") .
"</br>" .
$this->l->t('%1$sContact support%2$s for more information.', ["<a href='mailto:[email protected]' style='color:blue'>", "</a>"]);
$statusKind = "check";
}
elseif ($result[0]->isLicenseSuspended()) {
$status = $this->l->t("Current license has been suspended by Sendent.") .
"</br>" .
$this->l->t('%1$sContact support%2$s for more information.', ["<a href='mailto:[email protected]' style='color:blue'>", "</a>"]);
$statusKind = "check";
}
elseif ($result[0]->isLicenseExpired()) {
$status = $this->l->t("Current license has expired.") .
"</br>" .
$this->l->t('%1$sContact support%2$s if you experience issues with configuring your license key.', ["<a href='mailto:[email protected]' style='color:blue'>", "</a>"]);
$statusKind = "expired";
}
elseif (!$result[0]->isCheckNeeded() && !$result[0]->isLicenseExpired()) {
$status = $this->l->t("Current license is valid");
$statusKind = "valid";
}
elseif (!$this->licensemanager->isWithinUserCount($result[0]) && $this->licensemanager->isWithinGraceUserCount($result[0])) {
$status = $this->l->t("Current amount of active users exceeds licensed amount. Some users might not be able to use Sendent.");
$statusKind = "userlimit";
}
elseif (!$this->licensemanager->isWithinUserCount($result[0]) && !$this->licensemanager->isWithinGraceUserCount($result[0])) {
$status = $this->l->t("Current amount of active users exceeds licensed amount. Additional users trying to use Sendent will be prevented from doing so.");
$statusKind = "userlimit";
}
return new DataResponse(new LicenseStatus($status, $statusKind, $level,$licensekey, $dateExpiration, $dateLastCheck, $email, $group));
}
elseif (count($result) > 0 && $result[0]->getLevel() == "Error_incomplete") {
$email = $result[0]->getEmail();
$licensekey = $result[0]->getLicensekey();
$group = $result[0]->getNcgroup();
$status = $this->l->t('Missing (or incorrect) email address or license key. %1$sContact support%2$s to get your correct license information.', ["<a href='mailto:[email protected]' style='color:blue'>", "</a>"]);
return new DataResponse(new LicenseStatus($status, "error_incomplete" ,"-", $licensekey, "-", "-", $email, $group));
}
elseif (count($result) > 0 && $result[0]->getLevel() == License::ERROR_VALIDATING) {
$email = $result[0]->getEmail();
$licensekey = $result[0]->getLicensekey();
$group = $result[0]->getNcgroup();
return new DataResponse(new LicenseStatus($this->l->t("Cannot verify your license. Please make sure your licensekey and email address are correct before you try to 'Activate license'."), "error_validating","-", $licensekey, "-", "-", $email, $group));
}
else {
return new DataResponse(new LicenseStatus($this->l->t("No license configured"), "nolicense" ,"-", "-", "-", "-", "-"));
}
}
else {
return new DataResponse(new LicenseStatus($this->l->t("No license configured"), "nolicense" ,"-", "-", "-", "-", "-"));
}
} catch (Exception $e) {
$this->logger->error('Cannot verify license');
return new DataResponse(new LicenseStatus($this->l->t("Cannot verify your license. Please make sure your licensekey and email address are correct before you try to 'Activate license'."), "fatal" ,"-", "-", "-", "-", "-"));
}
}
/**
* @param string $license
* @param string $email
* @param string $ncgroup
*/
public function create(string $license, string $email, string $ncgroup) {
return $this->licensemanager->createLicense($license, $email, $ncgroup);
return $this->licensemanager->createLicense($license, '','', $email, $ncgroup);
}

/**
Expand Down
20 changes: 16 additions & 4 deletions lib/Db/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class License extends Entity implements JsonSerializable {
public const ERROR_VALIDATING = 'Error_validating';

protected $licensekey;
protected $licensekeytoken;
protected $email;
protected $dategraceperiodend;
protected $datelicenseend;
Expand All @@ -20,15 +21,17 @@ class License extends Entity implements JsonSerializable {
protected $datelastchecked;
protected $level;
protected $ncgroup;

protected $subscriptionstatus;
public function __construct() {
// add types in constructor
}
public function jsonSerialize() {
return [
'id' => $this->id,
'licensekey' => $this->licensekey,
'licensekeytoken' => $this->licensekeytoken,
'email' => $this->email,
'subscriptionstatus' => $this->subscriptionstatus,
'dategraceperiodend' => $this->dategraceperiodend,
'maxusers' => $this->maxusers,
'maxgraceusers' => $this->maxgraceusers,
Expand All @@ -43,7 +46,6 @@ public function isCheckNeeded(): bool {
$diffDay = new DateInterval('P7D');
if (date_create($this->datelastchecked) >= date_sub(date_create("now"), $diffDay) && $this->level != License::ERROR_VALIDATING) {
error_log(print_r("LICENSE-ISCHECKNEEDED: FALSE", true));

return false;
}
error_log(print_r("LICENSE-ISCHECKNEEDED: TRUE", true));
Expand All @@ -63,10 +65,20 @@ public function isCleared(): bool {
return false;
}
public function isLicenseExpired(): bool {
if (date_create($this->datelicenseend) < date_create("now")
&& date_create($this->dategraceperiodend) < date_create("now")) {
if ((date_create($this->datelicenseend) < date_create("now")
&& date_create($this->dategraceperiodend) < date_create("now"))
|| ($this->subscriptionstatus == "2" || $this->subscriptionstatus == "4" || $this->subscriptionstatus == "5" || $this->subscriptionstatus == "6" || $this->subscriptionstatus == "7" )) {
return true;
}
return false;
}
public function isLicenseSuspended(): bool {
return $this->subscriptionstatus == "5";
}
public function isLicenseInactive(): bool {
return $this->subscriptionstatus == "4";
}
public function isLicenseRenewedOrSwitched(): bool {
return $this->subscriptionstatus == "6" || $this->subscriptionstatus == "7";
}
}
8 changes: 4 additions & 4 deletions lib/Http/LicenseHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class LicenseHttpClient {
/** @var string */
protected $baseUrl;

public function __construct(IClientService $clientService, LoggerInterface $logger, string $baseUrl = "https://api.scwcloud.sendent.nl/") {
// public function __construct(IClientService $clientService, LoggerInterface $logger, string $baseUrl = "http://localhost:8085/") {
//public function __construct(IClientService $clientService, LoggerInterface $logger, string $baseUrl = "https://api.scwcloud.sendent.nl/") {
public function __construct(IClientService $clientService, LoggerInterface $logger, string $baseUrl = "http://127.0.0.1:8085/") {
$this->client = $clientService->newClient();
$this->logger = $logger;
$this->baseUrl = $baseUrl;
Expand All @@ -42,8 +42,8 @@ public function post(string $request, Dto\SubscriptionIn $data) {
try {
$response = $this->client->post($uri, [
'json' => $data->jsonSerialize(),
'header' => [
'api-version' => '1',
'headers' => [
'api-version' => '2.0',
],
]);
} catch (BadResponseException $e) {
Expand Down
Loading

0 comments on commit dba2943

Please sign in to comment.