]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/Delegation.php
Merge pull request #13090 from abanink/feature-openwebauth
[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 = (int)$_POST['parent_user'] ?? 0;
47                 $parent_password = $_POST['parent_password'] ?? '';
48
49                 if ($parent_uid != 0) {
50                         try {
51                                 User::getIdFromPasswordAuthentication($parent_uid, $parent_password);
52                                 DI::sysmsg()->addInfo(DI::l10n()->t('Delegation successfully granted.'));
53                         } catch (\Exception $ex) {
54                                 DI::sysmsg()->addNotice(DI::l10n()->t('Parent user not found, unavailable or password doesn\'t match.'));
55                                 return;
56                         }
57                 } else {
58                         DI::sysmsg()->addInfo(DI::l10n()->t('Delegation successfully revoked.'));
59                 }
60
61                 DBA::update('user', ['parent-uid' => $parent_uid], ['uid' => DI::userSession()->getLocalUserId()]);
62         }
63
64         protected function content(array $request = []): string
65         {
66                 parent::content();
67
68                 if (!DI::userSession()->getLocalUserId()) {
69                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
70                 }
71
72                 $args = DI::args();
73
74                 // @TODO Replace with router-provided arguments
75                 $action = $args->get(2);
76                 $user_id = $args->get(3);
77
78                 if ($action === 'add' && $user_id) {
79                         if (DI::userSession()->getSubManagedUserId()) {
80                                 DI::sysmsg()->addNotice(DI::l10n()->t('Delegated administrators can view but not change delegation permissions.'));
81                                 DI::baseUrl()->redirect('settings/delegation');
82                         }
83
84                         $user = User::getById($user_id, ['nickname']);
85                         if (DBA::isResult($user)) {
86                                 $condition = [
87                                         'uid' => DI::userSession()->getLocalUserId(),
88                                         'nurl' => Strings::normaliseLink(DI::baseUrl() . '/profile/' . $user['nickname'])
89                                 ];
90                                 if (DBA::exists('contact', $condition)) {
91                                         DBA::insert('manage', ['uid' => $user_id, 'mid' => DI::userSession()->getLocalUserId()]);
92                                 }
93                         } else {
94                                 DI::sysmsg()->addNotice(DI::l10n()->t('Delegate user not found.'));
95                         }
96
97                         DI::baseUrl()->redirect('settings/delegation');
98                 }
99
100                 if ($action === 'remove' && $user_id) {
101                         if (DI::userSession()->getSubManagedUserId()) {
102                                 DI::sysmsg()->addNotice(DI::l10n()->t('Delegated administrators can view but not change delegation permissions.'));
103                                 DI::baseUrl()->redirect('settings/delegation');
104                         }
105
106                         DBA::delete('manage', ['uid' => $user_id, 'mid' => DI::userSession()->getLocalUserId()]);
107                         DI::baseUrl()->redirect('settings/delegation');
108                 }
109
110                 // find everybody that currently has delegated management to this account/page
111                 $delegates = DBA::selectToArray('user', [], ['`uid` IN (SELECT `uid` FROM `manage` WHERE `mid` = ?)', DI::userSession()->getLocalUserId()]);
112
113                 $uids = [];
114                 foreach ($delegates as $user) {
115                         $uids[] = $user['uid'];
116                 }
117
118                 // find every contact who might be a candidate for delegation
119                 $potentials = [];
120                 $nicknames = [];
121
122                 $condition = ['baseurl' => DI::baseUrl(), 'self' => false, 'uid' => DI::userSession()->getLocalUserId(), 'blocked' => false];
123                 $contacts = DBA::select('contact', ['nick'], $condition);
124                 while ($contact = DBA::fetch($contacts)) {
125                         $nicknames[] = $contact['nick'];
126                 }
127                 DBA::close($contacts);
128
129                 // get user records for all potential page delegates who are not already delegates or managers
130                 $potentialDelegateUsers = DBA::selectToArray('user', ['uid', 'username', 'nickname'], ['nickname' => $nicknames]);
131                 foreach ($potentialDelegateUsers as $user) {
132                         if (!in_array($user['uid'], $uids)) {
133                                 $potentials[] = $user;
134                         }
135                 }
136
137                 $parent_user = null;
138                 $parent_password = null;
139                 $user = User::getById(DI::userSession()->getLocalUserId(), ['parent-uid', 'email']);
140                 if (DBA::isResult($user) && !DBA::exists('user', ['parent-uid' => DI::userSession()->getLocalUserId()])) {
141                         $parent_uid = $user['parent-uid'];
142                         $parents = [0 => DI::l10n()->t('No parent user')];
143
144                         $fields = ['uid', 'username', 'nickname'];
145                         $condition = ['email' => $user['email'], 'verified' => true, 'blocked' => false, 'parent-uid' => 0];
146                         $parent_users = DBA::selectToArray('user', $fields, $condition);
147                         foreach($parent_users as $parent) {
148                                 if ($parent['uid'] != DI::userSession()->getLocalUserId()) {
149                                         $parents[$parent['uid']] = sprintf('%s (%s)', $parent['username'], $parent['nickname']);
150                                 }
151                         }
152
153                         $parent_user = ['parent_user', DI::l10n()->t('Parent User'), $parent_uid, '', $parents];
154                         $parent_password = ['parent_password', DI::l10n()->t('Parent Password:'), '', DI::l10n()->t('Please enter the password of the parent account to legitimize your request.')];
155                 }
156
157                 $is_child_user = !empty($user['parent-uid']);
158
159                 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/delegation.tpl'), [
160                         '$form_security_token' => BaseModule::getFormSecurityToken('delegate'),
161                         '$account_header' => DI::l10n()->t('Additional Accounts'),
162                         '$account_desc' => DI::l10n()->t('Register additional accounts that are automatically connected to your existing account so you can manage them from this account.'),
163                         '$add_account' => DI::l10n()->t('Register an additional account'),
164                         '$parent_header' => DI::l10n()->t('Parent User'),
165                         '$parent_user' => $parent_user,
166                         '$parent_password' => $parent_password,
167                         '$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.'),
168                         '$is_child_user' => $is_child_user,
169                         '$submit' => DI::l10n()->t('Save Settings'),
170                         '$header' => DI::l10n()->t('Manage Accounts'),
171                         '$delegates_header' => DI::l10n()->t('Delegates'),
172                         '$base' => DI::baseUrl(),
173                         '$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.'),
174                         '$head_delegates' => DI::l10n()->t('Existing Page Delegates'),
175                         '$delegates' => $delegates,
176                         '$head_potentials' => DI::l10n()->t('Potential Delegates'),
177                         '$potentials' => $potentials,
178                         '$remove' => DI::l10n()->t('Remove'),
179                         '$add' => DI::l10n()->t('Add'),
180                         '$none' => DI::l10n()->t('No entries.')
181                 ]);
182
183                 return $o;
184         }
185 }