3 namespace Friendica\Module;
5 use Friendica\BaseModule;
6 use Friendica\Core\Hook;
7 use Friendica\Core\Renderer;
8 use Friendica\Core\Session;
9 use Friendica\Database\DBA;
11 use Friendica\Model\Contact;
12 use Friendica\Model\Notify\Type;
13 use Friendica\Model\User;
14 use Friendica\Network\HTTPException\ForbiddenException;
17 * Switches current user between delegates/parent user
19 class Delegation extends BaseModule
21 public static function post(array $parameters = [])
28 $orig_record = DI::app()->user;
30 if (Session::get('submanage')) {
31 $user = User::getById(Session::get('submanage'));
32 if (DBA::isResult($user)) {
33 $uid = intval($user['uid']);
38 $identity = intval($_POST['identity'] ?? 0);
46 $manages = DBA::selectToArray('manage', ['mid'], ['uid' => $uid]);
47 foreach ($manages as $manage) {
48 if ($identity == $manage['mid']) {
49 $limited_id = $manage['mid'];
55 $user = User::getById($limited_id);
57 // Check if the target user is one of our children
58 $user = DBA::selectFirst('user', [], ['uid' => $identity, 'parent-uid' => $orig_record['uid']]);
60 // Check if the target user is one of our siblings
61 if (!DBA::isResult($user) && ($orig_record['parent-uid'] != 0)) {
62 $user = DBA::selectFirst('user', [], ['uid' => $identity, 'parent-uid' => $orig_record['parent-uid']]);
65 // Check if it's our parent or our own user
66 if (!DBA::isResult($user)
68 $orig_record['parent-uid'] != 0 && $orig_record['parent-uid'] == $identity
70 $orig_record['uid'] != 0 && $orig_record['uid'] == $identity
73 $user = User::getById($identity);
77 if (!DBA::isResult($user)) {
83 DI::auth()->setForUser(DI::app(), $user, true, true);
86 Session::set('submanage', $original_id);
90 Hook::callAll('home_init', $ret);
92 DI::baseUrl()->redirect('profile/' . DI::app()->user['nickname']);
96 public static function content(array $parameters = [])
99 throw new ForbiddenException(DI::l10n()->t('Permission denied.'));
102 $identities = DI::app()->identities;
104 //getting additinal information for each identity
105 foreach ($identities as $key => $identity) {
106 $thumb = Contact::selectFirst(['thumb'], ['uid' => $identity['uid'], 'self' => true]);
107 if (!DBA::isResult($thumb)) {
111 $identities[$key]['thumb'] = $thumb['thumb'];
113 $identities[$key]['selected'] = ($identity['nickname'] === DI::app()->user['nickname']);
115 $condition = ["`uid` = ? AND `msg` != '' AND NOT (`type` IN (?, ?)) AND NOT `seen`", $identity['uid'], Type::INTRO, NOTIFY_MAIL];
116 $params = ['distinct' => true, 'expression' => 'parent'];
117 $notifications = DBA::count('notify', $condition, $params);
119 $params = ['distinct' => true, 'expression' => 'convid'];
120 $notifications += DBA::count('mail', ['uid' => $identity['uid'], 'seen' => false], $params);
122 $notifications += DBA::count('intro', ['blocked' => false, 'ignore' => false, 'uid' => $identity['uid']]);
124 $identities[$key]['notifications'] = $notifications;
127 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('delegation.tpl'), [
128 '$title' => DI::l10n()->t('Manage Identities and/or Pages'),
129 '$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'),
130 '$choose' => DI::l10n()->t('Select an identity to manage: '),
131 '$identities' => $identities,
132 '$submit' => DI::l10n()->t('Submit'),