]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/Delegation.php
Added parameters
[friendica.git] / src / Module / Settings / Delegation.php
1 <?php
2
3 namespace Friendica\Module\Settings;
4
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;
13 use Friendica\Model\User;
14 use Friendica\Module\BaseSettingsModule;
15 use Friendica\Network\HTTPException;
16 use Friendica\Util\Strings;
17
18 /**
19  * Account delegation settings module
20  */
21 class Delegation extends BaseSettingsModule
22 {
23         public static function post($parameters)
24         {
25                 if (!local_user() || !empty(self::getApp()->user['uid']) && self::getApp()->user['uid'] != local_user()) {
26                         throw new HTTPException\ForbiddenException(L10n::t('Permission denied.'));
27                 }
28
29                 BaseModule::checkFormSecurityTokenRedirectOnError('settings/delegation', 'delegate');
30
31                 $parent_uid = $_POST['parent_user'] ?? 0;
32                 $parent_password = $_POST['parent_password'] ?? '';
33
34                 if ($parent_uid != 0) {
35                         try {
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.'));
40                                 return;
41                         }
42                 } else {
43                         info(L10n::t('Delegation successfully revoked.'));
44                 }
45
46                 DBA::update('user', ['parent-uid' => $parent_uid], ['uid' => local_user()]);
47         }
48
49         public static function content($parameters)
50         {
51                 parent::content($parameters);
52
53                 if (!local_user()) {
54                         throw new HTTPException\ForbiddenException(L10n::t('Permission denied.'));
55                 }
56
57                 /** @var Arguments $args */
58                 $args = self::getClass(Arguments::class);
59
60                 // @TODO Replace with router-provided arguments
61                 $action = $args->get(2);
62                 $user_id = $args->get(3);
63
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                                 self::getApp()->internalRedirect('settings/delegation');
68                         }
69
70                         $user = User::getById($user_id, ['nickname']);
71                         if (DBA::isResult($user)) {
72                                 $condition = [
73                                         'uid' => local_user(),
74                                         'nurl' => Strings::normaliseLink(System::baseUrl() . '/profile/' . $user['nickname'])
75                                 ];
76                                 if (DBA::exists('contact', $condition)) {
77                                         DBA::insert('manage', ['uid' => $user_id, 'mid' => local_user()]);
78                                 }
79                         } else {
80                                 notice(L10n::t('Delegate user not found.'));
81                         }
82
83                         self::getApp()->internalRedirect('settings/delegation');
84                 }
85
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                                 self::getApp()->internalRedirect('settings/delegation');
90                         }
91
92                         DBA::delete('manage', ['uid' => $user_id, 'mid' => local_user()]);
93                         self::getApp()->internalRedirect('settings/delegation');
94                 }
95
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()]);
98
99                 $uids = [];
100                 foreach ($delegates as $user) {
101                         $uids[] = $user['uid'];
102                 }
103
104                 // find every contact who might be a candidate for delegation
105                 $potentials = [];
106
107                 $contacts = DBA::selectToArray(
108                         'contact',
109                         ['nurl'],
110                         [
111                                 "`self` = 0 AND SUBSTRING_INDEX(`nurl`, '/', 3) = ? AND `uid` = ? AND `network` = ?",
112                                 Strings::normaliseLink(System::baseUrl()),
113                                 local_user(),
114                                 Protocol::DFRN,
115                         ]
116                 );
117                 if ($contacts) {
118                         $nicknames = [];
119                         foreach ($contacts as $contact) {
120                                 $nicknames[] = "'" . DBA::escape(basename($contact['nurl'])) . "'";
121                         }
122
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;
128                                 }
129                         }
130                 }
131
132                 $parent_user = null;
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')];
138
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']);
145                                 }
146                         }
147
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.')];
150                 }
151
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.')
170                 ]);
171
172                 return $o;
173         }
174 }