Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #8 from owncloud/feat/license-info
Browse files Browse the repository at this point in the history
feat: admin panel with license information
  • Loading branch information
DeepDiver1975 authored Mar 24, 2021
2 parents 8a38fdd + ff47c0f commit 159f896
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 6 deletions.
3 changes: 3 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
<license/>
<prevent_group_restriction/>
</types>
<settings>
<admin>OCA\QNAP\AdminPanel</admin>
</settings>
<commands>
<command>OCA\QNAP\Command\CheckActiveUsers</command>
</commands>
Expand Down
40 changes: 40 additions & 0 deletions lib/AdminPanel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace OCA\QNAP;

use OCP\IUser;
use OCP\Settings\ISettings;
use OCP\Template;

class AdminPanel implements ISettings {
public function getPriority() {
return 10;
}

public function getPanel() {
$license = new QnapLicense();
$tmpl = new Template('qnap', 'settings-admin');
$tmpl->assign('licenses', $license->getLicenses());
$tmpl->assign('licensed_users', $license->getUserAllowance());
$tmpl->assign('active_users', $this->getUserCount());
# foreach ($params as $key => $value) {
# $tmpl->assign($key, $value);
# }
return $tmpl;
}

public function getSectionID() {
return 'security';
}

private function getUserCount(): int {
$numberOfActiveUsers = 0;
\OC::$server->getUserManager()->callForAllUsers(function (IUser $user) use (&$numberOfActiveUsers) {
if ($user->isEnabled()) {
$numberOfActiveUsers++;
}
});

return $numberOfActiveUsers;
}
}
18 changes: 12 additions & 6 deletions lib/LicenseParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,20 @@ public function loadLicensesText(string $licenseText): void {
$license['valid_from'] = $validFrom;
$license['valid_until'] = $validUntil;

$r['license_info'] = $license;

# future license
if ($validFrom->getTimestamp() > $now) {
$this->futureLicenses[]= $license;
$this->futureLicenses[]= $r;
continue;
}
# expired licenses
if ($this->timeFactory->getTime() > $validUntil->getTimestamp()) {
$this->expiredLicenses[]= $license;
$this->expiredLicenses[]= $r;
continue;
}
# active licenses
$this->activeLicenses[]= $license;
$this->activeLicenses[]= $r;
}
}

Expand All @@ -79,7 +81,7 @@ public function getExpirationTime(): int {
if (!empty($this->activeLicenses)) {
$expirations = \array_map(function ($l) {
/** @var \DateTime $validUntil */
$validUntil = $l['valid_until'];
$validUntil = $l['license_info']['valid_until'];
return $validUntil->getTimestamp();
}, $this->activeLicenses);
return \min($expirations);
Expand All @@ -88,7 +90,7 @@ public function getExpirationTime(): int {
if (!empty($this->expiredLicenses)) {
$expirations = \array_map(function ($l) {
/** @var \DateTime $validUntil */
$validUntil = $l['valid_until'];
$validUntil = $l['license_info']['valid_until'];
return $validUntil->getTimestamp();
}, $this->expiredLicenses);
return \min($expirations);
Expand All @@ -107,6 +109,10 @@ public function getUserAllowance(): int {
}

private function getLicenseUserAllowance($license): int {
return (int)($license['attributes']['owncloud_account'] ?? 0);
return (int)($license['license_info']['attributes']['owncloud_account'] ?? 0);
}

public function getLicenses(): array {
return \array_merge($this->activeLicenses, $this->expiredLicenses, $this->futureLicenses);
}
}
4 changes: 4 additions & 0 deletions lib/QnapLicense.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ public function getType(): int {
}
return self::LICENSE_TYPE_DEMO;
}

public function getLicenses(): array {
return $this->licenseParser->getLicenses();
}
}
34 changes: 34 additions & 0 deletions templates/settings-admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/** @var array $_ */
/** @var \OCP\IL10N $l */
?>
<div id="qnap" class="section">
<h2 class="inlineblock"><?php p($l->t('QNAP License')); ?></h2>
<p>
<?php p($l->t('Active users: %d', $_['active_users']))?>
<br>
<?php p($l->t('Licensed users: %d', $_['licensed_users']))?>
<br>
<a href="https://software.qnap.com/owncloud.html"><?php p($l->t('Visit the QNAP Store to purchase more licenses.')) ?></a>
</p>

<h3><?php p($l->t('License Overview')); ?></h3>
<table>
<thead>
<tr>
<th scope="col"><?php p($l->t('License ID')); ?></th>
<th scope="col"><?php p($l->t('Valid until')); ?></th>
<th scope="col"><?php p($l->t('Number of users')); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($_['licenses'] as $lic): ?>
<tr>
<td><?php p($lic['license_id']); ?></td>
<td><?php p($lic['license_info']['valid_until']->format(\DateTime::COOKIE)); ?></td>
<td><?php p($lic['license_info']['attributes']['owncloud_account']); ?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</div>

0 comments on commit 159f896

Please sign in to comment.