3 * @copyright Copyright (C) 2010-2023, 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;
24 use Friendica\BaseModule;
25 use Friendica\Core\Hook;
26 use Friendica\Core\Renderer;
27 use Friendica\Database\DBA;
29 use Friendica\Model\Notification;
30 use Friendica\Model\User;
31 use Friendica\Network\HTTPException\ForbiddenException;
32 use Friendica\Util\Proxy;
35 * Switches current user between delegates/parent user
37 class Delegation extends BaseModule
39 protected function post(array $request = [])
41 if (!DI::userSession()->getLocalUserId()) {
45 $uid = DI::userSession()->getLocalUserId();
46 $orig_record = User::getById(DI::app()->getLoggedInUserId());
48 if (DI::userSession()->getSubManagedUserId()) {
49 $user = User::getById(DI::userSession()->getSubManagedUserId());
50 if (DBA::isResult($user)) {
51 $uid = intval($user['uid']);
56 $identity = intval($_POST['identity'] ?? 0);
64 $manages = DBA::selectToArray('manage', ['mid'], ['uid' => $uid]);
65 foreach ($manages as $manage) {
66 if ($identity == $manage['mid']) {
67 $limited_id = $manage['mid'];
73 $user = User::getById($limited_id);
75 // Check if the target user is one of our children
76 $user = DBA::selectFirst('user', [], ['uid' => $identity, 'parent-uid' => $orig_record['uid']]);
78 // Check if the target user is one of our siblings
79 if (!DBA::isResult($user) && ($orig_record['parent-uid'] != 0)) {
80 $user = DBA::selectFirst('user', [], ['uid' => $identity, 'parent-uid' => $orig_record['parent-uid']]);
83 // Check if it's our parent or our own user
84 if (!DBA::isResult($user)
86 $orig_record['parent-uid'] != 0 && $orig_record['parent-uid'] == $identity
88 $orig_record['uid'] != 0 && $orig_record['uid'] == $identity
91 $user = User::getById($identity);
95 if (!DBA::isResult($user)) {
99 DI::session()->clear();
101 DI::auth()->setForUser(DI::app(), $user, true, true);
104 DI::userSession()->setSubManagedUserId($original_id);
108 Hook::callAll('home_init', $ret);
110 DI::sysmsg()->addNotice($this->t('You are now logged in as %s', $user['username']));
112 DI::baseUrl()->redirect('network');
115 protected function content(array $request = []): string
117 if (!DI::userSession()->getLocalUserId()) {
118 throw new ForbiddenException(DI::l10n()->t('Permission denied.'));
121 $identities = User::identities(DI::userSession()->getSubManagedUserId() ?: DI::userSession()->getLocalUserId());
123 //getting additinal information for each identity
124 foreach ($identities as $key => $identity) {
125 $identities[$key]['thumb'] = User::getAvatarUrl($identity, Proxy::SIZE_THUMB);
127 $identities[$key]['selected'] = ($identity['nickname'] === DI::app()->getLoggedInUserNickname());
129 $condition = ["`msg` != '' AND NOT (`type` IN (?, ?)) AND NOT `seen`", Notification\Type::INTRO, Notification\Type::MAIL];
130 $params = ['distinct' => true, 'expression' => 'parent'];
131 $notifications = DI::notify()->countForUser($identity['uid'], $condition, $params);
133 $params = ['distinct' => true, 'expression' => 'convid'];
134 $notifications += DBA::count('mail', ['uid' => $identity['uid'], 'seen' => false], $params);
136 $notifications += DI::intro()->countActiveForUser($identity['uid']);
138 $identities[$key]['notifications'] = $notifications;
141 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('delegation.tpl'), [
142 '$title' => DI::l10n()->t('Switch between your accounts'),
143 '$settings_label' => DI::l10n()->t('Manage your accounts'),
144 '$desc' => DI::l10n()->t('Toggle between different identities or community/group pages which share your account details or which you have been granted "manage" permissions'),
145 '$choose' => DI::l10n()->t('Select an identity to manage: '),
146 '$identities' => $identities,
147 '$submit' => DI::l10n()->t('Submit'),