diff options
author | dartcafe <github@dartcafe.de> | 2022-01-23 23:43:58 +0300 |
---|---|---|
committer | dartcafe <github@dartcafe.de> | 2022-01-23 23:43:58 +0300 |
commit | d64f9ce270ad2b7e5e0dae3f84a3ed5830309ee9 (patch) | |
tree | f8177eb13f5d6973e694fabbd044b356151bbf16 /lib | |
parent | e0c4aa169dd49efac429432f46fd196290eb5c81 (diff) |
remove \OC::$server
Signed-off-by: dartcafe <github@dartcafe.de>
Diffstat (limited to 'lib')
30 files changed, 364 insertions, 75 deletions
diff --git a/lib/Controller/PreferencesController.php b/lib/Controller/PreferencesController.php index 28c642b3..1da027d5 100644 --- a/lib/Controller/PreferencesController.php +++ b/lib/Controller/PreferencesController.php @@ -24,6 +24,7 @@ namespace OCA\Polls\Controller; use OCP\IRequest; +use OCP\IUserSession; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; @@ -39,17 +40,22 @@ class PreferencesController extends Controller { /** @var CalendarService */ private $calendarService; + /** @var IUserSession */ + private $userSession; + use ResponseHandle; public function __construct( string $appName, IRequest $request, PreferencesService $preferencesService, - CalendarService $calendarService + CalendarService $calendarService, + IUserSession $userSession ) { parent::__construct($appName, $request); - $this->preferencesService = $preferencesService; $this->calendarService = $calendarService; + $this->preferencesService = $preferencesService; + $this->userSession = $userSession; } /** @@ -68,7 +74,7 @@ class PreferencesController extends Controller { * @NoAdminRequired */ public function write(array $settings): DataResponse { - if (!\OC::$server->getUserSession()->isLoggedIn()) { + if ($this->userSession->isLoggedIn()) { return new DataResponse([], Http::STATUS_OK); } return $this->response(function () use ($settings) { diff --git a/lib/Controller/PublicController.php b/lib/Controller/PublicController.php index 975d717c..ffb44b6d 100644 --- a/lib/Controller/PublicController.php +++ b/lib/Controller/PublicController.php @@ -25,6 +25,7 @@ namespace OCA\Polls\Controller; use OCP\IRequest; use OCP\IURLGenerator; +use OCP\IUserSession; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; @@ -52,6 +53,9 @@ class PublicController extends Controller { /** @var IURLGenerator */ private $urlGenerator; + /** @var IUserSession */ + private $userSession; + /** @var Acl */ private $acl; @@ -94,6 +98,7 @@ class PublicController extends Controller { string $appName, IRequest $request, IURLGenerator $urlGenerator, + IUserSession $userSession, Acl $acl, CommentService $commentService, MailService $mailService, @@ -109,6 +114,7 @@ class PublicController extends Controller { ) { parent::__construct($appName, $request); $this->urlGenerator = $urlGenerator; + $this->userSession = $userSession; $this->acl = $acl; $this->commentService = $commentService; $this->mailService = $mailService; @@ -130,7 +136,7 @@ class PublicController extends Controller { * @return TemplateResponse|PublicTemplateResponse */ public function votePage() { - if (\OC::$server->getUserSession()->isLoggedIn()) { + if ($this->userSession->isLoggedIn()) { return new TemplateResponse('polls', 'polls.tmpl', [ 'urlGenerator' => $this->urlGenerator]); } else { diff --git a/lib/Cron/GroupDeletedJob.php b/lib/Cron/GroupDeletedJob.php index 7fdcf634..a469c9e9 100644 --- a/lib/Cron/GroupDeletedJob.php +++ b/lib/Cron/GroupDeletedJob.php @@ -34,6 +34,9 @@ use OCA\Polls\Db\ShareMapper; class GroupDeletedJob extends QueuedJob { + /** @var ISecureRandom */ + private $secureRandom; + /** @var ShareMapper **/ private $shareMapper; @@ -42,10 +45,12 @@ class GroupDeletedJob extends QueuedJob { public function __construct( ShareMapper $shareMapper, + ISecureRandom $secureRandom, ITimeFactory $time, LoggerInterface $logger ) { parent::__construct($time); + $this->secureRandom = $secureRandom; $this->shareMapper = $shareMapper; $this->logger = $logger; } @@ -60,7 +65,7 @@ class GroupDeletedJob extends QueuedJob { 'group' => $group ]); - $replacementName = 'deleted_' . \OC::$server->getSecureRandom()->generate( + $replacementName = 'deleted_' . $this->secureRandom->generate( 8, ISecureRandom::CHAR_DIGITS . ISecureRandom::CHAR_LOWER . diff --git a/lib/Cron/UserDeletedJob.php b/lib/Cron/UserDeletedJob.php index e4b63829..f099a118 100644 --- a/lib/Cron/UserDeletedJob.php +++ b/lib/Cron/UserDeletedJob.php @@ -44,6 +44,9 @@ class UserDeletedJob extends QueuedJob { /** @var CommentMapper **/ private $commentMapper; + /** @var ISecureRandom */ + private $secureRandom; + /** @var LogMapper **/ private $logMapper; @@ -70,6 +73,7 @@ class UserDeletedJob extends QueuedJob { public function __construct( CommentMapper $commentMapper, + ISecureRandom $secureRandom, LogMapper $logMapper, OptionMapper $optionMapper, PollMapper $pollMapper, @@ -86,6 +90,7 @@ class UserDeletedJob extends QueuedJob { $this->optionMapper = $optionMapper; $this->pollMapper = $pollMapper; $this->preferencesMapper = $preferencesMapper; + $this->secureRandom = $secureRandom; $this->shareMapper = $shareMapper; $this->subscriptionMapper = $subscriptionMapper; $this->voteMapper = $voteMapper; @@ -102,7 +107,7 @@ class UserDeletedJob extends QueuedJob { 'user' => $owner ]); - $replacementName = 'deleted_' . \OC::$server->getSecureRandom()->generate( + $replacementName = 'deleted_' . $this->secureRandom->generate( 8, ISecureRandom::CHAR_DIGITS . ISecureRandom::CHAR_LOWER . diff --git a/lib/Db/Comment.php b/lib/Db/Comment.php index a49f2ab9..d7a9a260 100644 --- a/lib/Db/Comment.php +++ b/lib/Db/Comment.php @@ -27,7 +27,9 @@ namespace OCA\Polls\Db; use JsonSerializable; +use OCA\Polls\Helper\Container; use OCP\IUser; +use OCP\IUserManager; use OCP\AppFramework\Db\Entity; /** @@ -57,9 +59,13 @@ class Comment extends Entity implements JsonSerializable { /** @var string $comment */ protected $comment = ''; + /** @var IUserManager */ + private $userManager; + public function __construct() { $this->addType('pollId', 'int'); $this->addType('timestamp', 'int'); + $this->userManager = Container::queryClass(IUserManager::class); } public function jsonSerialize() { @@ -80,10 +86,10 @@ class Comment extends Entity implements JsonSerializable { } return $this->getIsNoUser() ? $this->userId - : \OC::$server->getUserManager()->get($this->userId)->getDisplayName(); + : $this->userManager->get($this->userId)->getDisplayName(); } public function getIsNoUser(): bool { - return !(\OC::$server->getUserManager()->get($this->userId) instanceof IUser); + return !($this->userManager->get($this->userId) instanceof IUser); } } diff --git a/lib/Db/Option.php b/lib/Db/Option.php index c8c3fce4..930c6a89 100644 --- a/lib/Db/Option.php +++ b/lib/Db/Option.php @@ -27,8 +27,10 @@ namespace OCA\Polls\Db; use JsonSerializable; +use OCA\Polls\Helper\Container; use OCP\AppFramework\Db\Entity; use OCP\IUser; +use OCP\IUserManager; /** * @method int getId() @@ -99,6 +101,9 @@ class Option extends Entity implements JsonSerializable { /** @var bool $isBookedUp */ public $isBookedUp = false; + /** @var IUserManager */ + private $userManager; + public function __construct() { $this->addType('released', 'int'); $this->addType('pollId', 'int'); @@ -106,6 +111,7 @@ class Option extends Entity implements JsonSerializable { $this->addType('order', 'int'); $this->addType('confirmed', 'int'); $this->addType('duration', 'int'); + $this->userManager = Container::queryClass(IUserManager::class); } public function jsonSerialize() { @@ -185,10 +191,10 @@ class Option extends Entity implements JsonSerializable { } return $this->getOwnerIsNoUser() ? $this->getOwner() - : \OC::$server->getUserManager()->get($this->getOwner())->getDisplayName(); + : $this->userManager->get($this->getOwner())->getDisplayName(); } private function getOwnerIsNoUser(): bool { - return !\OC::$server->getUserManager()->get($this->getOwner()) instanceof IUser; + return !$this->userManager->get($this->getOwner()) instanceof IUser; } } diff --git a/lib/Db/Poll.php b/lib/Db/Poll.php index 63371877..3ba05f40 100644 --- a/lib/Db/Poll.php +++ b/lib/Db/Poll.php @@ -27,7 +27,10 @@ namespace OCA\Polls\Db; use JsonSerializable; +use OCA\Polls\Helper\Container; use OCP\IUser; +use OCP\IUserManager; +use OCP\IURLGenerator; use OCP\AppFramework\Db\Entity; use OCA\Polls\Model\UserGroup\User; @@ -152,6 +155,12 @@ class Poll extends Entity implements JsonSerializable { /** @var string $miscSettings*/ protected $miscSettings; + /** @var IURLGenerator */ + private $urlGenerator; + + /** @var IUserManager */ + private $userManager; + public function __construct() { $this->addType('created', 'int'); $this->addType('expire', 'int'); @@ -166,6 +175,8 @@ class Poll extends Entity implements JsonSerializable { $this->addType('important', 'int'); $this->addType('hideBookedUp', 'int'); $this->addType('useNo', 'int'); + $this->urlGenerator = Container::queryClass(IURLGenerator::class); + $this->userManager = Container::queryClass(IUserManager::class); } public function jsonSerialize() { @@ -230,7 +241,7 @@ class Poll extends Entity implements JsonSerializable { } public function getVoteUrl() : string { - return \OC::$server->getURLGenerator()->linkToRouteAbsolute( + return $this->urlGenerator->linkToRouteAbsolute( 'polls.page.vote', ['id' => $this->getId()] ); @@ -272,8 +283,8 @@ class Poll extends Entity implements JsonSerializable { } public function getDisplayName(): string { - return \OC::$server->getUserManager()->get($this->owner) instanceof IUser - ? \OC::$server->getUserManager()->get($this->owner)->getDisplayName() + return $this->userManager->get($this->owner) instanceof IUser + ? $this->userManager->get($this->owner)->getDisplayName() : $this->owner; } diff --git a/lib/Db/Preferences.php b/lib/Db/Preferences.php index b1ff1afe..b879e73c 100644 --- a/lib/Db/Preferences.php +++ b/lib/Db/Preferences.php @@ -27,6 +27,7 @@ use JsonSerializable; use OCA\Dashboard\Service\BackgroundService; use OCA\Polls\Helper\Container; use OCP\AppFramework\Db\Entity; +use OCP\IConfig; /** * @method integer getId() @@ -50,8 +51,12 @@ class Preferences extends Entity implements JsonSerializable { /** @var string $preferences */ protected $preferences; + /** @var IConfig */ + private $config; + public function __construct() { $this->addType('timestamp', 'int'); + $this->config = Container::queryClass(IConfig::class); } public function jsonSerialize() { @@ -69,13 +74,13 @@ class Preferences extends Entity implements JsonSerializable { */ public function getDashboardBackground(): array { if (Container::isAppEnabled('dashboard')) { - $background = \OC::$server->getConfig()->getUserValue($this->userId, 'dashboard', 'background'); + $background = $this->config->getUserValue($this->userId, 'dashboard', 'background'); return [ 'isInstalled' => true, - 'background' => \OC::$server->getConfig()->getUserValue($this->userId, 'dashboard', 'background'), + 'background' => $this->config->getUserValue($this->userId, 'dashboard', 'background'), 'themingDefaultBackground' => $background, 'shippedBackgrounds' => BackgroundService::SHIPPED_BACKGROUNDS, - 'backgroundVersion' => \OC::$server->getConfig()->getUserValue($this->userId, 'dashboard', 'backgroundVersion'), + 'backgroundVersion' => $this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion'), 'theming' => BackgroundService::SHIPPED_BACKGROUNDS[$background]['theming'] ?? 'light', ]; } diff --git a/lib/Db/Share.php b/lib/Db/Share.php index 9083806f..47fd0bfd 100644 --- a/lib/Db/Share.php +++ b/lib/Db/Share.php @@ -26,6 +26,7 @@ namespace OCA\Polls\Db; use JsonSerializable; use OCP\AppFramework\Db\Entity; +use OCP\IURLGenerator; use OCA\Polls\Model\UserGroup\UserBase; use OCA\Polls\Model\Settings\AppSettings; use OCA\Polls\Helper\Container; @@ -115,6 +116,9 @@ class Share extends Entity implements JsonSerializable { /** @var string $miscSettings*/ protected $miscSettings; + /** @var IURLGenerator */ + private $urlGenerator; + /** @var PollMapper */ protected $pollMapper; @@ -125,6 +129,7 @@ class Share extends Entity implements JsonSerializable { $this->addType('pollId', 'int'); $this->addType('invitationSent', 'int'); $this->addType('reminderSent', 'int'); + $this->urlGenerator = Container::queryClass(IURLGenerator::class); $this->appSettings = new AppSettings; } @@ -156,12 +161,12 @@ class Share extends Entity implements JsonSerializable { public function getURL(): string { if (in_array($this->type, [self::TYPE_USER, self::TYPE_ADMIN, self::TYPE_GROUP], true)) { - return \OC::$server->getUrlGenerator()->linkToRouteAbsolute( + return $this->urlGenerator->linkToRouteAbsolute( 'polls.page.vote', ['id' => $this->pollId] ); } elseif ($this->token) { - return \OC::$server->getUrlGenerator()->linkToRouteAbsolute( + return $this->urlGenerator->linkToRouteAbsolute( 'polls.public.vote_page', ['token' => $this->token] ); diff --git a/lib/Db/Vote.php b/lib/Db/Vote.php index fb7c7d88..06be75db 100644 --- a/lib/Db/Vote.php +++ b/lib/Db/Vote.php @@ -26,7 +26,9 @@ namespace OCA\Polls\Db; use JsonSerializable; +use OCA\Polls\Helper\Container; use OCP\IUser; +use OCP\IUserManager; use OCP\AppFramework\Db\Entity; /** @@ -59,10 +61,14 @@ class Vote extends Entity implements JsonSerializable { /** @var string $voteAnswer */ protected $voteAnswer; + /** @var IUserManager */ + private $userManager; + public function __construct() { $this->addType('id', 'int'); $this->addType('pollId', 'int'); $this->addType('voteOptionId', 'int'); + $this->userManager = Container::queryClass(IUserManager::class); } public function jsonSerialize() { @@ -84,10 +90,10 @@ class Vote extends Entity implements JsonSerializable { } return $this->getIsNoUser() ? $this->userId - : \OC::$server->getUserManager()->get($this->userId)->getDisplayName(); + : $this->userManager->get($this->userId)->getDisplayName(); } public function getIsNoUser(): bool { - return !(\OC::$server->getUserManager()->get($this->userId) instanceof IUser); + return !($this->userManager->get($this->userId) instanceof IUser); } } diff --git a/lib/Event/BaseEvent.php b/lib/Event/BaseEvent.php index dc27e8c4..418ed588 100644 --- a/lib/Event/BaseEvent.php +++ b/lib/Event/BaseEvent.php @@ -31,6 +31,7 @@ use OCA\Polls\Db\Poll; use OCA\Polls\Db\Share; use OCA\Polls\Db\Vote; use OCA\Polls\Helper\Container; +use OCP\IUserSession; abstract class BaseEvent extends Event { /** @var string */ @@ -51,12 +52,16 @@ abstract class BaseEvent extends Event { /** @var bool */ protected $log = true; + /** @var IUserSession */ + private $userSession; + public function __construct( $eventObject ) { parent::__construct(); $this->eventObject = $eventObject; $this->poll = Container::queryPoll($this->getPollId()); + $this->userSession = Container::queryClass(IUserSession::class); $this->activitySubjectParams['pollTitle'] = [ 'type' => 'highlight', @@ -91,8 +96,8 @@ abstract class BaseEvent extends Event { } public function getActor(): string { - if (\OC::$server->getUserSession()->isLoggedIn()) { - return \OC::$server->getUserSession()->getUser()->getUID(); + if ($this->userSession->isLoggedIn()) { + return $this->userSession->getUser()->getUID(); } return $this->eventObject->getUserId(); } diff --git a/lib/Helper/Container.php b/lib/Helper/Container.php index f8fd4620..6ede5f1a 100644 --- a/lib/Helper/Container.php +++ b/lib/Helper/Container.php @@ -29,6 +29,7 @@ use OCA\Polls\Db\PollMapper; use OCA\Polls\Db\Share; use OCA\Polls\Db\ShareMapper; use OCP\App\IAppManager; +use OCP\L10N\IFactory; use Psr\Container\ContainerInterface; abstract class Container { @@ -50,6 +51,9 @@ abstract class Container { ->findByPollAndUser($pollId, $userId); } + public static function getL10N(string $lang = null) { + return self::queryClass(IFactory::class)->get('polls', $lang); + } public static function isAppEnabled(string $app) : bool { return self::queryClass(IAppManager::class)->isEnabledForUser($app); } diff --git a/lib/Model/Acl.php b/lib/Model/Acl.php index 3d59255e..b616dfbe 100644 --- a/lib/Model/Acl.php +++ b/lib/Model/Acl.php @@ -33,6 +33,7 @@ use OCA\Polls\Db\PollMapper; use OCA\Polls\Db\VoteMapper; use OCA\Polls\Db\ShareMapper; use OCP\IUserManager; +use OCP\IUserSession; use OCP\IGroupManager; use OCP\AppFramework\Db\DoesNotExistException; @@ -62,6 +63,9 @@ class Acl implements JsonSerializable { /** @var IUserManager */ private $userManager; + /** @var IUserSession */ + private $userSession; + /** @var AppSettings */ private $appSettings; @@ -85,12 +89,14 @@ class Acl implements JsonSerializable { public function __construct( IUserManager $userManager, + IUserSession $userSession, IGroupManager $groupManager, PollMapper $pollMapper, VoteMapper $voteMapper, ShareMapper $shareMapper ) { $this->userManager = $userManager; + $this->userSession = $userSession; $this->groupManager = $groupManager; $this->pollMapper = $pollMapper; $this->voteMapper = $voteMapper; @@ -100,7 +106,6 @@ class Acl implements JsonSerializable { $this->appSettings = new AppSettings; } - /** * load share via token and than call setShare */ @@ -146,7 +151,7 @@ class Acl implements JsonSerializable { } public function getUserId(): string { - return $this->getIsLoggedIn() ? \OC::$server->getUserSession()->getUser()->getUID() : $this->share->getUserId(); + return $this->getIsLoggedIn() ? $this->userSession->getUser()->getUID() : $this->share->getUserId(); } public function validateUserId(string $userId): void { @@ -282,7 +287,7 @@ class Acl implements JsonSerializable { * getIsLogged - Is user logged in to nextcloud? */ private function getIsLoggedIn(): bool { - return \OC::$server->getUserSession()->isLoggedIn(); + return $this->userSession->isLoggedIn(); } /** diff --git a/lib/Model/Mail/MailBase.php b/lib/Model/Mail/MailBase.php index 94596378..d6479a40 100644 --- a/lib/Model/Mail/MailBase.php +++ b/lib/Model/Mail/MailBase.php @@ -30,10 +30,12 @@ use OCA\Polls\Db\Poll; use OCA\Polls\Helper\Container; use OCP\IL10N; use OCP\IUser; +use OCP\IUserManager; use OCA\Polls\Exceptions\InvalidEmailAddress; use OCP\L10N\IFactory; use OCP\Mail\IEMailTemplate; use OCP\Mail\IMailer; +use Psr\Log\LoggerInterface; class MailBase { private const TEMPLATE_CLASS = 'polls.Mail'; @@ -41,6 +43,9 @@ class MailBase { /** @var UserBase */ protected $recipient; + /** @var LoggerInterface */ + protected $logger; + /** @var Poll */ protected $poll; @@ -62,6 +67,9 @@ class MailBase { /** @var IEMailTemplate */ protected $emailTemplate; + /** @var IUserManager */ + private $userManager; + /** @var User */ protected $owner; @@ -73,7 +81,10 @@ class MailBase { $this->poll = $this->getPoll($pollId); $this->recipient = $this->getUser($recipientId); $this->url = $url ?? $this->poll->getVoteUrl(); - + $this->userManager = Container::queryClass(IUserManager::class); + $this->logger = Container::queryClass(LoggerInterface::class); + $this->mailer = Container::queryClass(IMailer::class); + $this->transFactory = Container::queryClass(IFactory::class); $this->initializeClass(); } @@ -84,8 +95,6 @@ class MailBase { $this->url = $this->getShareURL(); } - $this->mailer = Container::queryClass(IMailer::class); - $this->transFactory = Container::queryClass(IFactory::class); $this->trans = $this->transFactory->get( 'polls', $this->recipient->getLanguage() @@ -116,14 +125,14 @@ class MailBase { $message->useTemplate($this->emailTemplate); $this->mailer->send($message); } catch (\Exception $e) { - \OC::$server->getLogger()->error('Error sending Mail to ' . json_encode($this->recipient)); - \OC::$server->getLogger()->alert($e->getMessage()); + $this->logger->error('Error sending Mail to ' . json_encode($this->recipient)); + $this->logger->alert($e->getMessage()); throw $e; } } protected function getUser(string $userId) : UserBase { - if (\OC::$server->getUserManager()->get($userId) instanceof IUser) { + if ($this->userManager->get($userId) instanceof IUser) { // return User object return new User($userId); } diff --git a/lib/Model/UserGroup/Circle.php b/lib/Model/UserGroup/Circle.php index b7dedb50..eaea704e 100644 --- a/lib/Model/UserGroup/Circle.php +++ b/lib/Model/UserGroup/Circle.php @@ -43,7 +43,7 @@ class Circle extends UserBase { $this->icon = self::ICON; $this->circle = Circles::detailsCircle($id); $this->displayName = $this->circle->getName(); - $this->description = \OC::$server->getL10N('polls')->t('Circle'); + $this->description = Container::getL10N()->t('Circle'); $this->richObjectType = 'circle'; } else { throw new CirclesNotEnabledException(); diff --git a/lib/Model/UserGroup/Contact.php b/lib/Model/UserGroup/Contact.php index 728e14e5..f5286b2a 100644 --- a/lib/Model/UserGroup/Contact.php +++ b/lib/Model/UserGroup/Contact.php @@ -27,11 +27,15 @@ use OCA\Polls\Exceptions\MultipleContactsFound; use OCA\Polls\Exceptions\ContactsNotEnabledExceptions; use OCA\Polls\Helper\Container; use OCP\Contacts\IManager as IContactsManager; +use Psr\Log\LoggerInterface; class Contact extends UserBase { public const TYPE = 'contact'; public const ICON = 'icon-mail'; + /** @var LoggerInterface */ + protected $logger; + /** @var array */ private $contact = []; @@ -46,6 +50,7 @@ class Contact extends UserBase { } else { throw new ContactsNotEnabledExceptions(); } + $this->logger = Container::queryClass(LoggerInterface::class); } @@ -84,7 +89,7 @@ class Contact extends UserBase { // Don't throw an error, log the error and take the first entry if (count($contacts) > 1) { // throw new MultipleContactsFound('Multiple contacts found for id ' . $this->id); - \OC::$server->getLogger()->error('Multiple contacts found for id ' . $this->id); + $this->logger->error('Multiple contacts found for id ' . $this->id); } $this->contact = $contacts[0]; @@ -104,7 +109,7 @@ class Contact extends UserBase { if (isset($this->contact['ORG'])) { array_unshift($description, $this->organisation); } - $this->description = count($description) ? implode(", ", $description) : \OC::$server->getL10N('polls')->t('Contact'); + $this->description = count($description) ? implode(", ", $description) : Container::getL10N()->t('Contact'); } public static function isEnabled(): bool { diff --git a/lib/Model/UserGroup/ContactGroup.php b/lib/Model/UserGroup/ContactGroup.php index 8ab50c90..ecd6c64d 100644 --- a/lib/Model/UserGroup/ContactGroup.php +++ b/lib/Model/UserGroup/ContactGroup.php @@ -37,7 +37,7 @@ class ContactGroup extends UserBase { parent::__construct($id, self::TYPE); if (self::isEnabled()) { $this->icon = self::ICON; - $this->description = \OC::$server->getL10N('polls')->t('Contact group'); + $this->description = Container::getL10N()->t('Contact group'); $this->richObjectType = 'addressbook-contact'; } else { throw new ContactsNotEnabledExceptions(); diff --git a/lib/Model/UserGroup/Email.php b/lib/Model/UserGroup/Email.php index 671b40a7..0fc379cc 100644 --- a/lib/Model/UserGroup/Email.php +++ b/lib/Model/UserGroup/Email.php @@ -24,6 +24,8 @@ namespace OCA\Polls\Model\UserGroup; +use OCA\Polls\Helper\Container; + class Email extends UserBase { public const TYPE = 'email'; public const ICON = 'icon-mail'; @@ -33,7 +35,7 @@ class Email extends UserBase { string $displayName = '' ) { parent::__construct($id, self::TYPE); - $this->description = \OC::$server->getL10N('polls')->t('External Email'); + $this->description = Container::getL10N()->t('External Email'); $this->icon = self::ICON; $this->emailAddress = $id; $this->displayName = $displayName ? $displayName : $this->displayName; diff --git a/lib/Model/UserGroup/GenericUser.php b/lib/Model/UserGroup/GenericUser.php index 4d57a2c5..01adb4af 100644 --- a/lib/Model/UserGroup/GenericUser.php +++ b/lib/Model/UserGroup/GenericUser.php @@ -24,6 +24,8 @@ namespace OCA\Polls\Model\UserGroup; +use OCA\Polls\Helper\Container; + class GenericUser extends UserBase { public const TYPE = 'external'; public const ICON_DEFAULT = 'icon-share'; @@ -42,10 +44,10 @@ class GenericUser extends UserBase { if ($type === UserBase::TYPE_PUBLIC) { $this->icon = self::ICON_PUBLIC; - $this->description = \OC::$server->getL10N('polls')->t('Public link'); + $this->description = Container::getL10N()->t('Public link'); } else { $this->icon = self::ICON_DEFAULT; - $this->description = \OC::$server->getL10N('polls')->t('External user'); + $this->description = Container::getL10N()->t('External user'); } } } diff --git a/lib/Model/UserGroup/Group.php b/lib/Model/UserGroup/Group.php index c1a9702a..c2067517 100644 --- a/lib/Model/UserGroup/Group.php +++ b/lib/Model/UserGroup/Group.php @@ -40,7 +40,7 @@ class Group extends UserBase { parent::__construct($id, self::TYPE); $this->icon = self::ICON; $this->group = Container::queryClass(IGroupManager::class)->get($this->id); - $this->description = \OC::$server->getL10N('polls')->t('Group'); + $this->description = Container::getL10N()->t('Group'); $this->displayName = $this->group->getDisplayName(); $this->richObjectType = 'user-group'; } diff --git a/lib/Model/UserGroup/IUserBase.php b/lib/Model/UserGroup/IUserBase.php new file mode 100644 index 00000000..feb2610c --- /dev/null +++ b/lib/Model/UserGroup/IUserBase.php @@ -0,0 +1,155 @@ +<?php +/** + * @copyright Copyright (c) 2021 René Gieling <github@dartcafe.de> + * + * @author René Gieling <github@dartcafe.de> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Polls\Model\UserGroup; + +use DateTimeZone; + +interface IUserBase { + public const TYPE = 'generic'; + public const TYPE_PUBLIC = 'public'; + public const TYPE_EXTERNAL = 'external'; + public const TYPE_CIRCLE = Circle::TYPE; + public const TYPE_CONTACT = Contact::TYPE; + public const TYPE_CONTACTGROUP = ContactGroup::TYPE; + public const TYPE_EMAIL = Email::TYPE; + public const TYPE_GROUP = Group::TYPE; + public const TYPE_USER = User::TYPE; + public const TYPE_ADMIN = Admin::TYPE; + + /** + * @return string + */ + public function getId(): string; + + /** + * @return string + */ + public function getPublicId(): string; + + /** + * @return string + */ + public function getUser(): string; + + /** + * @return string + */ + public function getType(): string; + + /** + * @return string + */ + public function getLanguage(): string; + + /** + * @return DateTimeZone + */ + public function getTimeZone(): DateTimeZone; + + /** + * @return string + */ + public function getLocale(): string; + + /** + * @return string + */ + public function getDisplayName(): string; + + /** + * @return string + */ + public function getDescription(): string; + + /** + * @return string + */ + public function getIcon(): string; + + /** + * @return string + */ + public function getEmailAddress(): string; + + /** + * @return string + */ + public function getOrganisation(): string; + + /** + * @return string[] + */ + public function getCategories(): array; + + /** + * @return bool + */ + public function getIsNoUser(): bool; + + /** + * @return string + */ + public function setType(string $type): string; + + /** + * @return string + */ + public function setDisplayName(string $displayName): string; + + /** + * @return string + */ + public function setDescription(string $description): string; + + /** + * @return string + */ + public function setEmailAddress(string $emailAddress) : string; + + /** + * @return string + */ + public function setLanguage(string $language): string; + + /** + * @return string + */ + public function setLocale(string $locale): string; + + /** + * @return string + */ + public function setOrganisation(string $organisation): string; + + /** + * @return array + */ + public function getRichObjectString() : array; + + /** + * Default is array with self as single element + * @return array + */ + public function getMembers(): array ; +} diff --git a/lib/Model/UserGroup/User.php b/lib/Model/UserGroup/User.php index f33bd4a0..a1b7a756 100644 --- a/lib/Model/UserGroup/User.php +++ b/lib/Model/UserGroup/User.php @@ -25,6 +25,7 @@ namespace OCA\Polls\Model\UserGroup; use DateTimeZone; use OCA\Polls\Helper\Container; +use OCP\IConfig; use OCP\IUserManager; use OCP\IUser; @@ -32,6 +33,9 @@ class User extends UserBase { public const TYPE = 'user'; public const ICON = 'icon-user'; + /** @var IConfig */ + private $config; + /** @var IUser */ private $user; @@ -42,13 +46,14 @@ class User extends UserBase { parent::__construct($id, $type); $this->icon = self::ICON; $this->isNoUser = false; - $this->description = \OC::$server->getL10N('polls')->t('User'); + $this->description = Container::getL10N()->t('User'); + $this->config = Container::queryClass(IConfig::class); $this->user = Container::queryClass(IUserManager::class)->get($this->id); $this->displayName = $this->user->getDisplayName(); $this->emailAddress = $this->user->getEmailAddress(); - $this->language = \OC::$server->getConfig()->getUserValue($this->id, 'core', 'lang'); - $this->locale = \OC::$server->getConfig()->getUserValue($this->id, 'core', 'locale'); + $this->language = $this->config->getUserValue($this->id, 'core', 'lang'); + $this->locale = $this->config->getUserValue($this->id, 'core', 'locale'); } public function isEnabled(): bool { @@ -56,7 +61,7 @@ class User extends UserBase { } public function getTimeZone(): DateTimeZone { - $tz = \OC::$server->getConfig()->getUserValue($this->getId(), 'core', 'timezone'); + $tz = $this->config->getUserValue($this->getId(), 'core', 'timezone'); if ($tz) { return new DateTimeZone($tz); } diff --git a/lib/Model/UserGroup/UserBase.php b/lib/Model/UserGroup/UserBase.php index 4828a2f6..94244649 100644 --- a/lib/Model/UserGroup/UserBase.php +++ b/lib/Model/UserGroup/UserBase.php @@ -101,7 +101,7 @@ class UserBase implements \JsonSerializable { $this->language = $language; $this->locale = $locale; $this->icon = 'icon-share'; - $this->l10n = \OC::$server->getL10N('polls'); + $this->l10n = Container::getL10N(); $this->timezone = Container::queryClass(IDateTimeZone::class); } diff --git a/lib/Provider/ResourceProvider.php b/lib/Provider/ResourceProvider.php index 0bf963bf..1f8177dd 100644 --- a/lib/Provider/ResourceProvider.php +++ b/lib/Provider/ResourceProvider.php @@ -27,7 +27,6 @@ use OCA\Polls\Model\Acl; use OCA\Polls\Db\PollMapper; use OCA\Polls\Exceptions\NotAuthorizedException; use OCP\AppFramework\Db\DoesNotExistException; -use OCP\AppFramework\QueryException; use OCP\Collaboration\Resources\IManager; use OCP\Collaboration\Resources\IProvider; use OCP\Collaboration\Resources\IResource; @@ -46,13 +45,18 @@ class ResourceProvider implements IProvider { /** @var IURLGenerator */ private $urlGenerator; + /** @var IManager */ + private $resourceManager; + public function __construct( PollMapper $pollMapper, Acl $acl, + IManager $resourceManager, IURLGenerator $urlGenerator ) { $this->pollMapper = $pollMapper; $this->urlGenerator = $urlGenerator; + $this->resourceManager = $resourceManager; $this->acl = $acl; } @@ -90,16 +94,11 @@ class ResourceProvider implements IProvider { } public function invalidateAccessCache($pollId = null) { - try { - /** @var IManager $resourceManager */ - $resourceManager = \OC::$server->query(IManager::class); - } catch (QueryException $e) { - } if ($pollId !== null) { - $resource = $resourceManager->getResourceForUser(self::RESOURCE_TYPE, (string)$pollId, null); - $resourceManager->invalidateAccessCacheForResource($resource); + $resource = $this->resourceManager->getResourceForUser(self::RESOURCE_TYPE, (string)$pollId, null); + $this->resourceManager->invalidateAccessCacheForResource($resource); } else { - $resourceManager->invalidateAccessCacheForProvider($this); + $this->resourceManager->invalidateAccessCacheForProvider($this); } } } diff --git a/lib/Service/ActivityService.php b/lib/Service/ActivityService.php index f500d0a4..55eb9cf4 100644 --- a/lib/Service/ActivityService.php +++ b/lib/Service/ActivityService.php @@ -27,6 +27,7 @@ use OCP\Activity\IManager as ActivityManager; use OCP\Activity\IEvent as ActivityEvent; use OCP\EventDispatcher\Event; use OCP\IL10N; +use OCP\IUserSession; use OCP\L10N\IFactory; use OCA\Polls\Db\Share; use OCA\Polls\Event\CommentEvent; @@ -45,10 +46,13 @@ class ActivityService { /** @var IL10N */ protected $trans; + /** @var IUserSession */ + private $userSession; + /** @var string */ protected $shareType; - /** @var string */ + /** @var bool */ protected $userIsActor; /** @var string */ @@ -56,10 +60,12 @@ class ActivityService { public function __construct( IFactory $transFactory, + IUserSession $userSession, ActivityManager $activityManager ) { - $this->transFactory = $transFactory; $this->activityManager = $activityManager; + $this->transFactory = $transFactory; + $this->userSession = $userSession; } public function createActivityEvent(Event $event): ActivityEvent { @@ -80,7 +86,7 @@ class ActivityService { public function getActivityMessage(ActivityEvent $event, string $language, bool $filtered = false) : string { $this->trans = $this->transFactory->get($event->getApp(), $language); - $this->userIsActor = $event->getAuthor() === \OC::$server->getUserSession()->getUser()->getUID(); + $this->userIsActor = $event->getAuthor() === $this->userSession->getUser()->getUID(); $this->eventType = $event->getType(); $parameters = $event->getSubjectParameters(); $this->shareType = $parameters['shareType']['name'] ?? ''; diff --git a/lib/Service/LogService.php b/lib/Service/LogService.php index fac538ed..acba0067 100644 --- a/lib/Service/LogService.php +++ b/lib/Service/LogService.php @@ -25,9 +25,13 @@ namespace OCA\Polls\Service; use OCA\Polls\Db\Log; use OCA\Polls\Db\LogMapper; +use OCP\IUserSession; class LogService { + /** @var IUserSession */ + private $userSession; + /** @var LogMapper */ private $logMapper; @@ -35,11 +39,13 @@ class LogService { private $log; public function __construct( + IUserSession $userSession, LogMapper $logMapper, Log $log ) { $this->logMapper = $logMapper; $this->log = $log; + $this->userSession = $userSession; } /** @@ -50,7 +56,7 @@ class LogService { $this->log->setPollId($pollId); $this->log->setCreated(time()); $this->log->setMessageId($messageId); - $this->log->setUserId($userId ? $userId : \OC::$server->getUserSession()->getUser()->getUID()); + $this->log->setUserId($userId ? $userId : $this->userSession->getUser()->getUID()); return $this->logMapper->insert($this->log); } diff --git a/lib/Service/MailService.php b/lib/Service/MailService.php index b224cdfe..08d2f8f0 100644 --- a/lib/Service/MailService.php +++ b/lib/Service/MailService.php @@ -113,7 +113,7 @@ class MailService { public function resolveEmailAddress(int $pollId, string $userId): string { if ($this->userManager->get($userId) instanceof IUser) { - return \OC::$server->getConfig()->getUserValue($userId, 'settings', 'email'); + return $this->config->getUserValue($userId, 'settings', 'email'); } // if $userId is no site user, eval via shares diff --git a/lib/Service/PollService.php b/lib/Service/PollService.php index 44eeb363..7229a1e2 100644 --- a/lib/Service/PollService.php +++ b/lib/Service/PollService.php @@ -23,6 +23,8 @@ namespace OCA\Polls\Service; +use OCP\IUserSession; +use OCP\IGroupManager; use OCP\AppFramework\Db\DoesNotExistException; use OCP\EventDispatcher\IEventDispatcher; use OCP\Search\ISearchQuery; @@ -53,6 +55,12 @@ class PollService { /** @var IEventDispatcher */ private $eventDispatcher; + /** @var IUserSession */ + private $userSession; + + /** @var IGroupManager */ + private $groupManager; + /** @var PollMapper */ private $pollMapper; @@ -78,6 +86,8 @@ class PollService { Acl $acl, AppSettings $appSettings, IEventDispatcher $eventDispatcher, + IGroupManager $groupManager, + IUserSession $userSession, MailService $mailService, Poll $poll, PollMapper $pollMapper, @@ -88,10 +98,12 @@ class PollService { $this->acl = $acl; $this->appSettings = $appSettings; $this->eventDispatcher = $eventDispatcher; + $this->groupManager = $groupManager; $this->mailService = $mailService; $this->poll = $poll; $this->pollMapper = $pollMapper; $this->userId = $UserId; + $this->userSession = $userSession; $this->voteMapper = $voteMapper; $this->vote = $vote; } @@ -102,7 +114,7 @@ class PollService { public function list(): array { $pollList = []; try { - $polls = $this->pollMapper->findForMe(\OC::$server->getUserSession()->getUser()->getUID()); + $polls = $this->pollMapper->findForMe($this->userSession->getUser()->getUID()); foreach ($polls as $poll) { try { @@ -128,7 +140,7 @@ class PollService { public function search(ISearchQuery $query): array { $pollList = []; try { - $polls = $this->pollMapper->search(\OC::$server->getUserSession()->getUser()->getUID(), $query); + $polls = $this->pollMapper->search($this->userSession->getUser()->getUID(), $query); foreach ($polls as $poll) { try { @@ -152,8 +164,8 @@ class PollService { */ public function listForAdmin(): array { $pollList = []; - $userId = \OC::$server->getUserSession()->getUser()->getUID(); - if (\OC::$server->getGroupManager()->isAdmin($userId)) { + $userId = $this->userSession->getUser()->getUID(); + if ($this->groupManager->isAdmin($userId)) { try { $pollList = $this->pollMapper->findForAdmin($userId); } catch (DoesNotExistException $e) { @@ -173,7 +185,7 @@ class PollService { $this->eventDispatcher->dispatchTyped(new PollTakeOverEvent($this->poll)); - $this->poll->setOwner(\OC::$server->getUserSession()->getUser()->getUID()); + $this->poll->setOwner($this->userSession->getUser()->getUID()); $this->pollMapper->update($this->poll); return $this->poll; @@ -207,7 +219,7 @@ class PollService { $this->poll = new Poll(); $this->poll->setType($type); $this->poll->setCreated(time()); - $this->poll->setOwner(\OC::$server->getUserSession()->getUser()->getUID()); + $this->poll->setOwner($this->userSession->getUser()->getUID()); $this->poll->setTitle($title); $this->poll->setDescription(''); $this->poll->setAccess(Poll::ACCESS_HIDDEN); @@ -313,7 +325,7 @@ class PollService { $this->poll = new Poll(); $this->poll->setCreated(time()); - $this->poll->setOwner(\OC::$server->getUserSession()->getUser()->getUID()); + $this->poll->setOwner($this->userSession->getUser()->getUID()); $this->poll->setTitle('Clone of ' . $origin->getTitle()); $this->poll->setDeleted(0); $this->poll->setAccess(Poll::ACCESS_HIDDEN); diff --git a/lib/Service/ShareService.php b/lib/Service/ShareService.php index cd396a6e..30e17530 100644 --- a/lib/Service/ShareService.php +++ b/lib/Service/ShareService.php @@ -28,6 +28,7 @@ use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\MultipleObjectsReturnedException; use OCP\EventDispatcher\IEventDispatcher; use OCP\IGroupManager; +use OCP\IUserSession; use OCP\Security\ISecureRandom; use OCA\Polls\Exceptions\NotAuthorizedException; @@ -64,6 +65,9 @@ class ShareService { /** @var IGroupManager */ private $groupManager; + /** @var IUserSession */ + private $userSession; + /** @var SystemService */ private $systemService; @@ -73,6 +77,9 @@ class ShareService { /** @var PollMapper */ private $pollMapper; + /** @var ISecureRandom */ + private $secureRandom; + /** @var Share */ private $share; @@ -94,8 +101,10 @@ class ShareService { ?string $UserId, IEventDispatcher $eventDispatcher, IGroupManager $groupManager, - SystemService $systemService, + ISecureRandom $secureRandom, + IUserSession $userSession, ShareMapper $shareMapper, + SystemService $systemService, PollMapper $pollMapper, Share $share, MailService $mailService, @@ -107,13 +116,15 @@ class ShareService { $this->userId = $UserId; $this->eventDispatcher = $eventDispatcher; $this->groupManager = $groupManager; - $this->systemService = $systemService; + $this->secureRandom = $secureRandom; $this->shareMapper = $shareMapper; + $this->systemService = $systemService; $this->pollMapper = $pollMapper; $this->share = $share; $this->mailService = $mailService; $this->acl = $acl; $this->notificationService = $notificationService; + $this->userSession = $userSession; } /** @@ -168,7 +179,7 @@ class ShareService { } break; case Share::TYPE_GROUP: - if (!\OC::$server->getUserSession()->isLoggedIn()) { + if (!$this->userSession->isLoggedIn()) { throw new NotAuthorizedException; } @@ -200,7 +211,7 @@ class ShareService { } // Allow users entering the poll with a public share access - if ($this->share->getType() === Share::TYPE_PUBLIC && \OC::$server->getUserSession()->isLoggedIn()) { + if ($this->share->getType() === Share::TYPE_PUBLIC && $this->userSession->isLoggedIn()) { try { // Test if the user has already access. $this->acl->setPollId($this->share->getPollId()); @@ -209,7 +220,7 @@ class ShareService { // Return the created share return $this->create( $this->share->getPollId(), - UserBase::getUserGroupChild(Share::TYPE_USER, \OC::$server->getUserSession()->getUser()->getUID()), + UserBase::getUserGroupChild(Share::TYPE_USER, $this->userSession->getUser()->getUID()), true ); } @@ -235,7 +246,7 @@ class ShareService { */ private function create(int $pollId, UserBase $userGroup, bool $preventInvitation = false): Share { $preventInvitation = $userGroup->getType() === UserBase::TYPE_PUBLIC ?: $preventInvitation; - $token = \OC::$server->getSecureRandom()->generate( + $token = $this->secureRandom->generate( 16, ISecureRandom::CHAR_DIGITS . ISecureRandom::CHAR_LOWER . diff --git a/lib/Service/SystemService.php b/lib/Service/SystemService.php index 5deb2151..f075640e 100644 --- a/lib/Service/SystemService.php +++ b/lib/Service/SystemService.php @@ -26,6 +26,7 @@ namespace OCA\Polls\Service; use OCA\Polls\Exceptions\TooShortException; use OCA\Polls\Exceptions\InvalidUsernameException; use OCA\Polls\Exceptions\InvalidEmailAddress; +use OCA\Polls\Helper\Container; use OCA\Polls\Db\ShareMapper; use OCA\Polls\Db\VoteMapper; @@ -36,6 +37,7 @@ use OCA\Polls\Model\UserGroup\Email; use OCA\Polls\Model\UserGroup\Group; use OCA\Polls\Model\UserGroup\User; use OCA\Polls\Model\UserGroup\UserBase; +use OCP\IUserManager; class SystemService { @@ -46,11 +48,11 @@ class SystemService { private $shareMapper; public function __construct( - VoteMapper $voteMapper, - ShareMapper $shareMapper + ShareMapper $shareMapper, + VoteMapper $voteMapper ) { - $this->voteMapper = $voteMapper; $this->shareMapper = $shareMapper; + $this->voteMapper = $voteMapper; } /** @@ -84,7 +86,7 @@ class SystemService { */ public static function getSiteUsers(string $query = '', array $skip = []): array { $users = []; - foreach (\OC::$server->getUserManager()->searchDisplayName($query) as $user) { + foreach (Container::queryClass(IUserManager::class)->searchDisplayName($query) as $user) { if (!in_array($user->getUID(), $skip) && $user->isEnabled()) { $users[] = new User($user->getUID()); } |