From fd53b541e568dd345787aa4e75d82da29d2b61ba Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 18 Feb 2018 13:19:47 +0000 Subject: [PATCH] Frontend to set the parent-uid field --- mod/delegate.php | 77 ++++++++++++++++++++++++++++--------- view/templates/delegate.tpl | 21 ++++++++-- 2 files changed, 76 insertions(+), 22 deletions(-) diff --git a/mod/delegate.php b/mod/delegate.php index 3152eb370..a6a67c3b8 100644 --- a/mod/delegate.php +++ b/mod/delegate.php @@ -14,6 +14,24 @@ function delegate_init(App $a) return settings_init($a); } +function delegate_post(App $a) +{ + if (!local_user()) { + return; + } + + if (count($a->user) && x($a->user, 'uid') && $a->user['uid'] != local_user()) { + notice(L10n::t('Permission denied.') . EOL); + return; + } + + check_form_security_token_redirectOnErr('/delegate', 'delegate'); + + $parent_uid = defaults($_POST, 'parent_user', 0); + + dba::update('user', ['parent-uid' => $parent_uid], ['uid' => local_user()]); +} + function delegate_content(App $a) { if (!local_user()) { @@ -81,6 +99,7 @@ function delegate_content(App $a) } // find every contact who might be a candidate for delegation + $potentials = []; $r = q("SELECT `nurl` FROM `contact` @@ -92,34 +111,56 @@ function delegate_content(App $a) intval(local_user()), dbesc(NETWORK_DFRN) ); - if (!DBM::is_result($r)) { - notice(L10n::t('No potential page delegates located.') . EOL); - return; - } - - $nicknames = []; - foreach ($r as $rr) { - $nicknames[] = "'" . dbesc(basename($rr['nurl'])) . "'"; - } - - $potentials = []; - - $nicks = implode(',', $nicknames); - - // get user records for all potential page delegates who are not already delegates or managers - $r = q("SELECT `uid`, `username`, `nickname` FROM `user` WHERE `nickname` IN ($nicks)"); if (DBM::is_result($r)) { + $nicknames = []; foreach ($r as $rr) { - if (!in_array($rr['uid'], $uids)) { - $potentials[] = $rr; + $nicknames[] = "'" . dbesc(basename($rr['nurl'])) . "'"; + } + + $nicks = implode(',', $nicknames); + + // get user records for all potential page delegates who are not already delegates or managers + $r = q("SELECT `uid`, `username`, `nickname` FROM `user` WHERE `nickname` IN ($nicks)"); + if (DBM::is_result($r)) { + foreach ($r as $rr) { + if (!in_array($rr['uid'], $uids)) { + $potentials[] = $rr; + } } } } settings_init($a); + $user = dba::selectFirst('user', ['parent-uid', 'email'], ['uid' => local_user()]); + + $parent_user = null; + + if (DBM::is_result($user)) { + if (!dba::exists('user', ['parent-uid' => local_user()])) { + $parent_uid = $user['parent-uid']; + $parents = [0 => L10n::t('No parent user')]; + + $fields = ['uid', 'username', 'nickname']; + $condition = ['email' => $user['email'], 'verified' => true, 'blocked' => false, 'parent-uid' => 0]; + $parent_users = dba::select('user', $fields, $condition); + while ($parent = dba::fetch($parent_users)) { + if ($parent['uid'] != local_user()) { + $parents[$parent['uid']] = sprintf('%s (%s)', $parent['username'], $parent['nickname']); + } + } + $parent_user = ['parent_user', '', $parent_uid, '', $parents]; + } + } + $o = replace_macros(get_markup_template('delegate.tpl'), [ + '$form_security_token' => get_form_security_token('delegate'), + '$parent_header' => L10n::t('Parent User'), + '$parent_user' => $parent_user, + '$parent_desc' => L10n::t('Parent users have total control about this account, including the account settings. Please double check whom you give this access.'), + '$submit' => L10n::t('Save Settings'), '$header' => L10n::t('Delegate Page Management'), + '$delegates_header' => L10n::t('Delegates'), '$base' => System::baseUrl(), '$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.'), '$head_managers' => L10n::t('Existing Page Managers'), diff --git a/view/templates/delegate.tpl b/view/templates/delegate.tpl index 0897b8a57..2f791c48a 100644 --- a/view/templates/delegate.tpl +++ b/view/templates/delegate.tpl @@ -1,10 +1,23 @@ -

{{$header}}

+{{if $parent_user}} +

{{$parent_header}}

+
{{$parent_desc}}
+
+
+ +{{include file="field_select.tpl" field=$parent_user}} +
+
+
+{{/if}} + +

{{$delegates_header}}

+
{{$desc}}
{{if $managers}} -

{{$head_managers}}

+

{{$head_managers}}

{{foreach $managers as $x}} @@ -20,7 +33,7 @@ {{/if}} -

{{$head_delegates}}

+

{{$head_delegates}}

{{if $delegates}} {{foreach $delegates as $x}} @@ -39,7 +52,7 @@
-

{{$head_potentials}}

+

{{$head_potentials}}

{{if $potentials}} {{foreach $potentials as $x}}