3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module\Settings;
24 use Friendica\BaseModule;
25 use Friendica\Core\Renderer;
26 use Friendica\Core\Session;
27 use Friendica\Database\DBA;
29 use Friendica\Model\User;
30 use Friendica\Module\BaseSettings;
31 use Friendica\Network\HTTPException;
32 use Friendica\Util\Strings;
35 * Account delegation settings module
37 class Delegation extends BaseSettings
39 public static function post(array $parameters = [])
41 if (!local_user() || !empty(DI::app()->user['uid']) && DI::app()->user['uid'] != local_user()) {
42 throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
45 BaseModule::checkFormSecurityTokenRedirectOnError('settings/delegation', 'delegate');
47 $parent_uid = (int)$_POST['parent_user'] ?? 0;
48 $parent_password = $_POST['parent_password'] ?? '';
50 if ($parent_uid != 0) {
52 User::getIdFromPasswordAuthentication($parent_uid, $parent_password);
53 info(DI::l10n()->t('Delegation successfully granted.'));
54 } catch (\Exception $ex) {
55 notice(DI::l10n()->t('Parent user not found, unavailable or password doesn\'t match.'));
59 info(DI::l10n()->t('Delegation successfully revoked.'));
62 DBA::update('user', ['parent-uid' => $parent_uid], ['uid' => local_user()]);
65 public static function content(array $parameters = [])
67 parent::content($parameters);
70 throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
75 // @TODO Replace with router-provided arguments
76 $action = $args->get(2);
77 $user_id = $args->get(3);
79 if ($action === 'add' && $user_id) {
80 if (Session::get('submanage')) {
81 notice(DI::l10n()->t('Delegated administrators can view but not change delegation permissions.'));
82 DI::baseUrl()->redirect('settings/delegation');
85 $user = User::getById($user_id, ['nickname']);
86 if (DBA::isResult($user)) {
88 'uid' => local_user(),
89 'nurl' => Strings::normaliseLink(DI::baseUrl() . '/profile/' . $user['nickname'])
91 if (DBA::exists('contact', $condition)) {
92 DBA::insert('manage', ['uid' => $user_id, 'mid' => local_user()]);
95 notice(DI::l10n()->t('Delegate user not found.'));
98 DI::baseUrl()->redirect('settings/delegation');
101 if ($action === 'remove' && $user_id) {
102 if (Session::get('submanage')) {
103 notice(DI::l10n()->t('Delegated administrators can view but not change delegation permissions.'));
104 DI::baseUrl()->redirect('settings/delegation');
107 DBA::delete('manage', ['uid' => $user_id, 'mid' => local_user()]);
108 DI::baseUrl()->redirect('settings/delegation');
111 // find everybody that currently has delegated management to this account/page
112 $delegates = DBA::selectToArray('user', [], ['`uid` IN (SELECT `uid` FROM `manage` WHERE `mid` = ?)', local_user()]);
115 foreach ($delegates as $user) {
116 $uids[] = $user['uid'];
119 // find every contact who might be a candidate for delegation
123 $condition = ['baseurl' => DI::baseUrl(), 'self' => false, 'uid' => local_user(), 'blocked' => false];
124 $contacts = DBA::select('contact', ['nick'], $condition);
125 while ($contact = DBA::fetch($contacts)) {
126 $nicknames[] = $contact['nick'];
129 // get user records for all potential page delegates who are not already delegates or managers
130 $potentialDelegateUsers = DBA::selectToArray('user', ['uid', 'username', 'nickname'], ['nickname' => $nicknames]);
131 foreach ($potentialDelegateUsers as $user) {
132 if (!in_array($user['uid'], $uids)) {
133 $potentials[] = $user;
138 $parent_password = null;
139 $user = User::getById(local_user(), ['parent-uid', 'email']);
140 if (DBA::isResult($user) && !DBA::exists('user', ['parent-uid' => local_user()])) {
141 $parent_uid = $user['parent-uid'];
142 $parents = [0 => DI::l10n()->t('No parent user')];
144 $fields = ['uid', 'username', 'nickname'];
145 $condition = ['email' => $user['email'], 'verified' => true, 'blocked' => false, 'parent-uid' => 0];
146 $parent_users = DBA::selectToArray('user', $fields, $condition);
147 foreach($parent_users as $parent) {
148 if ($parent['uid'] != local_user()) {
149 $parents[$parent['uid']] = sprintf('%s (%s)', $parent['username'], $parent['nickname']);
153 $parent_user = ['parent_user', DI::l10n()->t('Parent User'), $parent_uid, '', $parents];
154 $parent_password = ['parent_password', DI::l10n()->t('Parent Password:'), '', DI::l10n()->t('Please enter the password of the parent account to legitimize your request.')];
157 $is_child_user = !empty($user['parent-uid']);
159 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/delegation.tpl'), [
160 '$form_security_token' => BaseModule::getFormSecurityToken('delegate'),
161 '$account_header' => DI::l10n()->t('Additional Accounts'),
162 '$account_desc' => DI::l10n()->t('Register additional accounts that are automatically connected to your existing account so you can manage them from this account.'),
163 '$add_account' => DI::l10n()->t('Register an additional account'),
164 '$parent_header' => DI::l10n()->t('Parent User'),
165 '$parent_user' => $parent_user,
166 '$parent_password' => $parent_password,
167 '$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.'),
168 '$is_child_user' => $is_child_user,
169 '$submit' => DI::l10n()->t('Save Settings'),
170 '$header' => DI::l10n()->t('Manage Accounts'),
171 '$delegates_header' => DI::l10n()->t('Delegates'),
172 '$base' => DI::baseUrl(),
173 '$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.'),
174 '$head_delegates' => DI::l10n()->t('Existing Page Delegates'),
175 '$delegates' => $delegates,
176 '$head_potentials' => DI::l10n()->t('Potential Delegates'),
177 '$potentials' => $potentials,
178 '$remove' => DI::l10n()->t('Remove'),
179 '$add' => DI::l10n()->t('Add'),
180 '$none' => DI::l10n()->t('No entries.')