Skip to content

Commit

Permalink
feature(separate-teams-license-NOTNEEDED-but-good-for-future-reference)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonvandebroek committed Dec 9, 2023
1 parent 8bc98d2 commit 6704130
Show file tree
Hide file tree
Showing 14 changed files with 746 additions and 73 deletions.
70 changes: 46 additions & 24 deletions lib/Controller/LicenseApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ private function handleException($e) {
* @NoCSRFRequired
*
* Returns license status for current user
* @param string $type
*
* @return DataResponse
*/
public function show(): DataResponse {
public function show(string $type = 'outlook'): DataResponse {

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

Expand All @@ -100,22 +101,23 @@ public function show(): DataResponse {
// 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]]);
return $this->showForNCGroup($userSendentGroups[array_keys($userSendentGroups)[0]], $type);
} else {
$this->logger->info('User ' . $this->userId . ' is not member of any sendent group');
return $this->showForNCGroup('');
return $this->showForNCGroup('', $type);
}

}
/**
* @NoAdminRequired
* @NoCSRFRequired
* @param string $type
*
* Returns license status for current user
*
* @return DataResponse
*/
public function showInternal(): DataResponse {
public function showInternal(string $type = 'outlook'): DataResponse {

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

Expand All @@ -137,10 +139,10 @@ public function showInternal(): DataResponse {
// 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]]);
return $this->showForNCGroup($userSendentGroups[array_keys($userSendentGroups)[0]], $type);
} else {
$this->logger->info('User ' . $this->userId . ' is not member of any sendent group');
return $this->showForNCGroup('');
return $this->showForNCGroup('', $type);
}

}
Expand All @@ -151,9 +153,10 @@ public function showInternal(): DataResponse {
* Returns license status for group $ncgroup
*
* @param string $ncgroup
* @param string $type
* @return DataResponse
*/
public function showForNCGroup(string $ncgroup = ''): DataResponse {
public function showForNCGroup(string $ncgroup = '', string $type = 'outlook'): DataResponse {

if ($ncgroup === "") {
$this->logger->info('Getting license information for default group');
Expand All @@ -163,10 +166,10 @@ public function showForNCGroup(string $ncgroup = ''): DataResponse {

try {
// Gets license for group $ncgroup
$result = $this->service->findByGroup($ncgroup);
$result = $this->service->findByGroup($ncgroup, $type);
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('');
$result = $this->service->findByGroup('', $type);
}

if (isset($result) && $result !== null && $result !== false) {
Expand All @@ -177,7 +180,7 @@ public function showForNCGroup(string $ncgroup = ''): DataResponse {
$this->logger->info('Check needed for license ' . $result[0]->getId());
try {
$this->licensemanager->renewLicense($result[0]);
$result = $this->service->findByGroup($result[0]->getNcgroup());
$result = $this->service->findByGroup($result[0]->getNcgroup(), $type);
if (isset($result) && $result !== null && $result !== false) {
if (is_array($result) && count($result) > 0
&& $result[0]->getLevel() != "Error_clear" && $result[0]->getLevel() != "Error_incomplete") {
Expand All @@ -197,6 +200,7 @@ public function showForNCGroup(string $ncgroup = ''): DataResponse {
$dateLastCheck = $result[0]->getDatelastchecked();
$level = $result[0]->getLevel();
$group = $result[0]->getNcgroup();
$product = $result[0]->getProduct();
$statusKind = "";
$status = "";

Expand All @@ -212,6 +216,12 @@ public function showForNCGroup(string $ncgroup = ''): DataResponse {
$status = $this->l->t("Revalidation of your license is required");
$statusKind = "check";
}
elseif (!$result[0]->isCheckNeeded() && !$result[0]->isLicenseExpired() && !$result[0]->isSupportedProduct()) {
$status = $this->l->t("Current license is not intended to be used with Sendent Synchroniser. It is only intended to be used for configuring: " . $product) .
"</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]->isLicenseRenewedOrSwitched()) {
$status = $this->l->t("Current license has been changed.") .
"</br>" .
Expand Down Expand Up @@ -282,9 +292,10 @@ public function showForNCGroup(string $ncgroup = ''): DataResponse {
* Returns license status for group $ncgroup
*
* @param string $ncgroup
* @param string $type
* @return DataResponse
*/
public function showForNCGroupInternal(string $ncgroup = ''): DataResponse {
public function showForNCGroupInternal(string $ncgroup = '', string $type = 'outlook'): DataResponse {

if ($ncgroup === "") {
$this->logger->info('Getting license information for default group');
Expand All @@ -294,10 +305,10 @@ public function showForNCGroupInternal(string $ncgroup = ''): DataResponse {

try {
// Gets license for group $ncgroup
$result = $this->service->findByGroup($ncgroup);
$result = $this->service->findByGroup($ncgroup, $type);
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('');
$result = $this->service->findByGroup('', $type);
}

if (isset($result) && $result !== null && $result !== false) {
Expand All @@ -308,7 +319,7 @@ public function showForNCGroupInternal(string $ncgroup = ''): DataResponse {
$this->logger->info('Check needed for license ' . $result[0]->getId());
try {
$this->licensemanager->renewLicense($result[0]);
$result = $this->service->findByGroup($result[0]->getNcgroup());
$result = $this->service->findByGroup($result[0]->getNcgroup(), $type);
if (isset($result) && $result !== null && $result !== false) {
if (is_array($result) && count($result) > 0
&& $result[0]->getLevel() != "Error_clear" && $result[0]->getLevel() != "Error_incomplete") {
Expand All @@ -328,6 +339,7 @@ public function showForNCGroupInternal(string $ncgroup = ''): DataResponse {
$dateLastCheck = $result[0]->getDatelastchecked();
$level = $result[0]->getLevel();
$group = $result[0]->getNcgroup();
$product = $result[0]->getProduct();
$statusKind = "";
$status = "";

Expand All @@ -343,6 +355,12 @@ public function showForNCGroupInternal(string $ncgroup = ''): DataResponse {
$status = $this->l->t("Revalidation of your license is required");
$statusKind = "check";
}
elseif (!$result[0]->isCheckNeeded() && !$result[0]->isLicenseExpired() && !$result[0]->isSupportedProduct()) {
$status = $this->l->t("Current license is not intended to be used with Sendent Synchroniser. It is only intended to be used for configuring: " . $product) .
"</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]->isLicenseRenewedOrSwitched()) {
$status = $this->l->t("Current license has been changed.") .
"</br>" .
Expand Down Expand Up @@ -421,37 +439,40 @@ public function showForNCGroupInternal(string $ncgroup = ''): DataResponse {
* @param string $license
* @param string $email
* @param string $ncgroup
* @param string $type
*/
public function create(string $license, string $email, string $ncgroup) {
return $this->licensemanager->createLicense($license, '','', $email, $ncgroup);
public function create(string $license, string $email, string $ncgroup, string $type = 'outlook') {
return $this->licensemanager->createLicense($license, '','', $email, $ncgroup, $type);
}

/**
* @param string $group
* @param string $type
*/
public function delete(string $group) {
public function delete(string $group, string $type = 'outlook') {
// Deletes requested settinglicense
return $this->licensemanager->deleteLicense($group);
return $this->licensemanager->deleteLicense($group, $type);
}

/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function renew() {
public function renew(string $type = 'outlook') {
// Finds out user's license
$license = $this->service->findUserLicense($this->userId);
$license = $this->service->findUserLicense($this->userId, $type);

$this->licensemanager->renewLicense($license);
}

/**
* @NoAdminRequired
* @NoCSRFRequired
* @param string $type
*/
public function validate() {
public function validate(string $type = 'outlook') {
// Finds out user's license
$license = $this->service->findUserLicense($this->userId);
$license = $this->service->findUserLicense($this->userId, $type);

// Unlicensed?
if (is_null($license)) {
Expand All @@ -465,9 +486,10 @@ public function validate() {
/**
*
* Generates a report of all licenses used
* @param string $type
*
*/
public function report() {
public function report(string $type = 'outlook') {

// Gets groups for which specific settings and/or license are defined and add it the default one
$sendentGroups = $this->appConfig->getAppValue('sendentGroups', '');
Expand All @@ -477,7 +499,7 @@ public function report() {
// Gets license of each groups, handling inheritance
$licenses = [];
foreach($sendentGroups as $gid) {
$license = $this->service->findByGroup($gid);
$license = $this->service->findByGroup($gid, $type);
if (!empty($license)) {
$license = $license[0]->jsonSerialize();
$license += ['inherited' => false];
Expand Down
23 changes: 22 additions & 1 deletion lib/Db/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class License extends Entity implements JsonSerializable {
protected $level;
protected $ncgroup;
protected $subscriptionstatus;
protected $type;
protected $technicallevel;
protected $product;
protected $istrial;

public function __construct() {
// add types in constructor
}
Expand All @@ -38,7 +43,11 @@ public function jsonSerialize() {
'level' => $this->level,
'datelicenseend' => $this->datelicenseend,
'datelastchecked' => $this->datelastchecked,
'ncgroup' => $this->ncgroup
'ncgroup' => $this->ncgroup,
'type' => $this->type,
'technicallevel' => $this->technicallevel,
'product' => $this->product,
'istrial' => $this->istrial
];
}

Expand All @@ -64,6 +73,7 @@ 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"))
Expand All @@ -72,12 +82,23 @@ public function isLicenseExpired(): bool {
}
return false;
}
public function isTrial() : bool{
return $this->istrial == 1;
}
public function isSupportedProduct() : bool{
return str_contains($this->product, 'Outlook') || str_contains($this->product, 'outlook')
|| str_contains($this->product, '365') || str_contains($this->product, '365')
|| str_contains($this->product, 'vsto') || str_contains($this->product, 'vsto')
|| str_contains($this->product, 'teams') || str_contains($this->product, 'Teams');
}
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";
}
Expand Down
9 changes: 6 additions & 3 deletions lib/Db/LicenseMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,14 @@ public function findByLicenseKey(string $key): \OCP\AppFramework\Db\Entity {
*
* @psalm-return array<\OCP\AppFramework\Db\Entity>
*/
public function findAll($limit = null, $offset = null): array {
public function findAll(string $type = 'outlook', $limit = null, $offset = null): array {
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from('sndnt_license')
->where(
$qb->expr()->eq('type', $qb->createNamedParameter($type, IQueryBuilder::PARAM_STR))
)
->setMaxResults($limit)
->setFirstResult($offset);

Expand All @@ -64,13 +67,13 @@ public function findAll($limit = null, $offset = null): array {
*
* @psalm-return array<\OCP\AppFramework\Db\Entity>
*/
public function findByGroup(string $gid = '', $limit = null, $offset = null): array {
public function findByGroup(string $gid = '', string $type = 'outlook', $limit = null, $offset = null): array {
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from('sndnt_license')
->where(
$qb->expr()->eq('ncgroup', $qb->createNamedParameter($gid, IQueryBuilder::PARAM_STR))
$qb->expr()->eq('ncgroup', $qb->createNamedParameter($gid, IQueryBuilder::PARAM_STR)->eq('type', $qb->createNamedParameter($type, IQueryBuilder::PARAM_STR)))
)
->setMaxResults($limit)
->setFirstResult($offset);
Expand Down
4 changes: 2 additions & 2 deletions lib/Http/LicenseHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class LicenseHttpClient {
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 = "https://api.scwcloud.sendent.dev/") {
//public function __construct(IClientService $clientService, LoggerInterface $logger, string $baseUrl = "http://127.0.0.1:8085/") {
//public function __construct(IClientService $clientService, LoggerInterface $logger, string $baseUrl = "https://api.scwcloud.sendent.dev/") {
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 Down
Loading

0 comments on commit 6704130

Please sign in to comment.