]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/Delegation.php
Merge pull request #8044 from annando/contact-adding
[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
106                 $contacts = DBA::selectToArray(
107                         'contact',
108                         ['nurl'],
109                         [
110                                 "`self` = 0 AND SUBSTRING_INDEX(`nurl`, '/', 3) = ? AND `uid` = ? AND `network` = ?",
111                                 Strings::normaliseLink(DI::baseUrl()),
112                                 local_user(),
113                                 Protocol::DFRN,
114                         ]
115                 );
116                 if ($contacts) {
117                         $nicknames = [];
118                         foreach ($contacts as $contact) {
119                                 $nicknames[] = "'" . DBA::escape(basename($contact['nurl'])) . "'";
120                         }
121
122                         // get user records for all potential page delegates who are not already delegates or managers
123                         $potentialDelegateUsers = DBA::selectToArray('user', ['uid', 'username', 'nickname'], ['nickname' => $nicknames]);
124                         foreach ($potentialDelegateUsers as $user) {
125                                 if (!in_array($user['uid'], $uids)) {
126                                         $potentials[] = $user;
127                                 }
128                         }
129                 }
130
131                 $parent_user = null;
132                 $parent_password = null;
133                 $user = User::getById(local_user(), ['parent-uid', 'email']);
134                 if (DBA::isResult($user) && !DBA::exists('user', ['parent-uid' => local_user()])) {
135                         $parent_uid = $user['parent-uid'];
136                         $parents = [0 => L10n::t('No parent user')];
137
138                         $fields = ['uid', 'username', 'nickname'];
139                         $condition = ['email' => $user['email'], 'verified' => true, 'blocked' => false, 'parent-uid' => 0];
140                         $parent_users = DBA::selectToArray('user', $fields, $condition);
141                         foreach($parent_users as $parent) {
142                                 if ($parent['uid'] != local_user()) {
143                                         $parents[$parent['uid']] = sprintf('%s (%s)', $parent['username'], $parent['nickname']);
144                                 }
145                         }
146
147                         $parent_user = ['parent_user', '', $parent_uid, '', $parents];
148                         $parent_password = ['parent_password', L10n::t('Parent Password:'), '', L10n::t('Please enter the password of the parent account to legitimize your request.')];
149                 }
150
151                 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/delegation.tpl'), [
152                         '$form_security_token' => BaseModule::getFormSecurityToken('delegate'),
153                         '$parent_header' => L10n::t('Parent User'),
154                         '$parent_user' => $parent_user,
155                         '$parent_password' => $parent_password,
156                         '$parent_desc' => L10n::t('Parent users have total control about this account, including the account settings. Please double check whom you give this access.'),
157                         '$submit' => L10n::t('Save Settings'),
158                         '$header' => L10n::t('Delegate Page Management'),
159                         '$delegates_header' => L10n::t('Delegates'),
160                         '$base' => DI::baseUrl(),
161                         '$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.'),
162                         '$head_delegates' => L10n::t('Existing Page Delegates'),
163                         '$delegates' => $delegates,
164                         '$head_potentials' => L10n::t('Potential Delegates'),
165                         '$potentials' => $potentials,
166                         '$remove' => L10n::t('Remove'),
167                         '$add' => L10n::t('Add'),
168                         '$none' => L10n::t('No entries.')
169                 ]);
170
171                 return $o;
172         }
173 }