3 namespace Friendica\Module\Settings;
5 use Friendica\App\Arguments;
6 use Friendica\BaseModule;
7 use Friendica\Core\L10n;
8 use Friendica\Core\Protocol;
9 use Friendica\Core\Renderer;
10 use Friendica\Core\Session;
11 use Friendica\Core\System;
12 use Friendica\Database\DBA;
13 use Friendica\Model\User;
14 use Friendica\Module\BaseSettingsModule;
15 use Friendica\Network\HTTPException;
16 use Friendica\Util\Strings;
18 class Delegation extends BaseSettingsModule
20 public static function post()
22 if (!local_user() || !empty(self::getApp()->user['uid']) && self::getApp()->user['uid'] != local_user()) {
23 throw new HTTPException\ForbiddenException(L10n::t('Permission denied.'));
26 BaseModule::checkFormSecurityTokenRedirectOnError('settings/delegation', 'delegate');
28 $parent_uid = $_POST['parent_user'] ?? 0;
29 $parent_password = $_POST['parent_password'] ?? '';
31 if ($parent_uid != 0) {
32 $user = DBA::selectFirst('user', ['nickname'], ['uid' => $parent_uid]);
33 if (!DBA::isResult($user)) {
34 notice(L10n::t('Parent user not found.'));
39 User::getIdFromPasswordAuthentication($user['nickname'], $parent_password);
40 info(L10n::t('Delegation successfully granted.'));
41 } catch (\Throwable $ex) {
42 notice(L10n::t('Parent user password doesn\'t match.'));
46 info(L10n::t('Delegation successfully revoked.'));
49 DBA::update('user', ['parent-uid' => $parent_uid], ['uid' => local_user()]);
52 public static function content()
57 throw new HTTPException\ForbiddenException(L10n::t('Permission denied.'));
60 /** @var Arguments $args */
61 $args = self::getClass(Arguments::class);
63 $action = $args->get(2);
64 $user_id = $args->get(3);
65 if ($action === 'add' && $user_id) {
66 if (Session::get('submanage')) {
67 notice(L10n::t('Delegated administrators can view but not change delegation permissions.'));
68 self::getApp()->internalRedirect('settings/delegation');
71 $user = DBA::selectFirst('user', ['nickname'], ['uid' => $user_id]);
72 if (DBA::isResult($user)) {
74 'uid' => local_user(),
75 'nurl' => Strings::normaliseLink(System::baseUrl() . '/profile/' . $user['nickname'])
77 if (DBA::exists('contact', $condition)) {
78 DBA::insert('manage', ['uid' => $user_id, 'mid' => local_user()]);
81 notice(L10n::t('Delegate user not found.'));
84 self::getApp()->internalRedirect('settings/delegation');
87 if ($action === 'remove' && $user_id) {
88 if (Session::get('submanage')) {
89 notice(L10n::t('Delegated administrators can view but not change delegation permissions.'));
90 self::getApp()->internalRedirect('settings/delegation');
93 DBA::delete('manage', ['uid' => $user_id, 'mid' => local_user()]);
94 self::getApp()->internalRedirect('settings/delegation');
97 // find everybody that currently has delegated management to this account/page
98 $delegates = DBA::selectToArray('user', [], ['`uid` IN (SELECT `uid` FROM `manage` WHERE `mid` = ?)', local_user()]);
101 foreach ($delegates as $user) {
102 $uids[] = $user['uid'];
105 // find every contact who might be a candidate for delegation
108 $contacts = DBA::selectToArray(
112 "`self` = 0 AND SUBSTRING_INDEX(`nurl`, '/', 3) = ? AND `uid` = ? AND `network` = ?",
113 Strings::normaliseLink(System::baseUrl()),
120 foreach ($contacts as $contact) {
121 $nicknames[] = "'" . DBA::escape(basename($contact['nurl'])) . "'";
124 // get user records for all potential page delegates who are not already delegates or managers
125 $potentialDelegateUsers = DBA::selectToArray('user', ['uid', 'username', 'nickname'], ['nickname' => $nicknames]);
126 foreach ($potentialDelegateUsers as $user) {
127 if (!in_array($user['uid'], $uids)) {
128 $potentials[] = $user;
134 $parent_password = null;
135 $user = DBA::selectFirst('user', ['parent-uid', 'email'], ['uid' => local_user()]);
136 if (DBA::isResult($user) && !DBA::exists('user', ['parent-uid' => local_user()])) {
137 $parent_uid = $user['parent-uid'];
138 $parents = [0 => L10n::t('No parent user')];
140 $fields = ['uid', 'username', 'nickname'];
141 $condition = ['email' => $user['email'], 'verified' => true, 'blocked' => false, 'parent-uid' => 0];
142 $parent_users = DBA::selectToArray('user', $fields, $condition);
143 foreach($parent_users as $parent) {
144 if ($parent['uid'] != local_user()) {
145 $parents[$parent['uid']] = sprintf('%s (%s)', $parent['username'], $parent['nickname']);
149 $parent_user = ['parent_user', '', $parent_uid, '', $parents];
150 $parent_password = ['parent_password', L10n::t('Parent Password:'), '', L10n::t('Please enter the password of the parent account to legitimize your request.')];
153 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/delegation.tpl'), [
154 '$form_security_token' => BaseModule::getFormSecurityToken('delegate'),
155 '$parent_header' => L10n::t('Parent User'),
156 '$parent_user' => $parent_user,
157 '$parent_password' => $parent_password,
158 '$parent_desc' => L10n::t('Parent users have total control about this account, including the account settings. Please double check whom you give this access.'),
159 '$submit' => L10n::t('Save Settings'),
160 '$header' => L10n::t('Delegate Page Management'),
161 '$delegates_header' => L10n::t('Delegates'),
162 '$base' => System::baseUrl(),
163 '$desc' => L10n::t('Delegates are able to manage all aspects of this account/page except for basic account settings. Please do not delegate your personal account to anybody that you do not trust completely.'),
164 '$head_delegates' => L10n::t('Existing Page Delegates'),
165 '$delegates' => $delegates,
166 '$head_potentials' => L10n::t('Potential Delegates'),
167 '$potentials' => $potentials,
168 '$remove' => L10n::t('Remove'),
169 '$add' => L10n::t('Add'),
170 '$none' => L10n::t('No entries.')