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;
14 use Friendica\Model\User;
15 use Friendica\Module\BaseSettingsModule;
16 use Friendica\Network\HTTPException;
17 use Friendica\Util\Strings;
20 * Account delegation settings module
22 class Delegation extends BaseSettingsModule
24 public static function post(array $parameters = [])
26 if (!local_user() || !empty(DI::app()->user['uid']) && DI::app()->user['uid'] != local_user()) {
27 throw new HTTPException\ForbiddenException(L10n::t('Permission denied.'));
30 BaseModule::checkFormSecurityTokenRedirectOnError('settings/delegation', 'delegate');
32 $parent_uid = $_POST['parent_user'] ?? 0;
33 $parent_password = $_POST['parent_password'] ?? '';
35 if ($parent_uid != 0) {
37 User::getIdFromPasswordAuthentication($parent_uid, $parent_password);
38 info(L10n::t('Delegation successfully granted.'));
39 } catch (\Exception $ex) {
40 notice(L10n::t('Parent user not found, unavailable or password doesn\'t match.'));
44 info(L10n::t('Delegation successfully revoked.'));
47 DBA::update('user', ['parent-uid' => $parent_uid], ['uid' => local_user()]);
50 public static function content(array $parameters = [])
52 parent::content($parameters);
55 throw new HTTPException\ForbiddenException(L10n::t('Permission denied.'));
60 // @TODO Replace with router-provided arguments
61 $action = $args->get(2);
62 $user_id = $args->get(3);
64 if ($action === 'add' && $user_id) {
65 if (Session::get('submanage')) {
66 notice(L10n::t('Delegated administrators can view but not change delegation permissions.'));
67 DI::baseUrl()->redirect('settings/delegation');
70 $user = User::getById($user_id, ['nickname']);
71 if (DBA::isResult($user)) {
73 'uid' => local_user(),
74 'nurl' => Strings::normaliseLink(System::baseUrl() . '/profile/' . $user['nickname'])
76 if (DBA::exists('contact', $condition)) {
77 DBA::insert('manage', ['uid' => $user_id, 'mid' => local_user()]);
80 notice(L10n::t('Delegate user not found.'));
83 DI::baseUrl()->redirect('settings/delegation');
86 if ($action === 'remove' && $user_id) {
87 if (Session::get('submanage')) {
88 notice(L10n::t('Delegated administrators can view but not change delegation permissions.'));
89 DI::baseUrl()->redirect('settings/delegation');
92 DBA::delete('manage', ['uid' => $user_id, 'mid' => local_user()]);
93 DI::baseUrl()->redirect('settings/delegation');
96 // find everybody that currently has delegated management to this account/page
97 $delegates = DBA::selectToArray('user', [], ['`uid` IN (SELECT `uid` FROM `manage` WHERE `mid` = ?)', local_user()]);
100 foreach ($delegates as $user) {
101 $uids[] = $user['uid'];
104 // find every contact who might be a candidate for delegation
107 $contacts = DBA::selectToArray(
111 "`self` = 0 AND SUBSTRING_INDEX(`nurl`, '/', 3) = ? AND `uid` = ? AND `network` = ?",
112 Strings::normaliseLink(System::baseUrl()),
119 foreach ($contacts as $contact) {
120 $nicknames[] = "'" . DBA::escape(basename($contact['nurl'])) . "'";
123 // get user records for all potential page delegates who are not already delegates or managers
124 $potentialDelegateUsers = DBA::selectToArray('user', ['uid', 'username', 'nickname'], ['nickname' => $nicknames]);
125 foreach ($potentialDelegateUsers as $user) {
126 if (!in_array($user['uid'], $uids)) {
127 $potentials[] = $user;
133 $parent_password = null;
134 $user = User::getById(local_user(), ['parent-uid', 'email']);
135 if (DBA::isResult($user) && !DBA::exists('user', ['parent-uid' => local_user()])) {
136 $parent_uid = $user['parent-uid'];
137 $parents = [0 => L10n::t('No parent user')];
139 $fields = ['uid', 'username', 'nickname'];
140 $condition = ['email' => $user['email'], 'verified' => true, 'blocked' => false, 'parent-uid' => 0];
141 $parent_users = DBA::selectToArray('user', $fields, $condition);
142 foreach($parent_users as $parent) {
143 if ($parent['uid'] != local_user()) {
144 $parents[$parent['uid']] = sprintf('%s (%s)', $parent['username'], $parent['nickname']);
148 $parent_user = ['parent_user', '', $parent_uid, '', $parents];
149 $parent_password = ['parent_password', L10n::t('Parent Password:'), '', L10n::t('Please enter the password of the parent account to legitimize your request.')];
152 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/delegation.tpl'), [
153 '$form_security_token' => BaseModule::getFormSecurityToken('delegate'),
154 '$parent_header' => L10n::t('Parent User'),
155 '$parent_user' => $parent_user,
156 '$parent_password' => $parent_password,
157 '$parent_desc' => L10n::t('Parent users have total control about this account, including the account settings. Please double check whom you give this access.'),
158 '$submit' => L10n::t('Save Settings'),
159 '$header' => L10n::t('Delegate Page Management'),
160 '$delegates_header' => L10n::t('Delegates'),
161 '$base' => System::baseUrl(),
162 '$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.'),
163 '$head_delegates' => L10n::t('Existing Page Delegates'),
164 '$delegates' => $delegates,
165 '$head_potentials' => L10n::t('Potential Delegates'),
166 '$potentials' => $potentials,
167 '$remove' => L10n::t('Remove'),
168 '$add' => L10n::t('Add'),
169 '$none' => L10n::t('No entries.')