]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/Delegation.php
Merge pull request #8094 from annando/deletecontact
[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\Database\DBA;
12 use Friendica\DI;
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(array $parameters = [])
24         {
25                 if (!local_user() || !empty(DI::app()->user['uid']) && DI::app()->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(array $parameters = [])
50         {
51                 parent::content($parameters);
52
53                 if (!local_user()) {
54                         throw new HTTPException\ForbiddenException(L10n::t('Permission denied.'));
55                 }
56
57                 $args = DI::args();
58
59                 // @TODO Replace with router-provided arguments
60                 $action = $args->get(2);
61                 $user_id = $args->get(3);
62
63                 if ($action === 'add' && $user_id) {
64                         if (Session::get('submanage')) {
65                                 notice(L10n::t('Delegated administrators can view but not change delegation permissions.'));
66                                 DI::baseUrl()->redirect('settings/delegation');
67                         }
68
69                         $user = User::getById($user_id, ['nickname']);
70                         if (DBA::isResult($user)) {
71                                 $condition = [
72                                         'uid' => local_user(),
73                                         'nurl' => Strings::normaliseLink(DI::baseUrl() . '/profile/' . $user['nickname'])
74                                 ];
75                                 if (DBA::exists('contact', $condition)) {
76                                         DBA::insert('manage', ['uid' => $user_id, 'mid' => local_user()]);
77                                 }
78                         } else {
79                                 notice(L10n::t('Delegate user not found.'));
80                         }
81
82                         DI::baseUrl()->redirect('settings/delegation');
83                 }
84
85                 if ($action === 'remove' && $user_id) {
86                         if (Session::get('submanage')) {
87                                 notice(L10n::t('Delegated administrators can view but not change delegation permissions.'));
88                                 DI::baseUrl()->redirect('settings/delegation');
89                         }
90
91                         DBA::delete('manage', ['uid' => $user_id, 'mid' => local_user()]);
92                         DI::baseUrl()->redirect('settings/delegation');
93                 }
94
95                 // find everybody that currently has delegated management to this account/page
96                 $delegates = DBA::selectToArray('user', [], ['`uid` IN (SELECT `uid` FROM `manage` WHERE `mid` = ?)', local_user()]);
97
98                 $uids = [];
99                 foreach ($delegates as $user) {
100                         $uids[] = $user['uid'];
101                 }
102
103                 // find every contact who might be a candidate for delegation
104                 $potentials = [];
105                 $nicknames = [];
106
107                 $condition = ['baseurl' => DI::baseUrl(), 'self' => false, 'uid' => local_user(), 'blocked' => false];
108                 $contacts = DBA::select('contact', ['nick'], $condition);
109                 while ($contact = DBA::fetch($contacts)) {
110                         $nicknames[] = $contact['nick'];
111                 }
112
113                 // get user records for all potential page delegates who are not already delegates or managers
114                 $potentialDelegateUsers = DBA::selectToArray('user', ['uid', 'username', 'nickname'], ['nickname' => $nicknames]);
115                 foreach ($potentialDelegateUsers as $user) {
116                         if (!in_array($user['uid'], $uids)) {
117                                 $potentials[] = $user;
118                         }
119                 }
120
121                 $parent_user = null;
122                 $parent_password = null;
123                 $user = User::getById(local_user(), ['parent-uid', 'email']);
124                 if (DBA::isResult($user) && !DBA::exists('user', ['parent-uid' => local_user()])) {
125                         $parent_uid = $user['parent-uid'];
126                         $parents = [0 => L10n::t('No parent user')];
127
128                         $fields = ['uid', 'username', 'nickname'];
129                         $condition = ['email' => $user['email'], 'verified' => true, 'blocked' => false, 'parent-uid' => 0];
130                         $parent_users = DBA::selectToArray('user', $fields, $condition);
131                         foreach($parent_users as $parent) {
132                                 if ($parent['uid'] != local_user()) {
133                                         $parents[$parent['uid']] = sprintf('%s (%s)', $parent['username'], $parent['nickname']);
134                                 }
135                         }
136
137                         $parent_user = ['parent_user', '', $parent_uid, '', $parents];
138                         $parent_password = ['parent_password', L10n::t('Parent Password:'), '', L10n::t('Please enter the password of the parent account to legitimize your request.')];
139                 }
140
141                 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/delegation.tpl'), [
142                         '$form_security_token' => BaseModule::getFormSecurityToken('delegate'),
143                         '$parent_header' => L10n::t('Parent User'),
144                         '$parent_user' => $parent_user,
145                         '$parent_password' => $parent_password,
146                         '$parent_desc' => L10n::t('Parent users have total control about this account, including the account settings. Please double check whom you give this access.'),
147                         '$submit' => L10n::t('Save Settings'),
148                         '$header' => L10n::t('Delegate Page Management'),
149                         '$delegates_header' => L10n::t('Delegates'),
150                         '$base' => DI::baseUrl(),
151                         '$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.'),
152                         '$head_delegates' => L10n::t('Existing Page Delegates'),
153                         '$delegates' => $delegates,
154                         '$head_potentials' => L10n::t('Potential Delegates'),
155                         '$potentials' => $potentials,
156                         '$remove' => L10n::t('Remove'),
157                         '$add' => L10n::t('Add'),
158                         '$none' => L10n::t('No entries.')
159                 ]);
160
161                 return $o;
162         }
163 }