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