streams/Zotlabs/Module/Settings/Permcats.php

149 lines
4.1 KiB
PHP
Raw Normal View History

<?php
namespace Zotlabs\Module\Settings;
2021-12-02 22:33:36 +00:00
use App;
use Zotlabs\Access\PermissionLimits;
use Zotlabs\Access\Permissions;
2018-06-05 01:40:11 +00:00
use Zotlabs\Lib\Libsync;
2021-12-02 22:33:36 +00:00
use Zotlabs\Lib\Permcat;
2021-12-02 23:02:31 +00:00
class Permcats
{
2021-12-02 23:02:31 +00:00
public function post()
{
2022-01-22 10:04:36 +00:00
//logger('$_REQUEST: ' . print_r($_REQUEST,true));
2021-12-03 03:01:39 +00:00
if (!local_channel()) {
2021-12-02 23:02:31 +00:00
return;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$channel = App::get_channel();
2021-12-02 23:02:31 +00:00
check_form_security_token_redirectOnErr('/settings/permcats', 'settings_permcats');
2021-12-02 23:02:31 +00:00
$all_perms = Permissions::Perms();
2021-12-02 23:02:31 +00:00
$name = escape_tags(trim($_POST['name']));
if (!$name) {
notice(t('Permission Name is required.') . EOL);
return;
}
2018-04-17 01:09:14 +00:00
2021-12-02 23:02:31 +00:00
$pcarr = [];
2021-12-02 23:02:31 +00:00
if ($all_perms) {
foreach ($all_perms as $perm => $desc) {
if (array_key_exists('perms_' . $perm, $_POST)) {
$pcarr[] = $perm;
}
}
}
2021-12-02 23:02:31 +00:00
Permcat::update(local_channel(), $name, $pcarr);
2021-12-02 23:02:31 +00:00
Libsync::build_sync_packet();
2022-01-22 10:04:36 +00:00
info(t('Permission role saved.') . EOL);
2021-12-02 23:02:31 +00:00
return;
}
2021-12-02 23:02:31 +00:00
public function get()
{
2021-12-03 03:01:39 +00:00
if (!local_channel()) {
2021-12-02 23:02:31 +00:00
return;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$channel = App::get_channel();
2021-12-03 03:01:39 +00:00
if (argc() > 2) {
2021-12-02 23:02:31 +00:00
$name = hex2bin(argv(2));
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
if (argc() > 3 && argv(3) === 'drop') {
Permcat::delete(local_channel(), $name);
Libsync::build_sync_packet();
json_return_and_die(['success' => true]);
}
2021-12-02 23:02:31 +00:00
$desc = t('Use this form to create permission rules for various classes of people or connections.');
2021-12-02 23:02:31 +00:00
$existing = [];
2021-12-02 23:02:31 +00:00
$pcat = new Permcat(local_channel());
$pcatlist = $pcat->listing();
$permcats = [];
if ($pcatlist) {
foreach ($pcatlist as $pc) {
2021-12-03 03:01:39 +00:00
if (($pc['name']) && ($name) && ($pc['name'] == $name)) {
2021-12-02 23:02:31 +00:00
$existing = $pc['perms'];
2021-12-03 03:01:39 +00:00
}
if (!$pc['system']) {
2021-12-02 23:02:31 +00:00
$permcats[bin2hex($pc['name'])] = $pc['localname'];
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
}
}
2022-01-22 10:04:36 +00:00
$hidden_perms = [];
2021-12-02 23:02:31 +00:00
$global_perms = Permissions::Perms();
2021-12-02 23:02:31 +00:00
foreach ($global_perms as $k => $v) {
$thisperm = Permcat::find_permcat($existing, $k);
2022-01-22 10:04:36 +00:00
2021-12-02 23:02:31 +00:00
$checkinherited = PermissionLimits::Get(local_channel(), $k);
2022-01-22 10:04:36 +00:00
$inherited = (($checkinherited & PERMS_SPECIFIC) ? false : true);
$thisperm = 0;
if ($existing) {
foreach ($existing as $ex) {
if ($ex['name'] === $k) {
$thisperm = $ex['value'];
break;
}
}
}
$perms[] = [ 'perms_' . $k, $v, $inherited ? 1 : intval($thisperm), '', [ t('No'), t('Yes') ], (($inherited) ? ' disabled="disabled" ' : '' )];
if ($inherited) {
$hidden_perms[] = ['perms_' . $k, 1 ];
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
}
2022-01-22 10:04:36 +00:00
2021-12-02 23:02:31 +00:00
$tpl = get_markup_template("settings_permcats.tpl");
$o .= replace_macros($tpl, array(
'$form_security_token' => get_form_security_token("settings_permcats"),
2022-01-22 10:04:36 +00:00
'$title' => t('Permission Roles'),
2021-12-02 23:02:31 +00:00
'$desc' => $desc,
'$desc2' => $desc2,
'$tokens' => $t,
'$permcats' => $permcats,
'$atoken' => $atoken,
'$url1' => z_root() . '/channel/' . $channel['channel_address'],
'$url2' => z_root() . '/photos/' . $channel['channel_address'],
2022-01-22 10:04:36 +00:00
'$name' => array('name', t('Role name') . ' <span class="required">*</span>', (($name) ? $name : ''), ''),
2021-12-02 23:02:31 +00:00
'$me' => t('My Settings'),
'$perms' => $perms,
2022-01-22 10:04:36 +00:00
'$hidden_perms' => $hidden_perms,
2021-12-02 23:02:31 +00:00
'$inherited' => t('inherited'),
'$notself' => 0,
'$self' => 1,
'$permlbl' => t('Individual Permissions'),
2022-01-22 10:04:36 +00:00
'$permnote' => t('Some individual permissions may have been preset or locked based on your channel type and privacy settings.'),
2021-12-02 23:02:31 +00:00
'$submit' => t('Submit')
));
return $o;
}
2017-03-19 12:33:02 +00:00
}