]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/Delegation.php
Move mod/delegate to src/Module/Settings/Delegation
[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 class Delegation extends BaseSettingsModule
19 {
20         public static function post()
21         {
22                 if (!local_user() || !empty(self::getApp()->user['uid']) && self::getApp()->user['uid'] != local_user()) {
23                         throw new HTTPException\ForbiddenException(L10n::t('Permission denied.'));
24                 }
25
26                 BaseModule::checkFormSecurityTokenRedirectOnError('settings/delegation', 'delegate');
27
28                 $parent_uid = $_POST['parent_user'] ?? 0;
29                 $parent_password = $_POST['parent_password'] ?? '';
30
31                 if ($parent_uid != 0) {
32                         $user = DBA::selectFirst('user', ['nickname'], ['uid' => $parent_uid]);
33                         if (!DBA::isResult($user)) {
34                                 notice(L10n::t('Parent user not found.'));
35                                 return;
36                         }
37
38                         try {
39                                 User::getIdFromPasswordAuthentication($user['nickname'], $parent_password);
40                                 info(L10n::t('Delegation successfully granted.'));
41                         } catch (\Throwable $ex) {
42                                 notice(L10n::t('Parent user password doesn\'t match.'));
43                                 return;
44                         }
45                 } else {
46                         info(L10n::t('Delegation successfully revoked.'));
47                 }
48
49                 DBA::update('user', ['parent-uid' => $parent_uid], ['uid' => local_user()]);
50         }
51
52         public static function content()
53         {
54                 parent::content();
55
56                 if (!local_user()) {
57                         throw new HTTPException\ForbiddenException(L10n::t('Permission denied.'));
58                 }
59
60                 /** @var Arguments $args */
61                 $args = self::getClass(Arguments::class);
62
63                 $action = $args->get(2);
64                 $user_id = $args->get(3);
65                 if ($action === 'add' && $user_id) {
66                         if (Session::get('submanage')) {
67                                 notice(L10n::t('Delegated administrators can view but not change delegation permissions.'));
68                                 self::getApp()->internalRedirect('settings/delegation');
69                         }
70
71                         $user = DBA::selectFirst('user', ['nickname'], ['uid' => $user_id]);
72                         if (DBA::isResult($user)) {
73                                 $condition = [
74                                         'uid' => local_user(),
75                                         'nurl' => Strings::normaliseLink(System::baseUrl() . '/profile/' . $user['nickname'])
76                                 ];
77                                 if (DBA::exists('contact', $condition)) {
78                                         DBA::insert('manage', ['uid' => $user_id, 'mid' => local_user()]);
79                                 }
80                         } else {
81                                 notice(L10n::t('Delegate user not found.'));
82                         }
83
84                         self::getApp()->internalRedirect('settings/delegation');
85                 }
86
87                 if ($action === 'remove' && $user_id) {
88                         if (Session::get('submanage')) {
89                                 notice(L10n::t('Delegated administrators can view but not change delegation permissions.'));
90                                 self::getApp()->internalRedirect('settings/delegation');
91                         }
92
93                         DBA::delete('manage', ['uid' => $user_id, 'mid' => local_user()]);
94                         self::getApp()->internalRedirect('settings/delegation');
95                 }
96
97                 // find everybody that currently has delegated management to this account/page
98                 $delegates = DBA::selectToArray('user', [], ['`uid` IN (SELECT `uid` FROM `manage` WHERE `mid` = ?)', local_user()]);
99
100                 $uids = [];
101                 foreach ($delegates as $user) {
102                         $uids[] = $user['uid'];
103                 }
104
105                 // find every contact who might be a candidate for delegation
106                 $potentials = [];
107
108                 $contacts = DBA::selectToArray(
109                         'contact',
110                         ['nurl'],
111                         [
112                                 "`self` = 0 AND SUBSTRING_INDEX(`nurl`, '/', 3) = ? AND `uid` = ? AND `network` = ?",
113                                 Strings::normaliseLink(System::baseUrl()),
114                                 local_user(),
115                                 Protocol::DFRN,
116                         ]
117                 );
118                 if ($contacts) {
119                         $nicknames = [];
120                         foreach ($contacts as $contact) {
121                                 $nicknames[] = "'" . DBA::escape(basename($contact['nurl'])) . "'";
122                         }
123
124                         // get user records for all potential page delegates who are not already delegates or managers
125                         $potentialDelegateUsers = DBA::selectToArray('user', ['uid', 'username', 'nickname'], ['nickname' => $nicknames]);
126                         foreach ($potentialDelegateUsers as $user) {
127                                 if (!in_array($user['uid'], $uids)) {
128                                         $potentials[] = $user;
129                                 }
130                         }
131                 }
132
133                 $parent_user = null;
134                 $parent_password = null;
135                 $user = DBA::selectFirst('user', ['parent-uid', 'email'], ['uid' => local_user()]);
136                 if (DBA::isResult($user) && !DBA::exists('user', ['parent-uid' => local_user()])) {
137                         $parent_uid = $user['parent-uid'];
138                         $parents = [0 => L10n::t('No parent user')];
139
140                         $fields = ['uid', 'username', 'nickname'];
141                         $condition = ['email' => $user['email'], 'verified' => true, 'blocked' => false, 'parent-uid' => 0];
142                         $parent_users = DBA::selectToArray('user', $fields, $condition);
143                         foreach($parent_users as $parent) {
144                                 if ($parent['uid'] != local_user()) {
145                                         $parents[$parent['uid']] = sprintf('%s (%s)', $parent['username'], $parent['nickname']);
146                                 }
147                         }
148
149                         $parent_user = ['parent_user', '', $parent_uid, '', $parents];
150                         $parent_password = ['parent_password', L10n::t('Parent Password:'), '', L10n::t('Please enter the password of the parent account to legitimize your request.')];
151                 }
152
153                 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/delegation.tpl'), [
154                         '$form_security_token' => BaseModule::getFormSecurityToken('delegate'),
155                         '$parent_header' => L10n::t('Parent User'),
156                         '$parent_user' => $parent_user,
157                         '$parent_password' => $parent_password,
158                         '$parent_desc' => L10n::t('Parent users have total control about this account, including the account settings. Please double check whom you give this access.'),
159                         '$submit' => L10n::t('Save Settings'),
160                         '$header' => L10n::t('Delegate Page Management'),
161                         '$delegates_header' => L10n::t('Delegates'),
162                         '$base' => System::baseUrl(),
163                         '$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.'),
164                         '$head_delegates' => L10n::t('Existing Page Delegates'),
165                         '$delegates' => $delegates,
166                         '$head_potentials' => L10n::t('Potential Delegates'),
167                         '$potentials' => $potentials,
168                         '$remove' => L10n::t('Remove'),
169                         '$add' => L10n::t('Add'),
170                         '$none' => L10n::t('No entries.')
171                 ]);
172
173                 return $o;
174         }
175 }