@@ -36,46 +36,46 @@ |
||
36 | 36 | * @package OC\Security\CSRF |
37 | 37 | */ |
38 | 38 | class CsrfToken { |
39 | - /** @var string */ |
|
40 | - private $value; |
|
41 | - /** @var string */ |
|
42 | - private $encryptedValue = ''; |
|
39 | + /** @var string */ |
|
40 | + private $value; |
|
41 | + /** @var string */ |
|
42 | + private $encryptedValue = ''; |
|
43 | 43 | |
44 | - /** |
|
45 | - * @param string $value Value of the token. Can be encrypted or not encrypted. |
|
46 | - */ |
|
47 | - public function __construct(string $value) { |
|
48 | - $this->value = $value; |
|
49 | - } |
|
44 | + /** |
|
45 | + * @param string $value Value of the token. Can be encrypted or not encrypted. |
|
46 | + */ |
|
47 | + public function __construct(string $value) { |
|
48 | + $this->value = $value; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Encrypted value of the token. This is used to mitigate BREACH alike |
|
53 | - * vulnerabilities. For display measures do use this functionality. |
|
54 | - * |
|
55 | - * @return string |
|
56 | - */ |
|
57 | - public function getEncryptedValue(): string { |
|
58 | - if ($this->encryptedValue === '') { |
|
59 | - $sharedSecret = random_bytes(\strlen($this->value)); |
|
60 | - $this->encryptedValue = base64_encode($this->value ^ $sharedSecret) . ':' . base64_encode($sharedSecret); |
|
61 | - } |
|
51 | + /** |
|
52 | + * Encrypted value of the token. This is used to mitigate BREACH alike |
|
53 | + * vulnerabilities. For display measures do use this functionality. |
|
54 | + * |
|
55 | + * @return string |
|
56 | + */ |
|
57 | + public function getEncryptedValue(): string { |
|
58 | + if ($this->encryptedValue === '') { |
|
59 | + $sharedSecret = random_bytes(\strlen($this->value)); |
|
60 | + $this->encryptedValue = base64_encode($this->value ^ $sharedSecret) . ':' . base64_encode($sharedSecret); |
|
61 | + } |
|
62 | 62 | |
63 | - return $this->encryptedValue; |
|
64 | - } |
|
63 | + return $this->encryptedValue; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * The unencrypted value of the token. Used for decrypting an already |
|
68 | - * encrypted token. |
|
69 | - * |
|
70 | - * @return string |
|
71 | - */ |
|
72 | - public function getDecryptedValue(): string { |
|
73 | - $token = explode(':', $this->value); |
|
74 | - if (\count($token) !== 2) { |
|
75 | - return ''; |
|
76 | - } |
|
77 | - $obfuscatedToken = $token[0]; |
|
78 | - $secret = $token[1]; |
|
79 | - return base64_decode($obfuscatedToken) ^ base64_decode($secret); |
|
80 | - } |
|
66 | + /** |
|
67 | + * The unencrypted value of the token. Used for decrypting an already |
|
68 | + * encrypted token. |
|
69 | + * |
|
70 | + * @return string |
|
71 | + */ |
|
72 | + public function getDecryptedValue(): string { |
|
73 | + $token = explode(':', $this->value); |
|
74 | + if (\count($token) !== 2) { |
|
75 | + return ''; |
|
76 | + } |
|
77 | + $obfuscatedToken = $token[0]; |
|
78 | + $secret = $token[1]; |
|
79 | + return base64_decode($obfuscatedToken) ^ base64_decode($secret); |
|
80 | + } |
|
81 | 81 | } |
@@ -35,59 +35,59 @@ |
||
35 | 35 | * @package OC\Security\CSRF\TokenStorage |
36 | 36 | */ |
37 | 37 | class SessionStorage { |
38 | - /** @var ISession */ |
|
39 | - private $session; |
|
38 | + /** @var ISession */ |
|
39 | + private $session; |
|
40 | 40 | |
41 | - /** |
|
42 | - * @param ISession $session |
|
43 | - */ |
|
44 | - public function __construct(ISession $session) { |
|
45 | - $this->session = $session; |
|
46 | - } |
|
41 | + /** |
|
42 | + * @param ISession $session |
|
43 | + */ |
|
44 | + public function __construct(ISession $session) { |
|
45 | + $this->session = $session; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * @param ISession $session |
|
50 | - */ |
|
51 | - public function setSession(ISession $session) { |
|
52 | - $this->session = $session; |
|
53 | - } |
|
48 | + /** |
|
49 | + * @param ISession $session |
|
50 | + */ |
|
51 | + public function setSession(ISession $session) { |
|
52 | + $this->session = $session; |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Returns the current token or throws an exception if none is found. |
|
57 | - * |
|
58 | - * @return string |
|
59 | - * @throws \Exception |
|
60 | - */ |
|
61 | - public function getToken(): string { |
|
62 | - $token = $this->session->get('requesttoken'); |
|
63 | - if (empty($token)) { |
|
64 | - throw new \Exception('Session does not contain a requesttoken'); |
|
65 | - } |
|
55 | + /** |
|
56 | + * Returns the current token or throws an exception if none is found. |
|
57 | + * |
|
58 | + * @return string |
|
59 | + * @throws \Exception |
|
60 | + */ |
|
61 | + public function getToken(): string { |
|
62 | + $token = $this->session->get('requesttoken'); |
|
63 | + if (empty($token)) { |
|
64 | + throw new \Exception('Session does not contain a requesttoken'); |
|
65 | + } |
|
66 | 66 | |
67 | - return $token; |
|
68 | - } |
|
67 | + return $token; |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * Set the valid current token to $value. |
|
72 | - * |
|
73 | - * @param string $value |
|
74 | - */ |
|
75 | - public function setToken(string $value) { |
|
76 | - $this->session->set('requesttoken', $value); |
|
77 | - } |
|
70 | + /** |
|
71 | + * Set the valid current token to $value. |
|
72 | + * |
|
73 | + * @param string $value |
|
74 | + */ |
|
75 | + public function setToken(string $value) { |
|
76 | + $this->session->set('requesttoken', $value); |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * Removes the current token. |
|
81 | - */ |
|
82 | - public function removeToken() { |
|
83 | - $this->session->remove('requesttoken'); |
|
84 | - } |
|
85 | - /** |
|
86 | - * Whether the storage has a storage. |
|
87 | - * |
|
88 | - * @return bool |
|
89 | - */ |
|
90 | - public function hasToken(): bool { |
|
91 | - return $this->session->exists('requesttoken'); |
|
92 | - } |
|
79 | + /** |
|
80 | + * Removes the current token. |
|
81 | + */ |
|
82 | + public function removeToken() { |
|
83 | + $this->session->remove('requesttoken'); |
|
84 | + } |
|
85 | + /** |
|
86 | + * Whether the storage has a storage. |
|
87 | + * |
|
88 | + * @return bool |
|
89 | + */ |
|
90 | + public function hasToken(): bool { |
|
91 | + return $this->session->exists('requesttoken'); |
|
92 | + } |
|
93 | 93 | } |
@@ -34,78 +34,78 @@ |
||
34 | 34 | * @package OC\Security\CSRF |
35 | 35 | */ |
36 | 36 | class CsrfTokenManager { |
37 | - /** @var CsrfTokenGenerator */ |
|
38 | - private $tokenGenerator; |
|
39 | - /** @var SessionStorage */ |
|
40 | - private $sessionStorage; |
|
41 | - /** @var CsrfToken|null */ |
|
42 | - private $csrfToken = null; |
|
37 | + /** @var CsrfTokenGenerator */ |
|
38 | + private $tokenGenerator; |
|
39 | + /** @var SessionStorage */ |
|
40 | + private $sessionStorage; |
|
41 | + /** @var CsrfToken|null */ |
|
42 | + private $csrfToken = null; |
|
43 | 43 | |
44 | - /** |
|
45 | - * @param CsrfTokenGenerator $tokenGenerator |
|
46 | - * @param SessionStorage $storageInterface |
|
47 | - */ |
|
48 | - public function __construct(CsrfTokenGenerator $tokenGenerator, |
|
49 | - SessionStorage $storageInterface) { |
|
50 | - $this->tokenGenerator = $tokenGenerator; |
|
51 | - $this->sessionStorage = $storageInterface; |
|
52 | - } |
|
44 | + /** |
|
45 | + * @param CsrfTokenGenerator $tokenGenerator |
|
46 | + * @param SessionStorage $storageInterface |
|
47 | + */ |
|
48 | + public function __construct(CsrfTokenGenerator $tokenGenerator, |
|
49 | + SessionStorage $storageInterface) { |
|
50 | + $this->tokenGenerator = $tokenGenerator; |
|
51 | + $this->sessionStorage = $storageInterface; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Returns the current CSRF token, if none set it will create a new one. |
|
56 | - * |
|
57 | - * @return CsrfToken |
|
58 | - */ |
|
59 | - public function getToken(): CsrfToken { |
|
60 | - if (!\is_null($this->csrfToken)) { |
|
61 | - return $this->csrfToken; |
|
62 | - } |
|
54 | + /** |
|
55 | + * Returns the current CSRF token, if none set it will create a new one. |
|
56 | + * |
|
57 | + * @return CsrfToken |
|
58 | + */ |
|
59 | + public function getToken(): CsrfToken { |
|
60 | + if (!\is_null($this->csrfToken)) { |
|
61 | + return $this->csrfToken; |
|
62 | + } |
|
63 | 63 | |
64 | - if ($this->sessionStorage->hasToken()) { |
|
65 | - $value = $this->sessionStorage->getToken(); |
|
66 | - } else { |
|
67 | - $value = $this->tokenGenerator->generateToken(); |
|
68 | - $this->sessionStorage->setToken($value); |
|
69 | - } |
|
64 | + if ($this->sessionStorage->hasToken()) { |
|
65 | + $value = $this->sessionStorage->getToken(); |
|
66 | + } else { |
|
67 | + $value = $this->tokenGenerator->generateToken(); |
|
68 | + $this->sessionStorage->setToken($value); |
|
69 | + } |
|
70 | 70 | |
71 | - $this->csrfToken = new CsrfToken($value); |
|
72 | - return $this->csrfToken; |
|
73 | - } |
|
71 | + $this->csrfToken = new CsrfToken($value); |
|
72 | + return $this->csrfToken; |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Invalidates any current token and sets a new one. |
|
77 | - * |
|
78 | - * @return CsrfToken |
|
79 | - */ |
|
80 | - public function refreshToken(): CsrfToken { |
|
81 | - $value = $this->tokenGenerator->generateToken(); |
|
82 | - $this->sessionStorage->setToken($value); |
|
83 | - $this->csrfToken = new CsrfToken($value); |
|
84 | - return $this->csrfToken; |
|
85 | - } |
|
75 | + /** |
|
76 | + * Invalidates any current token and sets a new one. |
|
77 | + * |
|
78 | + * @return CsrfToken |
|
79 | + */ |
|
80 | + public function refreshToken(): CsrfToken { |
|
81 | + $value = $this->tokenGenerator->generateToken(); |
|
82 | + $this->sessionStorage->setToken($value); |
|
83 | + $this->csrfToken = new CsrfToken($value); |
|
84 | + return $this->csrfToken; |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * Remove the current token from the storage. |
|
89 | - */ |
|
90 | - public function removeToken() { |
|
91 | - $this->csrfToken = null; |
|
92 | - $this->sessionStorage->removeToken(); |
|
93 | - } |
|
87 | + /** |
|
88 | + * Remove the current token from the storage. |
|
89 | + */ |
|
90 | + public function removeToken() { |
|
91 | + $this->csrfToken = null; |
|
92 | + $this->sessionStorage->removeToken(); |
|
93 | + } |
|
94 | 94 | |
95 | - /** |
|
96 | - * Verifies whether the provided token is valid. |
|
97 | - * |
|
98 | - * @param CsrfToken $token |
|
99 | - * @return bool |
|
100 | - */ |
|
101 | - public function isTokenValid(CsrfToken $token): bool { |
|
102 | - if (!$this->sessionStorage->hasToken()) { |
|
103 | - return false; |
|
104 | - } |
|
95 | + /** |
|
96 | + * Verifies whether the provided token is valid. |
|
97 | + * |
|
98 | + * @param CsrfToken $token |
|
99 | + * @return bool |
|
100 | + */ |
|
101 | + public function isTokenValid(CsrfToken $token): bool { |
|
102 | + if (!$this->sessionStorage->hasToken()) { |
|
103 | + return false; |
|
104 | + } |
|
105 | 105 | |
106 | - return hash_equals( |
|
107 | - $this->sessionStorage->getToken(), |
|
108 | - $token->getDecryptedValue() |
|
109 | - ); |
|
110 | - } |
|
106 | + return hash_equals( |
|
107 | + $this->sessionStorage->getToken(), |
|
108 | + $token->getDecryptedValue() |
|
109 | + ); |
|
110 | + } |
|
111 | 111 | } |
@@ -27,48 +27,48 @@ |
||
27 | 27 | use OCP\Command\ICommand; |
28 | 28 | |
29 | 29 | class QueueBus implements IBus { |
30 | - /** |
|
31 | - * @var ICommand[]|callable[] |
|
32 | - */ |
|
33 | - private $queue = []; |
|
30 | + /** |
|
31 | + * @var ICommand[]|callable[] |
|
32 | + */ |
|
33 | + private $queue = []; |
|
34 | 34 | |
35 | - /** |
|
36 | - * Schedule a command to be fired |
|
37 | - * |
|
38 | - * @param \OCP\Command\ICommand | callable $command |
|
39 | - */ |
|
40 | - public function push($command) { |
|
41 | - $this->queue[] = $command; |
|
42 | - } |
|
35 | + /** |
|
36 | + * Schedule a command to be fired |
|
37 | + * |
|
38 | + * @param \OCP\Command\ICommand | callable $command |
|
39 | + */ |
|
40 | + public function push($command) { |
|
41 | + $this->queue[] = $command; |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Require all commands using a trait to be run synchronous |
|
46 | - * |
|
47 | - * @param string $trait |
|
48 | - */ |
|
49 | - public function requireSync($trait) { |
|
50 | - } |
|
44 | + /** |
|
45 | + * Require all commands using a trait to be run synchronous |
|
46 | + * |
|
47 | + * @param string $trait |
|
48 | + */ |
|
49 | + public function requireSync($trait) { |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * @param \OCP\Command\ICommand | callable $command |
|
54 | - */ |
|
55 | - private function runCommand($command) { |
|
56 | - if ($command instanceof ICommand) { |
|
57 | - // ensure the command can be serialized |
|
58 | - $serialized = serialize($command); |
|
59 | - if (strlen($serialized) > 4000) { |
|
60 | - throw new \InvalidArgumentException('Trying to push a command which serialized form can not be stored in the database (>4000 character)'); |
|
61 | - } |
|
62 | - $unserialized = unserialize($serialized); |
|
63 | - $unserialized->handle(); |
|
64 | - } else { |
|
65 | - $command(); |
|
66 | - } |
|
67 | - } |
|
52 | + /** |
|
53 | + * @param \OCP\Command\ICommand | callable $command |
|
54 | + */ |
|
55 | + private function runCommand($command) { |
|
56 | + if ($command instanceof ICommand) { |
|
57 | + // ensure the command can be serialized |
|
58 | + $serialized = serialize($command); |
|
59 | + if (strlen($serialized) > 4000) { |
|
60 | + throw new \InvalidArgumentException('Trying to push a command which serialized form can not be stored in the database (>4000 character)'); |
|
61 | + } |
|
62 | + $unserialized = unserialize($serialized); |
|
63 | + $unserialized->handle(); |
|
64 | + } else { |
|
65 | + $command(); |
|
66 | + } |
|
67 | + } |
|
68 | 68 | |
69 | - public function run() { |
|
70 | - while ($command = array_shift($this->queue)) { |
|
71 | - $this->runCommand($command); |
|
72 | - } |
|
73 | - } |
|
69 | + public function run() { |
|
70 | + while ($command = array_shift($this->queue)) { |
|
71 | + $this->runCommand($command); |
|
72 | + } |
|
73 | + } |
|
74 | 74 | } |
@@ -38,194 +38,194 @@ |
||
38 | 38 | |
39 | 39 | class Collection implements ICollection { |
40 | 40 | |
41 | - /** @var IManager|Manager */ |
|
42 | - protected $manager; |
|
43 | - |
|
44 | - /** @var IDBConnection */ |
|
45 | - protected $connection; |
|
46 | - |
|
47 | - /** @var int */ |
|
48 | - protected $id; |
|
49 | - |
|
50 | - /** @var string */ |
|
51 | - protected $name; |
|
52 | - |
|
53 | - /** @var IUser|null */ |
|
54 | - protected $userForAccess; |
|
55 | - |
|
56 | - /** @var bool|null */ |
|
57 | - protected $access; |
|
58 | - |
|
59 | - /** @var IResource[] */ |
|
60 | - protected $resources; |
|
61 | - |
|
62 | - public function __construct( |
|
63 | - IManager $manager, |
|
64 | - IDBConnection $connection, |
|
65 | - int $id, |
|
66 | - string $name, |
|
67 | - ?IUser $userForAccess = null, |
|
68 | - ?bool $access = null |
|
69 | - ) { |
|
70 | - $this->manager = $manager; |
|
71 | - $this->connection = $connection; |
|
72 | - $this->id = $id; |
|
73 | - $this->name = $name; |
|
74 | - $this->userForAccess = $userForAccess; |
|
75 | - $this->access = $access; |
|
76 | - $this->resources = []; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * @return int |
|
81 | - * @since 16.0.0 |
|
82 | - */ |
|
83 | - public function getId(): int { |
|
84 | - return $this->id; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * @return string |
|
89 | - * @since 16.0.0 |
|
90 | - */ |
|
91 | - public function getName(): string { |
|
92 | - return $this->name; |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * @param string $name |
|
97 | - * @since 16.0.0 |
|
98 | - */ |
|
99 | - public function setName(string $name): void { |
|
100 | - $query = $this->connection->getQueryBuilder(); |
|
101 | - $query->update(Manager::TABLE_COLLECTIONS) |
|
102 | - ->set('name', $query->createNamedParameter($name)) |
|
103 | - ->where($query->expr()->eq('id', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT))); |
|
104 | - $query->execute(); |
|
105 | - |
|
106 | - $this->name = $name; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * @return IResource[] |
|
111 | - * @since 16.0.0 |
|
112 | - */ |
|
113 | - public function getResources(): array { |
|
114 | - if (empty($this->resources)) { |
|
115 | - $this->resources = $this->manager->getResourcesByCollectionForUser($this, $this->userForAccess); |
|
116 | - } |
|
117 | - |
|
118 | - return $this->resources; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Adds a resource to a collection |
|
123 | - * |
|
124 | - * @param IResource $resource |
|
125 | - * @throws ResourceException when the resource is already part of the collection |
|
126 | - * @since 16.0.0 |
|
127 | - */ |
|
128 | - public function addResource(IResource $resource): void { |
|
129 | - array_map(function (IResource $r) use ($resource) { |
|
130 | - if ($this->isSameResource($r, $resource)) { |
|
131 | - throw new ResourceException('Already part of the collection'); |
|
132 | - } |
|
133 | - }, $this->getResources()); |
|
134 | - |
|
135 | - $this->resources[] = $resource; |
|
136 | - |
|
137 | - $query = $this->connection->getQueryBuilder(); |
|
138 | - $query->insert(Manager::TABLE_RESOURCES) |
|
139 | - ->values([ |
|
140 | - 'collection_id' => $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT), |
|
141 | - 'resource_type' => $query->createNamedParameter($resource->getType()), |
|
142 | - 'resource_id' => $query->createNamedParameter($resource->getId()), |
|
143 | - ]); |
|
144 | - |
|
145 | - try { |
|
146 | - $query->execute(); |
|
147 | - } catch (ConstraintViolationException $e) { |
|
148 | - throw new ResourceException('Already part of the collection'); |
|
149 | - } |
|
150 | - |
|
151 | - $this->manager->invalidateAccessCacheForCollection($this); |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Removes a resource from a collection |
|
156 | - * |
|
157 | - * @param IResource $resource |
|
158 | - * @since 16.0.0 |
|
159 | - */ |
|
160 | - public function removeResource(IResource $resource): void { |
|
161 | - $this->resources = array_filter($this->getResources(), function (IResource $r) use ($resource) { |
|
162 | - return !$this->isSameResource($r, $resource); |
|
163 | - }); |
|
164 | - |
|
165 | - $query = $this->connection->getQueryBuilder(); |
|
166 | - $query->delete(Manager::TABLE_RESOURCES) |
|
167 | - ->where($query->expr()->eq('collection_id', $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT))) |
|
168 | - ->andWhere($query->expr()->eq('resource_type', $query->createNamedParameter($resource->getType()))) |
|
169 | - ->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($resource->getId()))); |
|
170 | - $query->execute(); |
|
171 | - |
|
172 | - if (empty($this->resources)) { |
|
173 | - $this->removeCollection(); |
|
174 | - } else { |
|
175 | - $this->manager->invalidateAccessCacheForCollection($this); |
|
176 | - } |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Can a user/guest access the collection |
|
181 | - * |
|
182 | - * @param IUser|null $user |
|
183 | - * @return bool |
|
184 | - * @since 16.0.0 |
|
185 | - */ |
|
186 | - public function canAccess(?IUser $user): bool { |
|
187 | - if ($user instanceof IUser) { |
|
188 | - return $this->canUserAccess($user); |
|
189 | - } |
|
190 | - return $this->canGuestAccess(); |
|
191 | - } |
|
192 | - |
|
193 | - protected function canUserAccess(IUser $user): bool { |
|
194 | - if (\is_bool($this->access) && $this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) { |
|
195 | - return $this->access; |
|
196 | - } |
|
197 | - |
|
198 | - $access = $this->manager->canAccessCollection($this, $user); |
|
199 | - if ($this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) { |
|
200 | - $this->access = $access; |
|
201 | - } |
|
202 | - return $access; |
|
203 | - } |
|
204 | - |
|
205 | - protected function canGuestAccess(): bool { |
|
206 | - if (\is_bool($this->access) && !$this->userForAccess instanceof IUser) { |
|
207 | - return $this->access; |
|
208 | - } |
|
209 | - |
|
210 | - $access = $this->manager->canAccessCollection($this, null); |
|
211 | - if (!$this->userForAccess instanceof IUser) { |
|
212 | - $this->access = $access; |
|
213 | - } |
|
214 | - return $access; |
|
215 | - } |
|
216 | - |
|
217 | - protected function isSameResource(IResource $resource1, IResource $resource2): bool { |
|
218 | - return $resource1->getType() === $resource2->getType() && |
|
219 | - $resource1->getId() === $resource2->getId(); |
|
220 | - } |
|
221 | - |
|
222 | - protected function removeCollection(): void { |
|
223 | - $query = $this->connection->getQueryBuilder(); |
|
224 | - $query->delete(Manager::TABLE_COLLECTIONS) |
|
225 | - ->where($query->expr()->eq('id', $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT))); |
|
226 | - $query->execute(); |
|
227 | - |
|
228 | - $this->manager->invalidateAccessCacheForCollection($this); |
|
229 | - $this->id = 0; |
|
230 | - } |
|
41 | + /** @var IManager|Manager */ |
|
42 | + protected $manager; |
|
43 | + |
|
44 | + /** @var IDBConnection */ |
|
45 | + protected $connection; |
|
46 | + |
|
47 | + /** @var int */ |
|
48 | + protected $id; |
|
49 | + |
|
50 | + /** @var string */ |
|
51 | + protected $name; |
|
52 | + |
|
53 | + /** @var IUser|null */ |
|
54 | + protected $userForAccess; |
|
55 | + |
|
56 | + /** @var bool|null */ |
|
57 | + protected $access; |
|
58 | + |
|
59 | + /** @var IResource[] */ |
|
60 | + protected $resources; |
|
61 | + |
|
62 | + public function __construct( |
|
63 | + IManager $manager, |
|
64 | + IDBConnection $connection, |
|
65 | + int $id, |
|
66 | + string $name, |
|
67 | + ?IUser $userForAccess = null, |
|
68 | + ?bool $access = null |
|
69 | + ) { |
|
70 | + $this->manager = $manager; |
|
71 | + $this->connection = $connection; |
|
72 | + $this->id = $id; |
|
73 | + $this->name = $name; |
|
74 | + $this->userForAccess = $userForAccess; |
|
75 | + $this->access = $access; |
|
76 | + $this->resources = []; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * @return int |
|
81 | + * @since 16.0.0 |
|
82 | + */ |
|
83 | + public function getId(): int { |
|
84 | + return $this->id; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * @return string |
|
89 | + * @since 16.0.0 |
|
90 | + */ |
|
91 | + public function getName(): string { |
|
92 | + return $this->name; |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * @param string $name |
|
97 | + * @since 16.0.0 |
|
98 | + */ |
|
99 | + public function setName(string $name): void { |
|
100 | + $query = $this->connection->getQueryBuilder(); |
|
101 | + $query->update(Manager::TABLE_COLLECTIONS) |
|
102 | + ->set('name', $query->createNamedParameter($name)) |
|
103 | + ->where($query->expr()->eq('id', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT))); |
|
104 | + $query->execute(); |
|
105 | + |
|
106 | + $this->name = $name; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * @return IResource[] |
|
111 | + * @since 16.0.0 |
|
112 | + */ |
|
113 | + public function getResources(): array { |
|
114 | + if (empty($this->resources)) { |
|
115 | + $this->resources = $this->manager->getResourcesByCollectionForUser($this, $this->userForAccess); |
|
116 | + } |
|
117 | + |
|
118 | + return $this->resources; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Adds a resource to a collection |
|
123 | + * |
|
124 | + * @param IResource $resource |
|
125 | + * @throws ResourceException when the resource is already part of the collection |
|
126 | + * @since 16.0.0 |
|
127 | + */ |
|
128 | + public function addResource(IResource $resource): void { |
|
129 | + array_map(function (IResource $r) use ($resource) { |
|
130 | + if ($this->isSameResource($r, $resource)) { |
|
131 | + throw new ResourceException('Already part of the collection'); |
|
132 | + } |
|
133 | + }, $this->getResources()); |
|
134 | + |
|
135 | + $this->resources[] = $resource; |
|
136 | + |
|
137 | + $query = $this->connection->getQueryBuilder(); |
|
138 | + $query->insert(Manager::TABLE_RESOURCES) |
|
139 | + ->values([ |
|
140 | + 'collection_id' => $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT), |
|
141 | + 'resource_type' => $query->createNamedParameter($resource->getType()), |
|
142 | + 'resource_id' => $query->createNamedParameter($resource->getId()), |
|
143 | + ]); |
|
144 | + |
|
145 | + try { |
|
146 | + $query->execute(); |
|
147 | + } catch (ConstraintViolationException $e) { |
|
148 | + throw new ResourceException('Already part of the collection'); |
|
149 | + } |
|
150 | + |
|
151 | + $this->manager->invalidateAccessCacheForCollection($this); |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Removes a resource from a collection |
|
156 | + * |
|
157 | + * @param IResource $resource |
|
158 | + * @since 16.0.0 |
|
159 | + */ |
|
160 | + public function removeResource(IResource $resource): void { |
|
161 | + $this->resources = array_filter($this->getResources(), function (IResource $r) use ($resource) { |
|
162 | + return !$this->isSameResource($r, $resource); |
|
163 | + }); |
|
164 | + |
|
165 | + $query = $this->connection->getQueryBuilder(); |
|
166 | + $query->delete(Manager::TABLE_RESOURCES) |
|
167 | + ->where($query->expr()->eq('collection_id', $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT))) |
|
168 | + ->andWhere($query->expr()->eq('resource_type', $query->createNamedParameter($resource->getType()))) |
|
169 | + ->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($resource->getId()))); |
|
170 | + $query->execute(); |
|
171 | + |
|
172 | + if (empty($this->resources)) { |
|
173 | + $this->removeCollection(); |
|
174 | + } else { |
|
175 | + $this->manager->invalidateAccessCacheForCollection($this); |
|
176 | + } |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Can a user/guest access the collection |
|
181 | + * |
|
182 | + * @param IUser|null $user |
|
183 | + * @return bool |
|
184 | + * @since 16.0.0 |
|
185 | + */ |
|
186 | + public function canAccess(?IUser $user): bool { |
|
187 | + if ($user instanceof IUser) { |
|
188 | + return $this->canUserAccess($user); |
|
189 | + } |
|
190 | + return $this->canGuestAccess(); |
|
191 | + } |
|
192 | + |
|
193 | + protected function canUserAccess(IUser $user): bool { |
|
194 | + if (\is_bool($this->access) && $this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) { |
|
195 | + return $this->access; |
|
196 | + } |
|
197 | + |
|
198 | + $access = $this->manager->canAccessCollection($this, $user); |
|
199 | + if ($this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) { |
|
200 | + $this->access = $access; |
|
201 | + } |
|
202 | + return $access; |
|
203 | + } |
|
204 | + |
|
205 | + protected function canGuestAccess(): bool { |
|
206 | + if (\is_bool($this->access) && !$this->userForAccess instanceof IUser) { |
|
207 | + return $this->access; |
|
208 | + } |
|
209 | + |
|
210 | + $access = $this->manager->canAccessCollection($this, null); |
|
211 | + if (!$this->userForAccess instanceof IUser) { |
|
212 | + $this->access = $access; |
|
213 | + } |
|
214 | + return $access; |
|
215 | + } |
|
216 | + |
|
217 | + protected function isSameResource(IResource $resource1, IResource $resource2): bool { |
|
218 | + return $resource1->getType() === $resource2->getType() && |
|
219 | + $resource1->getId() === $resource2->getId(); |
|
220 | + } |
|
221 | + |
|
222 | + protected function removeCollection(): void { |
|
223 | + $query = $this->connection->getQueryBuilder(); |
|
224 | + $query->delete(Manager::TABLE_COLLECTIONS) |
|
225 | + ->where($query->expr()->eq('id', $query->createNamedParameter($this->id, IQueryBuilder::PARAM_INT))); |
|
226 | + $query->execute(); |
|
227 | + |
|
228 | + $this->manager->invalidateAccessCacheForCollection($this); |
|
229 | + $this->id = 0; |
|
230 | + } |
|
231 | 231 | } |
@@ -25,92 +25,92 @@ |
||
25 | 25 | namespace OC\OCS; |
26 | 26 | |
27 | 27 | class Provider extends \OCP\AppFramework\Controller { |
28 | - /** @var \OCP\App\IAppManager */ |
|
29 | - private $appManager; |
|
28 | + /** @var \OCP\App\IAppManager */ |
|
29 | + private $appManager; |
|
30 | 30 | |
31 | - /** |
|
32 | - * @param string $appName |
|
33 | - * @param \OCP\IRequest $request |
|
34 | - * @param \OCP\App\IAppManager $appManager |
|
35 | - */ |
|
36 | - public function __construct($appName, |
|
37 | - \OCP\IRequest $request, |
|
38 | - \OCP\App\IAppManager $appManager) { |
|
39 | - parent::__construct($appName, $request); |
|
40 | - $this->appManager = $appManager; |
|
41 | - } |
|
31 | + /** |
|
32 | + * @param string $appName |
|
33 | + * @param \OCP\IRequest $request |
|
34 | + * @param \OCP\App\IAppManager $appManager |
|
35 | + */ |
|
36 | + public function __construct($appName, |
|
37 | + \OCP\IRequest $request, |
|
38 | + \OCP\App\IAppManager $appManager) { |
|
39 | + parent::__construct($appName, $request); |
|
40 | + $this->appManager = $appManager; |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * @return \OCP\AppFramework\Http\JSONResponse |
|
45 | - */ |
|
46 | - public function buildProviderList() { |
|
47 | - $services = [ |
|
48 | - 'PRIVATE_DATA' => [ |
|
49 | - 'version' => 1, |
|
50 | - 'endpoints' => [ |
|
51 | - 'store' => '/ocs/v2.php/privatedata/setattribute', |
|
52 | - 'read' => '/ocs/v2.php/privatedata/getattribute', |
|
53 | - 'delete' => '/ocs/v2.php/privatedata/deleteattribute', |
|
54 | - ], |
|
55 | - ], |
|
56 | - ]; |
|
43 | + /** |
|
44 | + * @return \OCP\AppFramework\Http\JSONResponse |
|
45 | + */ |
|
46 | + public function buildProviderList() { |
|
47 | + $services = [ |
|
48 | + 'PRIVATE_DATA' => [ |
|
49 | + 'version' => 1, |
|
50 | + 'endpoints' => [ |
|
51 | + 'store' => '/ocs/v2.php/privatedata/setattribute', |
|
52 | + 'read' => '/ocs/v2.php/privatedata/getattribute', |
|
53 | + 'delete' => '/ocs/v2.php/privatedata/deleteattribute', |
|
54 | + ], |
|
55 | + ], |
|
56 | + ]; |
|
57 | 57 | |
58 | - if ($this->appManager->isEnabledForUser('files_sharing')) { |
|
59 | - $services['SHARING'] = [ |
|
60 | - 'version' => 1, |
|
61 | - 'endpoints' => [ |
|
62 | - 'share' => '/ocs/v2.php/apps/files_sharing/api/v1/shares', |
|
63 | - ], |
|
64 | - ]; |
|
65 | - $services['FEDERATED_SHARING'] = [ |
|
66 | - 'version' => 1, |
|
67 | - 'endpoints' => [ |
|
68 | - 'share' => '/ocs/v2.php/cloud/shares', |
|
69 | - 'webdav' => '/public.php/webdav/', |
|
70 | - ], |
|
71 | - ]; |
|
72 | - } |
|
58 | + if ($this->appManager->isEnabledForUser('files_sharing')) { |
|
59 | + $services['SHARING'] = [ |
|
60 | + 'version' => 1, |
|
61 | + 'endpoints' => [ |
|
62 | + 'share' => '/ocs/v2.php/apps/files_sharing/api/v1/shares', |
|
63 | + ], |
|
64 | + ]; |
|
65 | + $services['FEDERATED_SHARING'] = [ |
|
66 | + 'version' => 1, |
|
67 | + 'endpoints' => [ |
|
68 | + 'share' => '/ocs/v2.php/cloud/shares', |
|
69 | + 'webdav' => '/public.php/webdav/', |
|
70 | + ], |
|
71 | + ]; |
|
72 | + } |
|
73 | 73 | |
74 | - if ($this->appManager->isEnabledForUser('federation')) { |
|
75 | - if (isset($services['FEDERATED_SHARING'])) { |
|
76 | - $services['FEDERATED_SHARING']['endpoints']['shared-secret'] = '/ocs/v2.php/cloud/shared-secret'; |
|
77 | - $services['FEDERATED_SHARING']['endpoints']['system-address-book'] = '/remote.php/dav/addressbooks/system/system/system'; |
|
78 | - $services['FEDERATED_SHARING']['endpoints']['carddav-user'] = 'system'; |
|
79 | - } else { |
|
80 | - $services['FEDERATED_SHARING'] = [ |
|
81 | - 'version' => 1, |
|
82 | - 'endpoints' => [ |
|
83 | - 'shared-secret' => '/ocs/v2.php/cloud/shared-secret', |
|
84 | - 'system-address-book' => '/remote.php/dav/addressbooks/system/system/system', |
|
85 | - 'carddav-user' => 'system' |
|
86 | - ], |
|
87 | - ]; |
|
88 | - } |
|
89 | - } |
|
74 | + if ($this->appManager->isEnabledForUser('federation')) { |
|
75 | + if (isset($services['FEDERATED_SHARING'])) { |
|
76 | + $services['FEDERATED_SHARING']['endpoints']['shared-secret'] = '/ocs/v2.php/cloud/shared-secret'; |
|
77 | + $services['FEDERATED_SHARING']['endpoints']['system-address-book'] = '/remote.php/dav/addressbooks/system/system/system'; |
|
78 | + $services['FEDERATED_SHARING']['endpoints']['carddav-user'] = 'system'; |
|
79 | + } else { |
|
80 | + $services['FEDERATED_SHARING'] = [ |
|
81 | + 'version' => 1, |
|
82 | + 'endpoints' => [ |
|
83 | + 'shared-secret' => '/ocs/v2.php/cloud/shared-secret', |
|
84 | + 'system-address-book' => '/remote.php/dav/addressbooks/system/system/system', |
|
85 | + 'carddav-user' => 'system' |
|
86 | + ], |
|
87 | + ]; |
|
88 | + } |
|
89 | + } |
|
90 | 90 | |
91 | - if ($this->appManager->isEnabledForUser('activity')) { |
|
92 | - $services['ACTIVITY'] = [ |
|
93 | - 'version' => 1, |
|
94 | - 'endpoints' => [ |
|
95 | - 'list' => '/ocs/v2.php/cloud/activity', |
|
96 | - ], |
|
97 | - ]; |
|
98 | - } |
|
91 | + if ($this->appManager->isEnabledForUser('activity')) { |
|
92 | + $services['ACTIVITY'] = [ |
|
93 | + 'version' => 1, |
|
94 | + 'endpoints' => [ |
|
95 | + 'list' => '/ocs/v2.php/cloud/activity', |
|
96 | + ], |
|
97 | + ]; |
|
98 | + } |
|
99 | 99 | |
100 | - if ($this->appManager->isEnabledForUser('provisioning_api')) { |
|
101 | - $services['PROVISIONING'] = [ |
|
102 | - 'version' => 1, |
|
103 | - 'endpoints' => [ |
|
104 | - 'user' => '/ocs/v2.php/cloud/users', |
|
105 | - 'groups' => '/ocs/v2.php/cloud/groups', |
|
106 | - 'apps' => '/ocs/v2.php/cloud/apps', |
|
107 | - ], |
|
108 | - ]; |
|
109 | - } |
|
100 | + if ($this->appManager->isEnabledForUser('provisioning_api')) { |
|
101 | + $services['PROVISIONING'] = [ |
|
102 | + 'version' => 1, |
|
103 | + 'endpoints' => [ |
|
104 | + 'user' => '/ocs/v2.php/cloud/users', |
|
105 | + 'groups' => '/ocs/v2.php/cloud/groups', |
|
106 | + 'apps' => '/ocs/v2.php/cloud/apps', |
|
107 | + ], |
|
108 | + ]; |
|
109 | + } |
|
110 | 110 | |
111 | - return new \OCP\AppFramework\Http\JSONResponse([ |
|
112 | - 'version' => 2, |
|
113 | - 'services' => $services, |
|
114 | - ]); |
|
115 | - } |
|
111 | + return new \OCP\AppFramework\Http\JSONResponse([ |
|
112 | + 'version' => 2, |
|
113 | + 'services' => $services, |
|
114 | + ]); |
|
115 | + } |
|
116 | 116 | } |
@@ -27,56 +27,56 @@ |
||
27 | 27 | |
28 | 28 | class LoginResult { |
29 | 29 | |
30 | - /** @var bool */ |
|
31 | - private $success; |
|
30 | + /** @var bool */ |
|
31 | + private $success; |
|
32 | 32 | |
33 | - /** @var LoginData */ |
|
34 | - private $loginData; |
|
33 | + /** @var LoginData */ |
|
34 | + private $loginData; |
|
35 | 35 | |
36 | - /** @var string|null */ |
|
37 | - private $redirectUrl; |
|
36 | + /** @var string|null */ |
|
37 | + private $redirectUrl; |
|
38 | 38 | |
39 | - /** @var string|null */ |
|
40 | - private $errorMessage; |
|
39 | + /** @var string|null */ |
|
40 | + private $errorMessage; |
|
41 | 41 | |
42 | - private function __construct(bool $success, LoginData $loginData) { |
|
43 | - $this->success = $success; |
|
44 | - $this->loginData = $loginData; |
|
45 | - } |
|
42 | + private function __construct(bool $success, LoginData $loginData) { |
|
43 | + $this->success = $success; |
|
44 | + $this->loginData = $loginData; |
|
45 | + } |
|
46 | 46 | |
47 | - private function setRedirectUrl(string $url) { |
|
48 | - $this->redirectUrl = $url; |
|
49 | - } |
|
47 | + private function setRedirectUrl(string $url) { |
|
48 | + $this->redirectUrl = $url; |
|
49 | + } |
|
50 | 50 | |
51 | - private function setErrorMessage(string $msg) { |
|
52 | - $this->errorMessage = $msg; |
|
53 | - } |
|
51 | + private function setErrorMessage(string $msg) { |
|
52 | + $this->errorMessage = $msg; |
|
53 | + } |
|
54 | 54 | |
55 | - public static function success(LoginData $data, ?string $redirectUrl = null) { |
|
56 | - $result = new static(true, $data); |
|
57 | - if ($redirectUrl !== null) { |
|
58 | - $result->setRedirectUrl($redirectUrl); |
|
59 | - } |
|
60 | - return $result; |
|
61 | - } |
|
55 | + public static function success(LoginData $data, ?string $redirectUrl = null) { |
|
56 | + $result = new static(true, $data); |
|
57 | + if ($redirectUrl !== null) { |
|
58 | + $result->setRedirectUrl($redirectUrl); |
|
59 | + } |
|
60 | + return $result; |
|
61 | + } |
|
62 | 62 | |
63 | - public static function failure(LoginData $data, string $msg = null): LoginResult { |
|
64 | - $result = new static(false, $data); |
|
65 | - if ($msg !== null) { |
|
66 | - $result->setErrorMessage($msg); |
|
67 | - } |
|
68 | - return $result; |
|
69 | - } |
|
63 | + public static function failure(LoginData $data, string $msg = null): LoginResult { |
|
64 | + $result = new static(false, $data); |
|
65 | + if ($msg !== null) { |
|
66 | + $result->setErrorMessage($msg); |
|
67 | + } |
|
68 | + return $result; |
|
69 | + } |
|
70 | 70 | |
71 | - public function isSuccess(): bool { |
|
72 | - return $this->success; |
|
73 | - } |
|
71 | + public function isSuccess(): bool { |
|
72 | + return $this->success; |
|
73 | + } |
|
74 | 74 | |
75 | - public function getRedirectUrl(): ?string { |
|
76 | - return $this->redirectUrl; |
|
77 | - } |
|
75 | + public function getRedirectUrl(): ?string { |
|
76 | + return $this->redirectUrl; |
|
77 | + } |
|
78 | 78 | |
79 | - public function getErrorMessage(): ?string { |
|
80 | - return $this->errorMessage; |
|
81 | - } |
|
79 | + public function getErrorMessage(): ?string { |
|
80 | + return $this->errorMessage; |
|
81 | + } |
|
82 | 82 | } |
@@ -35,62 +35,62 @@ |
||
35 | 35 | |
36 | 36 | class ProviderManager { |
37 | 37 | |
38 | - /** @var ProviderLoader */ |
|
39 | - private $providerLoader; |
|
38 | + /** @var ProviderLoader */ |
|
39 | + private $providerLoader; |
|
40 | 40 | |
41 | - /** @var IRegistry */ |
|
42 | - private $providerRegistry; |
|
41 | + /** @var IRegistry */ |
|
42 | + private $providerRegistry; |
|
43 | 43 | |
44 | - public function __construct(ProviderLoader $providerLoader, IRegistry $providerRegistry) { |
|
45 | - $this->providerLoader = $providerLoader; |
|
46 | - $this->providerRegistry = $providerRegistry; |
|
47 | - } |
|
44 | + public function __construct(ProviderLoader $providerLoader, IRegistry $providerRegistry) { |
|
45 | + $this->providerLoader = $providerLoader; |
|
46 | + $this->providerRegistry = $providerRegistry; |
|
47 | + } |
|
48 | 48 | |
49 | - private function getProvider(string $providerId, IUser $user): IProvider { |
|
50 | - $providers = $this->providerLoader->getProviders($user); |
|
49 | + private function getProvider(string $providerId, IUser $user): IProvider { |
|
50 | + $providers = $this->providerLoader->getProviders($user); |
|
51 | 51 | |
52 | - if (!isset($providers[$providerId])) { |
|
53 | - throw new InvalidProviderException($providerId); |
|
54 | - } |
|
52 | + if (!isset($providers[$providerId])) { |
|
53 | + throw new InvalidProviderException($providerId); |
|
54 | + } |
|
55 | 55 | |
56 | - return $providers[$providerId]; |
|
57 | - } |
|
56 | + return $providers[$providerId]; |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Try to enable the provider with the given id for the given user |
|
61 | - * |
|
62 | - * @param IUser $user |
|
63 | - * |
|
64 | - * @return bool whether the provider supports this operation |
|
65 | - */ |
|
66 | - public function tryEnableProviderFor(string $providerId, IUser $user): bool { |
|
67 | - $provider = $this->getProvider($providerId, $user); |
|
59 | + /** |
|
60 | + * Try to enable the provider with the given id for the given user |
|
61 | + * |
|
62 | + * @param IUser $user |
|
63 | + * |
|
64 | + * @return bool whether the provider supports this operation |
|
65 | + */ |
|
66 | + public function tryEnableProviderFor(string $providerId, IUser $user): bool { |
|
67 | + $provider = $this->getProvider($providerId, $user); |
|
68 | 68 | |
69 | - if ($provider instanceof IActivatableByAdmin) { |
|
70 | - $provider->enableFor($user); |
|
71 | - $this->providerRegistry->enableProviderFor($provider, $user); |
|
72 | - return true; |
|
73 | - } else { |
|
74 | - return false; |
|
75 | - } |
|
76 | - } |
|
69 | + if ($provider instanceof IActivatableByAdmin) { |
|
70 | + $provider->enableFor($user); |
|
71 | + $this->providerRegistry->enableProviderFor($provider, $user); |
|
72 | + return true; |
|
73 | + } else { |
|
74 | + return false; |
|
75 | + } |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * Try to disable the provider with the given id for the given user |
|
80 | - * |
|
81 | - * @param IUser $user |
|
82 | - * |
|
83 | - * @return bool whether the provider supports this operation |
|
84 | - */ |
|
85 | - public function tryDisableProviderFor(string $providerId, IUser $user): bool { |
|
86 | - $provider = $this->getProvider($providerId, $user); |
|
78 | + /** |
|
79 | + * Try to disable the provider with the given id for the given user |
|
80 | + * |
|
81 | + * @param IUser $user |
|
82 | + * |
|
83 | + * @return bool whether the provider supports this operation |
|
84 | + */ |
|
85 | + public function tryDisableProviderFor(string $providerId, IUser $user): bool { |
|
86 | + $provider = $this->getProvider($providerId, $user); |
|
87 | 87 | |
88 | - if ($provider instanceof IDeactivatableByAdmin) { |
|
89 | - $provider->disableFor($user); |
|
90 | - $this->providerRegistry->disableProviderFor($provider, $user); |
|
91 | - return true; |
|
92 | - } else { |
|
93 | - return false; |
|
94 | - } |
|
95 | - } |
|
88 | + if ($provider instanceof IDeactivatableByAdmin) { |
|
89 | + $provider->disableFor($user); |
|
90 | + $this->providerRegistry->disableProviderFor($provider, $user); |
|
91 | + return true; |
|
92 | + } else { |
|
93 | + return false; |
|
94 | + } |
|
95 | + } |
|
96 | 96 | } |
@@ -32,82 +32,82 @@ |
||
32 | 32 | |
33 | 33 | class MandatoryTwoFactor { |
34 | 34 | |
35 | - /** @var IConfig */ |
|
36 | - private $config; |
|
35 | + /** @var IConfig */ |
|
36 | + private $config; |
|
37 | 37 | |
38 | - /** @var IGroupManager */ |
|
39 | - private $groupManager; |
|
38 | + /** @var IGroupManager */ |
|
39 | + private $groupManager; |
|
40 | 40 | |
41 | - public function __construct(IConfig $config, IGroupManager $groupManager) { |
|
42 | - $this->config = $config; |
|
43 | - $this->groupManager = $groupManager; |
|
44 | - } |
|
41 | + public function __construct(IConfig $config, IGroupManager $groupManager) { |
|
42 | + $this->config = $config; |
|
43 | + $this->groupManager = $groupManager; |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Get the state of enforced two-factor auth |
|
48 | - */ |
|
49 | - public function getState(): EnforcementState { |
|
50 | - return new EnforcementState( |
|
51 | - $this->config->getSystemValue('twofactor_enforced', 'false') === 'true', |
|
52 | - $this->config->getSystemValue('twofactor_enforced_groups', []), |
|
53 | - $this->config->getSystemValue('twofactor_enforced_excluded_groups', []) |
|
54 | - ); |
|
55 | - } |
|
46 | + /** |
|
47 | + * Get the state of enforced two-factor auth |
|
48 | + */ |
|
49 | + public function getState(): EnforcementState { |
|
50 | + return new EnforcementState( |
|
51 | + $this->config->getSystemValue('twofactor_enforced', 'false') === 'true', |
|
52 | + $this->config->getSystemValue('twofactor_enforced_groups', []), |
|
53 | + $this->config->getSystemValue('twofactor_enforced_excluded_groups', []) |
|
54 | + ); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * Set the state of enforced two-factor auth |
|
59 | - */ |
|
60 | - public function setState(EnforcementState $state) { |
|
61 | - $this->config->setSystemValue('twofactor_enforced', $state->isEnforced() ? 'true' : 'false'); |
|
62 | - $this->config->setSystemValue('twofactor_enforced_groups', $state->getEnforcedGroups()); |
|
63 | - $this->config->setSystemValue('twofactor_enforced_excluded_groups', $state->getExcludedGroups()); |
|
64 | - } |
|
57 | + /** |
|
58 | + * Set the state of enforced two-factor auth |
|
59 | + */ |
|
60 | + public function setState(EnforcementState $state) { |
|
61 | + $this->config->setSystemValue('twofactor_enforced', $state->isEnforced() ? 'true' : 'false'); |
|
62 | + $this->config->setSystemValue('twofactor_enforced_groups', $state->getEnforcedGroups()); |
|
63 | + $this->config->setSystemValue('twofactor_enforced_excluded_groups', $state->getExcludedGroups()); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Check if two-factor auth is enforced for a specific user |
|
68 | - * |
|
69 | - * The admin(s) can enforce two-factor auth system-wide, for certain groups only |
|
70 | - * and also have the option to exclude users of certain groups. This method will |
|
71 | - * check their membership of those groups. |
|
72 | - * |
|
73 | - * @param IUser $user |
|
74 | - * |
|
75 | - * @return bool |
|
76 | - */ |
|
77 | - public function isEnforcedFor(IUser $user): bool { |
|
78 | - $state = $this->getState(); |
|
79 | - if (!$state->isEnforced()) { |
|
80 | - return false; |
|
81 | - } |
|
82 | - $uid = $user->getUID(); |
|
66 | + /** |
|
67 | + * Check if two-factor auth is enforced for a specific user |
|
68 | + * |
|
69 | + * The admin(s) can enforce two-factor auth system-wide, for certain groups only |
|
70 | + * and also have the option to exclude users of certain groups. This method will |
|
71 | + * check their membership of those groups. |
|
72 | + * |
|
73 | + * @param IUser $user |
|
74 | + * |
|
75 | + * @return bool |
|
76 | + */ |
|
77 | + public function isEnforcedFor(IUser $user): bool { |
|
78 | + $state = $this->getState(); |
|
79 | + if (!$state->isEnforced()) { |
|
80 | + return false; |
|
81 | + } |
|
82 | + $uid = $user->getUID(); |
|
83 | 83 | |
84 | - /* |
|
84 | + /* |
|
85 | 85 | * If there is a list of enforced groups, we only enforce 2FA for members of those groups. |
86 | 86 | * For all the other users it is not enforced (overruling the excluded groups list). |
87 | 87 | */ |
88 | - if (!empty($state->getEnforcedGroups())) { |
|
89 | - foreach ($state->getEnforcedGroups() as $group) { |
|
90 | - if ($this->groupManager->isInGroup($uid, $group)) { |
|
91 | - return true; |
|
92 | - } |
|
93 | - } |
|
94 | - // Not a member of any of these groups -> no 2FA enforced |
|
95 | - return false; |
|
96 | - } |
|
88 | + if (!empty($state->getEnforcedGroups())) { |
|
89 | + foreach ($state->getEnforcedGroups() as $group) { |
|
90 | + if ($this->groupManager->isInGroup($uid, $group)) { |
|
91 | + return true; |
|
92 | + } |
|
93 | + } |
|
94 | + // Not a member of any of these groups -> no 2FA enforced |
|
95 | + return false; |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * If the user is member of an excluded group, 2FA won't be enforced. |
|
100 | - */ |
|
101 | - foreach ($state->getExcludedGroups() as $group) { |
|
102 | - if ($this->groupManager->isInGroup($uid, $group)) { |
|
103 | - return false; |
|
104 | - } |
|
105 | - } |
|
98 | + /** |
|
99 | + * If the user is member of an excluded group, 2FA won't be enforced. |
|
100 | + */ |
|
101 | + foreach ($state->getExcludedGroups() as $group) { |
|
102 | + if ($this->groupManager->isInGroup($uid, $group)) { |
|
103 | + return false; |
|
104 | + } |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * No enforced groups configured and user not member of an excluded groups, |
|
109 | - * so 2FA is enforced. |
|
110 | - */ |
|
111 | - return true; |
|
112 | - } |
|
107 | + /** |
|
108 | + * No enforced groups configured and user not member of an excluded groups, |
|
109 | + * so 2FA is enforced. |
|
110 | + */ |
|
111 | + return true; |
|
112 | + } |
|
113 | 113 | } |