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