Add "Yourself" ACL choice and allow multiple ACL selectors on a single page

This commit is contained in:
Hypolite Petovan 2020-01-13 21:58:01 -05:00
parent ebf60cee33
commit f0474c07ce
2 changed files with 96 additions and 54 deletions

View file

@ -228,19 +228,34 @@ class ACL
/** /**
* Returns the ACL list of contacts for a given user id * Returns the ACL list of contacts for a given user id
* *
* @param int $user_id * @param int $user_id
* @param array $condition Additional contact lookup table conditions
* @return array * @return array
* @throws \Exception * @throws \Exception
*/ */
public static function getContactListByUserId(int $user_id) public static function getContactListByUserId(int $user_id, array $condition = [])
{ {
$fields = ['id', 'name', 'addr', 'micro']; $fields = ['id', 'name', 'addr', 'micro'];
$params = ['order' => ['name']]; $params = ['order' => ['name']];
$acl_contacts = Contact::selectToArray($fields, $acl_contacts = Contact::selectToArray(
['uid' => $user_id, 'self' => false, 'blocked' => false, 'archive' => false, 'deleted' => false, $fields,
'pending' => false, 'rel' => [Contact::FOLLOWER, Contact::FRIEND]], $params array_merge([
'uid' => $user_id,
'self' => false,
'blocked' => false,
'archive' => false,
'deleted' => false,
'pending' => false,
'rel' => [Contact::FOLLOWER, Contact::FRIEND]
], $condition),
$params
); );
$acl_yourself = Contact::selectFirst($fields, ['uid' => $user_id, 'self' => true]);
$acl_yourself['name'] = L10n::t('Yourself');
$acl_contacts[] = $acl_yourself;
$acl_forums = Contact::selectToArray($fields, $acl_forums = Contact::selectToArray($fields,
['uid' => $user_id, 'self' => false, 'blocked' => false, 'archive' => false, 'deleted' => false, ['uid' => $user_id, 'self' => false, 'blocked' => false, 'archive' => false, 'deleted' => false,
'pending' => false, 'contact-type' => Contact::TYPE_COMMUNITY], $params 'pending' => false, 'contact-type' => Contact::TYPE_COMMUNITY], $params
@ -295,26 +310,38 @@ class ACL
/** /**
* Return the full jot ACL selector HTML * Return the full jot ACL selector HTML
* *
* @param Page $page * @param Page $page
* @param array $user User array * @param array $user User array
* @param bool $for_federation * @param bool $for_federation
* @param array $default_permissions Static defaults permission array: * @param array $default_permissions Static defaults permission array:
* [ * [
* 'allow_cid' => [], * 'allow_cid' => [],
* 'allow_gid' => [], * 'allow_gid' => [],
* 'deny_cid' => [], * 'deny_cid' => [],
* 'deny_gid' => [], * 'deny_gid' => [],
* 'hidewall' => true/false * 'hidewall' => true/false
* ] * ]
* @param array $condition
* @param string $form_prefix
* @return string * @return string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
public static function getFullSelectorHTML(Page $page, array $user = null, bool $for_federation = false, array $default_permissions = []) public static function getFullSelectorHTML(
{ Page $page,
array $user = null,
bool $for_federation = false,
array $default_permissions = [],
array $condition = [],
$form_prefix = ''
) {
if (empty($user['uid'])) { if (empty($user['uid'])) {
return ''; return '';
} }
static $input_group_id = 0;
$input_group_id++;
$page->registerFooterScript(Theme::getPathForFile('asset/typeahead.js/dist/typeahead.bundle.js')); $page->registerFooterScript(Theme::getPathForFile('asset/typeahead.js/dist/typeahead.bundle.js'));
$page->registerFooterScript(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.js')); $page->registerFooterScript(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.js'));
$page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.css')); $page->registerStylesheet(Theme::getPathForFile('js/friendica-tagsinput/friendica-tagsinput.css'));
@ -373,12 +400,21 @@ class ACL
} }
} }
$acl_contacts = self::getContactListByUserId($user['uid']); $acl_contacts = self::getContactListByUserId($user['uid'], $condition);
$acl_groups = self::getGroupListByUserId($user['uid']); $acl_groups = self::getGroupListByUserId($user['uid']);
$acl_list = array_merge($acl_groups, $acl_contacts); $acl_list = array_merge($acl_groups, $acl_contacts);
$input_names = [
'visibility' => $form_prefix ? $form_prefix . '[visibility]' : 'visibility',
'group_allow' => $form_prefix ? $form_prefix . '[group_allow]' : 'group_allow',
'contact_allow' => $form_prefix ? $form_prefix . '[contact_allow]' : 'contact_allow',
'group_deny' => $form_prefix ? $form_prefix . '[group_deny]' : 'group_deny',
'contact_deny' => $form_prefix ? $form_prefix . '[contact_deny]' : 'contact_deny',
'emailcc' => $form_prefix ? $form_prefix . '[emailcc]' : 'emailcc',
];
$tpl = Renderer::getMarkupTemplate('acl_selector.tpl'); $tpl = Renderer::getMarkupTemplate('acl_selector.tpl');
$o = Renderer::replaceMacros($tpl, [ $o = Renderer::replaceMacros($tpl, [
'$public_title' => DI::l10n()->t('Public'), '$public_title' => DI::l10n()->t('Public'),
@ -402,6 +438,8 @@ class ACL
'$for_federation' => $for_federation, '$for_federation' => $for_federation,
'$jotnets_fields' => $jotnets_fields, '$jotnets_fields' => $jotnets_fields,
'$user_hidewall' => $default_permissions['hidewall'], '$user_hidewall' => $default_permissions['hidewall'],
'$input_names' => $input_names,
'$input_group_id' => $input_group_id,
]); ]);
return $o; return $o;

View file

@ -1,11 +1,11 @@
<div id="acl-wrapper"> <div id="acl-wrapper">
<div class="panel-group" id="visibility-accordion" role="tablist" aria-multiselectable="true"> <div class="panel-group" id="visibility-accordion-{{$input_group_id}}" role="tablist" aria-multiselectable="true">
<div class="panel panel-success"> <div class="panel panel-success">
<label class="panel-heading{{if $visibility != 'public'}} collapsed{{/if}}" id="visibility-public-heading" aria-expanded="{{if $visibility == 'public'}}true{{else}}false{{/if}}"> <label class="panel-heading{{if $visibility != 'public'}} collapsed{{/if}}" id="visibility-public-heading-{{$input_group_id}}" aria-expanded="{{if $visibility == 'public'}}true{{else}}false{{/if}}">
<input type="radio" name="visibility" id="visibility-public" value="public" tabindex="14" {{if $visibility == 'public'}}checked{{/if}}> <input type="radio" name="{{$input_names.visibility}}" id="visibility-public-{{$input_group_id}}" value="public" tabindex="14" {{if $visibility == 'public'}}checked{{/if}}>
<i class="fa fa-globe"></i> {{$public_title}} <i class="fa fa-globe"></i> {{$public_title}}
</label> </label>
<fieldset id="visibility-public-panel" class="panel-collapse collapse{{if $visibility == 'public'}} in{{/if}}" role="tabpanel" aria-labelledby="visibility-public-heading" {{if $visibility != 'public'}}disabled{{/if}}> <fieldset id="visibility-public-panel-{{$input_group_id}}" class="panel-collapse collapse{{if $visibility == 'public'}} in{{/if}}" role="tabpanel" aria-labelledby="visibility-public-heading-{{$input_group_id}}" {{if $visibility != 'public'}}disabled{{/if}}>
<div class="panel-body"> <div class="panel-body">
<p>{{$public_desc}}</p> <p>{{$public_desc}}</p>
{{if $for_federation}} {{if $for_federation}}
@ -39,26 +39,26 @@
</fieldset> </fieldset>
</div> </div>
<div class="panel panel-info"> <div class="panel panel-info">
<label class="panel-heading{{if $visibility != 'custom'}} collapsed{{/if}}" id="visibility-custom-heading" aria-expanded="{{if $visibility == 'custom'}}true{{else}}false{{/if}}"> <label class="panel-heading{{if $visibility != 'custom'}} collapsed{{/if}}" id="visibility-custom-heading-{{$input_group_id}}" aria-expanded="{{if $visibility == 'custom'}}true{{else}}false{{/if}}">
<input type="radio" name="visibility" id="visibility-custom" value="custom" tabindex="15" {{if $visibility == 'custom'}}checked{{/if}}> <input type="radio" name="{{$input_names.visibility}}" id="visibility-custom-{{$input_group_id}}" value="custom" tabindex="15" {{if $visibility == 'custom'}}checked{{/if}}>
<i class="fa fa-lock"></i> {{$custom_title}} <i class="fa fa-lock"></i> {{$custom_title}}
</label> </label>
<fieldset id="visibility-custom-panel" class="panel-collapse collapse{{if $visibility == 'custom'}} in{{/if}}" role="tabpanel" aria-labelledby="visibility-custom-heading" {{if $visibility != 'custom'}}disabled{{/if}}> <fieldset id="visibility-custom-panel-{{$input_group_id}}" class="panel-collapse collapse{{if $visibility == 'custom'}} in{{/if}}" role="tabpanel" aria-labelledby="visibility-custom-heading-{{$input_group_id}}" {{if $visibility != 'custom'}}disabled{{/if}}>
<input type="hidden" name="group_allow" value="{{$group_allow}}"/> <input type="hidden" name="{{$input_names.group_allow}}" value="{{$group_allow}}"/>
<input type="hidden" name="contact_allow" value="{{$contact_allow}}"/> <input type="hidden" name="{{$input_names.contact_allow}}" value="{{$contact_allow}}"/>
<input type="hidden" name="group_deny" value="{{$group_deny}}"/> <input type="hidden" name="{{$input_names.group_deny}}" value="{{$group_deny}}"/>
<input type="hidden" name="contact_deny" value="{{$contact_deny}}"/> <input type="hidden" name="{{$input_names.contact_deny}}" value="{{$contact_deny}}"/>
<div class="panel-body"> <div class="panel-body">
<p>{{$custom_desc}}</p> <p>{{$custom_desc}}</p>
<div class="form-group"> <div class="form-group">
<label for="acl_allow">{{$allow_label}}</label> <label for="acl_allow-{{$input_group_id}}">{{$allow_label}}</label>
<input type="text" class="form-control input-lg" id="acl_allow"> <input type="text" class="form-control input-lg" id="acl_allow-{{$input_group_id}}">
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="acl_deny">{{$deny_label}}</label> <label for="acl_deny-{{$input_group_id}}">{{$deny_label}}</label>
<input type="text" class="form-control input-lg" id="acl_deny"> <input type="text" class="form-control input-lg" id="acl_deny-{{$input_group_id}}">
</div> </div>
</div> </div>
</fieldset> </fieldset>
@ -68,29 +68,29 @@
{{if $for_federation}} {{if $for_federation}}
<div class="form-group"> <div class="form-group">
<label for="profile-jot-email" id="profile-jot-email-label">{{$emailcc}}</label> <label for="profile-jot-email" id="profile-jot-email-label-{{$input_group_id}}">{{$emailcc}}</label>
<input type="text" name="emailcc" id="profile-jot-email" class="form-control" title="{{$emtitle}}" /> <input type="text" name="{{$input_names.emailcc}}" id="profile-jot-email-{{$input_group_id}}" class="form-control" title="{{$emtitle}}" />
</div> </div>
<div id="profile-jot-email-end"></div> <div id="profile-jot-email-end-{{$input_group_id}}"></div>
{{/if}} {{/if}}
</div> </div>
<script type="text/javascript"> <script type="text/javascript">
$(function() { $(function() {
let $acl_allow_input = $('#acl_allow'); let $acl_allow_input = $('#acl_allow-{{$input_group_id}}');
let $contact_allow_input = $('[name=contact_allow]'); let $contact_allow_input = $('[name="{{$input_names.contact_allow}}"]');
let $group_allow_input = $('[name=group_allow]'); let $group_allow_input = $('[name="{{$input_names.group_allow}}"]');
let $acl_deny_input = $('#acl_deny'); let $acl_deny_input = $('#acl_deny-{{$input_group_id}}');
let $contact_deny_input = $('[name=contact_deny]'); let $contact_deny_input = $('[name="{{$input_names.contact_deny}}"]');
let $group_deny_input = $('[name=group_deny]'); let $group_deny_input = $('[name="{{$input_names.group_deny}}"]');
let $visibility_public_panel = $('#visibility-public-panel'); let $visibility_public_panel = $('#visibility-public-panel-{{$input_group_id}}');
let $visibility_custom_panel = $('#visibility-custom-panel'); let $visibility_custom_panel = $('#visibility-custom-panel-{{$input_group_id}}');
let $visibility_public_radio = $('#visibility-public'); let $visibility_public_radio = $('#visibility-public-{{$input_group_id}}');
let $visibility_custom_radio = $('#visibility-custom'); let $visibility_custom_radio = $('#visibility-custom-{{$input_group_id}}');
// Frio specific // Frio specific
if ($.fn.collapse) { if ($.fn.collapse) {
$visibility_public_panel.collapse({parent: '#visibility-accordion', toggle: false}); $visibility_public_panel.collapse({parent: '#visibility-accordion-{{$input_group_id}}', toggle: false});
$visibility_custom_panel.collapse({parent: '#visibility-accordion', toggle: false}); $visibility_custom_panel.collapse({parent: '#visibility-accordion-{{$input_group_id}}', toggle: false});
} }
$visibility_public_radio.on('change', function (e) { $visibility_public_radio.on('change', function (e) {
@ -101,13 +101,13 @@
$visibility_public_panel.prop('disabled', false); $visibility_public_panel.prop('disabled', false);
$visibility_custom_panel.prop('disabled', true); $visibility_custom_panel.prop('disabled', true);
$('.profile-jot-net input[type=checkbox]').each(function() { $('#visibility-public-panel-{{$input_group_id}} .profile-jot-net input[type=checkbox]').each(function() {
// Restores checkbox state if it had been saved // Restores checkbox state if it had been saved
if ($(this).attr('data-checked') !== undefined) { if ($(this).attr('data-checked') !== undefined) {
$(this).prop('checked', $(this).attr('data-checked') === 'true'); $(this).prop('checked', $(this).attr('data-checked') === 'true');
} }
}); });
$('.profile-jot-net input').attr('disabled', false); $('#visibility-public-panel-{{$input_group_id}} .profile-jot-net input').attr('disabled', false);
}); });
$visibility_custom_radio.on('change', function(e) { $visibility_custom_radio.on('change', function(e) {
@ -118,13 +118,13 @@
$visibility_public_panel.prop('disabled', true); $visibility_public_panel.prop('disabled', true);
$visibility_custom_panel.prop('disabled', false); $visibility_custom_panel.prop('disabled', false);
$('.profile-jot-net input[type=checkbox]').each(function() { $('#visibility-public-panel-{{$input_group_id}} .profile-jot-net input[type=checkbox]').each(function() {
// Saves current checkbox state // Saves current checkbox state
$(this) $(this)
.attr('data-checked', $(this).prop('checked')) .attr('data-checked', $(this).prop('checked'))
.prop('checked', false); .prop('checked', false);
}); });
$('.profile-jot-net input').attr('disabled', 'disabled'); $('#visibility-public-panel-{{$input_group_id}} .profile-jot-net input').attr('disabled', 'disabled');
}); });
// Custom visibility tags inputs // Custom visibility tags inputs
@ -233,9 +233,11 @@
// Anti-duplicate callback + acl fields value generation // Anti-duplicate callback + acl fields value generation
$acl_allow_input.on('itemAdded', function (event) { $acl_allow_input.on('itemAdded itemRemoved', function (event) {
// Removes duplicate in the opposite acl box if (event.type === 'itemAdded') {
$acl_deny_input.tagsinput('remove', event.item); // Removes duplicate in the opposite acl box
$acl_deny_input.tagsinput('remove', event.item);
}
// Update the real acl field // Update the real acl field
$group_allow_input.val(''); $group_allow_input.val('');
@ -249,9 +251,11 @@
}); });
}); });
$acl_deny_input.on('itemAdded', function (event) { $acl_deny_input.on('itemAdded itemRemoved', function (event) {
// Removes duplicate in the opposite acl box if (event.type === 'itemAdded') {
$acl_allow_input.tagsinput('remove', event.item); // Removes duplicate in the opposite acl box
$acl_allow_input.tagsinput('remove', event.item);
}
// Update the real acl field // Update the real acl field
$group_deny_input.val(''); $group_deny_input.val('');