Skip to content

Commit 2484bde

Browse files
authored
Merge pull request #160 from pdsinterop/feature/admin-panel
Feature/admin panel
2 parents 4e49e20 + 52fdd1d commit 2484bde

7 files changed

Lines changed: 164 additions & 90 deletions

File tree

solid/appinfo/info.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ When you do this, the Solid App can store data in your Nextcloud account through
2121
<nextcloud min-version="28" max-version="30"/>
2222
</dependencies>
2323
<settings>
24-
<admin>\OCA\Solid\Settings</admin>
24+
<admin>OCA\Solid\Settings\SolidAdmin</admin>
25+
<admin-section>OCA\Solid\Sections\SolidAdmin</admin-section>
2526
</settings>
2627
<navigations>
2728
<navigation>

solid/img/app-dark.svg

Lines changed: 5 additions & 0 deletions
Loading

solid/lib/BaseServerConfig.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
namespace OCA\Solid;
3+
4+
use OCP\IConfig;
5+
6+
class BaseServerConfig {
7+
private IConfig $config;
8+
9+
/**
10+
* @param IConfig $config
11+
*/
12+
public function __construct(IConfig $config) {
13+
$this->config = $config;
14+
}
15+
16+
/**
17+
* @return string
18+
*/
19+
public function getPrivateKey() {
20+
$result = $this->config->getAppValue('solid','privateKey');
21+
if (!$result) {
22+
// generate and save a new set if we don't have a private key;
23+
$keys = $this->generateKeySet();
24+
$this->config->setAppValue('solid','privateKey',$keys['privateKey']);
25+
$this->config->setAppValue('solid','encryptionKey',$keys['encryptionKey']);
26+
}
27+
return $this->config->getAppValue('solid','privateKey');
28+
}
29+
30+
/**
31+
* @param string $privateKey
32+
*/
33+
public function setPrivateKey($privateKey) {
34+
$this->config->setAppValue('solid','privateKey',$privateKey);
35+
}
36+
37+
/**
38+
* @return string
39+
*/
40+
public function getEncryptionKey() {
41+
return $this->config->getAppValue('solid','encryptionKey');
42+
}
43+
44+
/**
45+
* @param string $publicKey
46+
*/
47+
public function setEncryptionKey($publicKey) {
48+
$this->config->setAppValue('solid','encryptionKey',$publicKey);
49+
}
50+
51+
private function generateKeySet() {
52+
$config = array(
53+
"digest_alg" => "sha256",
54+
"private_key_bits" => 2048,
55+
"private_key_type" => OPENSSL_KEYTYPE_RSA,
56+
);
57+
// Create the private and public key
58+
$key = openssl_pkey_new($config);
59+
60+
// Extract the private key from $key to $privateKey
61+
openssl_pkey_export($key, $privateKey);
62+
$encryptionKey = base64_encode(random_bytes(32));
63+
$result = array(
64+
"privateKey" => $privateKey,
65+
"encryptionKey" => $encryptionKey
66+
);
67+
return $result;
68+
}
69+
}

solid/lib/Sections/SolidAdmin.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
namespace OCA\Solid\Sections;
3+
4+
use OCP\IL10N;
5+
use OCP\IURLGenerator;
6+
use OCP\Settings\IIconSection;
7+
8+
class SolidAdmin implements IIconSection {
9+
private IL10N $l;
10+
private IURLGenerator $urlGenerator;
11+
12+
public function __construct(IL10N $l, IURLGenerator $urlGenerator) {
13+
$this->l = $l;
14+
$this->urlGenerator = $urlGenerator;
15+
}
16+
17+
public function getIcon(): string {
18+
return $this->urlGenerator->imagePath('solid', 'app-dark.svg');
19+
}
20+
21+
public function getID(): string {
22+
return 'solid';
23+
}
24+
25+
public function getName(): string {
26+
return $this->l->t('Solid');
27+
}
28+
29+
public function getPriority(): int {
30+
return 98;
31+
}
32+
}

solid/lib/ServerConfig.php

Lines changed: 9 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,26 @@
44
use OCP\IConfig;
55
use OCP\IUserManager;
66
use OCP\IUrlGenerator;
7-
7+
use OCA\Solid\BaseServerConfig;
8+
89
/**
910
* @package OCA\Solid
1011
*/
11-
class ServerConfig {
12-
/** @var IConfig */
13-
private $config;
12+
class ServerConfig extends BaseServerConfig {
13+
private IConfig $config;
14+
private IUrlGenerator $urlGenerator;
15+
private IUserManager $userManager;
1416

1517
/**
1618
* @param IConfig $config
19+
* @param IUrlGenerator $urlGenerator
20+
* @param IUserManager $userManager
1721
*/
1822
public function __construct(IConfig $config, IUrlGenerator $urlGenerator, IUserManager $userManager) {
1923
$this->config = $config;
2024
$this->userManager = $userManager;
2125
$this->urlGenerator = $urlGenerator;
22-
}
23-
24-
/**
25-
* @return string
26-
*/
27-
public function getPrivateKey() {
28-
$result = $this->config->getAppValue('solid','privateKey');
29-
if (!$result) {
30-
// generate and save a new set if we don't have a private key;
31-
$keys = $this->generateKeySet();
32-
$this->config->setAppValue('solid','privateKey',$keys['privateKey']);
33-
$this->config->setAppValue('solid','encryptionKey',$keys['encryptionKey']);
34-
}
35-
return $this->config->getAppValue('solid','privateKey');
36-
}
37-
38-
/**
39-
* @param string $privateKey
40-
*/
41-
public function setPrivateKey($privateKey) {
42-
$this->config->setAppValue('solid','privateKey',$privateKey);
43-
}
44-
45-
/**
46-
* @return string
47-
*/
48-
public function getEncryptionKey() {
49-
return $this->config->getAppValue('solid','encryptionKey');
50-
}
51-
52-
/**
53-
* @param string $publicKey
54-
*/
55-
public function setEncryptionKey($publicKey) {
56-
$this->config->setAppValue('solid','encryptionKey',$publicKey);
26+
parent::__construct($config);
5727
}
5828

5929
/**
@@ -194,22 +164,4 @@ public function setProfileData($userId, $profileData) {
194164
$user->setDisplayName($fields['name']);
195165
}
196166
}
197-
private function generateKeySet() {
198-
$config = array(
199-
"digest_alg" => "sha256",
200-
"private_key_bits" => 2048,
201-
"private_key_type" => OPENSSL_KEYTYPE_RSA,
202-
);
203-
// Create the private and public key
204-
$key = openssl_pkey_new($config);
205-
206-
// Extract the private key from $key to $privateKey
207-
openssl_pkey_export($key, $privateKey);
208-
$encryptionKey = base64_encode(random_bytes(32));
209-
$result = array(
210-
"privateKey" => $privateKey,
211-
"encryptionKey" => $encryptionKey
212-
);
213-
return $result;
214-
}
215167
}

solid/lib/Settings.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

solid/lib/Settings/SolidAdmin.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
namespace OCA\Solid\Settings;
3+
4+
use OCP\AppFramework\Http\TemplateResponse;
5+
use OCP\IConfig;
6+
use OCP\IL10N;
7+
use OCP\Settings\ISettings;
8+
use OCA\Solid\BaseServerConfig;
9+
10+
class SolidAdmin implements ISettings {
11+
private IL10N $l;
12+
private IConfig $config;
13+
private BaseServerConfig $serverConfig;
14+
15+
public function __construct(IConfig $config, IL10N $l) {
16+
$this->config = $config;
17+
$this->l = $l;
18+
$this->serverConfig = new BaseServerConfig($config);
19+
}
20+
21+
/**
22+
* @return TemplateResponse
23+
*/
24+
public function getForm() {
25+
$parameters = [
26+
'privateKey' => $this->serverConfig->getPrivateKey(),
27+
'encryptionKey' => $this->serverConfig->getEncryptionKey()
28+
];
29+
30+
return new TemplateResponse('solid', 'admin', $parameters, '');
31+
}
32+
33+
public function getSection() {
34+
return 'solid'; // Name of the previously created section.
35+
}
36+
37+
/**
38+
* @return int whether the form should be rather on the top or bottom of
39+
* the admin section. The forms are arranged in ascending order of the
40+
* priority values. It is required to return a value between 0 and 100.
41+
*
42+
* E.g.: 70
43+
*/
44+
public function getPriority() {
45+
return 70;
46+
}
47+
}

0 commit comments

Comments
 (0)