Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Manager.php « Push « private « lib - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 99410d43e0e5a298b81a3c8dc0d818014bc20ba2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php

declare(strict_types=1);
/**
 * @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl>
 *
 * @author Roeland Jago Douma <roeland@famdouma.nl>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace OC\Push;

use OCP\AppFramework\QueryException;
use OCP\ILogger;
use OCP\IServerContainer;
use OCP\Push\IManager;
use OCP\Push\IPushApp;
use OCP\Push\IValidateAccess;

class Manager implements IPushApp {

	/** @var array */
	private $validatorClasses = [];

	/** @var string */
	private $pushClass;

	/** @var IPushApp */
	private $push;

	/** @var IServerContainer */
	private $container;
	/** @var ILogger */
	private $logger;

	public function __construct(IServerContainer $container, ILogger $logger) {
		$this->container = $container;
		$this->logger = $logger;
	}

	public function registerAccessValidator(string $appId, string $service): void {
		$this->logger->debug('Adding access validator "' . $service . '" for app: '. $appId, ['app' => 'internal_push']);
		$this->validatorClasses[$appId] = $service;
	}

	public function registerPushApp(string $service): void {
		$this->logger->debug('Registering push app "' . $service, ['app' => 'internal_push']);
		$this->pushClass = $service;
	}

	public function isAvailable(): bool {
		return $this->getPushApp()->isAvailable();
	}

	public function push(string $appId, string $topic, \JsonSerializable $payload): void {
		$this->getPushApp()->push($appId, $topic, $payload);
	}

	public function generateJWT(string $appId, string $topic): string {
		return $this->getPushApp()->generateJWT($appId, $topic);
	}

	public function validateAccess(string $userId, string $appId, string $topic): bool {
		if (!isset($this->validatorClasses[$appId])) {
			$this->logger->debug('No validator found for ' . $appId, ['app' => 'internal_push']);
			return false;
		}

		try {
			$validator = $this->container->query($this->validatorClasses[$appId]);
		} catch (QueryException $e) {
			$this->logger->debug('Could not query ' . $appId, ['app' => 'internal_push']);
			return false;
		}

		if (!($validator instanceof IValidateAccess)) {
			$this->logger->debug($this->validatorClasses[$appId] . ' is not an instance of ' . IValidateAccess::class, ['app' => 'internal_push']);
			return false;
		}

		try {
			return $validator->hasAccess($topic, $userId);
		} catch (\Throwable $e) {
			$this->logger->logException($e, ['app' => 'internal_push']);
			return false;
		}
	}

	public function getEndpoint(string $appId, string $topic): string {
		return $this->getPushApp()->getEndpoint($appId, $topic);
	}


	private function getPushApp(): IPushApp {
		if ($this->push !== null) {
			return $this->push;
		}

		$push = new VoidPushApp();
		if ($this->pushClass !== null) {
			try {
				$query = $this->container->query($this->pushClass);
				if ($query instanceof IPushApp) {
					$push = $query;
				} else {
					$this->logger->debug($this->pushClass . ' is not and instance of ' . IPushApp::class, ['app' => 'internal_push']);
				}
			} catch (QueryException $e) {
				$this->logger->debug('Could not query ' . $this->pushClass, ['app' => 'internal_push']);
			}
		}

		$this->push = $push;
		return $push;
	}


}