@@ -42,224 +42,224 @@ |
||
| 42 | 42 | use OCP\SystemTag\TagNotFoundException; |
| 43 | 43 | |
| 44 | 44 | class Listener { |
| 45 | - /** @var IGroupManager */ |
|
| 46 | - protected $groupManager; |
|
| 47 | - /** @var IManager */ |
|
| 48 | - protected $activityManager; |
|
| 49 | - /** @var IUserSession */ |
|
| 50 | - protected $session; |
|
| 51 | - /** @var IConfig */ |
|
| 52 | - protected $config; |
|
| 53 | - /** @var \OCP\SystemTag\ISystemTagManager */ |
|
| 54 | - protected $tagManager; |
|
| 55 | - /** @var \OCP\App\IAppManager */ |
|
| 56 | - protected $appManager; |
|
| 57 | - /** @var \OCP\Files\Config\IMountProviderCollection */ |
|
| 58 | - protected $mountCollection; |
|
| 59 | - /** @var \OCP\Files\IRootFolder */ |
|
| 60 | - protected $rootFolder; |
|
| 61 | - /** @var IShareHelper */ |
|
| 62 | - protected $shareHelper; |
|
| 45 | + /** @var IGroupManager */ |
|
| 46 | + protected $groupManager; |
|
| 47 | + /** @var IManager */ |
|
| 48 | + protected $activityManager; |
|
| 49 | + /** @var IUserSession */ |
|
| 50 | + protected $session; |
|
| 51 | + /** @var IConfig */ |
|
| 52 | + protected $config; |
|
| 53 | + /** @var \OCP\SystemTag\ISystemTagManager */ |
|
| 54 | + protected $tagManager; |
|
| 55 | + /** @var \OCP\App\IAppManager */ |
|
| 56 | + protected $appManager; |
|
| 57 | + /** @var \OCP\Files\Config\IMountProviderCollection */ |
|
| 58 | + protected $mountCollection; |
|
| 59 | + /** @var \OCP\Files\IRootFolder */ |
|
| 60 | + protected $rootFolder; |
|
| 61 | + /** @var IShareHelper */ |
|
| 62 | + protected $shareHelper; |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Listener constructor. |
|
| 66 | - * |
|
| 67 | - * @param IGroupManager $groupManager |
|
| 68 | - * @param IManager $activityManager |
|
| 69 | - * @param IUserSession $session |
|
| 70 | - * @param IConfig $config |
|
| 71 | - * @param ISystemTagManager $tagManager |
|
| 72 | - * @param IAppManager $appManager |
|
| 73 | - * @param IMountProviderCollection $mountCollection |
|
| 74 | - * @param IRootFolder $rootFolder |
|
| 75 | - * @param IShareHelper $shareHelper |
|
| 76 | - */ |
|
| 77 | - public function __construct(IGroupManager $groupManager, |
|
| 78 | - IManager $activityManager, |
|
| 79 | - IUserSession $session, |
|
| 80 | - IConfig $config, |
|
| 81 | - ISystemTagManager $tagManager, |
|
| 82 | - IAppManager $appManager, |
|
| 83 | - IMountProviderCollection $mountCollection, |
|
| 84 | - IRootFolder $rootFolder, |
|
| 85 | - IShareHelper $shareHelper) { |
|
| 86 | - $this->groupManager = $groupManager; |
|
| 87 | - $this->activityManager = $activityManager; |
|
| 88 | - $this->session = $session; |
|
| 89 | - $this->config = $config; |
|
| 90 | - $this->tagManager = $tagManager; |
|
| 91 | - $this->appManager = $appManager; |
|
| 92 | - $this->mountCollection = $mountCollection; |
|
| 93 | - $this->rootFolder = $rootFolder; |
|
| 94 | - $this->shareHelper = $shareHelper; |
|
| 95 | - } |
|
| 64 | + /** |
|
| 65 | + * Listener constructor. |
|
| 66 | + * |
|
| 67 | + * @param IGroupManager $groupManager |
|
| 68 | + * @param IManager $activityManager |
|
| 69 | + * @param IUserSession $session |
|
| 70 | + * @param IConfig $config |
|
| 71 | + * @param ISystemTagManager $tagManager |
|
| 72 | + * @param IAppManager $appManager |
|
| 73 | + * @param IMountProviderCollection $mountCollection |
|
| 74 | + * @param IRootFolder $rootFolder |
|
| 75 | + * @param IShareHelper $shareHelper |
|
| 76 | + */ |
|
| 77 | + public function __construct(IGroupManager $groupManager, |
|
| 78 | + IManager $activityManager, |
|
| 79 | + IUserSession $session, |
|
| 80 | + IConfig $config, |
|
| 81 | + ISystemTagManager $tagManager, |
|
| 82 | + IAppManager $appManager, |
|
| 83 | + IMountProviderCollection $mountCollection, |
|
| 84 | + IRootFolder $rootFolder, |
|
| 85 | + IShareHelper $shareHelper) { |
|
| 86 | + $this->groupManager = $groupManager; |
|
| 87 | + $this->activityManager = $activityManager; |
|
| 88 | + $this->session = $session; |
|
| 89 | + $this->config = $config; |
|
| 90 | + $this->tagManager = $tagManager; |
|
| 91 | + $this->appManager = $appManager; |
|
| 92 | + $this->mountCollection = $mountCollection; |
|
| 93 | + $this->rootFolder = $rootFolder; |
|
| 94 | + $this->shareHelper = $shareHelper; |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * @param ManagerEvent $event |
|
| 99 | - */ |
|
| 100 | - public function event(ManagerEvent $event) { |
|
| 101 | - $actor = $this->session->getUser(); |
|
| 102 | - if ($actor instanceof IUser) { |
|
| 103 | - $actor = $actor->getUID(); |
|
| 104 | - } else { |
|
| 105 | - $actor = ''; |
|
| 106 | - } |
|
| 107 | - $tag = $event->getTag(); |
|
| 97 | + /** |
|
| 98 | + * @param ManagerEvent $event |
|
| 99 | + */ |
|
| 100 | + public function event(ManagerEvent $event) { |
|
| 101 | + $actor = $this->session->getUser(); |
|
| 102 | + if ($actor instanceof IUser) { |
|
| 103 | + $actor = $actor->getUID(); |
|
| 104 | + } else { |
|
| 105 | + $actor = ''; |
|
| 106 | + } |
|
| 107 | + $tag = $event->getTag(); |
|
| 108 | 108 | |
| 109 | - $activity = $this->activityManager->generateEvent(); |
|
| 110 | - $activity->setApp('systemtags') |
|
| 111 | - ->setType('systemtags') |
|
| 112 | - ->setAuthor($actor) |
|
| 113 | - ->setObject('systemtag', (int)$tag->getId(), $tag->getName()); |
|
| 114 | - if ($event->getEvent() === ManagerEvent::EVENT_CREATE) { |
|
| 115 | - $activity->setSubject(Provider::CREATE_TAG, [ |
|
| 116 | - $actor, |
|
| 117 | - $this->prepareTagAsParameter($event->getTag()), |
|
| 118 | - ]); |
|
| 119 | - } else if ($event->getEvent() === ManagerEvent::EVENT_UPDATE) { |
|
| 120 | - $activity->setSubject(Provider::UPDATE_TAG, [ |
|
| 121 | - $actor, |
|
| 122 | - $this->prepareTagAsParameter($event->getTag()), |
|
| 123 | - $this->prepareTagAsParameter($event->getTagBefore()), |
|
| 124 | - ]); |
|
| 125 | - } else if ($event->getEvent() === ManagerEvent::EVENT_DELETE) { |
|
| 126 | - $activity->setSubject(Provider::DELETE_TAG, [ |
|
| 127 | - $actor, |
|
| 128 | - $this->prepareTagAsParameter($event->getTag()), |
|
| 129 | - ]); |
|
| 130 | - } else { |
|
| 131 | - return; |
|
| 132 | - } |
|
| 109 | + $activity = $this->activityManager->generateEvent(); |
|
| 110 | + $activity->setApp('systemtags') |
|
| 111 | + ->setType('systemtags') |
|
| 112 | + ->setAuthor($actor) |
|
| 113 | + ->setObject('systemtag', (int)$tag->getId(), $tag->getName()); |
|
| 114 | + if ($event->getEvent() === ManagerEvent::EVENT_CREATE) { |
|
| 115 | + $activity->setSubject(Provider::CREATE_TAG, [ |
|
| 116 | + $actor, |
|
| 117 | + $this->prepareTagAsParameter($event->getTag()), |
|
| 118 | + ]); |
|
| 119 | + } else if ($event->getEvent() === ManagerEvent::EVENT_UPDATE) { |
|
| 120 | + $activity->setSubject(Provider::UPDATE_TAG, [ |
|
| 121 | + $actor, |
|
| 122 | + $this->prepareTagAsParameter($event->getTag()), |
|
| 123 | + $this->prepareTagAsParameter($event->getTagBefore()), |
|
| 124 | + ]); |
|
| 125 | + } else if ($event->getEvent() === ManagerEvent::EVENT_DELETE) { |
|
| 126 | + $activity->setSubject(Provider::DELETE_TAG, [ |
|
| 127 | + $actor, |
|
| 128 | + $this->prepareTagAsParameter($event->getTag()), |
|
| 129 | + ]); |
|
| 130 | + } else { |
|
| 131 | + return; |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | - $group = $this->groupManager->get('admin'); |
|
| 135 | - if ($group instanceof IGroup) { |
|
| 136 | - foreach ($group->getUsers() as $user) { |
|
| 137 | - $activity->setAffectedUser($user->getUID()); |
|
| 138 | - $this->activityManager->publish($activity); |
|
| 139 | - } |
|
| 140 | - } |
|
| 134 | + $group = $this->groupManager->get('admin'); |
|
| 135 | + if ($group instanceof IGroup) { |
|
| 136 | + foreach ($group->getUsers() as $user) { |
|
| 137 | + $activity->setAffectedUser($user->getUID()); |
|
| 138 | + $this->activityManager->publish($activity); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | 142 | |
| 143 | - if ($actor !== '' && ($event->getEvent() === ManagerEvent::EVENT_CREATE || $event->getEvent() === ManagerEvent::EVENT_UPDATE)) { |
|
| 144 | - $this->updateLastUsedTags($actor, $event->getTag()); |
|
| 145 | - } |
|
| 146 | - } |
|
| 143 | + if ($actor !== '' && ($event->getEvent() === ManagerEvent::EVENT_CREATE || $event->getEvent() === ManagerEvent::EVENT_UPDATE)) { |
|
| 144 | + $this->updateLastUsedTags($actor, $event->getTag()); |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | - /** |
|
| 149 | - * @param MapperEvent $event |
|
| 150 | - */ |
|
| 151 | - public function mapperEvent(MapperEvent $event) { |
|
| 152 | - $tagIds = $event->getTags(); |
|
| 153 | - if ($event->getObjectType() !== 'files' ||empty($tagIds) |
|
| 154 | - || !in_array($event->getEvent(), [MapperEvent::EVENT_ASSIGN, MapperEvent::EVENT_UNASSIGN]) |
|
| 155 | - || !$this->appManager->isInstalled('activity')) { |
|
| 156 | - // System tags not for files, no tags, not (un-)assigning or no activity-app enabled (save the energy) |
|
| 157 | - return; |
|
| 158 | - } |
|
| 148 | + /** |
|
| 149 | + * @param MapperEvent $event |
|
| 150 | + */ |
|
| 151 | + public function mapperEvent(MapperEvent $event) { |
|
| 152 | + $tagIds = $event->getTags(); |
|
| 153 | + if ($event->getObjectType() !== 'files' ||empty($tagIds) |
|
| 154 | + || !in_array($event->getEvent(), [MapperEvent::EVENT_ASSIGN, MapperEvent::EVENT_UNASSIGN]) |
|
| 155 | + || !$this->appManager->isInstalled('activity')) { |
|
| 156 | + // System tags not for files, no tags, not (un-)assigning or no activity-app enabled (save the energy) |
|
| 157 | + return; |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - try { |
|
| 161 | - $tags = $this->tagManager->getTagsByIds($tagIds); |
|
| 162 | - } catch (TagNotFoundException $e) { |
|
| 163 | - // User assigned/unassigned a non-existing tag, ignore... |
|
| 164 | - return; |
|
| 165 | - } |
|
| 160 | + try { |
|
| 161 | + $tags = $this->tagManager->getTagsByIds($tagIds); |
|
| 162 | + } catch (TagNotFoundException $e) { |
|
| 163 | + // User assigned/unassigned a non-existing tag, ignore... |
|
| 164 | + return; |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - if (empty($tags)) { |
|
| 168 | - return; |
|
| 169 | - } |
|
| 167 | + if (empty($tags)) { |
|
| 168 | + return; |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - // Get all mount point owners |
|
| 172 | - $cache = $this->mountCollection->getMountCache(); |
|
| 173 | - $mounts = $cache->getMountsForFileId($event->getObjectId()); |
|
| 174 | - if (empty($mounts)) { |
|
| 175 | - return; |
|
| 176 | - } |
|
| 171 | + // Get all mount point owners |
|
| 172 | + $cache = $this->mountCollection->getMountCache(); |
|
| 173 | + $mounts = $cache->getMountsForFileId($event->getObjectId()); |
|
| 174 | + if (empty($mounts)) { |
|
| 175 | + return; |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | - $users = []; |
|
| 179 | - foreach ($mounts as $mount) { |
|
| 180 | - $owner = $mount->getUser()->getUID(); |
|
| 181 | - $ownerFolder = $this->rootFolder->getUserFolder($owner); |
|
| 182 | - $nodes = $ownerFolder->getById($event->getObjectId()); |
|
| 183 | - if (!empty($nodes)) { |
|
| 184 | - /** @var Node $node */ |
|
| 185 | - $node = array_shift($nodes); |
|
| 186 | - $al = $this->shareHelper->getPathsForAccessList($node); |
|
| 187 | - $users += $al['users']; |
|
| 188 | - } |
|
| 189 | - } |
|
| 178 | + $users = []; |
|
| 179 | + foreach ($mounts as $mount) { |
|
| 180 | + $owner = $mount->getUser()->getUID(); |
|
| 181 | + $ownerFolder = $this->rootFolder->getUserFolder($owner); |
|
| 182 | + $nodes = $ownerFolder->getById($event->getObjectId()); |
|
| 183 | + if (!empty($nodes)) { |
|
| 184 | + /** @var Node $node */ |
|
| 185 | + $node = array_shift($nodes); |
|
| 186 | + $al = $this->shareHelper->getPathsForAccessList($node); |
|
| 187 | + $users += $al['users']; |
|
| 188 | + } |
|
| 189 | + } |
|
| 190 | 190 | |
| 191 | - $actor = $this->session->getUser(); |
|
| 192 | - if ($actor instanceof IUser) { |
|
| 193 | - $actor = $actor->getUID(); |
|
| 194 | - } else { |
|
| 195 | - $actor = ''; |
|
| 196 | - } |
|
| 191 | + $actor = $this->session->getUser(); |
|
| 192 | + if ($actor instanceof IUser) { |
|
| 193 | + $actor = $actor->getUID(); |
|
| 194 | + } else { |
|
| 195 | + $actor = ''; |
|
| 196 | + } |
|
| 197 | 197 | |
| 198 | - $activity = $this->activityManager->generateEvent(); |
|
| 199 | - $activity->setApp('systemtags') |
|
| 200 | - ->setType('systemtags') |
|
| 201 | - ->setAuthor($actor) |
|
| 202 | - ->setObject($event->getObjectType(), (int) $event->getObjectId()); |
|
| 198 | + $activity = $this->activityManager->generateEvent(); |
|
| 199 | + $activity->setApp('systemtags') |
|
| 200 | + ->setType('systemtags') |
|
| 201 | + ->setAuthor($actor) |
|
| 202 | + ->setObject($event->getObjectType(), (int) $event->getObjectId()); |
|
| 203 | 203 | |
| 204 | - foreach ($users as $user => $path) { |
|
| 205 | - $user = (string)$user; // numerical ids could be ints which are not accepted everywhere |
|
| 206 | - $activity->setAffectedUser($user); |
|
| 204 | + foreach ($users as $user => $path) { |
|
| 205 | + $user = (string)$user; // numerical ids could be ints which are not accepted everywhere |
|
| 206 | + $activity->setAffectedUser($user); |
|
| 207 | 207 | |
| 208 | - foreach ($tags as $tag) { |
|
| 209 | - // don't publish activity for non-admins if tag is invisible |
|
| 210 | - if (!$tag->isUserVisible() && !$this->groupManager->isAdmin($user)) { |
|
| 211 | - continue; |
|
| 212 | - } |
|
| 213 | - if ($event->getEvent() === MapperEvent::EVENT_ASSIGN) { |
|
| 214 | - $activity->setSubject(Provider::ASSIGN_TAG, [ |
|
| 215 | - $actor, |
|
| 216 | - $path, |
|
| 217 | - $this->prepareTagAsParameter($tag), |
|
| 218 | - ]); |
|
| 219 | - } else if ($event->getEvent() === MapperEvent::EVENT_UNASSIGN) { |
|
| 220 | - $activity->setSubject(Provider::UNASSIGN_TAG, [ |
|
| 221 | - $actor, |
|
| 222 | - $path, |
|
| 223 | - $this->prepareTagAsParameter($tag), |
|
| 224 | - ]); |
|
| 225 | - } |
|
| 208 | + foreach ($tags as $tag) { |
|
| 209 | + // don't publish activity for non-admins if tag is invisible |
|
| 210 | + if (!$tag->isUserVisible() && !$this->groupManager->isAdmin($user)) { |
|
| 211 | + continue; |
|
| 212 | + } |
|
| 213 | + if ($event->getEvent() === MapperEvent::EVENT_ASSIGN) { |
|
| 214 | + $activity->setSubject(Provider::ASSIGN_TAG, [ |
|
| 215 | + $actor, |
|
| 216 | + $path, |
|
| 217 | + $this->prepareTagAsParameter($tag), |
|
| 218 | + ]); |
|
| 219 | + } else if ($event->getEvent() === MapperEvent::EVENT_UNASSIGN) { |
|
| 220 | + $activity->setSubject(Provider::UNASSIGN_TAG, [ |
|
| 221 | + $actor, |
|
| 222 | + $path, |
|
| 223 | + $this->prepareTagAsParameter($tag), |
|
| 224 | + ]); |
|
| 225 | + } |
|
| 226 | 226 | |
| 227 | - $this->activityManager->publish($activity); |
|
| 228 | - } |
|
| 229 | - } |
|
| 227 | + $this->activityManager->publish($activity); |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | 230 | |
| 231 | - if ($actor !== '' && $event->getEvent() === MapperEvent::EVENT_ASSIGN) { |
|
| 232 | - foreach ($tags as $tag) { |
|
| 233 | - $this->updateLastUsedTags($actor, $tag); |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - } |
|
| 231 | + if ($actor !== '' && $event->getEvent() === MapperEvent::EVENT_ASSIGN) { |
|
| 232 | + foreach ($tags as $tag) { |
|
| 233 | + $this->updateLastUsedTags($actor, $tag); |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + } |
|
| 237 | 237 | |
| 238 | - /** |
|
| 239 | - * @param string $actor |
|
| 240 | - * @param ISystemTag $tag |
|
| 241 | - */ |
|
| 242 | - protected function updateLastUsedTags($actor, ISystemTag $tag) { |
|
| 243 | - $lastUsedTags = $this->config->getUserValue($actor, 'systemtags', 'last_used', '[]'); |
|
| 244 | - $lastUsedTags = json_decode($lastUsedTags, true); |
|
| 238 | + /** |
|
| 239 | + * @param string $actor |
|
| 240 | + * @param ISystemTag $tag |
|
| 241 | + */ |
|
| 242 | + protected function updateLastUsedTags($actor, ISystemTag $tag) { |
|
| 243 | + $lastUsedTags = $this->config->getUserValue($actor, 'systemtags', 'last_used', '[]'); |
|
| 244 | + $lastUsedTags = json_decode($lastUsedTags, true); |
|
| 245 | 245 | |
| 246 | - array_unshift($lastUsedTags, $tag->getId()); |
|
| 247 | - array_unique($lastUsedTags); |
|
| 248 | - $lastUsedTags = array_slice($lastUsedTags, 0, 10); |
|
| 246 | + array_unshift($lastUsedTags, $tag->getId()); |
|
| 247 | + array_unique($lastUsedTags); |
|
| 248 | + $lastUsedTags = array_slice($lastUsedTags, 0, 10); |
|
| 249 | 249 | |
| 250 | - $this->config->setUserValue($actor, 'systemtags', 'last_used', json_encode($lastUsedTags)); |
|
| 251 | - } |
|
| 250 | + $this->config->setUserValue($actor, 'systemtags', 'last_used', json_encode($lastUsedTags)); |
|
| 251 | + } |
|
| 252 | 252 | |
| 253 | - /** |
|
| 254 | - * @param ISystemTag $tag |
|
| 255 | - * @return string |
|
| 256 | - */ |
|
| 257 | - protected function prepareTagAsParameter(ISystemTag $tag) { |
|
| 258 | - return json_encode([ |
|
| 259 | - 'id' => $tag->getId(), |
|
| 260 | - 'name' => $tag->getName(), |
|
| 261 | - 'assignable' => $tag->isUserAssignable(), |
|
| 262 | - 'visible' => $tag->isUserVisible(), |
|
| 263 | - ]); |
|
| 264 | - } |
|
| 253 | + /** |
|
| 254 | + * @param ISystemTag $tag |
|
| 255 | + * @return string |
|
| 256 | + */ |
|
| 257 | + protected function prepareTagAsParameter(ISystemTag $tag) { |
|
| 258 | + return json_encode([ |
|
| 259 | + 'id' => $tag->getId(), |
|
| 260 | + 'name' => $tag->getName(), |
|
| 261 | + 'assignable' => $tag->isUserAssignable(), |
|
| 262 | + 'visible' => $tag->isUserVisible(), |
|
| 263 | + ]); |
|
| 264 | + } |
|
| 265 | 265 | } |
@@ -29,53 +29,53 @@ |
||
| 29 | 29 | |
| 30 | 30 | abstract class Session implements \ArrayAccess, ISession { |
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * @var bool |
|
| 34 | - */ |
|
| 35 | - protected $sessionClosed = false; |
|
| 32 | + /** |
|
| 33 | + * @var bool |
|
| 34 | + */ |
|
| 35 | + protected $sessionClosed = false; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * $name serves as a namespace for the session keys |
|
| 39 | - * |
|
| 40 | - * @param string $name |
|
| 41 | - */ |
|
| 42 | - abstract public function __construct(string $name); |
|
| 37 | + /** |
|
| 38 | + * $name serves as a namespace for the session keys |
|
| 39 | + * |
|
| 40 | + * @param string $name |
|
| 41 | + */ |
|
| 42 | + abstract public function __construct(string $name); |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * @param mixed $offset |
|
| 46 | - * @return bool |
|
| 47 | - */ |
|
| 48 | - public function offsetExists($offset): bool { |
|
| 49 | - return $this->exists($offset); |
|
| 50 | - } |
|
| 44 | + /** |
|
| 45 | + * @param mixed $offset |
|
| 46 | + * @return bool |
|
| 47 | + */ |
|
| 48 | + public function offsetExists($offset): bool { |
|
| 49 | + return $this->exists($offset); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * @param mixed $offset |
|
| 54 | - * @return mixed |
|
| 55 | - */ |
|
| 56 | - public function offsetGet($offset) { |
|
| 57 | - return $this->get($offset); |
|
| 58 | - } |
|
| 52 | + /** |
|
| 53 | + * @param mixed $offset |
|
| 54 | + * @return mixed |
|
| 55 | + */ |
|
| 56 | + public function offsetGet($offset) { |
|
| 57 | + return $this->get($offset); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * @param mixed $offset |
|
| 62 | - * @param mixed $value |
|
| 63 | - */ |
|
| 64 | - public function offsetSet($offset, $value) { |
|
| 65 | - $this->set($offset, $value); |
|
| 66 | - } |
|
| 60 | + /** |
|
| 61 | + * @param mixed $offset |
|
| 62 | + * @param mixed $value |
|
| 63 | + */ |
|
| 64 | + public function offsetSet($offset, $value) { |
|
| 65 | + $this->set($offset, $value); |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * @param mixed $offset |
|
| 70 | - */ |
|
| 71 | - public function offsetUnset($offset) { |
|
| 72 | - $this->remove($offset); |
|
| 73 | - } |
|
| 68 | + /** |
|
| 69 | + * @param mixed $offset |
|
| 70 | + */ |
|
| 71 | + public function offsetUnset($offset) { |
|
| 72 | + $this->remove($offset); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Close the session and release the lock |
|
| 77 | - */ |
|
| 78 | - public function close() { |
|
| 79 | - $this->sessionClosed = true; |
|
| 80 | - } |
|
| 75 | + /** |
|
| 76 | + * Close the session and release the lock |
|
| 77 | + */ |
|
| 78 | + public function close() { |
|
| 79 | + $this->sessionClosed = true; |
|
| 80 | + } |
|
| 81 | 81 | } |
@@ -38,240 +38,240 @@ |
||
| 38 | 38 | use Symfony\Component\Console\Output\OutputInterface; |
| 39 | 39 | |
| 40 | 40 | class ListCommand extends Base { |
| 41 | - /** |
|
| 42 | - * @var GlobalStoragesService |
|
| 43 | - */ |
|
| 44 | - protected $globalService; |
|
| 41 | + /** |
|
| 42 | + * @var GlobalStoragesService |
|
| 43 | + */ |
|
| 44 | + protected $globalService; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * @var UserStoragesService |
|
| 48 | - */ |
|
| 49 | - protected $userService; |
|
| 46 | + /** |
|
| 47 | + * @var UserStoragesService |
|
| 48 | + */ |
|
| 49 | + protected $userService; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @var IUserSession |
|
| 53 | - */ |
|
| 54 | - protected $userSession; |
|
| 51 | + /** |
|
| 52 | + * @var IUserSession |
|
| 53 | + */ |
|
| 54 | + protected $userSession; |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * @var IUserManager |
|
| 58 | - */ |
|
| 59 | - protected $userManager; |
|
| 56 | + /** |
|
| 57 | + * @var IUserManager |
|
| 58 | + */ |
|
| 59 | + protected $userManager; |
|
| 60 | 60 | |
| 61 | - const ALL = -1; |
|
| 61 | + const ALL = -1; |
|
| 62 | 62 | |
| 63 | - function __construct(GlobalStoragesService $globalService, UserStoragesService $userService, IUserSession $userSession, IUserManager $userManager) { |
|
| 64 | - parent::__construct(); |
|
| 65 | - $this->globalService = $globalService; |
|
| 66 | - $this->userService = $userService; |
|
| 67 | - $this->userSession = $userSession; |
|
| 68 | - $this->userManager = $userManager; |
|
| 69 | - } |
|
| 63 | + function __construct(GlobalStoragesService $globalService, UserStoragesService $userService, IUserSession $userSession, IUserManager $userManager) { |
|
| 64 | + parent::__construct(); |
|
| 65 | + $this->globalService = $globalService; |
|
| 66 | + $this->userService = $userService; |
|
| 67 | + $this->userSession = $userSession; |
|
| 68 | + $this->userManager = $userManager; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - protected function configure() { |
|
| 72 | - $this |
|
| 73 | - ->setName('files_external:list') |
|
| 74 | - ->setDescription('List configured admin or personal mounts') |
|
| 75 | - ->addArgument( |
|
| 76 | - 'user_id', |
|
| 77 | - InputArgument::OPTIONAL, |
|
| 78 | - 'user id to list the personal mounts for, if no user is provided admin mounts will be listed' |
|
| 79 | - )->addOption( |
|
| 80 | - 'show-password', |
|
| 81 | - '', |
|
| 82 | - InputOption::VALUE_NONE, |
|
| 83 | - 'show passwords and secrets' |
|
| 84 | - )->addOption( |
|
| 85 | - 'full', |
|
| 86 | - null, |
|
| 87 | - InputOption::VALUE_NONE, |
|
| 88 | - 'don\'t truncate long values in table output' |
|
| 89 | - )->addOption( |
|
| 90 | - 'all', |
|
| 91 | - 'a', |
|
| 92 | - InputOption::VALUE_NONE, |
|
| 93 | - 'show both system wide mounts and all personal mounts' |
|
| 94 | - ); |
|
| 95 | - parent::configure(); |
|
| 96 | - } |
|
| 71 | + protected function configure() { |
|
| 72 | + $this |
|
| 73 | + ->setName('files_external:list') |
|
| 74 | + ->setDescription('List configured admin or personal mounts') |
|
| 75 | + ->addArgument( |
|
| 76 | + 'user_id', |
|
| 77 | + InputArgument::OPTIONAL, |
|
| 78 | + 'user id to list the personal mounts for, if no user is provided admin mounts will be listed' |
|
| 79 | + )->addOption( |
|
| 80 | + 'show-password', |
|
| 81 | + '', |
|
| 82 | + InputOption::VALUE_NONE, |
|
| 83 | + 'show passwords and secrets' |
|
| 84 | + )->addOption( |
|
| 85 | + 'full', |
|
| 86 | + null, |
|
| 87 | + InputOption::VALUE_NONE, |
|
| 88 | + 'don\'t truncate long values in table output' |
|
| 89 | + )->addOption( |
|
| 90 | + 'all', |
|
| 91 | + 'a', |
|
| 92 | + InputOption::VALUE_NONE, |
|
| 93 | + 'show both system wide mounts and all personal mounts' |
|
| 94 | + ); |
|
| 95 | + parent::configure(); |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 99 | - if ($input->getOption('all')) { |
|
| 100 | - /** @var $mounts StorageConfig[] */ |
|
| 101 | - $mounts = $this->globalService->getStorageForAllUsers(); |
|
| 102 | - $userId = self::ALL; |
|
| 103 | - } else { |
|
| 104 | - $userId = $input->getArgument('user_id'); |
|
| 105 | - $storageService = $this->getStorageService($userId); |
|
| 98 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 99 | + if ($input->getOption('all')) { |
|
| 100 | + /** @var $mounts StorageConfig[] */ |
|
| 101 | + $mounts = $this->globalService->getStorageForAllUsers(); |
|
| 102 | + $userId = self::ALL; |
|
| 103 | + } else { |
|
| 104 | + $userId = $input->getArgument('user_id'); |
|
| 105 | + $storageService = $this->getStorageService($userId); |
|
| 106 | 106 | |
| 107 | - /** @var $mounts StorageConfig[] */ |
|
| 108 | - $mounts = $storageService->getAllStorages(); |
|
| 109 | - } |
|
| 107 | + /** @var $mounts StorageConfig[] */ |
|
| 108 | + $mounts = $storageService->getAllStorages(); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - $this->listMounts($userId, $mounts, $input, $output); |
|
| 112 | - } |
|
| 111 | + $this->listMounts($userId, $mounts, $input, $output); |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * @param $userId $userId |
|
| 116 | - * @param StorageConfig[] $mounts |
|
| 117 | - * @param InputInterface $input |
|
| 118 | - * @param OutputInterface $output |
|
| 119 | - */ |
|
| 120 | - public function listMounts($userId, array $mounts, InputInterface $input, OutputInterface $output) { |
|
| 121 | - $outputType = $input->getOption('output'); |
|
| 122 | - if (count($mounts) === 0) { |
|
| 123 | - if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) { |
|
| 124 | - $output->writeln('[]'); |
|
| 125 | - } else { |
|
| 126 | - if ($userId === self::ALL) { |
|
| 127 | - $output->writeln("<info>No mounts configured</info>"); |
|
| 128 | - } else if ($userId) { |
|
| 129 | - $output->writeln("<info>No mounts configured by $userId</info>"); |
|
| 130 | - } else { |
|
| 131 | - $output->writeln("<info>No admin mounts configured</info>"); |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - return; |
|
| 135 | - } |
|
| 114 | + /** |
|
| 115 | + * @param $userId $userId |
|
| 116 | + * @param StorageConfig[] $mounts |
|
| 117 | + * @param InputInterface $input |
|
| 118 | + * @param OutputInterface $output |
|
| 119 | + */ |
|
| 120 | + public function listMounts($userId, array $mounts, InputInterface $input, OutputInterface $output) { |
|
| 121 | + $outputType = $input->getOption('output'); |
|
| 122 | + if (count($mounts) === 0) { |
|
| 123 | + if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) { |
|
| 124 | + $output->writeln('[]'); |
|
| 125 | + } else { |
|
| 126 | + if ($userId === self::ALL) { |
|
| 127 | + $output->writeln("<info>No mounts configured</info>"); |
|
| 128 | + } else if ($userId) { |
|
| 129 | + $output->writeln("<info>No mounts configured by $userId</info>"); |
|
| 130 | + } else { |
|
| 131 | + $output->writeln("<info>No admin mounts configured</info>"); |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + return; |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - $headers = ['Mount ID', 'Mount Point', 'Storage', 'Authentication Type', 'Configuration', 'Options']; |
|
| 137 | + $headers = ['Mount ID', 'Mount Point', 'Storage', 'Authentication Type', 'Configuration', 'Options']; |
|
| 138 | 138 | |
| 139 | - if (!$userId || $userId === self::ALL) { |
|
| 140 | - $headers[] = 'Applicable Users'; |
|
| 141 | - $headers[] = 'Applicable Groups'; |
|
| 142 | - } |
|
| 143 | - if ($userId === self::ALL) { |
|
| 144 | - $headers[] = 'Type'; |
|
| 145 | - } |
|
| 139 | + if (!$userId || $userId === self::ALL) { |
|
| 140 | + $headers[] = 'Applicable Users'; |
|
| 141 | + $headers[] = 'Applicable Groups'; |
|
| 142 | + } |
|
| 143 | + if ($userId === self::ALL) { |
|
| 144 | + $headers[] = 'Type'; |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - if (!$input->getOption('show-password')) { |
|
| 148 | - $hideKeys = ['password', 'refresh_token', 'token', 'client_secret', 'public_key', 'private_key']; |
|
| 149 | - foreach ($mounts as $mount) { |
|
| 150 | - $config = $mount->getBackendOptions(); |
|
| 151 | - foreach ($config as $key => $value) { |
|
| 152 | - if (in_array($key, $hideKeys)) { |
|
| 153 | - $mount->setBackendOption($key, '***'); |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - } |
|
| 147 | + if (!$input->getOption('show-password')) { |
|
| 148 | + $hideKeys = ['password', 'refresh_token', 'token', 'client_secret', 'public_key', 'private_key']; |
|
| 149 | + foreach ($mounts as $mount) { |
|
| 150 | + $config = $mount->getBackendOptions(); |
|
| 151 | + foreach ($config as $key => $value) { |
|
| 152 | + if (in_array($key, $hideKeys)) { |
|
| 153 | + $mount->setBackendOption($key, '***'); |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) { |
|
| 160 | - $keys = array_map(function ($header) { |
|
| 161 | - return strtolower(str_replace(' ', '_', $header)); |
|
| 162 | - }, $headers); |
|
| 159 | + if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) { |
|
| 160 | + $keys = array_map(function ($header) { |
|
| 161 | + return strtolower(str_replace(' ', '_', $header)); |
|
| 162 | + }, $headers); |
|
| 163 | 163 | |
| 164 | - $pairs = array_map(function (StorageConfig $config) use ($keys, $userId) { |
|
| 165 | - $values = [ |
|
| 166 | - $config->getId(), |
|
| 167 | - $config->getMountPoint(), |
|
| 168 | - $config->getBackend()->getStorageClass(), |
|
| 169 | - $config->getAuthMechanism()->getIdentifier(), |
|
| 170 | - $config->getBackendOptions(), |
|
| 171 | - $config->getMountOptions() |
|
| 172 | - ]; |
|
| 173 | - if (!$userId || $userId === self::ALL) { |
|
| 174 | - $values[] = $config->getApplicableUsers(); |
|
| 175 | - $values[] = $config->getApplicableGroups(); |
|
| 176 | - } |
|
| 177 | - if ($userId === self::ALL) { |
|
| 178 | - $values[] = $config->getType() === StorageConfig::MOUNT_TYPE_ADMIN ? 'admin' : 'personal'; |
|
| 179 | - } |
|
| 164 | + $pairs = array_map(function (StorageConfig $config) use ($keys, $userId) { |
|
| 165 | + $values = [ |
|
| 166 | + $config->getId(), |
|
| 167 | + $config->getMountPoint(), |
|
| 168 | + $config->getBackend()->getStorageClass(), |
|
| 169 | + $config->getAuthMechanism()->getIdentifier(), |
|
| 170 | + $config->getBackendOptions(), |
|
| 171 | + $config->getMountOptions() |
|
| 172 | + ]; |
|
| 173 | + if (!$userId || $userId === self::ALL) { |
|
| 174 | + $values[] = $config->getApplicableUsers(); |
|
| 175 | + $values[] = $config->getApplicableGroups(); |
|
| 176 | + } |
|
| 177 | + if ($userId === self::ALL) { |
|
| 178 | + $values[] = $config->getType() === StorageConfig::MOUNT_TYPE_ADMIN ? 'admin' : 'personal'; |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | - return array_combine($keys, $values); |
|
| 182 | - }, $mounts); |
|
| 183 | - if ($outputType === self::OUTPUT_FORMAT_JSON) { |
|
| 184 | - $output->writeln(json_encode(array_values($pairs))); |
|
| 185 | - } else { |
|
| 186 | - $output->writeln(json_encode(array_values($pairs), JSON_PRETTY_PRINT)); |
|
| 187 | - } |
|
| 188 | - } else { |
|
| 189 | - $full = $input->getOption('full'); |
|
| 190 | - $defaultMountOptions = [ |
|
| 191 | - 'encrypt' => true, |
|
| 192 | - 'previews' => true, |
|
| 193 | - 'filesystem_check_changes' => 1, |
|
| 194 | - 'enable_sharing' => false, |
|
| 195 | - 'encoding_compatibility' => false, |
|
| 196 | - 'readonly' => false, |
|
| 197 | - ]; |
|
| 198 | - $rows = array_map(function (StorageConfig $config) use ($userId, $defaultMountOptions, $full) { |
|
| 199 | - $storageConfig = $config->getBackendOptions(); |
|
| 200 | - $keys = array_keys($storageConfig); |
|
| 201 | - $values = array_values($storageConfig); |
|
| 181 | + return array_combine($keys, $values); |
|
| 182 | + }, $mounts); |
|
| 183 | + if ($outputType === self::OUTPUT_FORMAT_JSON) { |
|
| 184 | + $output->writeln(json_encode(array_values($pairs))); |
|
| 185 | + } else { |
|
| 186 | + $output->writeln(json_encode(array_values($pairs), JSON_PRETTY_PRINT)); |
|
| 187 | + } |
|
| 188 | + } else { |
|
| 189 | + $full = $input->getOption('full'); |
|
| 190 | + $defaultMountOptions = [ |
|
| 191 | + 'encrypt' => true, |
|
| 192 | + 'previews' => true, |
|
| 193 | + 'filesystem_check_changes' => 1, |
|
| 194 | + 'enable_sharing' => false, |
|
| 195 | + 'encoding_compatibility' => false, |
|
| 196 | + 'readonly' => false, |
|
| 197 | + ]; |
|
| 198 | + $rows = array_map(function (StorageConfig $config) use ($userId, $defaultMountOptions, $full) { |
|
| 199 | + $storageConfig = $config->getBackendOptions(); |
|
| 200 | + $keys = array_keys($storageConfig); |
|
| 201 | + $values = array_values($storageConfig); |
|
| 202 | 202 | |
| 203 | - if (!$full) { |
|
| 204 | - $values = array_map(function ($value) { |
|
| 205 | - if (is_string($value) && strlen($value) > 32) { |
|
| 206 | - return substr($value, 0, 6) . '...' . substr($value, -6, 6); |
|
| 207 | - } else { |
|
| 208 | - return $value; |
|
| 209 | - } |
|
| 210 | - }, $values); |
|
| 211 | - } |
|
| 203 | + if (!$full) { |
|
| 204 | + $values = array_map(function ($value) { |
|
| 205 | + if (is_string($value) && strlen($value) > 32) { |
|
| 206 | + return substr($value, 0, 6) . '...' . substr($value, -6, 6); |
|
| 207 | + } else { |
|
| 208 | + return $value; |
|
| 209 | + } |
|
| 210 | + }, $values); |
|
| 211 | + } |
|
| 212 | 212 | |
| 213 | - $configStrings = array_map(function ($key, $value) { |
|
| 214 | - return $key . ': ' . json_encode($value); |
|
| 215 | - }, $keys, $values); |
|
| 216 | - $configString = implode(', ', $configStrings); |
|
| 213 | + $configStrings = array_map(function ($key, $value) { |
|
| 214 | + return $key . ': ' . json_encode($value); |
|
| 215 | + }, $keys, $values); |
|
| 216 | + $configString = implode(', ', $configStrings); |
|
| 217 | 217 | |
| 218 | - $mountOptions = $config->getMountOptions(); |
|
| 219 | - // hide defaults |
|
| 220 | - foreach ($mountOptions as $key => $value) { |
|
| 221 | - if ($value === $defaultMountOptions[$key]) { |
|
| 222 | - unset($mountOptions[$key]); |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - $keys = array_keys($mountOptions); |
|
| 226 | - $values = array_values($mountOptions); |
|
| 218 | + $mountOptions = $config->getMountOptions(); |
|
| 219 | + // hide defaults |
|
| 220 | + foreach ($mountOptions as $key => $value) { |
|
| 221 | + if ($value === $defaultMountOptions[$key]) { |
|
| 222 | + unset($mountOptions[$key]); |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + $keys = array_keys($mountOptions); |
|
| 226 | + $values = array_values($mountOptions); |
|
| 227 | 227 | |
| 228 | - $optionsStrings = array_map(function ($key, $value) { |
|
| 229 | - return $key . ': ' . json_encode($value); |
|
| 230 | - }, $keys, $values); |
|
| 231 | - $optionsString = implode(', ', $optionsStrings); |
|
| 228 | + $optionsStrings = array_map(function ($key, $value) { |
|
| 229 | + return $key . ': ' . json_encode($value); |
|
| 230 | + }, $keys, $values); |
|
| 231 | + $optionsString = implode(', ', $optionsStrings); |
|
| 232 | 232 | |
| 233 | - $values = [ |
|
| 234 | - $config->getId(), |
|
| 235 | - $config->getMountPoint(), |
|
| 236 | - $config->getBackend()->getText(), |
|
| 237 | - $config->getAuthMechanism()->getText(), |
|
| 238 | - $configString, |
|
| 239 | - $optionsString |
|
| 240 | - ]; |
|
| 233 | + $values = [ |
|
| 234 | + $config->getId(), |
|
| 235 | + $config->getMountPoint(), |
|
| 236 | + $config->getBackend()->getText(), |
|
| 237 | + $config->getAuthMechanism()->getText(), |
|
| 238 | + $configString, |
|
| 239 | + $optionsString |
|
| 240 | + ]; |
|
| 241 | 241 | |
| 242 | - if (!$userId || $userId === self::ALL) { |
|
| 243 | - $applicableUsers = implode(', ', $config->getApplicableUsers()); |
|
| 244 | - $applicableGroups = implode(', ', $config->getApplicableGroups()); |
|
| 245 | - if ($applicableUsers === '' && $applicableGroups === '') { |
|
| 246 | - $applicableUsers = 'All'; |
|
| 247 | - } |
|
| 248 | - $values[] = $applicableUsers; |
|
| 249 | - $values[] = $applicableGroups; |
|
| 250 | - } |
|
| 251 | - if ($userId === self::ALL) { |
|
| 252 | - $values[] = $config->getType() === StorageConfig::MOUNT_TYPE_ADMIN ? 'Admin' : 'Personal'; |
|
| 253 | - } |
|
| 242 | + if (!$userId || $userId === self::ALL) { |
|
| 243 | + $applicableUsers = implode(', ', $config->getApplicableUsers()); |
|
| 244 | + $applicableGroups = implode(', ', $config->getApplicableGroups()); |
|
| 245 | + if ($applicableUsers === '' && $applicableGroups === '') { |
|
| 246 | + $applicableUsers = 'All'; |
|
| 247 | + } |
|
| 248 | + $values[] = $applicableUsers; |
|
| 249 | + $values[] = $applicableGroups; |
|
| 250 | + } |
|
| 251 | + if ($userId === self::ALL) { |
|
| 252 | + $values[] = $config->getType() === StorageConfig::MOUNT_TYPE_ADMIN ? 'Admin' : 'Personal'; |
|
| 253 | + } |
|
| 254 | 254 | |
| 255 | - return $values; |
|
| 256 | - }, $mounts); |
|
| 255 | + return $values; |
|
| 256 | + }, $mounts); |
|
| 257 | 257 | |
| 258 | - $table = new Table($output); |
|
| 259 | - $table->setHeaders($headers); |
|
| 260 | - $table->setRows($rows); |
|
| 261 | - $table->render(); |
|
| 262 | - } |
|
| 263 | - } |
|
| 258 | + $table = new Table($output); |
|
| 259 | + $table->setHeaders($headers); |
|
| 260 | + $table->setRows($rows); |
|
| 261 | + $table->render(); |
|
| 262 | + } |
|
| 263 | + } |
|
| 264 | 264 | |
| 265 | - protected function getStorageService($userId) { |
|
| 266 | - if (!empty($userId)) { |
|
| 267 | - $user = $this->userManager->get($userId); |
|
| 268 | - if (is_null($user)) { |
|
| 269 | - throw new NoUserException("user $userId not found"); |
|
| 270 | - } |
|
| 271 | - $this->userSession->setUser($user); |
|
| 272 | - return $this->userService; |
|
| 273 | - } else { |
|
| 274 | - return $this->globalService; |
|
| 275 | - } |
|
| 276 | - } |
|
| 265 | + protected function getStorageService($userId) { |
|
| 266 | + if (!empty($userId)) { |
|
| 267 | + $user = $this->userManager->get($userId); |
|
| 268 | + if (is_null($user)) { |
|
| 269 | + throw new NoUserException("user $userId not found"); |
|
| 270 | + } |
|
| 271 | + $this->userSession->setUser($user); |
|
| 272 | + return $this->userService; |
|
| 273 | + } else { |
|
| 274 | + return $this->globalService; |
|
| 275 | + } |
|
| 276 | + } |
|
| 277 | 277 | } |
@@ -65,237 +65,237 @@ |
||
| 65 | 65 | * @since 6.0.0 |
| 66 | 66 | */ |
| 67 | 67 | interface IRequest { |
| 68 | - /** |
|
| 69 | - * @since 9.1.0 |
|
| 70 | - */ |
|
| 71 | - const USER_AGENT_CLIENT_ANDROID = '/^Mozilla\/5\.0 \(Android\) (ownCloud|Nextcloud)\-android.*$/'; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @since 13.0.0 |
|
| 75 | - */ |
|
| 76 | - const USER_AGENT_TALK_ANDROID = '/^Mozilla\/5\.0 \(Android\) Nextcloud\-Talk v.*$/'; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @since 9.1.0 |
|
| 80 | - */ |
|
| 81 | - const USER_AGENT_CLIENT_DESKTOP = '/^Mozilla\/5\.0 \([A-Za-z ]+\) (mirall|csyncoC)\/.*$/'; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * @since 9.1.0 |
|
| 85 | - */ |
|
| 86 | - const USER_AGENT_CLIENT_IOS = '/^Mozilla\/5\.0 \(iOS\) (ownCloud|Nextcloud)\-iOS.*$/'; |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * @since 13.0.0 |
|
| 90 | - */ |
|
| 91 | - const USER_AGENT_TALK_IOS = '/^Mozilla\/5\.0 \(iOS\) Nextcloud\-Talk v.*$/'; |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * @since 13.0.1 |
|
| 95 | - */ |
|
| 96 | - const USER_AGENT_OUTLOOK_ADDON = '/^Mozilla\/5\.0 \([A-Za-z ]+\) Nextcloud\-Outlook v.*$/'; |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * @since 13.0.1 |
|
| 100 | - */ |
|
| 101 | - const USER_AGENT_THUNDERBIRD_ADDON = '/^Mozilla\/5\.0 \([A-Za-z ]+\) Nextcloud\-Thunderbird v.*$/'; |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @param string $name |
|
| 105 | - * |
|
| 106 | - * @return string |
|
| 107 | - * @since 6.0.0 |
|
| 108 | - */ |
|
| 109 | - public function getHeader(string $name): string; |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Lets you access post and get parameters by the index |
|
| 113 | - * In case of json requests the encoded json body is accessed |
|
| 114 | - * |
|
| 115 | - * @param string $key the key which you want to access in the URL Parameter |
|
| 116 | - * placeholder, $_POST or $_GET array. |
|
| 117 | - * The priority how they're returned is the following: |
|
| 118 | - * 1. URL parameters |
|
| 119 | - * 2. POST parameters |
|
| 120 | - * 3. GET parameters |
|
| 121 | - * @param mixed $default If the key is not found, this value will be returned |
|
| 122 | - * @return mixed the content of the array |
|
| 123 | - * @since 6.0.0 |
|
| 124 | - */ |
|
| 125 | - public function getParam(string $key, $default = null); |
|
| 126 | - |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Returns all params that were received, be it from the request |
|
| 130 | - * |
|
| 131 | - * (as GET or POST) or through the URL by the route |
|
| 132 | - * |
|
| 133 | - * @return array the array with all parameters |
|
| 134 | - * @since 6.0.0 |
|
| 135 | - */ |
|
| 136 | - public function getParams(): array; |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Returns the method of the request |
|
| 140 | - * |
|
| 141 | - * @return string the method of the request (POST, GET, etc) |
|
| 142 | - * @since 6.0.0 |
|
| 143 | - */ |
|
| 144 | - public function getMethod(): string; |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Shortcut for accessing an uploaded file through the $_FILES array |
|
| 148 | - * |
|
| 149 | - * @param string $key the key that will be taken from the $_FILES array |
|
| 150 | - * @return array the file in the $_FILES element |
|
| 151 | - * @since 6.0.0 |
|
| 152 | - */ |
|
| 153 | - public function getUploadedFile(string $key); |
|
| 154 | - |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Shortcut for getting env variables |
|
| 158 | - * |
|
| 159 | - * @param string $key the key that will be taken from the $_ENV array |
|
| 160 | - * @return array the value in the $_ENV element |
|
| 161 | - * @since 6.0.0 |
|
| 162 | - */ |
|
| 163 | - public function getEnv(string $key); |
|
| 164 | - |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Shortcut for getting cookie variables |
|
| 168 | - * |
|
| 169 | - * @param string $key the key that will be taken from the $_COOKIE array |
|
| 170 | - * @return string|null the value in the $_COOKIE element |
|
| 171 | - * @since 6.0.0 |
|
| 172 | - */ |
|
| 173 | - public function getCookie(string $key); |
|
| 174 | - |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Checks if the CSRF check was correct |
|
| 178 | - * |
|
| 179 | - * @return bool true if CSRF check passed |
|
| 180 | - * @since 6.0.0 |
|
| 181 | - */ |
|
| 182 | - public function passesCSRFCheck(): bool; |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Checks if the strict cookie has been sent with the request if the request |
|
| 186 | - * is including any cookies. |
|
| 187 | - * |
|
| 188 | - * @return bool |
|
| 189 | - * @since 9.0.0 |
|
| 190 | - */ |
|
| 191 | - public function passesStrictCookieCheck(): bool; |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Checks if the lax cookie has been sent with the request if the request |
|
| 195 | - * is including any cookies. |
|
| 196 | - * |
|
| 197 | - * @return bool |
|
| 198 | - * @since 9.0.0 |
|
| 199 | - */ |
|
| 200 | - public function passesLaxCookieCheck(): bool; |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Returns an ID for the request, value is not guaranteed to be unique and is mostly meant for logging |
|
| 204 | - * If `mod_unique_id` is installed this value will be taken. |
|
| 205 | - * |
|
| 206 | - * @return string |
|
| 207 | - * @since 8.1.0 |
|
| 208 | - */ |
|
| 209 | - public function getId(): string; |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * Returns the remote address, if the connection came from a trusted proxy |
|
| 213 | - * and `forwarded_for_headers` has been configured then the IP address |
|
| 214 | - * specified in this header will be returned instead. |
|
| 215 | - * Do always use this instead of $_SERVER['REMOTE_ADDR'] |
|
| 216 | - * |
|
| 217 | - * @return string IP address |
|
| 218 | - * @since 8.1.0 |
|
| 219 | - */ |
|
| 220 | - public function getRemoteAddress(): string; |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Returns the server protocol. It respects reverse proxy servers and load |
|
| 224 | - * balancers. |
|
| 225 | - * |
|
| 226 | - * @return string Server protocol (http or https) |
|
| 227 | - * @since 8.1.0 |
|
| 228 | - */ |
|
| 229 | - public function getServerProtocol(): string; |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * Returns the used HTTP protocol. |
|
| 233 | - * |
|
| 234 | - * @return string HTTP protocol. HTTP/2, HTTP/1.1 or HTTP/1.0. |
|
| 235 | - * @since 8.2.0 |
|
| 236 | - */ |
|
| 237 | - public function getHttpProtocol(): string; |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Returns the request uri, even if the website uses one or more |
|
| 241 | - * reverse proxies |
|
| 242 | - * |
|
| 243 | - * @return string |
|
| 244 | - * @since 8.1.0 |
|
| 245 | - */ |
|
| 246 | - public function getRequestUri(): string; |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * Get raw PathInfo from request (not urldecoded) |
|
| 250 | - * |
|
| 251 | - * @throws \Exception |
|
| 252 | - * @return string Path info |
|
| 253 | - * @since 8.1.0 |
|
| 254 | - */ |
|
| 255 | - public function getRawPathInfo(): string; |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Get PathInfo from request |
|
| 259 | - * |
|
| 260 | - * @throws \Exception |
|
| 261 | - * @return string|false Path info or false when not found |
|
| 262 | - * @since 8.1.0 |
|
| 263 | - */ |
|
| 264 | - public function getPathInfo(); |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * Returns the script name, even if the website uses one or more |
|
| 268 | - * reverse proxies |
|
| 269 | - * |
|
| 270 | - * @return string the script name |
|
| 271 | - * @since 8.1.0 |
|
| 272 | - */ |
|
| 273 | - public function getScriptName(): string; |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Checks whether the user agent matches a given regex |
|
| 277 | - * |
|
| 278 | - * @param array $agent array of agent names |
|
| 279 | - * @return bool true if at least one of the given agent matches, false otherwise |
|
| 280 | - * @since 8.1.0 |
|
| 281 | - */ |
|
| 282 | - public function isUserAgent(array $agent): bool; |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * Returns the unverified server host from the headers without checking |
|
| 286 | - * whether it is a trusted domain |
|
| 287 | - * |
|
| 288 | - * @return string Server host |
|
| 289 | - * @since 8.1.0 |
|
| 290 | - */ |
|
| 291 | - public function getInsecureServerHost(): string; |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * Returns the server host from the headers, or the first configured |
|
| 295 | - * trusted domain if the host isn't in the trusted list |
|
| 296 | - * |
|
| 297 | - * @return string Server host |
|
| 298 | - * @since 8.1.0 |
|
| 299 | - */ |
|
| 300 | - public function getServerHost(): string; |
|
| 68 | + /** |
|
| 69 | + * @since 9.1.0 |
|
| 70 | + */ |
|
| 71 | + const USER_AGENT_CLIENT_ANDROID = '/^Mozilla\/5\.0 \(Android\) (ownCloud|Nextcloud)\-android.*$/'; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @since 13.0.0 |
|
| 75 | + */ |
|
| 76 | + const USER_AGENT_TALK_ANDROID = '/^Mozilla\/5\.0 \(Android\) Nextcloud\-Talk v.*$/'; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @since 9.1.0 |
|
| 80 | + */ |
|
| 81 | + const USER_AGENT_CLIENT_DESKTOP = '/^Mozilla\/5\.0 \([A-Za-z ]+\) (mirall|csyncoC)\/.*$/'; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * @since 9.1.0 |
|
| 85 | + */ |
|
| 86 | + const USER_AGENT_CLIENT_IOS = '/^Mozilla\/5\.0 \(iOS\) (ownCloud|Nextcloud)\-iOS.*$/'; |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * @since 13.0.0 |
|
| 90 | + */ |
|
| 91 | + const USER_AGENT_TALK_IOS = '/^Mozilla\/5\.0 \(iOS\) Nextcloud\-Talk v.*$/'; |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * @since 13.0.1 |
|
| 95 | + */ |
|
| 96 | + const USER_AGENT_OUTLOOK_ADDON = '/^Mozilla\/5\.0 \([A-Za-z ]+\) Nextcloud\-Outlook v.*$/'; |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @since 13.0.1 |
|
| 100 | + */ |
|
| 101 | + const USER_AGENT_THUNDERBIRD_ADDON = '/^Mozilla\/5\.0 \([A-Za-z ]+\) Nextcloud\-Thunderbird v.*$/'; |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @param string $name |
|
| 105 | + * |
|
| 106 | + * @return string |
|
| 107 | + * @since 6.0.0 |
|
| 108 | + */ |
|
| 109 | + public function getHeader(string $name): string; |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Lets you access post and get parameters by the index |
|
| 113 | + * In case of json requests the encoded json body is accessed |
|
| 114 | + * |
|
| 115 | + * @param string $key the key which you want to access in the URL Parameter |
|
| 116 | + * placeholder, $_POST or $_GET array. |
|
| 117 | + * The priority how they're returned is the following: |
|
| 118 | + * 1. URL parameters |
|
| 119 | + * 2. POST parameters |
|
| 120 | + * 3. GET parameters |
|
| 121 | + * @param mixed $default If the key is not found, this value will be returned |
|
| 122 | + * @return mixed the content of the array |
|
| 123 | + * @since 6.0.0 |
|
| 124 | + */ |
|
| 125 | + public function getParam(string $key, $default = null); |
|
| 126 | + |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Returns all params that were received, be it from the request |
|
| 130 | + * |
|
| 131 | + * (as GET or POST) or through the URL by the route |
|
| 132 | + * |
|
| 133 | + * @return array the array with all parameters |
|
| 134 | + * @since 6.0.0 |
|
| 135 | + */ |
|
| 136 | + public function getParams(): array; |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Returns the method of the request |
|
| 140 | + * |
|
| 141 | + * @return string the method of the request (POST, GET, etc) |
|
| 142 | + * @since 6.0.0 |
|
| 143 | + */ |
|
| 144 | + public function getMethod(): string; |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Shortcut for accessing an uploaded file through the $_FILES array |
|
| 148 | + * |
|
| 149 | + * @param string $key the key that will be taken from the $_FILES array |
|
| 150 | + * @return array the file in the $_FILES element |
|
| 151 | + * @since 6.0.0 |
|
| 152 | + */ |
|
| 153 | + public function getUploadedFile(string $key); |
|
| 154 | + |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Shortcut for getting env variables |
|
| 158 | + * |
|
| 159 | + * @param string $key the key that will be taken from the $_ENV array |
|
| 160 | + * @return array the value in the $_ENV element |
|
| 161 | + * @since 6.0.0 |
|
| 162 | + */ |
|
| 163 | + public function getEnv(string $key); |
|
| 164 | + |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Shortcut for getting cookie variables |
|
| 168 | + * |
|
| 169 | + * @param string $key the key that will be taken from the $_COOKIE array |
|
| 170 | + * @return string|null the value in the $_COOKIE element |
|
| 171 | + * @since 6.0.0 |
|
| 172 | + */ |
|
| 173 | + public function getCookie(string $key); |
|
| 174 | + |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Checks if the CSRF check was correct |
|
| 178 | + * |
|
| 179 | + * @return bool true if CSRF check passed |
|
| 180 | + * @since 6.0.0 |
|
| 181 | + */ |
|
| 182 | + public function passesCSRFCheck(): bool; |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Checks if the strict cookie has been sent with the request if the request |
|
| 186 | + * is including any cookies. |
|
| 187 | + * |
|
| 188 | + * @return bool |
|
| 189 | + * @since 9.0.0 |
|
| 190 | + */ |
|
| 191 | + public function passesStrictCookieCheck(): bool; |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Checks if the lax cookie has been sent with the request if the request |
|
| 195 | + * is including any cookies. |
|
| 196 | + * |
|
| 197 | + * @return bool |
|
| 198 | + * @since 9.0.0 |
|
| 199 | + */ |
|
| 200 | + public function passesLaxCookieCheck(): bool; |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Returns an ID for the request, value is not guaranteed to be unique and is mostly meant for logging |
|
| 204 | + * If `mod_unique_id` is installed this value will be taken. |
|
| 205 | + * |
|
| 206 | + * @return string |
|
| 207 | + * @since 8.1.0 |
|
| 208 | + */ |
|
| 209 | + public function getId(): string; |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * Returns the remote address, if the connection came from a trusted proxy |
|
| 213 | + * and `forwarded_for_headers` has been configured then the IP address |
|
| 214 | + * specified in this header will be returned instead. |
|
| 215 | + * Do always use this instead of $_SERVER['REMOTE_ADDR'] |
|
| 216 | + * |
|
| 217 | + * @return string IP address |
|
| 218 | + * @since 8.1.0 |
|
| 219 | + */ |
|
| 220 | + public function getRemoteAddress(): string; |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Returns the server protocol. It respects reverse proxy servers and load |
|
| 224 | + * balancers. |
|
| 225 | + * |
|
| 226 | + * @return string Server protocol (http or https) |
|
| 227 | + * @since 8.1.0 |
|
| 228 | + */ |
|
| 229 | + public function getServerProtocol(): string; |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * Returns the used HTTP protocol. |
|
| 233 | + * |
|
| 234 | + * @return string HTTP protocol. HTTP/2, HTTP/1.1 or HTTP/1.0. |
|
| 235 | + * @since 8.2.0 |
|
| 236 | + */ |
|
| 237 | + public function getHttpProtocol(): string; |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Returns the request uri, even if the website uses one or more |
|
| 241 | + * reverse proxies |
|
| 242 | + * |
|
| 243 | + * @return string |
|
| 244 | + * @since 8.1.0 |
|
| 245 | + */ |
|
| 246 | + public function getRequestUri(): string; |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * Get raw PathInfo from request (not urldecoded) |
|
| 250 | + * |
|
| 251 | + * @throws \Exception |
|
| 252 | + * @return string Path info |
|
| 253 | + * @since 8.1.0 |
|
| 254 | + */ |
|
| 255 | + public function getRawPathInfo(): string; |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Get PathInfo from request |
|
| 259 | + * |
|
| 260 | + * @throws \Exception |
|
| 261 | + * @return string|false Path info or false when not found |
|
| 262 | + * @since 8.1.0 |
|
| 263 | + */ |
|
| 264 | + public function getPathInfo(); |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * Returns the script name, even if the website uses one or more |
|
| 268 | + * reverse proxies |
|
| 269 | + * |
|
| 270 | + * @return string the script name |
|
| 271 | + * @since 8.1.0 |
|
| 272 | + */ |
|
| 273 | + public function getScriptName(): string; |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Checks whether the user agent matches a given regex |
|
| 277 | + * |
|
| 278 | + * @param array $agent array of agent names |
|
| 279 | + * @return bool true if at least one of the given agent matches, false otherwise |
|
| 280 | + * @since 8.1.0 |
|
| 281 | + */ |
|
| 282 | + public function isUserAgent(array $agent): bool; |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * Returns the unverified server host from the headers without checking |
|
| 286 | + * whether it is a trusted domain |
|
| 287 | + * |
|
| 288 | + * @return string Server host |
|
| 289 | + * @since 8.1.0 |
|
| 290 | + */ |
|
| 291 | + public function getInsecureServerHost(): string; |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * Returns the server host from the headers, or the first configured |
|
| 295 | + * trusted domain if the host isn't in the trusted list |
|
| 296 | + * |
|
| 297 | + * @return string Server host |
|
| 298 | + * @since 8.1.0 |
|
| 299 | + */ |
|
| 300 | + public function getServerHost(): string; |
|
| 301 | 301 | } |
@@ -34,55 +34,55 @@ |
||
| 34 | 34 | |
| 35 | 35 | class ProvisioningApiMiddleware extends Middleware { |
| 36 | 36 | |
| 37 | - /** @var IControllerMethodReflector */ |
|
| 38 | - private $reflector; |
|
| 37 | + /** @var IControllerMethodReflector */ |
|
| 38 | + private $reflector; |
|
| 39 | 39 | |
| 40 | - /** @var bool */ |
|
| 41 | - private $isAdmin; |
|
| 40 | + /** @var bool */ |
|
| 41 | + private $isAdmin; |
|
| 42 | 42 | |
| 43 | - /** @var bool */ |
|
| 44 | - private $isSubAdmin; |
|
| 43 | + /** @var bool */ |
|
| 44 | + private $isSubAdmin; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * ProvisioningApiMiddleware constructor. |
|
| 48 | - * |
|
| 49 | - * @param IControllerMethodReflector $reflector |
|
| 50 | - * @param bool $isAdmin |
|
| 51 | - * @param bool $isSubAdmin |
|
| 52 | - */ |
|
| 53 | - public function __construct( |
|
| 54 | - IControllerMethodReflector $reflector, |
|
| 55 | - bool $isAdmin, |
|
| 56 | - bool $isSubAdmin) { |
|
| 57 | - $this->reflector = $reflector; |
|
| 58 | - $this->isAdmin = $isAdmin; |
|
| 59 | - $this->isSubAdmin = $isSubAdmin; |
|
| 60 | - } |
|
| 46 | + /** |
|
| 47 | + * ProvisioningApiMiddleware constructor. |
|
| 48 | + * |
|
| 49 | + * @param IControllerMethodReflector $reflector |
|
| 50 | + * @param bool $isAdmin |
|
| 51 | + * @param bool $isSubAdmin |
|
| 52 | + */ |
|
| 53 | + public function __construct( |
|
| 54 | + IControllerMethodReflector $reflector, |
|
| 55 | + bool $isAdmin, |
|
| 56 | + bool $isSubAdmin) { |
|
| 57 | + $this->reflector = $reflector; |
|
| 58 | + $this->isAdmin = $isAdmin; |
|
| 59 | + $this->isSubAdmin = $isSubAdmin; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * @param Controller $controller |
|
| 64 | - * @param string $methodName |
|
| 65 | - * |
|
| 66 | - * @throws NotSubAdminException |
|
| 67 | - */ |
|
| 68 | - public function beforeController($controller, $methodName) { |
|
| 69 | - if (!$this->isAdmin && !$this->reflector->hasAnnotation('NoSubAdminRequired') && !$this->isSubAdmin) { |
|
| 70 | - throw new NotSubAdminException(); |
|
| 71 | - } |
|
| 72 | - } |
|
| 62 | + /** |
|
| 63 | + * @param Controller $controller |
|
| 64 | + * @param string $methodName |
|
| 65 | + * |
|
| 66 | + * @throws NotSubAdminException |
|
| 67 | + */ |
|
| 68 | + public function beforeController($controller, $methodName) { |
|
| 69 | + if (!$this->isAdmin && !$this->reflector->hasAnnotation('NoSubAdminRequired') && !$this->isSubAdmin) { |
|
| 70 | + throw new NotSubAdminException(); |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * @param Controller $controller |
|
| 76 | - * @param string $methodName |
|
| 77 | - * @param \Exception $exception |
|
| 78 | - * @throws \Exception |
|
| 79 | - * @return Response |
|
| 80 | - */ |
|
| 81 | - public function afterException($controller, $methodName, \Exception $exception) { |
|
| 82 | - if ($exception instanceof NotSubAdminException) { |
|
| 83 | - throw new OCSException($exception->getMessage(), \OCP\API::RESPOND_UNAUTHORISED); |
|
| 84 | - } |
|
| 74 | + /** |
|
| 75 | + * @param Controller $controller |
|
| 76 | + * @param string $methodName |
|
| 77 | + * @param \Exception $exception |
|
| 78 | + * @throws \Exception |
|
| 79 | + * @return Response |
|
| 80 | + */ |
|
| 81 | + public function afterException($controller, $methodName, \Exception $exception) { |
|
| 82 | + if ($exception instanceof NotSubAdminException) { |
|
| 83 | + throw new OCSException($exception->getMessage(), \OCP\API::RESPOND_UNAUTHORISED); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - throw $exception; |
|
| 87 | - } |
|
| 86 | + throw $exception; |
|
| 87 | + } |
|
| 88 | 88 | } |
@@ -36,91 +36,91 @@ |
||
| 36 | 36 | use OCP\IRequest; |
| 37 | 37 | |
| 38 | 38 | class AppsController extends OCSController { |
| 39 | - /** @var \OCP\App\IAppManager */ |
|
| 40 | - private $appManager; |
|
| 39 | + /** @var \OCP\App\IAppManager */ |
|
| 40 | + private $appManager; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * @param string $appName |
|
| 44 | - * @param IRequest $request |
|
| 45 | - * @param IAppManager $appManager |
|
| 46 | - */ |
|
| 47 | - public function __construct( |
|
| 48 | - string $appName, |
|
| 49 | - IRequest $request, |
|
| 50 | - IAppManager $appManager |
|
| 51 | - ) { |
|
| 52 | - parent::__construct($appName, $request); |
|
| 42 | + /** |
|
| 43 | + * @param string $appName |
|
| 44 | + * @param IRequest $request |
|
| 45 | + * @param IAppManager $appManager |
|
| 46 | + */ |
|
| 47 | + public function __construct( |
|
| 48 | + string $appName, |
|
| 49 | + IRequest $request, |
|
| 50 | + IAppManager $appManager |
|
| 51 | + ) { |
|
| 52 | + parent::__construct($appName, $request); |
|
| 53 | 53 | |
| 54 | - $this->appManager = $appManager; |
|
| 55 | - } |
|
| 54 | + $this->appManager = $appManager; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * @param string $filter |
|
| 59 | - * @return DataResponse |
|
| 60 | - * @throws OCSException |
|
| 61 | - */ |
|
| 62 | - public function getApps(string $filter = null): DataResponse { |
|
| 63 | - $apps = (new OC_App())->listAllApps(); |
|
| 64 | - $list = []; |
|
| 65 | - foreach($apps as $app) { |
|
| 66 | - $list[] = $app['id']; |
|
| 67 | - } |
|
| 68 | - if($filter){ |
|
| 69 | - switch($filter){ |
|
| 70 | - case 'enabled': |
|
| 71 | - return new DataResponse(['apps' => \OC_App::getEnabledApps()]); |
|
| 72 | - break; |
|
| 73 | - case 'disabled': |
|
| 74 | - $enabled = OC_App::getEnabledApps(); |
|
| 75 | - return new DataResponse(['apps' => array_diff($list, $enabled)]); |
|
| 76 | - break; |
|
| 77 | - default: |
|
| 78 | - // Invalid filter variable |
|
| 79 | - throw new OCSException('', 101); |
|
| 80 | - } |
|
| 57 | + /** |
|
| 58 | + * @param string $filter |
|
| 59 | + * @return DataResponse |
|
| 60 | + * @throws OCSException |
|
| 61 | + */ |
|
| 62 | + public function getApps(string $filter = null): DataResponse { |
|
| 63 | + $apps = (new OC_App())->listAllApps(); |
|
| 64 | + $list = []; |
|
| 65 | + foreach($apps as $app) { |
|
| 66 | + $list[] = $app['id']; |
|
| 67 | + } |
|
| 68 | + if($filter){ |
|
| 69 | + switch($filter){ |
|
| 70 | + case 'enabled': |
|
| 71 | + return new DataResponse(['apps' => \OC_App::getEnabledApps()]); |
|
| 72 | + break; |
|
| 73 | + case 'disabled': |
|
| 74 | + $enabled = OC_App::getEnabledApps(); |
|
| 75 | + return new DataResponse(['apps' => array_diff($list, $enabled)]); |
|
| 76 | + break; |
|
| 77 | + default: |
|
| 78 | + // Invalid filter variable |
|
| 79 | + throw new OCSException('', 101); |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - } else { |
|
| 83 | - return new DataResponse(['apps' => $list]); |
|
| 84 | - } |
|
| 85 | - } |
|
| 82 | + } else { |
|
| 83 | + return new DataResponse(['apps' => $list]); |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * @param string $app |
|
| 89 | - * @return DataResponse |
|
| 90 | - * @throws OCSException |
|
| 91 | - */ |
|
| 92 | - public function getAppInfo(string $app): DataResponse { |
|
| 93 | - $info = \OCP\App::getAppInfo($app); |
|
| 94 | - if(!is_null($info)) { |
|
| 95 | - return new DataResponse(OC_App::getAppInfo($app)); |
|
| 96 | - } |
|
| 87 | + /** |
|
| 88 | + * @param string $app |
|
| 89 | + * @return DataResponse |
|
| 90 | + * @throws OCSException |
|
| 91 | + */ |
|
| 92 | + public function getAppInfo(string $app): DataResponse { |
|
| 93 | + $info = \OCP\App::getAppInfo($app); |
|
| 94 | + if(!is_null($info)) { |
|
| 95 | + return new DataResponse(OC_App::getAppInfo($app)); |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - throw new OCSException('The request app was not found', \OCP\API::RESPOND_NOT_FOUND); |
|
| 99 | - } |
|
| 98 | + throw new OCSException('The request app was not found', \OCP\API::RESPOND_NOT_FOUND); |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * @PasswordConfirmationRequired |
|
| 103 | - * @param string $app |
|
| 104 | - * @return DataResponse |
|
| 105 | - * @throws OCSException |
|
| 106 | - */ |
|
| 107 | - public function enable(string $app): DataResponse { |
|
| 108 | - try { |
|
| 109 | - $this->appManager->enableApp($app); |
|
| 110 | - } catch (AppPathNotFoundException $e) { |
|
| 111 | - throw new OCSException('The request app was not found', \OCP\API::RESPOND_NOT_FOUND); |
|
| 112 | - } |
|
| 113 | - return new DataResponse(); |
|
| 114 | - } |
|
| 101 | + /** |
|
| 102 | + * @PasswordConfirmationRequired |
|
| 103 | + * @param string $app |
|
| 104 | + * @return DataResponse |
|
| 105 | + * @throws OCSException |
|
| 106 | + */ |
|
| 107 | + public function enable(string $app): DataResponse { |
|
| 108 | + try { |
|
| 109 | + $this->appManager->enableApp($app); |
|
| 110 | + } catch (AppPathNotFoundException $e) { |
|
| 111 | + throw new OCSException('The request app was not found', \OCP\API::RESPOND_NOT_FOUND); |
|
| 112 | + } |
|
| 113 | + return new DataResponse(); |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - /** |
|
| 117 | - * @PasswordConfirmationRequired |
|
| 118 | - * @param string $app |
|
| 119 | - * @return DataResponse |
|
| 120 | - */ |
|
| 121 | - public function disable(string $app): DataResponse { |
|
| 122 | - $this->appManager->disableApp($app); |
|
| 123 | - return new DataResponse(); |
|
| 124 | - } |
|
| 116 | + /** |
|
| 117 | + * @PasswordConfirmationRequired |
|
| 118 | + * @param string $app |
|
| 119 | + * @return DataResponse |
|
| 120 | + */ |
|
| 121 | + public function disable(string $app): DataResponse { |
|
| 122 | + $this->appManager->disableApp($app); |
|
| 123 | + return new DataResponse(); |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | 126 | } |
@@ -36,70 +36,70 @@ |
||
| 36 | 36 | |
| 37 | 37 | class Helper { |
| 38 | 38 | |
| 39 | - public static function registerHooks() { |
|
| 40 | - \OCP\Util::connectHook('OC_Filesystem', 'post_rename', '\OCA\Files_Sharing\Updater', 'renameHook'); |
|
| 41 | - \OCP\Util::connectHook('OC_Filesystem', 'post_delete', '\OCA\Files_Sharing\Hooks', 'unshareChildren'); |
|
| 39 | + public static function registerHooks() { |
|
| 40 | + \OCP\Util::connectHook('OC_Filesystem', 'post_rename', '\OCA\Files_Sharing\Updater', 'renameHook'); |
|
| 41 | + \OCP\Util::connectHook('OC_Filesystem', 'post_delete', '\OCA\Files_Sharing\Hooks', 'unshareChildren'); |
|
| 42 | 42 | |
| 43 | - \OCP\Util::connectHook('OC_User', 'post_deleteUser', '\OCA\Files_Sharing\Hooks', 'deleteUser'); |
|
| 44 | - } |
|
| 43 | + \OCP\Util::connectHook('OC_User', 'post_deleteUser', '\OCA\Files_Sharing\Hooks', 'deleteUser'); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * check if file name already exists and generate unique target |
|
| 48 | - * |
|
| 49 | - * @param string $path |
|
| 50 | - * @param array $excludeList |
|
| 51 | - * @param View $view |
|
| 52 | - * @return string $path |
|
| 53 | - */ |
|
| 54 | - public static function generateUniqueTarget($path, $excludeList, $view) { |
|
| 55 | - $pathinfo = pathinfo($path); |
|
| 56 | - $ext = isset($pathinfo['extension']) ? '.'.$pathinfo['extension'] : ''; |
|
| 57 | - $name = $pathinfo['filename']; |
|
| 58 | - $dir = $pathinfo['dirname']; |
|
| 59 | - $i = 2; |
|
| 60 | - while ($view->file_exists($path) || in_array($path, $excludeList)) { |
|
| 61 | - $path = Filesystem::normalizePath($dir . '/' . $name . ' ('.$i.')' . $ext); |
|
| 62 | - $i++; |
|
| 63 | - } |
|
| 46 | + /** |
|
| 47 | + * check if file name already exists and generate unique target |
|
| 48 | + * |
|
| 49 | + * @param string $path |
|
| 50 | + * @param array $excludeList |
|
| 51 | + * @param View $view |
|
| 52 | + * @return string $path |
|
| 53 | + */ |
|
| 54 | + public static function generateUniqueTarget($path, $excludeList, $view) { |
|
| 55 | + $pathinfo = pathinfo($path); |
|
| 56 | + $ext = isset($pathinfo['extension']) ? '.'.$pathinfo['extension'] : ''; |
|
| 57 | + $name = $pathinfo['filename']; |
|
| 58 | + $dir = $pathinfo['dirname']; |
|
| 59 | + $i = 2; |
|
| 60 | + while ($view->file_exists($path) || in_array($path, $excludeList)) { |
|
| 61 | + $path = Filesystem::normalizePath($dir . '/' . $name . ' ('.$i.')' . $ext); |
|
| 62 | + $i++; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - return $path; |
|
| 66 | - } |
|
| 65 | + return $path; |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * get default share folder |
|
| 70 | - * |
|
| 71 | - * @param \OC\Files\View |
|
| 72 | - * @return string |
|
| 73 | - */ |
|
| 74 | - public static function getShareFolder($view = null) { |
|
| 75 | - if ($view === null) { |
|
| 76 | - $view = Filesystem::getView(); |
|
| 77 | - } |
|
| 78 | - $shareFolder = \OC::$server->getConfig()->getSystemValue('share_folder', '/'); |
|
| 79 | - $shareFolder = Filesystem::normalizePath($shareFolder); |
|
| 68 | + /** |
|
| 69 | + * get default share folder |
|
| 70 | + * |
|
| 71 | + * @param \OC\Files\View |
|
| 72 | + * @return string |
|
| 73 | + */ |
|
| 74 | + public static function getShareFolder($view = null) { |
|
| 75 | + if ($view === null) { |
|
| 76 | + $view = Filesystem::getView(); |
|
| 77 | + } |
|
| 78 | + $shareFolder = \OC::$server->getConfig()->getSystemValue('share_folder', '/'); |
|
| 79 | + $shareFolder = Filesystem::normalizePath($shareFolder); |
|
| 80 | 80 | |
| 81 | - if (!$view->file_exists($shareFolder)) { |
|
| 82 | - $dir = ''; |
|
| 83 | - $subdirs = explode('/', $shareFolder); |
|
| 84 | - foreach ($subdirs as $subdir) { |
|
| 85 | - $dir = $dir . '/' . $subdir; |
|
| 86 | - if (!$view->is_dir($dir)) { |
|
| 87 | - $view->mkdir($dir); |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - } |
|
| 81 | + if (!$view->file_exists($shareFolder)) { |
|
| 82 | + $dir = ''; |
|
| 83 | + $subdirs = explode('/', $shareFolder); |
|
| 84 | + foreach ($subdirs as $subdir) { |
|
| 85 | + $dir = $dir . '/' . $subdir; |
|
| 86 | + if (!$view->is_dir($dir)) { |
|
| 87 | + $view->mkdir($dir); |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - return $shareFolder; |
|
| 92 | + return $shareFolder; |
|
| 93 | 93 | |
| 94 | - } |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * set default share folder |
|
| 98 | - * |
|
| 99 | - * @param string $shareFolder |
|
| 100 | - */ |
|
| 101 | - public static function setShareFolder($shareFolder) { |
|
| 102 | - \OC::$server->getConfig()->setSystemValue('share_folder', $shareFolder); |
|
| 103 | - } |
|
| 96 | + /** |
|
| 97 | + * set default share folder |
|
| 98 | + * |
|
| 99 | + * @param string $shareFolder |
|
| 100 | + */ |
|
| 101 | + public static function setShareFolder($shareFolder) { |
|
| 102 | + \OC::$server->getConfig()->setSystemValue('share_folder', $shareFolder); |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | 105 | } |
@@ -22,11 +22,11 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | |
| 24 | 24 | return [ |
| 25 | - 'routes' => [ |
|
| 26 | - ['name' => 'Admin#createCredentials', 'url' => '/credentials', 'verb' => 'GET'], |
|
| 27 | - ['name' => 'Admin#setChannel', 'url' => '/channel', 'verb' => 'POST'], |
|
| 28 | - ], |
|
| 29 | - 'ocs' => [ |
|
| 30 | - ['name' => 'API#getAppList', 'url' => '/api/{apiVersion}/applist/{newVersion}', 'verb' => 'GET', 'requirements' => ['apiVersion' => 'v1']], |
|
| 31 | - ], |
|
| 25 | + 'routes' => [ |
|
| 26 | + ['name' => 'Admin#createCredentials', 'url' => '/credentials', 'verb' => 'GET'], |
|
| 27 | + ['name' => 'Admin#setChannel', 'url' => '/channel', 'verb' => 'POST'], |
|
| 28 | + ], |
|
| 29 | + 'ocs' => [ |
|
| 30 | + ['name' => 'API#getAppList', 'url' => '/api/{apiVersion}/applist/{newVersion}', 'verb' => 'GET', 'requirements' => ['apiVersion' => 'v1']], |
|
| 31 | + ], |
|
| 32 | 32 | ]; |
@@ -30,158 +30,158 @@ |
||
| 30 | 30 | |
| 31 | 31 | class Session { |
| 32 | 32 | |
| 33 | - /** @var ISession */ |
|
| 34 | - protected $session; |
|
| 35 | - |
|
| 36 | - const NOT_INITIALIZED = '0'; |
|
| 37 | - const INIT_EXECUTED = '1'; |
|
| 38 | - const INIT_SUCCESSFUL = '2'; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @param ISession $session |
|
| 42 | - */ |
|
| 43 | - public function __construct(ISession $session) { |
|
| 44 | - $this->session = $session; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Sets status of encryption app |
|
| 49 | - * |
|
| 50 | - * @param string $status INIT_SUCCESSFUL, INIT_EXECUTED, NOT_INITIALIZED |
|
| 51 | - */ |
|
| 52 | - public function setStatus($status) { |
|
| 53 | - $this->session->set('encryptionInitialized', $status); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Gets status if we already tried to initialize the encryption app |
|
| 58 | - * |
|
| 59 | - * @return string init status INIT_SUCCESSFUL, INIT_EXECUTED, NOT_INITIALIZED |
|
| 60 | - */ |
|
| 61 | - public function getStatus() { |
|
| 62 | - $status = $this->session->get('encryptionInitialized'); |
|
| 63 | - if (is_null($status)) { |
|
| 64 | - $status = self::NOT_INITIALIZED; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - return $status; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * check if encryption was initialized successfully |
|
| 72 | - * |
|
| 73 | - * @return bool |
|
| 74 | - */ |
|
| 75 | - public function isReady() { |
|
| 76 | - $status = $this->getStatus(); |
|
| 77 | - return $status === self::INIT_SUCCESSFUL; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Gets user or public share private key from session |
|
| 82 | - * |
|
| 83 | - * @return string $privateKey The user's plaintext private key |
|
| 84 | - * @throws Exceptions\PrivateKeyMissingException |
|
| 85 | - */ |
|
| 86 | - public function getPrivateKey() { |
|
| 87 | - $key = $this->session->get('privateKey'); |
|
| 88 | - if (is_null($key)) { |
|
| 89 | - throw new Exceptions\PrivateKeyMissingException('please try to log-out and log-in again', 0); |
|
| 90 | - } |
|
| 91 | - return $key; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * check if private key is set |
|
| 96 | - * |
|
| 97 | - * @return boolean |
|
| 98 | - */ |
|
| 99 | - public function isPrivateKeySet() { |
|
| 100 | - $key = $this->session->get('privateKey'); |
|
| 101 | - if (is_null($key)) { |
|
| 102 | - return false; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - return true; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Sets user private key to session |
|
| 110 | - * |
|
| 111 | - * @param string $key users private key |
|
| 112 | - * |
|
| 113 | - * @note this should only be set on login |
|
| 114 | - */ |
|
| 115 | - public function setPrivateKey($key) { |
|
| 116 | - $this->session->set('privateKey', $key); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * store data needed for the decrypt all operation in the session |
|
| 121 | - * |
|
| 122 | - * @param string $user |
|
| 123 | - * @param string $key |
|
| 124 | - */ |
|
| 125 | - public function prepareDecryptAll($user, $key) { |
|
| 126 | - $this->session->set('decryptAll', true); |
|
| 127 | - $this->session->set('decryptAllKey', $key); |
|
| 128 | - $this->session->set('decryptAllUid', $user); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * check if we are in decrypt all mode |
|
| 133 | - * |
|
| 134 | - * @return bool |
|
| 135 | - */ |
|
| 136 | - public function decryptAllModeActivated() { |
|
| 137 | - $decryptAll = $this->session->get('decryptAll'); |
|
| 138 | - return ($decryptAll === true); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * get uid used for decrypt all operation |
|
| 143 | - * |
|
| 144 | - * @return string |
|
| 145 | - * @throws \Exception |
|
| 146 | - */ |
|
| 147 | - public function getDecryptAllUid() { |
|
| 148 | - $uid = $this->session->get('decryptAllUid'); |
|
| 149 | - if (is_null($uid) && $this->decryptAllModeActivated()) { |
|
| 150 | - throw new \Exception('No uid found while in decrypt all mode'); |
|
| 151 | - } elseif (is_null($uid)) { |
|
| 152 | - throw new \Exception('Please activate decrypt all mode first'); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - return $uid; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * get private key for decrypt all operation |
|
| 160 | - * |
|
| 161 | - * @return string |
|
| 162 | - * @throws PrivateKeyMissingException |
|
| 163 | - */ |
|
| 164 | - public function getDecryptAllKey() { |
|
| 165 | - $privateKey = $this->session->get('decryptAllKey'); |
|
| 166 | - if (is_null($privateKey) && $this->decryptAllModeActivated()) { |
|
| 167 | - throw new PrivateKeyMissingException('No private key found while in decrypt all mode'); |
|
| 168 | - } elseif (is_null($privateKey)) { |
|
| 169 | - throw new PrivateKeyMissingException('Please activate decrypt all mode first'); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - return $privateKey; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * remove keys from session |
|
| 177 | - */ |
|
| 178 | - public function clear() { |
|
| 179 | - $this->session->remove('publicSharePrivateKey'); |
|
| 180 | - $this->session->remove('privateKey'); |
|
| 181 | - $this->session->remove('encryptionInitialized'); |
|
| 182 | - $this->session->remove('decryptAll'); |
|
| 183 | - $this->session->remove('decryptAllKey'); |
|
| 184 | - $this->session->remove('decryptAllUid'); |
|
| 185 | - } |
|
| 33 | + /** @var ISession */ |
|
| 34 | + protected $session; |
|
| 35 | + |
|
| 36 | + const NOT_INITIALIZED = '0'; |
|
| 37 | + const INIT_EXECUTED = '1'; |
|
| 38 | + const INIT_SUCCESSFUL = '2'; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @param ISession $session |
|
| 42 | + */ |
|
| 43 | + public function __construct(ISession $session) { |
|
| 44 | + $this->session = $session; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Sets status of encryption app |
|
| 49 | + * |
|
| 50 | + * @param string $status INIT_SUCCESSFUL, INIT_EXECUTED, NOT_INITIALIZED |
|
| 51 | + */ |
|
| 52 | + public function setStatus($status) { |
|
| 53 | + $this->session->set('encryptionInitialized', $status); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Gets status if we already tried to initialize the encryption app |
|
| 58 | + * |
|
| 59 | + * @return string init status INIT_SUCCESSFUL, INIT_EXECUTED, NOT_INITIALIZED |
|
| 60 | + */ |
|
| 61 | + public function getStatus() { |
|
| 62 | + $status = $this->session->get('encryptionInitialized'); |
|
| 63 | + if (is_null($status)) { |
|
| 64 | + $status = self::NOT_INITIALIZED; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + return $status; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * check if encryption was initialized successfully |
|
| 72 | + * |
|
| 73 | + * @return bool |
|
| 74 | + */ |
|
| 75 | + public function isReady() { |
|
| 76 | + $status = $this->getStatus(); |
|
| 77 | + return $status === self::INIT_SUCCESSFUL; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Gets user or public share private key from session |
|
| 82 | + * |
|
| 83 | + * @return string $privateKey The user's plaintext private key |
|
| 84 | + * @throws Exceptions\PrivateKeyMissingException |
|
| 85 | + */ |
|
| 86 | + public function getPrivateKey() { |
|
| 87 | + $key = $this->session->get('privateKey'); |
|
| 88 | + if (is_null($key)) { |
|
| 89 | + throw new Exceptions\PrivateKeyMissingException('please try to log-out and log-in again', 0); |
|
| 90 | + } |
|
| 91 | + return $key; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * check if private key is set |
|
| 96 | + * |
|
| 97 | + * @return boolean |
|
| 98 | + */ |
|
| 99 | + public function isPrivateKeySet() { |
|
| 100 | + $key = $this->session->get('privateKey'); |
|
| 101 | + if (is_null($key)) { |
|
| 102 | + return false; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + return true; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Sets user private key to session |
|
| 110 | + * |
|
| 111 | + * @param string $key users private key |
|
| 112 | + * |
|
| 113 | + * @note this should only be set on login |
|
| 114 | + */ |
|
| 115 | + public function setPrivateKey($key) { |
|
| 116 | + $this->session->set('privateKey', $key); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * store data needed for the decrypt all operation in the session |
|
| 121 | + * |
|
| 122 | + * @param string $user |
|
| 123 | + * @param string $key |
|
| 124 | + */ |
|
| 125 | + public function prepareDecryptAll($user, $key) { |
|
| 126 | + $this->session->set('decryptAll', true); |
|
| 127 | + $this->session->set('decryptAllKey', $key); |
|
| 128 | + $this->session->set('decryptAllUid', $user); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * check if we are in decrypt all mode |
|
| 133 | + * |
|
| 134 | + * @return bool |
|
| 135 | + */ |
|
| 136 | + public function decryptAllModeActivated() { |
|
| 137 | + $decryptAll = $this->session->get('decryptAll'); |
|
| 138 | + return ($decryptAll === true); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * get uid used for decrypt all operation |
|
| 143 | + * |
|
| 144 | + * @return string |
|
| 145 | + * @throws \Exception |
|
| 146 | + */ |
|
| 147 | + public function getDecryptAllUid() { |
|
| 148 | + $uid = $this->session->get('decryptAllUid'); |
|
| 149 | + if (is_null($uid) && $this->decryptAllModeActivated()) { |
|
| 150 | + throw new \Exception('No uid found while in decrypt all mode'); |
|
| 151 | + } elseif (is_null($uid)) { |
|
| 152 | + throw new \Exception('Please activate decrypt all mode first'); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + return $uid; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * get private key for decrypt all operation |
|
| 160 | + * |
|
| 161 | + * @return string |
|
| 162 | + * @throws PrivateKeyMissingException |
|
| 163 | + */ |
|
| 164 | + public function getDecryptAllKey() { |
|
| 165 | + $privateKey = $this->session->get('decryptAllKey'); |
|
| 166 | + if (is_null($privateKey) && $this->decryptAllModeActivated()) { |
|
| 167 | + throw new PrivateKeyMissingException('No private key found while in decrypt all mode'); |
|
| 168 | + } elseif (is_null($privateKey)) { |
|
| 169 | + throw new PrivateKeyMissingException('Please activate decrypt all mode first'); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + return $privateKey; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * remove keys from session |
|
| 177 | + */ |
|
| 178 | + public function clear() { |
|
| 179 | + $this->session->remove('publicSharePrivateKey'); |
|
| 180 | + $this->session->remove('privateKey'); |
|
| 181 | + $this->session->remove('encryptionInitialized'); |
|
| 182 | + $this->session->remove('decryptAll'); |
|
| 183 | + $this->session->remove('decryptAllKey'); |
|
| 184 | + $this->session->remove('decryptAllUid'); |
|
| 185 | + } |
|
| 186 | 186 | |
| 187 | 187 | } |