update listing, collections and callbacks

This commit is contained in:
envoyr
2022-02-24 21:39:31 +01:00
parent 703e436b32
commit 5964c3b685
20 changed files with 544 additions and 372 deletions

View File

@@ -220,6 +220,29 @@ class Certificates extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Resou
$cert['letsencrypt'] = Settings::Get('system.le_froxlor_enabled');
$cert['loginname'] = 'froxlor.panel';
}
// Set data from certificate
$cert_data = openssl_x509_parse($cert['ssl_cert_file']);
if ($cert_data) {
$cert['validfromdate'] = date('Y-m-d H:i:s', $cert_data['validFrom_time_t']);
$cert['validtodate'] = date('Y-m-d H:i:s', $cert_data['validTo_time_t']);
$cert['isvalid'] = (bool) $cert_data['validTo_time_t'] > time();
$cert['issuer'] = $cert_data['issuer']['O'] ?? null;
}
// Set subject alt names from certificate
$cert['san'] = null;
if (isset($cert_data['extensions']['subjectAltName']) && ! empty($cert_data['extensions']['subjectAltName'])) {
$SANs = explode(",", $cert_data['extensions']['subjectAltName']);
$SANs = array_map('trim', $SANs);
foreach ($SANs as $san) {
$san = str_replace("DNS:", "", $san);
if ($san != $cert_data['subject']['CN'] && strpos($san, "othername:") === false) {
$cert['san'][] = $san;
}
}
}
$result[] = $cert;
}
return $this->response(array(

View File

@@ -0,0 +1,58 @@
<?php
namespace Froxlor\UI\Callbacks;
use Froxlor\UI\Panel\UI;
/**
* This file is part of the Froxlor project.
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Froxlor team <team@froxlor.org> (2010-)
* @author Maurice Preuß <hello@envoyr.com>
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Listing
*
*/
class Impersonate
{
public static function admin(string $data, array $attributes): array
{
$linker = UI::getLinker();
return [
'type' => 'link',
'data' => [
'text' => $data,
'href' => $linker->getLink([
'section' => 'admins',
'page' => 'admins',
'action' => 'su',
'id' => $attributes['adminid'],
]),
]
];
}
public static function customer(string $data, array $attributes): array
{
$linker = UI::getLinker();
return [
'type' => 'link',
'data' => [
'text' => $data,
'href' => $linker->getLink([
'section' => 'customers',
'page' => 'customers',
'action' => 'su',
'sort' => $attributes['loginname'],
'id' => $attributes['customerid'],
]),
]
];
}
}

View File

@@ -18,11 +18,22 @@ namespace Froxlor\UI\Callbacks;
*/
class Text
{
public static function boolean(string $data): array
public static function boolean(?string $data): array
{
return [
'type' => 'boolean',
'data' => (bool) $data
];
}
}
public static function domainWithSan(string $data, array $attributes): array
{
return [
'type' => 'domainWithSan',
'data' => [
'domain' => $data,
'san' => implode(', ', $attributes['san'] ?? []),
]
];
}
}

View File

@@ -19,59 +19,85 @@ namespace Froxlor\UI;
*/
class Collection
{
private array $items;
private array $userInfo;
private array $params;
private string $class;
private array $has = [];
private array $params;
private array $userinfo;
public function __construct(string $class, array $userInfo, array $params = [])
{
$this->class = $class;
$this->params = $params;
$this->userInfo = $userInfo;
$this->items = $this->getListing($this->class, $this->params);
$this->userinfo = $userInfo;
}
private function getListing($class, $params)
private function getListing($class, $params): array
{
return json_decode($class::getLocal($this->userInfo, $params)->listing(), true);
return json_decode($class::getLocal($this->userinfo, $params)->listing(), true);
}
public function count()
public function count(): int
{
return json_decode($this->class::getLocal($this->userInfo, $this->params)->listingCount(), true);
return json_decode($this->class::getLocal($this->userinfo, $this->params)->listingCount(), true)['data'];
}
public function get()
public function get(): array
{
return $this->items;
}
$result = $this->getListing($this->class, $this->params);
public function getData()
{
return $this->get()['data'];
}
public function getJson()
{
return json_encode($this->get());
}
public function has($column, $class, $parentKey = 'id', $childKey = 'id', $params = []): Collection
{
// check if the api result contains any items (not the overall listingCount as we might be in a search-resultset)
if (count($this->items) > 0) {
$attributes = $this->getListing($class, $params);
if (count($result)) {
foreach ($this->has as $has) {
$attributes = $this->getListing($has['class'], $has['params']);
foreach ($this->items['data']['list'] as $key => $item) {
foreach ($attributes['data']['list'] as $list) {
if ($item[$parentKey] == $list[$childKey]) {
$this->items['data']['list'][$key][$column] = $list;
foreach ($result['data']['list'] as $key => $item) {
foreach ($attributes['data']['list'] as $list) {
if ($item[$has['parentKey']] == $list[$has['childKey']]) {
$result['data']['list'][$key][$has['column']] = $list;
}
}
}
}
}
return $result;
}
public function getData(): array
{
return $this->get()['data'];
}
public function getList(): array
{
return $this->getData()['list'];
}
public function getJson(): string
{
return json_encode($this->get());
}
public function has(string $column, string $class, string $parentKey = 'id', string $childKey = 'id', array $params = []): Collection
{
$this->has[] = [
'column' => $column,
'class' => $class,
'parentKey' => $parentKey,
'childKey' => $childKey,
'params' => $params
];
return $this;
}
public function withPagination(array $columns): Collection
{
// TODO: handle 'sortable' => true in $columns
$pagination = new Pagination($this->userinfo, $columns, $this->count());
$this->params = array_merge($this->params, $pagination->getApiCommandParams());
return $this;
}
}

View File

@@ -20,14 +20,14 @@ use Froxlor\UI\Panel\UI;
*/
class Listing
{
public static function format(Collection $collection, array $tabellisting): array
public static function format(array $list, array $tabellisting): array
{
return [
'title' => $tabellisting['title'],
'icon' => $tabellisting['icon'],
'table' => [
'th' => self::generateTableHeadings($tabellisting),
'tr' => self::generateTableRows($collection, $tabellisting),
'tr' => self::generateTableRows($list, $tabellisting),
],
'pagination' => null, // TODO: write some logic
];
@@ -43,26 +43,31 @@ class Listing
continue;
}
$heading[] = $tabellisting['columns'][$visible_column]['label'];
$heading[] = [
'text' => $tabellisting['columns'][$visible_column]['label'],
'class' => $tabellisting['columns'][$visible_column]['class'] ?? null,
];
}
// Table headings for actions
if (isset($tabellisting['actions'])) {
$heading[] = UI::getLng('panel.options');
$heading[] = [
'text' => UI::getLng('panel.options'),
'class' => 'text-end',
];
}
return $heading;
}
private static function generateTableRows(Collection $collection, array $tabellisting): array
private static function generateTableRows(array $list, array $tabellisting): array
{
$rows = [];
$items = $collection->getData()['list'];
// Create new row from item
foreach ($items as $key => $item) {
foreach ($list as $row => $item) {
// Generate columns from item
foreach ($tabellisting['visible_columns'] as $visible_column) {
foreach ($tabellisting['visible_columns'] as $col => $visible_column) {
if (isset($tabellisting['columns'][$visible_column]['visible']) && !$tabellisting['columns'][$visible_column]['visible']) {
continue;
}
@@ -71,22 +76,30 @@ class Listing
$column = $tabellisting['columns'][$visible_column]['column'];
$data = self::getMultiArrayFromString($item, $column);
// TODO: contextual_class ...
if ($format_callback) {
$rows[$key][] = call_user_func($format_callback, $data, $item);
$rows[$row]['td'][$col]['data'] = call_user_func($format_callback, $data, $item);
} else {
$rows[$key][] = $data;
$rows[$row]['td'][$col]['data'] = $data;
}
$rows[$row]['td'][$col]['class'] = $tabellisting['columns'][$visible_column]['class'] ?? null;
}
// TODO: contextual_class ...
//if (...) {
// $rows[$key]['class'] = '...';
//}
// Set all actions for row
if (isset($tabellisting['actions'])) {
$actions = self::setLinks($tabellisting['actions'], $item);
$rows[$key]['action'] = [
'type' => 'actions',
'data' => $actions
$rows[$row]['td'][] = [
'class' => 'text-end',
'data' => [
'type' => 'actions',
'data' => $actions
]
];
}
}
@@ -94,7 +107,7 @@ class Listing
return $rows;
}
private static function setLinks($actions, array $item)
private static function setLinks(array $actions, array $item): array
{
$linker = UI::getLinker();
@@ -119,7 +132,7 @@ class Listing
return $actions;
}
public static function getVisibleColumnsForListing($listing, $default_columns)
public static function getVisibleColumnsForListing(string $listing, array $default_columns): array
{
// Hier käme dann die Logik, die das aus der DB zieht ...
// alternativ nimmt er die $default_columns, wenn kein Eintrag
@@ -128,7 +141,7 @@ class Listing
return $default_columns;
}
public static function getMultiArrayFromString($arr, $str)
public static function getMultiArrayFromString(array $arr, string $str)
{
foreach (explode('.', $str) as $key) {
if (!array_key_exists($key, $arr)) {