3 namespace Friendica\Module;
5 use Friendica\BaseModule;
6 use Friendica\App\Authentication;
7 use Friendica\Core\Hook;
8 use Friendica\Core\L10n;
9 use Friendica\Core\Renderer;
10 use Friendica\Core\Session;
11 use Friendica\Database\DBA;
13 use Friendica\Model\Contact;
14 use Friendica\Model\User;
15 use Friendica\Network\HTTPException\ForbiddenException;
18 * Switches current user between delegates/parent user
20 class Delegation extends BaseModule
22 public static function post(array $parameters = [])
29 $orig_record = DI::app()->user;
31 if (Session::get('submanage')) {
32 $user = User::getById(Session::get('submanage'));
33 if (DBA::isResult($user)) {
34 $uid = intval($user['uid']);
39 $identity = intval($_POST['identity'] ?? 0);
47 $manages = DBA::selectToArray('manage', ['mid'], ['uid' => $uid]);
48 foreach ($manages as $manage) {
49 if ($identity == $manage['mid']) {
50 $limited_id = $manage['mid'];
56 $user = User::getById($limited_id);
58 // Check if the target user is one of our children
59 $user = DBA::selectFirst('user', [], ['uid' => $identity, 'parent-uid' => $orig_record['uid']]);
61 // Check if the target user is one of our siblings
62 if (!DBA::isResult($user) && ($orig_record['parent-uid'] != 0)) {
63 $user = DBA::selectFirst('user', [], ['uid' => $identity, 'parent-uid' => $orig_record['parent-uid']]);
66 // Check if it's our parent or our own user
67 if (!DBA::isResult($user)
69 $orig_record['parent-uid'] != 0 && $orig_record['parent-uid'] == $identity
71 $orig_record['uid'] != 0 && $orig_record['uid'] == $identity
74 $user = User::getById($identity);
78 if (!DBA::isResult($user)) {
84 /** @var Authentication $authentication */
85 $authentication = self::getClass(Authentication::class);
86 $authentication->setForUser(DI::app(), $user, true, true);
89 Session::set('submanage', $original_id);
93 Hook::callAll('home_init', $ret);
95 DI::app()->internalRedirect('profile/' . DI::app()->user['nickname']);
99 public static function content(array $parameters = [])
102 throw new ForbiddenException(L10n::t('Permission denied.'));
105 $identities = DI::app()->identities;
107 //getting additinal information for each identity
108 foreach ($identities as $key => $identity) {
109 $thumb = Contact::selectFirst(['thumb'], ['uid' => $identity['uid'], 'self' => true]);
110 if (!DBA::isResult($thumb)) {
114 $identities[$key]['thumb'] = $thumb['thumb'];
116 $identities[$key]['selected'] = ($identity['nickname'] === DI::app()->user['nickname']);
118 $condition = ["`uid` = ? AND `msg` != '' AND NOT (`type` IN (?, ?)) AND NOT `seen`", $identity['uid'], NOTIFY_INTRO, NOTIFY_MAIL];
119 $params = ['distinct' => true, 'expression' => 'parent'];
120 $notifications = DBA::count('notify', $condition, $params);
122 $params = ['distinct' => true, 'expression' => 'convid'];
123 $notifications += DBA::count('mail', ['uid' => $identity['uid'], 'seen' => false], $params);
125 $notifications += DBA::count('intro', ['blocked' => false, 'ignore' => false, 'uid' => $identity['uid']]);
127 $identities[$key]['notifications'] = $notifications;
130 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('delegation.tpl'), [
131 '$title' => L10n::t('Manage Identities and/or Pages'),
132 '$desc' => L10n::t('Toggle between different identities or community/group pages which share your account details or which you have been granted "manage" permissions'),
133 '$choose' => L10n::t('Select an identity to manage: '),
134 '$identities' => $identities,
135 '$submit' => L10n::t('Submit'),