]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Settings/Delegation.php
Merge remote-tracking branch 'upstream/develop' into audience
[friendica.git] / src / Module / Settings / Delegation.php
index d7f49e6a907cec48f0868e8ad4d97f855b06ecb8..134b002c1bc79c85df3820b26d1f9d2f45b07f06 100644 (file)
@@ -1,25 +1,43 @@
 <?php
+/**
+ * @copyright Copyright (C) 2010-2023, the Friendica project
+ *
+ * @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 <https://www.gnu.org/licenses/>.
+ *
+ */
 
 namespace Friendica\Module\Settings;
 
 use Friendica\BaseModule;
 use Friendica\Core\Renderer;
-use Friendica\Core\Session;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\User;
-use Friendica\Module\BaseSettingsModule;
+use Friendica\Module\BaseSettings;
 use Friendica\Network\HTTPException;
 use Friendica\Util\Strings;
 
 /**
  * Account delegation settings module
  */
-class Delegation extends BaseSettingsModule
+class Delegation extends BaseSettings
 {
-       public static function post(array $parameters = [])
+       protected function post(array $request = [])
        {
-               if (!local_user() || !empty(DI::app()->user['uid']) && DI::app()->user['uid'] != local_user()) {
+               if (!DI::app()->isLoggedIn()) {
                        throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
                }
 
@@ -31,23 +49,23 @@ class Delegation extends BaseSettingsModule
                if ($parent_uid != 0) {
                        try {
                                User::getIdFromPasswordAuthentication($parent_uid, $parent_password);
-                               info(DI::l10n()->t('Delegation successfully granted.'));
+                               DI::sysmsg()->addInfo(DI::l10n()->t('Delegation successfully granted.'));
                        } catch (\Exception $ex) {
-                               notice(DI::l10n()->t('Parent user not found, unavailable or password doesn\'t match.'));
+                               DI::sysmsg()->addNotice(DI::l10n()->t('Parent user not found, unavailable or password doesn\'t match.'));
                                return;
                        }
                } else {
-                       info(DI::l10n()->t('Delegation successfully revoked.'));
+                       DI::sysmsg()->addInfo(DI::l10n()->t('Delegation successfully revoked.'));
                }
 
-               DBA::update('user', ['parent-uid' => $parent_uid], ['uid' => local_user()]);
+               DBA::update('user', ['parent-uid' => $parent_uid], ['uid' => DI::userSession()->getLocalUserId()]);
        }
 
-       public static function content(array $parameters = [])
+       protected function content(array $request = []): string
        {
-               parent::content($parameters);
+               parent::content();
 
-               if (!local_user()) {
+               if (!DI::userSession()->getLocalUserId()) {
                        throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
                }
 
@@ -58,39 +76,39 @@ class Delegation extends BaseSettingsModule
                $user_id = $args->get(3);
 
                if ($action === 'add' && $user_id) {
-                       if (Session::get('submanage')) {
-                               notice(DI::l10n()->t('Delegated administrators can view but not change delegation permissions.'));
+                       if (DI::userSession()->getSubManagedUserId()) {
+                               DI::sysmsg()->addNotice(DI::l10n()->t('Delegated administrators can view but not change delegation permissions.'));
                                DI::baseUrl()->redirect('settings/delegation');
                        }
 
                        $user = User::getById($user_id, ['nickname']);
                        if (DBA::isResult($user)) {
                                $condition = [
-                                       'uid' => local_user(),
+                                       'uid' => DI::userSession()->getLocalUserId(),
                                        'nurl' => Strings::normaliseLink(DI::baseUrl() . '/profile/' . $user['nickname'])
                                ];
                                if (DBA::exists('contact', $condition)) {
-                                       DBA::insert('manage', ['uid' => $user_id, 'mid' => local_user()]);
+                                       DBA::insert('manage', ['uid' => $user_id, 'mid' => DI::userSession()->getLocalUserId()]);
                                }
                        } else {
-                               notice(DI::l10n()->t('Delegate user not found.'));
+                               DI::sysmsg()->addNotice(DI::l10n()->t('Delegate user not found.'));
                        }
 
                        DI::baseUrl()->redirect('settings/delegation');
                }
 
                if ($action === 'remove' && $user_id) {
-                       if (Session::get('submanage')) {
-                               notice(DI::l10n()->t('Delegated administrators can view but not change delegation permissions.'));
+                       if (DI::userSession()->getSubManagedUserId()) {
+                               DI::sysmsg()->addNotice(DI::l10n()->t('Delegated administrators can view but not change delegation permissions.'));
                                DI::baseUrl()->redirect('settings/delegation');
                        }
 
-                       DBA::delete('manage', ['uid' => $user_id, 'mid' => local_user()]);
+                       DBA::delete('manage', ['uid' => $user_id, 'mid' => DI::userSession()->getLocalUserId()]);
                        DI::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` = ?)', local_user()]);
+               $delegates = DBA::selectToArray('user', [], ['`uid` IN (SELECT `uid` FROM `manage` WHERE `mid` = ?)', DI::userSession()->getLocalUserId()]);
 
                $uids = [];
                foreach ($delegates as $user) {
@@ -101,11 +119,12 @@ class Delegation extends BaseSettingsModule
                $potentials = [];
                $nicknames = [];
 
-               $condition = ['baseurl' => DI::baseUrl(), 'self' => false, 'uid' => local_user(), 'blocked' => false];
+               $condition = ['baseurl' => DI::baseUrl(), 'self' => false, 'uid' => DI::userSession()->getLocalUserId(), 'blocked' => false];
                $contacts = DBA::select('contact', ['nick'], $condition);
                while ($contact = DBA::fetch($contacts)) {
                        $nicknames[] = $contact['nick'];
                }
+               DBA::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]);
@@ -117,8 +136,8 @@ class Delegation extends BaseSettingsModule
 
                $parent_user = null;
                $parent_password = null;
-               $user = User::getById(local_user(), ['parent-uid', 'email']);
-               if (DBA::isResult($user) && !DBA::exists('user', ['parent-uid' => local_user()])) {
+               $user = User::getById(DI::userSession()->getLocalUserId(), ['parent-uid', 'email']);
+               if (DBA::isResult($user) && !DBA::exists('user', ['parent-uid' => DI::userSession()->getLocalUserId()])) {
                        $parent_uid = $user['parent-uid'];
                        $parents = [0 => DI::l10n()->t('No parent user')];
 
@@ -126,7 +145,7 @@ class Delegation extends BaseSettingsModule
                        $condition = ['email' => $user['email'], 'verified' => true, 'blocked' => false, 'parent-uid' => 0];
                        $parent_users = DBA::selectToArray('user', $fields, $condition);
                        foreach($parent_users as $parent) {
-                               if ($parent['uid'] != local_user()) {
+                               if ($parent['uid'] != DI::userSession()->getLocalUserId()) {
                                        $parents[$parent['uid']] = sprintf('%s (%s)', $parent['username'], $parent['nickname']);
                                }
                        }