3 * @copyright Copyright (C) 2010-2024, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module\User;
25 use Friendica\BaseModule;
26 use Friendica\Contact\Introduction\Repository\Introduction;
27 use Friendica\Core\Hook;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Renderer;
30 use Friendica\Core\Session\Capability\IHandleUserSessions;
31 use Friendica\Database\Database;
32 use Friendica\Model\Notification;
33 use Friendica\Model\User;
34 use Friendica\Module\Response;
35 use Friendica\Navigation\Notifications\Repository\Notify;
36 use Friendica\Navigation\SystemMessages;
37 use Friendica\Network\HTTPException\ForbiddenException;
38 use Friendica\Security\Authentication;
40 use Psr\Log\LoggerInterface;
43 * Switches current user between delegates/parent user
45 class Delegation extends BaseModule
47 /** @var IHandleUserSessions */
51 /** @var Authentication */
53 /** @var SystemMessages */
54 private $systemMessages;
57 /** @var Introduction */
62 public function __construct(App $app, Introduction $intro, Notify $notify, SystemMessages $systemMessages, Authentication $auth, Database $db, IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Util\Profiler $profiler, Response $response, array $server, array $parameters = [])
64 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
66 $this->session = $session;
69 $this->systemMessages = $systemMessages;
70 $this->notify = $notify;
71 $this->intro = $intro;
75 protected function post(array $request = [])
77 if (!$this->session->getLocalUserId()) {
81 $uid = $this->session->getLocalUserId();
82 $orig_record = User::getById($this->session->getLocalUserId());
84 if ($this->session->getSubManagedUserId()) {
85 $user = User::getById($this->session->getSubManagedUserId());
86 if ($this->db->isResult($user)) {
87 $uid = intval($user['uid']);
92 $identity = intval($request['identity'] ?? 0);
100 $manages = $this->db->selectToArray('manage', ['mid'], ['uid' => $uid]);
101 foreach ($manages as $manage) {
102 if ($identity == $manage['mid']) {
103 $limited_id = $manage['mid'];
109 $user = User::getById($limited_id);
111 // Check if the target user is one of our children
112 $user = $this->db->selectFirst('user', [], ['uid' => $identity, 'parent-uid' => $orig_record['uid']]);
114 // Check if the target user is one of our siblings
115 if (!$this->db->isResult($user) && $orig_record['parent-uid']) {
116 $user = $this->db->selectFirst('user', [], ['uid' => $identity, 'parent-uid' => $orig_record['parent-uid']]);
119 // Check if it's our parent or our own user
120 if (!$this->db->isResult($user)
122 $orig_record['parent-uid'] && $orig_record['parent-uid'] === $identity
124 $orig_record['uid'] && $orig_record['uid'] === $identity
127 $user = User::getById($identity);
131 if (!$this->db->isResult($user)) {
135 $this->session->clear();
137 $this->auth->setForUser($this->app, $user, true, true);
140 $this->session->setSubManagedUserId($original_id);
144 Hook::callAll('home_init', $ret);
146 $this->systemMessages->addNotice($this->t('You are now logged in as %s', $user['username']));
148 $this->baseUrl->redirect('network');
151 protected function content(array $request = []): string
153 if (!$this->session->getLocalUserId()) {
154 throw new ForbiddenException($this->t('Permission denied.'));
157 $identities = User::identities($this->session->getSubManagedUserId() ?: $this->session->getLocalUserId());
159 //getting additional information for each identity
160 foreach ($identities as $key => $identity) {
161 $identities[$key]['thumb'] = User::getAvatarUrl($identity, Util\Proxy::SIZE_THUMB);
163 $identities[$key]['selected'] = ($identity['nickname'] === $this->session->getLocalUserNickname());
165 $notifications = $this->notify->countForUser(
167 ["`msg` != '' AND NOT (`type` IN (?, ?)) AND NOT `seen`", Notification\Type::INTRO, Notification\Type::MAIL],
168 ['distinct' => true, 'expression' => 'parent']
171 $notifications += $this->db->count(
173 ['uid' => $identity['uid'], 'seen' => false],
174 ['distinct' => true, 'expression' => 'convid']
177 $notifications += $this->intro->countActiveForUser($identity['uid']);
179 $identities[$key]['notifications'] = $notifications;
182 $tpl = Renderer::getMarkupTemplate('delegation.tpl');
183 return Renderer::replaceMacros($tpl, [
185 'title' => $this->t('Switch between your accounts'),
186 'settings_label' => $this->t('Manage your accounts'),
187 'desc' => $this->t('Toggle between different identities or community/group pages which share your account details or which you have been granted "manage" permissions'),
188 'choose' => $this->t('Select an identity to manage: '),
189 'submit' => $this->t('Submit'),
192 '$identities' => $identities,