]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/Delegation.php
Use full text search
[friendica.git] / src / Module / Settings / Delegation.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\Settings;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\Renderer;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Model\User;
29 use Friendica\Module\BaseSettings;
30 use Friendica\Network\HTTPException;
31 use Friendica\Util\Strings;
32
33 /**
34  * Account delegation settings module
35  */
36 class Delegation extends BaseSettings
37 {
38         protected function post(array $request = [])
39         {
40                 if (!DI::app()->isLoggedIn()) {
41                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
42                 }
43
44                 BaseModule::checkFormSecurityTokenRedirectOnError('settings/delegation', 'delegate');
45
46                 $parent_uid = $request['parent_user'] ?? null;
47                 $parent_password = $request['parent_password'] ?? '';
48
49                 if ($parent_uid) {
50                         try {
51                                 // An integer value will trigger the direct user query on uid in User::getAuthenticationInfo
52                                 $parent_uid = (int)$parent_uid;
53                                 User::getIdFromPasswordAuthentication($parent_uid, $parent_password);
54                                 DI::sysmsg()->addInfo(DI::l10n()->t('Delegation successfully granted.'));
55                         } catch (\Exception $ex) {
56                                 DI::sysmsg()->addNotice(DI::l10n()->t('Parent user not found, unavailable or password doesn\'t match.'));
57                                 return;
58                         }
59                 } else {
60                         DI::sysmsg()->addInfo(DI::l10n()->t('Delegation successfully revoked.'));
61                 }
62
63                 DBA::update('user', ['parent-uid' => $parent_uid], ['uid' => DI::userSession()->getLocalUserId()]);
64         }
65
66         protected function content(array $request = []): string
67         {
68                 parent::content();
69
70                 if (!DI::userSession()->getLocalUserId()) {
71                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
72                 }
73
74                 $args = DI::args();
75
76                 // @TODO Replace with router-provided arguments
77                 $action = $args->get(2);
78                 $user_id = $args->get(3);
79
80                 if ($action === 'add' && $user_id) {
81                         if (DI::userSession()->getSubManagedUserId()) {
82                                 DI::sysmsg()->addNotice(DI::l10n()->t('Delegated administrators can view but not change delegation permissions.'));
83                                 DI::baseUrl()->redirect('settings/delegation');
84                         }
85
86                         $user = User::getById($user_id, ['nickname']);
87                         if (DBA::isResult($user)) {
88                                 $condition = [
89                                         'uid' => DI::userSession()->getLocalUserId(),
90                                         'nurl' => Strings::normaliseLink(DI::baseUrl() . '/profile/' . $user['nickname'])
91                                 ];
92                                 if (DBA::exists('contact', $condition)) {
93                                         DBA::insert('manage', ['uid' => $user_id, 'mid' => DI::userSession()->getLocalUserId()]);
94                                 }
95                         } else {
96                                 DI::sysmsg()->addNotice(DI::l10n()->t('Delegate user not found.'));
97                         }
98
99                         DI::baseUrl()->redirect('settings/delegation');
100                 }
101
102                 if ($action === 'remove' && $user_id) {
103                         if (DI::userSession()->getSubManagedUserId()) {
104                                 DI::sysmsg()->addNotice(DI::l10n()->t('Delegated administrators can view but not change delegation permissions.'));
105                                 DI::baseUrl()->redirect('settings/delegation');
106                         }
107
108                         DBA::delete('manage', ['uid' => $user_id, 'mid' => DI::userSession()->getLocalUserId()]);
109                         DI::baseUrl()->redirect('settings/delegation');
110                 }
111
112                 // find everybody that currently has delegated management to this account/page
113                 $delegates = DBA::selectToArray('user', [], ['`uid` IN (SELECT `uid` FROM `manage` WHERE `mid` = ?)', DI::userSession()->getLocalUserId()]);
114
115                 $uids = [];
116                 foreach ($delegates as $user) {
117                         $uids[] = $user['uid'];
118                 }
119
120                 // find every contact who might be a candidate for delegation
121                 $potentials = [];
122                 $nicknames = [];
123
124                 $condition = ['baseurl' => DI::baseUrl(), 'self' => false, 'uid' => DI::userSession()->getLocalUserId(), 'blocked' => false];
125                 $contacts = DBA::select('contact', ['nick'], $condition);
126                 while ($contact = DBA::fetch($contacts)) {
127                         $nicknames[] = $contact['nick'];
128                 }
129                 DBA::close($contacts);
130
131                 // get user records for all potential page delegates who are not already delegates or managers
132                 $potentialDelegateUsers = DBA::selectToArray('user', ['uid', 'username', 'nickname'], ['nickname' => $nicknames]);
133                 foreach ($potentialDelegateUsers as $user) {
134                         if (!in_array($user['uid'], $uids)) {
135                                 $potentials[] = $user;
136                         }
137                 }
138
139                 $parent_user = null;
140                 $parent_password = null;
141                 $user = User::getById(DI::userSession()->getLocalUserId(), ['parent-uid', 'email']);
142                 if (DBA::isResult($user) && !DBA::exists('user', ['parent-uid' => DI::userSession()->getLocalUserId()])) {
143                         $parent_uid = $user['parent-uid'];
144                         $parents = [0 => DI::l10n()->t('No parent user')];
145
146                         $fields = ['uid', 'username', 'nickname'];
147                         $condition = ['email' => $user['email'], 'verified' => true, 'blocked' => false, 'parent-uid' => null];
148                         $parent_users = DBA::selectToArray('user', $fields, $condition);
149                         foreach($parent_users as $parent) {
150                                 if ($parent['uid'] != DI::userSession()->getLocalUserId()) {
151                                         $parents[$parent['uid']] = sprintf('%s (%s)', $parent['username'], $parent['nickname']);
152                                 }
153                         }
154
155                         $parent_user = ['parent_user', DI::l10n()->t('Parent User'), $parent_uid, '', $parents];
156                         $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                 }
158
159                 $is_child_user = !empty($user['parent-uid']);
160
161                 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/delegation.tpl'), [
162                         '$form_security_token' => BaseModule::getFormSecurityToken('delegate'),
163                         '$account_header' => DI::l10n()->t('Additional Accounts'),
164                         '$account_desc' => DI::l10n()->t('Register additional accounts that are automatically connected to your existing account so you can manage them from this account.'),
165                         '$add_account' => DI::l10n()->t('Register an additional account'),
166                         '$parent_header' => DI::l10n()->t('Parent User'),
167                         '$parent_user' => $parent_user,
168                         '$parent_password' => $parent_password,
169                         '$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.'),
170                         '$is_child_user' => $is_child_user,
171                         '$submit' => DI::l10n()->t('Save Settings'),
172                         '$header' => DI::l10n()->t('Manage Accounts'),
173                         '$delegates_header' => DI::l10n()->t('Delegates'),
174                         '$base' => DI::baseUrl(),
175                         '$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.'),
176                         '$head_delegates' => DI::l10n()->t('Existing Page Delegates'),
177                         '$delegates' => $delegates,
178                         '$head_potentials' => DI::l10n()->t('Potential Delegates'),
179                         '$potentials' => $potentials,
180                         '$remove' => DI::l10n()->t('Remove'),
181                         '$add' => DI::l10n()->t('Add'),
182                         '$none' => DI::l10n()->t('No entries.')
183                 ]);
184
185                 return $o;
186         }
187 }