From 2968bf8241d2969c4d51f1651fc3f8c7688b2fca Mon Sep 17 00:00:00 2001 From: Mario Date: Wed, 15 Dec 2021 12:17:19 +0000 Subject: merge branch perms_ng into dev --- Zotlabs/Module/Permcats.php | 148 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 115 insertions(+), 33 deletions(-) (limited to 'Zotlabs/Module/Permcats.php') diff --git a/Zotlabs/Module/Permcats.php b/Zotlabs/Module/Permcats.php index 58566373a..a46253ec2 100644 --- a/Zotlabs/Module/Permcats.php +++ b/Zotlabs/Module/Permcats.php @@ -6,6 +6,7 @@ use App; use Zotlabs\Web\Controller; use Zotlabs\Lib\Apps; use Zotlabs\Lib\Libsync; +use Zotlabs\Lib\AccessList; class Permcats extends Controller { @@ -14,24 +15,59 @@ class Permcats extends Controller { if(! local_channel()) return; - if(! Apps::system_app_installed(local_channel(), 'Permission Categories')) - return; - $channel = App::get_channel(); check_form_security_token_redirectOnErr('/permcats', 'permcats'); - $all_perms = \Zotlabs\Access\Permissions::Perms(); - $name = escape_tags(trim($_POST['name'])); - if(! $name) { + $is_system_role = isset($_POST['is_system_role']); + $return_path = z_root() . '/permcats/' . $_POST['return_path']; + $group_hash = ((isset($_POST['group_select'])) ? $_POST['group_select'] : ''); + $contacts = []; + + if ($group_hash === 'all_contacts') { + $r = q("SELECT abook_xchan FROM abook WHERE abook_channel = %d and abook_self = 0 and abook_pending = 0", + intval(local_channel()) + ); + + if ($r) { + $contacts = ids_to_array($r, 'abook_xchan'); + } + } + + if (!$contacts && $group_hash) { + $group = AccessList::by_hash(local_channel(), $group_hash); + } + + if ($group) { + $contacts = AccessList::members_xchan(local_channel(), $group['id']); + } + + if(! $name ) { notice( t('Permission category name is required.') . EOL); return; } + set_pconfig(local_channel(), 'system', 'default_permcat', 'default'); + + if (isset($_POST['default_role'])) { + set_pconfig(local_channel(), 'system', 'default_permcat', $name); + } + + if ($is_system_role) { + // if we have a system role just set the default and assign if aplicable and be done with it + if ($contacts) + \Zotlabs\Lib\Permcat::assign($channel, $name, $contacts); + + info( t('Contact role saved.') . EOL); + Libsync::build_sync_packet(); + goaway($return_path); + return; + } $pcarr = []; + $all_perms = \Zotlabs\Access\Permissions::Perms(); if($all_perms) { foreach($all_perms as $perm => $desc) { @@ -41,11 +77,16 @@ class Permcats extends Controller { } } - \Zotlabs\Lib\Permcat::update(local_channel(),$name,$pcarr); + \Zotlabs\Lib\Permcat::update(local_channel(), $name, $pcarr); + + if ($contacts) { + \Zotlabs\Lib\Permcat::assign($channel, $name, $contacts); + } Libsync::build_sync_packet(); - info( t('Permission category saved.') . EOL); + info( t('Contact role saved.') . EOL); + goaway($return_path); return; } @@ -56,13 +97,6 @@ class Permcats extends Controller { if(! local_channel()) return; - if(! Apps::system_app_installed(local_channel(), 'Permission Categories')) { - //Do not display any associated widgets at this point - App::$pdl = ''; - $papp = Apps::get_papp('Permission Categories'); - return Apps::app_render($papp, 'module'); - } - $channel = App::get_channel(); if(argc() > 1) @@ -70,24 +104,49 @@ class Permcats extends Controller { if(argc() > 2 && argv(2) === 'drop') { \Zotlabs\Lib\Permcat::delete(local_channel(),$name); + + // TODO: assign all members of the deleted role to the default role + Libsync::build_sync_packet(); json_return_and_die([ 'success' => true ]); } - $desc = t('Use this form to create permission rules for various classes of people or connections.'); - $existing = []; $pcat = new \Zotlabs\Lib\Permcat(local_channel()); $pcatlist = $pcat->listing(); + +/* not yet ready + $test = $pcatlist[4]['perms']; + $role_sql = ''; + + foreach ($test as $t) + $role_sql .= "( k = '" . dbesc($t['name']) . "' AND v = '" . intval($t['value']) . "' ) OR "; + + $role_sql = rtrim($role_sql, ' OR '); + + // get all xchans belonging to a permission role + $q = q("SELECT xchan FROM abconfig WHERE chan = %d AND cat = 'my_perms' AND ( $role_sql ) GROUP BY xchan HAVING count(xchan) = %d", + intval(local_channel()), + intval(count($test)) + ); +*/ + + $is_system_role = false; $permcats = []; if($pcatlist) { foreach($pcatlist as $pc) { - if(($pc['name']) && ($name) && ($pc['name'] == $name)) + if(($pc['name']) && ($name) && ($pc['name'] == $name)) { $existing = $pc['perms']; - if(! $pc['system']) - $permcats[bin2hex($pc['name'])] = $pc['localname']; + if (isset($pc['system']) && intval($pc['system'])) + $is_system_role = $pc['name']; + } + + $permcats[bin2hex($pc['name'])] = $pc['localname']; + + if($pc['name'] == $name) + $localname = $pc['localname']; } } @@ -98,33 +157,56 @@ class Permcats extends Controller { $checkinherited = \Zotlabs\Access\PermissionLimits::Get(local_channel(),$k); if($existing[$k]) - $thisperm = "1"; - - $perms[] = array('perms_' . $k, $v, '',$thisperm, 1, (($checkinherited & PERMS_SPECIFIC) ? '' : '1'), '', $checkinherited); + $thisperm = 1; + + $perms[] = [ + 'perms_' . $k, + $v, + '', + $thisperm, + 1, + (($checkinherited & PERMS_SPECIFIC) ? '' : '1'), + '', + $checkinherited + ]; } + $is_default_role = (get_pconfig(local_channel(),'system','default_permcat','default') == $name); + + $group_select_options = [ + 'selected' => '', + 'form_id' => 'group_select', + 'label' => t('Assign this role to'), + 'after' => [ + 'name' => t('All my contacts'), + 'id' => 'all_contacts', + 'selected' => false + ] + ]; + $group_select = AccessList::select(local_channel(), $group_select_options); $tpl = get_markup_template("permcats.tpl"); $o .= replace_macros($tpl, array( '$form_security_token' => get_form_security_token("permcats"), - '$title' => t('Permission Categories'), - '$desc' => $desc, - '$desc2' => $desc2, + '$default_role' => array('default_role', t('Use this role as default for new contacts'), intval($is_default_role), '', [t('No'), t('Yes')]), + + '$title' => t('Contact Roles'), '$tokens' => $t, '$permcats' => $permcats, '$atoken' => $atoken, '$url1' => z_root() . '/channel/' . $channel['channel_address'], '$url2' => z_root() . '/photos/' . $channel['channel_address'], - '$name' => array('name', t('Permission category name') . ' *', (($name) ? $name : ''), ''), - '$me' => t('My Settings'), + '$name' => ['name', t('Role name') . ' *', (($localname) ? $localname : ''), (($is_system_role) ? t('System role - not editable') : '') , '', (($is_system_role) ? 'disabled' : '')], '$perms' => $perms, '$inherited' => t('inherited'), - '$notself' => 0, - '$self' => 1, - '$permlbl' => t('Individual Permissions'), - '$permnote' => t('Some permissions may be inherited from your channel\'s privacy settings, which have higher priority than individual settings. You can not change those settings here.'), - '$submit' => t('Submit') + '$is_system_role' => $is_system_role, + '$permlbl' => t('Role Permissions'), + '$permnote' => t('Some permissions may be inherited from your channel role, which have higher priority than contact role settings.'), + '$submit' => t('Submit'), + '$return_path' => argv(1), + '$group_select' => $group_select, + )); return $o; } -- cgit v1.2.3 From 220ed35f5855f22344d7a815da9bb1f6c96f1002 Mon Sep 17 00:00:00 2001 From: Mario Date: Fri, 17 Dec 2021 14:59:25 +0100 Subject: implement contact role deletion --- Zotlabs/Module/Permcats.php | 96 +++++++++++++++++++++++++++++++-------------- 1 file changed, 67 insertions(+), 29 deletions(-) (limited to 'Zotlabs/Module/Permcats.php') diff --git a/Zotlabs/Module/Permcats.php b/Zotlabs/Module/Permcats.php index a46253ec2..c33580f06 100644 --- a/Zotlabs/Module/Permcats.php +++ b/Zotlabs/Module/Permcats.php @@ -19,13 +19,47 @@ class Permcats extends Controller { check_form_security_token_redirectOnErr('/permcats', 'permcats'); - - $name = escape_tags(trim($_POST['name'])); - $is_system_role = isset($_POST['is_system_role']); - $return_path = z_root() . '/permcats/' . $_POST['return_path']; - $group_hash = ((isset($_POST['group_select'])) ? $_POST['group_select'] : ''); + $name = escape_tags(trim($_REQUEST['name'])); + $is_system_role = isset($_REQUEST['is_system_role']); + $return_path = z_root() . '/permcats/' . $_REQUEST['return_path']; + $group_hash = $_REQUEST['group_select'] ?? ''; + $deleted_role = $_REQUEST['deleted_role'] ?? ''; + $new_role = $_REQUEST['new_role'] ?? ''; $contacts = []; + if (argv(1) && hex2bin(argv(1)) !== $name) { + $return_path = z_root() . '/permcats/' . bin2hex($name); + } + + if($deleted_role && $new_role) { + $r = q("SELECT abook_xchan FROM abook WHERE abook_channel = %d AND abook_role = '%s' AND abook_self = 0 AND abook_pending = 0", + intval(local_channel()), + dbesc($deleted_role) + ); + + if ($r) { + $contacts = ids_to_array($r, 'abook_xchan'); + } + + if ($contacts) { + \Zotlabs\Lib\Permcat::assign($channel, $new_role, $contacts); + } + + \Zotlabs\Lib\Permcat::delete(local_channel(), $deleted_role); + + $default_role = get_pconfig(local_channel(), 'system', 'default_permcat', 'default'); + if($deleted_role === $default_role) { + set_pconfig(local_channel(), 'system', 'default_permcat', $new_role); + } + + Libsync::build_sync_packet(); + info(t('Contact role deleted.') . EOL); + + goaway(z_root() . '/permcats/' . bin2hex($new_role)); + + return; + } + if ($group_hash === 'all_contacts') { $r = q("SELECT abook_xchan FROM abook WHERE abook_channel = %d and abook_self = 0 and abook_pending = 0", intval(local_channel()) @@ -51,14 +85,15 @@ class Permcats extends Controller { set_pconfig(local_channel(), 'system', 'default_permcat', 'default'); - if (isset($_POST['default_role'])) { + if (isset($_REQUEST['default_role'])) { set_pconfig(local_channel(), 'system', 'default_permcat', $name); } if ($is_system_role) { // if we have a system role just set the default and assign if aplicable and be done with it - if ($contacts) + if ($contacts) { \Zotlabs\Lib\Permcat::assign($channel, $name, $contacts); + } info( t('Contact role saved.') . EOL); Libsync::build_sync_packet(); @@ -99,19 +134,10 @@ class Permcats extends Controller { $channel = App::get_channel(); - if(argc() > 1) + if(argc() > 1) { $name = hex2bin(argv(1)); - - if(argc() > 2 && argv(2) === 'drop') { - \Zotlabs\Lib\Permcat::delete(local_channel(),$name); - - // TODO: assign all members of the deleted role to the default role - - Libsync::build_sync_packet(); - json_return_and_die([ 'success' => true ]); } - $existing = []; $pcat = new \Zotlabs\Lib\Permcat(local_channel()); @@ -134,7 +160,9 @@ class Permcats extends Controller { */ $is_system_role = false; - $permcats = []; + $delete_role_select_options = []; + $is_default_role = (get_pconfig(local_channel(),'system','default_permcat','default') === $name); + if($pcatlist) { foreach($pcatlist as $pc) { if(($pc['name']) && ($name) && ($pc['name'] == $name)) { @@ -143,13 +171,26 @@ class Permcats extends Controller { $is_system_role = $pc['name']; } - $permcats[bin2hex($pc['name'])] = $pc['localname']; - - if($pc['name'] == $name) + if($pc['name'] == $name) { $localname = $pc['localname']; + } + + if ($pc['name'] !== $name) { + $delete_role_select_options[$pc['name']] = $pc['localname']; + } + } } + // select for delete action + $delete_role_select = [ + 'new_role', + (($is_default_role) ? t('Role to assign affected contacts and default role to') : t('Role to assign affected contacts to')), + '', + '', + $delete_role_select_options + ]; + $global_perms = \Zotlabs\Access\Permissions::Perms(); foreach($global_perms as $k => $v) { @@ -171,7 +212,6 @@ class Permcats extends Controller { ]; } - $is_default_role = (get_pconfig(local_channel(),'system','default_permcat','default') == $name); $group_select_options = [ 'selected' => '', @@ -189,15 +229,11 @@ class Permcats extends Controller { $tpl = get_markup_template("permcats.tpl"); $o .= replace_macros($tpl, array( '$form_security_token' => get_form_security_token("permcats"), - '$default_role' => array('default_role', t('Use this role as default for new contacts'), intval($is_default_role), '', [t('No'), t('Yes')]), - + '$default_role' => array('default_role', t('Automatically assign this role to new contacts'), intval($is_default_role), '', [t('No'), t('Yes')]), '$title' => t('Contact Roles'), - '$tokens' => $t, - '$permcats' => $permcats, - '$atoken' => $atoken, - '$url1' => z_root() . '/channel/' . $channel['channel_address'], - '$url2' => z_root() . '/photos/' . $channel['channel_address'], '$name' => ['name', t('Role name') . ' *', (($localname) ? $localname : ''), (($is_system_role) ? t('System role - not editable') : '') , '', (($is_system_role) ? 'disabled' : '')], + '$delete_label' => t('Deleting') . ' ' . $localname, + '$current_role' => $name, '$perms' => $perms, '$inherited' => t('inherited'), '$is_system_role' => $is_system_role, @@ -206,6 +242,8 @@ class Permcats extends Controller { '$submit' => t('Submit'), '$return_path' => argv(1), '$group_select' => $group_select, + '$delete_role_select' => $delete_role_select, + '$delet_role_button' => t('Delete') )); return $o; -- cgit v1.2.3 From b14a530efb7aeb983f369535d56f62e32043ee39 Mon Sep 17 00:00:00 2001 From: Mario Date: Sat, 8 Jan 2022 13:22:15 +0000 Subject: missing nav_set_selected() --- Zotlabs/Module/Permcats.php | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) (limited to 'Zotlabs/Module/Permcats.php') diff --git a/Zotlabs/Module/Permcats.php b/Zotlabs/Module/Permcats.php index c33580f06..8be5bba88 100644 --- a/Zotlabs/Module/Permcats.php +++ b/Zotlabs/Module/Permcats.php @@ -132,6 +132,8 @@ class Permcats extends Controller { if(! local_channel()) return; + nav_set_selected('Contact Roles'); + $channel = App::get_channel(); if(argc() > 1) { @@ -143,22 +145,6 @@ class Permcats extends Controller { $pcat = new \Zotlabs\Lib\Permcat(local_channel()); $pcatlist = $pcat->listing(); -/* not yet ready - $test = $pcatlist[4]['perms']; - $role_sql = ''; - - foreach ($test as $t) - $role_sql .= "( k = '" . dbesc($t['name']) . "' AND v = '" . intval($t['value']) . "' ) OR "; - - $role_sql = rtrim($role_sql, ' OR '); - - // get all xchans belonging to a permission role - $q = q("SELECT xchan FROM abconfig WHERE chan = %d AND cat = 'my_perms' AND ( $role_sql ) GROUP BY xchan HAVING count(xchan) = %d", - intval(local_channel()), - intval(count($test)) - ); -*/ - $is_system_role = false; $delete_role_select_options = []; $is_default_role = (get_pconfig(local_channel(),'system','default_permcat','default') === $name); -- cgit v1.2.3