Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
80.95% |
34 / 42 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
UtilsController | |
80.95% |
34 / 42 |
|
80.00% |
4 / 5 |
8.44 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
setAllowAnonymousCreation | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
deleteOptionsValues | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
saveOptionValue | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
getOptionsValues | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Nextcloud - cospend |
4 | * |
5 | * This file is licensed under the Affero General Public License version 3 or |
6 | * later. See the COPYING file. |
7 | * |
8 | * @author Julien Veyssier <eneiluj@posteo.net> |
9 | * @copyright Julien Veyssier 2019 |
10 | */ |
11 | |
12 | namespace OCA\Cospend\Controller; |
13 | |
14 | use OCP\IConfig; |
15 | use OCP\AppFramework\Http\ContentSecurityPolicy; |
16 | use OCP\IRequest; |
17 | use OCP\AppFramework\Http\DataResponse; |
18 | use OCP\AppFramework\Controller; |
19 | |
20 | class UtilsController extends Controller { |
21 | |
22 | /** |
23 | * @var IConfig |
24 | */ |
25 | private $config; |
26 | /** |
27 | * @var string|null |
28 | */ |
29 | private $userId; |
30 | |
31 | public function __construct(string $appName, |
32 | IRequest $request, |
33 | IConfig $config, |
34 | ?string $userId) { |
35 | parent::__construct($appName, $request); |
36 | $this->config = $config; |
37 | $this->userId = $userId; |
38 | } |
39 | |
40 | /** |
41 | * set global point quota |
42 | */ |
43 | public function setAllowAnonymousCreation($allow): DataResponse { |
44 | $this->config->setAppValue('cospend', 'allowAnonymousCreation', $allow); |
45 | $response = new DataResponse(['done' => '1']); |
46 | $csp = new ContentSecurityPolicy(); |
47 | $csp->addAllowedImageDomain('*') |
48 | ->addAllowedMediaDomain('*') |
49 | ->addAllowedConnectDomain('*'); |
50 | $response->setContentSecurityPolicy($csp); |
51 | return $response; |
52 | } |
53 | |
54 | /** |
55 | * Delete user options |
56 | * @NoAdminRequired |
57 | */ |
58 | public function deleteOptionsValues(): DataResponse { |
59 | $keys = $this->config->getUserKeys($this->userId, 'cospend'); |
60 | foreach ($keys as $key) { |
61 | $this->config->deleteUserValue($this->userId, 'cospend', $key); |
62 | } |
63 | |
64 | $response = new DataResponse(['done' => 1]); |
65 | $csp = new ContentSecurityPolicy(); |
66 | $csp->addAllowedImageDomain('*') |
67 | ->addAllowedMediaDomain('*') |
68 | ->addAllowedConnectDomain('*'); |
69 | $response->setContentSecurityPolicy($csp); |
70 | return $response; |
71 | } |
72 | |
73 | /** |
74 | * Save options values to the DB for current user |
75 | * @NoAdminRequired |
76 | */ |
77 | public function saveOptionValue($options): DataResponse { |
78 | foreach ($options as $key => $value) { |
79 | $this->config->setUserValue($this->userId, 'cospend', $key, $value); |
80 | } |
81 | |
82 | $response = new DataResponse(['done' => true]); |
83 | $csp = new ContentSecurityPolicy(); |
84 | $csp->addAllowedImageDomain('*') |
85 | ->addAllowedMediaDomain('*') |
86 | ->addAllowedConnectDomain('*'); |
87 | $response->setContentSecurityPolicy($csp); |
88 | return $response; |
89 | } |
90 | |
91 | /** |
92 | * get options values from the config for current user |
93 | * @NoAdminRequired |
94 | */ |
95 | public function getOptionsValues(): DataResponse { |
96 | $ov = array(); |
97 | $keys = $this->config->getUserKeys($this->userId, 'cospend'); |
98 | foreach ($keys as $key) { |
99 | $value = $this->config->getUserValue($this->userId, 'cospend', $key); |
100 | $ov[$key] = $value; |
101 | } |
102 | |
103 | $response = new DataResponse(['values' => $ov]); |
104 | $csp = new ContentSecurityPolicy(); |
105 | $csp->addAllowedImageDomain('*') |
106 | ->addAllowedMediaDomain('*') |
107 | ->addAllowedConnectDomain('*'); |
108 | $response->setContentSecurityPolicy($csp); |
109 | return $response; |
110 | } |
111 | } |