]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/Delegation.php
579d2bf82a0bcc6d5356321b3f4d6544e23862cc
[friendica.git] / src / Module / Settings / Delegation.php
1 <?php
2
3 namespace Friendica\Module\Settings;
4
5 use Friendica\BaseModule;
6 use Friendica\Core\Renderer;
7 use Friendica\Core\Session;
8 use Friendica\Database\DBA;
9 use Friendica\DI;
10 use Friendica\Model\User;
11 use Friendica\Module\BaseSettingsModule;
12 use Friendica\Network\HTTPException;
13 use Friendica\Util\Strings;
14
15 /**
16  * Account delegation settings module
17  */
18 class Delegation extends BaseSettingsModule
19 {
20         public static function post(array $parameters = [])
21         {
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.'));
24                 }
25
26                 BaseModule::checkFormSecurityTokenRedirectOnError('settings/delegation', 'delegate');
27
28                 $parent_uid = (int)$_POST['parent_user'] ?? 0;
29                 $parent_password = $_POST['parent_password'] ?? '';
30
31                 if ($parent_uid != 0) {
32                         try {
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.'));
37                                 return;
38                         }
39                 } else {
40                         info(DI::l10n()->t('Delegation successfully revoked.'));
41                 }
42
43                 DBA::update('user', ['parent-uid' => $parent_uid], ['uid' => local_user()]);
44         }
45
46         public static function content(array $parameters = [])
47         {
48                 parent::content($parameters);
49
50                 if (!local_user()) {
51                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
52                 }
53
54                 $args = DI::args();
55
56                 // @TODO Replace with router-provided arguments
57                 $action = $args->get(2);
58                 $user_id = $args->get(3);
59
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');
64                         }
65
66                         $user = User::getById($user_id, ['nickname']);
67                         if (DBA::isResult($user)) {
68                                 $condition = [
69                                         'uid' => local_user(),
70                                         'nurl' => Strings::normaliseLink(DI::baseUrl() . '/profile/' . $user['nickname'])
71                                 ];
72                                 if (DBA::exists('contact', $condition)) {
73                                         DBA::insert('manage', ['uid' => $user_id, 'mid' => local_user()]);
74                                 }
75                         } else {
76                                 notice(DI::l10n()->t('Delegate user not found.'));
77                         }
78
79                         DI::baseUrl()->redirect('settings/delegation');
80                 }
81
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');
86                         }
87
88                         DBA::delete('manage', ['uid' => $user_id, 'mid' => local_user()]);
89                         DI::baseUrl()->redirect('settings/delegation');
90                 }
91
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()]);
94
95                 $uids = [];
96                 foreach ($delegates as $user) {
97                         $uids[] = $user['uid'];
98                 }
99
100                 // find every contact who might be a candidate for delegation
101                 $potentials = [];
102                 $nicknames = [];
103
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'];
108                 }
109
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;
115                         }
116                 }
117
118                 $parent_user = null;
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')];
124
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']);
131                                 }
132                         }
133
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.')];
136                 }
137
138                 $is_child_user = !empty($user['parent-uid']);
139
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.')
162                 ]);
163
164                 return $o;
165         }
166 }