]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Settings/Delegation.php
Refactor Delegation modules
[friendica.git] / src / Module / Settings / Delegation.php
index 3fd5fef49ff46e4ab0e5f425e35f0e281eb5e96a..e122dc62d51a9c6d8c9bfe555628562c32756609 100644 (file)
 
 namespace Friendica\Module\Settings;
 
+use Friendica\App;
 use Friendica\BaseModule;
+use Friendica\Core\L10n;
 use Friendica\Core\Renderer;
-use Friendica\Database\DBA;
-use Friendica\DI;
+use Friendica\Core\Session\Capability\IHandleUserSessions;
+use Friendica\Database\Database;
 use Friendica\Model\User;
 use Friendica\Module\BaseSettings;
+use Friendica\Module\Response;
+use Friendica\Navigation\SystemMessages;
 use Friendica\Network\HTTPException;
+use Friendica\Util\Profiler;
 use Friendica\Util\Strings;
+use Psr\Log\LoggerInterface;
 
 /**
  * Account delegation settings module
  */
 class Delegation extends BaseSettings
 {
+       /** @var SystemMessages */
+       private $systemMessages;
+       /** @var Database */
+       private $db;
+
+       public function __construct(Database $db, SystemMessages $systemMessages, IHandleUserSessions $session, App\Page $page, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
+       {
+               parent::__construct($session, $page, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
+
+               $this->systemMessages = $systemMessages;
+               $this->db             = $db;
+       }
+
        protected function post(array $request = [])
        {
-               if (!DI::app()->isLoggedIn()) {
-                       throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
+               if (!$this->session->isAuthenticated()) {
+                       return;
                }
 
                BaseModule::checkFormSecurityTokenRedirectOnError('settings/delegation', 'delegate');
 
-               $parent_uid = $request['parent_user'] ?? null;
+               $parent_uid      = $request['parent_user'] ?? null;
                $parent_password = $request['parent_password'] ?? '';
 
                if ($parent_uid) {
@@ -51,66 +70,63 @@ class Delegation extends BaseSettings
                                // An integer value will trigger the direct user query on uid in User::getAuthenticationInfo
                                $parent_uid = (int)$parent_uid;
                                User::getIdFromPasswordAuthentication($parent_uid, $parent_password);
-                               DI::sysmsg()->addInfo(DI::l10n()->t('Delegation successfully granted.'));
+                               $this->systemMessages->addInfo($this->t('Delegation successfully granted.'));
                        } catch (\Exception $ex) {
-                               DI::sysmsg()->addNotice(DI::l10n()->t('Parent user not found, unavailable or password doesn\'t match.'));
+                               $this->systemMessages->addNotice($this->t('Parent user not found, unavailable or password doesn\'t match.'));
                                return;
                        }
                } else {
-                       DI::sysmsg()->addInfo(DI::l10n()->t('Delegation successfully revoked.'));
+                       $this->systemMessages->addInfo($this->t('Delegation successfully revoked.'));
                }
 
-               DBA::update('user', ['parent-uid' => $parent_uid], ['uid' => DI::userSession()->getLocalUserId()]);
+               $this->db->update('user', ['parent-uid' => $parent_uid], ['uid' => $this->session->getLocalUserId()]);
        }
 
        protected function content(array $request = []): string
        {
                parent::content();
 
-               if (!DI::userSession()->getLocalUserId()) {
-                       throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
+               if (!$this->session->isAuthenticated()) {
+                       throw new HTTPException\ForbiddenException($this->t('Permission denied.'));
                }
 
-               $args = DI::args();
-
-               // @TODO Replace with router-provided arguments
-               $action = $args->get(2);
-               $user_id = $args->get(3);
+               $action  = $this->parameters['action'] ?? '';
+               $user_id = $this->parameters['user_id'] ?? 0;
 
                if ($action === 'add' && $user_id) {
-                       if (DI::userSession()->getSubManagedUserId()) {
-                               DI::sysmsg()->addNotice(DI::l10n()->t('Delegated administrators can view but not change delegation permissions.'));
-                               DI::baseUrl()->redirect('settings/delegation');
+                       if ($this->session->getSubManagedUserId()) {
+                               $this->systemMessages->addNotice($this->t('Delegated administrators can view but not change delegation permissions.'));
+                               $this->baseUrl->redirect('settings/delegation');
                        }
 
                        $user = User::getById($user_id, ['nickname']);
-                       if (DBA::isResult($user)) {
+                       if ($this->db->isResult($user)) {
                                $condition = [
-                                       'uid' => DI::userSession()->getLocalUserId(),
-                                       'nurl' => Strings::normaliseLink(DI::baseUrl() . '/profile/' . $user['nickname'])
+                                       'uid'  => $this->session->getLocalUserId(),
+                                       'nurl' => Strings::normaliseLink($this->baseUrl . '/profile/' . $user['nickname'])
                                ];
-                               if (DBA::exists('contact', $condition)) {
-                                       DBA::insert('manage', ['uid' => $user_id, 'mid' => DI::userSession()->getLocalUserId()]);
+                               if ($this->db->exists('contact', $condition)) {
+                                       $this->db->insert('manage', ['uid' => $user_id, 'mid' => $this->session->getLocalUserId()]);
                                }
                        } else {
-                               DI::sysmsg()->addNotice(DI::l10n()->t('Delegate user not found.'));
+                               $this->systemMessages->addNotice($this->t('Delegate user not found.'));
                        }
 
-                       DI::baseUrl()->redirect('settings/delegation');
+                       $this->baseUrl->redirect('settings/delegation');
                }
 
                if ($action === 'remove' && $user_id) {
-                       if (DI::userSession()->getSubManagedUserId()) {
-                               DI::sysmsg()->addNotice(DI::l10n()->t('Delegated administrators can view but not change delegation permissions.'));
-                               DI::baseUrl()->redirect('settings/delegation');
+                       if ($this->session->getSubManagedUserId()) {
+                               $this->systemMessages->addNotice($this->t('Delegated administrators can view but not change delegation permissions.'));
+                               $this->baseUrl->redirect('settings/delegation');
                        }
 
-                       DBA::delete('manage', ['uid' => $user_id, 'mid' => DI::userSession()->getLocalUserId()]);
-                       DI::baseUrl()->redirect('settings/delegation');
+                       $this->db->delete('manage', ['uid' => $user_id, 'mid' => $this->session->getLocalUserId()]);
+                       $this->baseUrl->redirect('settings/delegation');
                }
 
                // find everybody that currently has delegated management to this account/page
-               $delegates = DBA::selectToArray('user', [], ['`uid` IN (SELECT `uid` FROM `manage` WHERE `mid` = ?)', DI::userSession()->getLocalUserId()]);
+               $delegates = $this->db->selectToArray('user', [], ['`uid` IN (SELECT `uid` FROM `manage` WHERE `mid` = ?)', $this->session->getLocalUserId()]);
 
                $uids = [];
                foreach ($delegates as $user) {
@@ -119,69 +135,76 @@ class Delegation extends BaseSettings
 
                // find every contact who might be a candidate for delegation
                $potentials = [];
-               $nicknames = [];
+               $nicknames  = [];
 
-               $condition = ['baseurl' => DI::baseUrl(), 'self' => false, 'uid' => DI::userSession()->getLocalUserId(), 'blocked' => false];
-               $contacts = DBA::select('contact', ['nick'], $condition);
-               while ($contact = DBA::fetch($contacts)) {
+               $condition = ['baseurl' => $this->baseUrl, 'self' => false, 'uid' => $this->session->getLocalUserId(), 'blocked' => false];
+               $contacts  = $this->db->select('contact', ['nick'], $condition);
+               while ($contact = $this->db->fetch($contacts)) {
                        $nicknames[] = $contact['nick'];
                }
-               DBA::close($contacts);
+               $this->db->close($contacts);
 
                // get user records for all potential page delegates who are not already delegates or managers
-               $potentialDelegateUsers = DBA::selectToArray('user', ['uid', 'username', 'nickname'], ['nickname' => $nicknames]);
+               $potentialDelegateUsers = $this->db->selectToArray(
+                       'user',
+                       ['uid', 'username', 'nickname'],
+                       [
+                               'nickname'        => $nicknames,
+                               'account_removed' => false,
+                               'account_expired' => false,
+                               'blocked'         => false,
+                       ]
+               );
                foreach ($potentialDelegateUsers as $user) {
                        if (!in_array($user['uid'], $uids)) {
                                $potentials[] = $user;
                        }
                }
 
-               $parent_user = null;
+               $parent_user     = null;
                $parent_password = null;
-               $user = User::getById(DI::userSession()->getLocalUserId(), ['parent-uid', 'email']);
-               if (DBA::isResult($user) && !DBA::exists('user', ['parent-uid' => DI::userSession()->getLocalUserId()])) {
+               $user            = User::getById($this->session->getLocalUserId(), ['parent-uid', 'email']);
+               if ($this->db->isResult($user) && !$this->db->exists('user', ['parent-uid' => $this->session->getLocalUserId()])) {
                        $parent_uid = $user['parent-uid'];
-                       $parents = [0 => DI::l10n()->t('No parent user')];
+                       $parents    = [0 => $this->t('No parent user')];
 
-                       $fields = ['uid', 'username', 'nickname'];
-                       $condition = ['email' => $user['email'], 'verified' => true, 'blocked' => false, 'parent-uid' => null];
-                       $parent_users = DBA::selectToArray('user', $fields, $condition);
-                       foreach($parent_users as $parent) {
-                               if ($parent['uid'] != DI::userSession()->getLocalUserId()) {
+                       $fields       = ['uid', 'username', 'nickname'];
+                       $condition    = ['email' => $user['email'], 'verified' => true, 'blocked' => false, 'parent-uid' => null];
+                       $parent_users = $this->db->selectToArray('user', $fields, $condition);
+                       foreach ($parent_users as $parent) {
+                               if ($parent['uid'] != $this->session->getLocalUserId()) {
                                        $parents[$parent['uid']] = sprintf('%s (%s)', $parent['username'], $parent['nickname']);
                                }
                        }
 
-                       $parent_user = ['parent_user', DI::l10n()->t('Parent User'), $parent_uid, '', $parents];
-                       $parent_password = ['parent_password', DI::l10n()->t('Parent Password:'), '', DI::l10n()->t('Please enter the password of the parent account to legitimize your request.')];
+                       $parent_user     = ['parent_user', $this->t('Parent User'), $parent_uid, '', $parents];
+                       $parent_password = ['parent_password', $this->t('Parent Password:'), '', $this->t('Please enter the password of the parent account to legitimize your request.')];
                }
 
                $is_child_user = !empty($user['parent-uid']);
 
-               $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/delegation.tpl'), [
+               return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/delegation.tpl'), [
+                       '$l10n' => [
+                               'account_header'   => $this->t('Additional Accounts'),
+                               'account_desc'     => $this->t('Register additional accounts that are automatically connected to your existing account so you can manage them from this account.'),
+                               'add_account'      => $this->t('Register an additional account'),
+                               'parent_header'    => $this->t('Parent User'),
+                               'parent_desc'      => $this->t('Parent users have total control about this account, including the account settings. Please double check whom you give this access.'),
+                               'submit'           => $this->t('Save Settings'),
+                               'header'           => $this->t('Manage Accounts'),
+                               'delegates_header' => $this->t('Delegates'),
+                               'desc'             => $this->t('Delegates are able to manage all aspects of this account/page except for basic account settings. Please do not delegate your personal account to anybody that you do not trust completely.'),
+                               'head_delegates'   => $this->t('Existing Page Delegates'),
+                               'head_potentials'  => $this->t('Potential Delegates'),
+                               'none'             => $this->t('No entries.'),
+                       ],
+
                        '$form_security_token' => BaseModule::getFormSecurityToken('delegate'),
-                       '$account_header' => DI::l10n()->t('Additional Accounts'),
-                       '$account_desc' => DI::l10n()->t('Register additional accounts that are automatically connected to your existing account so you can manage them from this account.'),
-                       '$add_account' => DI::l10n()->t('Register an additional account'),
-                       '$parent_header' => DI::l10n()->t('Parent User'),
-                       '$parent_user' => $parent_user,
-                       '$parent_password' => $parent_password,
-                       '$parent_desc' => DI::l10n()->t('Parent users have total control about this account, including the account settings. Please double check whom you give this access.'),
-                       '$is_child_user' => $is_child_user,
-                       '$submit' => DI::l10n()->t('Save Settings'),
-                       '$header' => DI::l10n()->t('Manage Accounts'),
-                       '$delegates_header' => DI::l10n()->t('Delegates'),
-                       '$base' => DI::baseUrl(),
-                       '$desc' => DI::l10n()->t('Delegates are able to manage all aspects of this account/page except for basic account settings. Please do not delegate your personal account to anybody that you do not trust completely.'),
-                       '$head_delegates' => DI::l10n()->t('Existing Page Delegates'),
-                       '$delegates' => $delegates,
-                       '$head_potentials' => DI::l10n()->t('Potential Delegates'),
-                       '$potentials' => $potentials,
-                       '$remove' => DI::l10n()->t('Remove'),
-                       '$add' => DI::l10n()->t('Add'),
-                       '$none' => DI::l10n()->t('No entries.')
+                       '$parent_user'         => $parent_user,
+                       '$parent_password'     => $parent_password,
+                       '$is_child_user'       => $is_child_user,
+                       '$delegates'           => $delegates,
+                       '$potentials'          => $potentials,
                ]);
-
-               return $o;
        }
 }