3 namespace Friendica\Module\Settings;
5 use Friendica\BaseModule;
6 use Friendica\Core\Renderer;
7 use Friendica\Core\Session;
8 use Friendica\Database\DBA;
10 use Friendica\Model\User;
11 use Friendica\Module\BaseSettingsModule;
12 use Friendica\Network\HTTPException;
13 use Friendica\Util\Strings;
16 * Account delegation settings module
18 class Delegation extends BaseSettingsModule
20 public static function post(array $parameters = [])
22 if (!local_user() || !empty(DI::app()->user['uid']) && DI::app()->user['uid'] != local_user()) {
23 throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
26 BaseModule::checkFormSecurityTokenRedirectOnError('settings/delegation', 'delegate');
28 $parent_uid = (int)$_POST['parent_user'] ?? 0;
29 $parent_password = $_POST['parent_password'] ?? '';
31 if ($parent_uid != 0) {
33 User::getIdFromPasswordAuthentication($parent_uid, $parent_password);
34 info(DI::l10n()->t('Delegation successfully granted.'));
35 } catch (\Exception $ex) {
36 notice(DI::l10n()->t('Parent user not found, unavailable or password doesn\'t match.'));
40 info(DI::l10n()->t('Delegation successfully revoked.'));
43 DBA::update('user', ['parent-uid' => $parent_uid], ['uid' => local_user()]);
46 public static function content(array $parameters = [])
48 parent::content($parameters);
51 throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
56 // @TODO Replace with router-provided arguments
57 $action = $args->get(2);
58 $user_id = $args->get(3);
60 if ($action === 'add' && $user_id) {
61 if (Session::get('submanage')) {
62 notice(DI::l10n()->t('Delegated administrators can view but not change delegation permissions.'));
63 DI::baseUrl()->redirect('settings/delegation');
66 $user = User::getById($user_id, ['nickname']);
67 if (DBA::isResult($user)) {
69 'uid' => local_user(),
70 'nurl' => Strings::normaliseLink(DI::baseUrl() . '/profile/' . $user['nickname'])
72 if (DBA::exists('contact', $condition)) {
73 DBA::insert('manage', ['uid' => $user_id, 'mid' => local_user()]);
76 notice(DI::l10n()->t('Delegate user not found.'));
79 DI::baseUrl()->redirect('settings/delegation');
82 if ($action === 'remove' && $user_id) {
83 if (Session::get('submanage')) {
84 notice(DI::l10n()->t('Delegated administrators can view but not change delegation permissions.'));
85 DI::baseUrl()->redirect('settings/delegation');
88 DBA::delete('manage', ['uid' => $user_id, 'mid' => local_user()]);
89 DI::baseUrl()->redirect('settings/delegation');
92 // find everybody that currently has delegated management to this account/page
93 $delegates = DBA::selectToArray('user', [], ['`uid` IN (SELECT `uid` FROM `manage` WHERE `mid` = ?)', local_user()]);
96 foreach ($delegates as $user) {
97 $uids[] = $user['uid'];
100 // find every contact who might be a candidate for delegation
104 $condition = ['baseurl' => DI::baseUrl(), 'self' => false, 'uid' => local_user(), 'blocked' => false];
105 $contacts = DBA::select('contact', ['nick'], $condition);
106 while ($contact = DBA::fetch($contacts)) {
107 $nicknames[] = $contact['nick'];
110 // get user records for all potential page delegates who are not already delegates or managers
111 $potentialDelegateUsers = DBA::selectToArray('user', ['uid', 'username', 'nickname'], ['nickname' => $nicknames]);
112 foreach ($potentialDelegateUsers as $user) {
113 if (!in_array($user['uid'], $uids)) {
114 $potentials[] = $user;
119 $parent_password = null;
120 $user = User::getById(local_user(), ['parent-uid', 'email']);
121 if (DBA::isResult($user) && !DBA::exists('user', ['parent-uid' => local_user()])) {
122 $parent_uid = $user['parent-uid'];
123 $parents = [0 => DI::l10n()->t('No parent user')];
125 $fields = ['uid', 'username', 'nickname'];
126 $condition = ['email' => $user['email'], 'verified' => true, 'blocked' => false, 'parent-uid' => 0];
127 $parent_users = DBA::selectToArray('user', $fields, $condition);
128 foreach($parent_users as $parent) {
129 if ($parent['uid'] != local_user()) {
130 $parents[$parent['uid']] = sprintf('%s (%s)', $parent['username'], $parent['nickname']);
134 $parent_user = ['parent_user', '', $parent_uid, '', $parents];
135 $parent_password = ['parent_password', DI::l10n()->t('Parent Password:'), '', DI::l10n()->t('Please enter the password of the parent account to legitimize your request.')];
138 $is_child_user = !empty($user['parent-uid']);
140 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/delegation.tpl'), [
141 '$form_security_token' => BaseModule::getFormSecurityToken('delegate'),
142 '$account_header' => DI::l10n()->t('Additional Accounts'),
143 '$account_desc' => DI::l10n()->t('Register additional accounts that are automatically connected to your existing account so you can manage it from this account.'),
144 '$add_account' => DI::l10n()->t('Register an additional account'),
145 '$parent_header' => DI::l10n()->t('Parent User'),
146 '$parent_user' => $parent_user,
147 '$parent_password' => $parent_password,
148 '$parent_desc' => DI::l10n()->t('Parent users have total control about this account, including the account settings. Please double check whom you give this access.'),
149 '$is_child_user' => $is_child_user,
150 '$submit' => DI::l10n()->t('Save Settings'),
151 '$header' => DI::l10n()->t('Manage Accounts'),
152 '$delegates_header' => DI::l10n()->t('Delegates'),
153 '$base' => DI::baseUrl(),
154 '$desc' => DI::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.'),
155 '$head_delegates' => DI::l10n()->t('Existing Page Delegates'),
156 '$delegates' => $delegates,
157 '$head_potentials' => DI::l10n()->t('Potential Delegates'),
158 '$potentials' => $potentials,
159 '$remove' => DI::l10n()->t('Remove'),
160 '$add' => DI::l10n()->t('Add'),
161 '$none' => DI::l10n()->t('No entries.')