Verified Commit a788404b authored by Côme Chilliet's avatar Côme Chilliet
Browse files

:tractor: feat(gpg) Migrate GPG to selectManagement

issue #6006
Showing with 250 additions and 317 deletions
+250 -317
......@@ -44,7 +44,7 @@ class gpgAccount extends simplePlugin
'name' => _('GPG keys'),
'class' => ['fullwidth'],
'attrs' => [
new PgpKeyAttribute(
new PgpKeysAttribute(
'', _('GPG keys of this user'),
'fdUserKeyDN', TRUE
)
......
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2013-2019 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
class PgpKeySelectDialog extends GenericSelectManagementDialog
{
protected $dialogClass = 'pgpKeySelect';
}
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2013-2019 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
class PgpKeysAttribute extends GenericDialogAttribute
{
protected $dialogClass = 'PgpKeySelectDialog';
protected $height = 200;
protected $displayed_values;
function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = [], $acl = '')
{
parent::__construct($label, $description, $ldapName, $required, $defaultValue, 'dn', NULL, $acl);
$this->displayed_values = pgpKeySelect::getPgpFields();
}
public function getSelectManagementParameters (): array
{
/*
* objectTypes
* multiSelect
* neededAttrs
* blackList
* whiteList
* filterElementDefinitions
* */
$neededAttrs = array_merge(
[
'objectClass' => '*',
'dn' => 'raw',
],
array_map(
function ($label)
{
return '*';
},
$this->displayed_values
)
);
return [
['pgpKey'],
TRUE,
$neededAttrs,
$this->getFilterBlackList(),
$this->getFilterWhiteList(),
];
}
function addValue (string $dn, $entry = NULL)
{
if (!in_array($dn, $this->value)) {
$this->value[] = $dn;
$this->displays[] = $entry;
}
}
protected function fillDisplayValue ($i)
{
global $config;
$ldap = $config->get_ldap_link();
$value = $this->value[$i];
$ldap->cat($value);
if (!$ldap->count()) {
unset($this->value[$i]);
} else {
$attrs = $ldap->fetch();
$this->displays[$i] = $attrs;
}
}
function loadPostValue ()
{
if ($this->isVisible()) {
$id = $this->getHtmlId();
foreach ($_POST as $name => $value) {
if (preg_match('/^'.$id.'_del_/', $name)) {
$key = preg_replace('/^'.$id.'_del_/', '', $name);
$key = preg_replace('/_[xy]$/', '', $key);
$this->removeValue($key);
break;
}
}
if (isset($_POST['add'.$this->getLdapName().'_dialog'])) {
$this->plugin->openDialog(new $this->dialogClass($this->plugin, $this));
}
}
}
function renderOnlyFormInput (): string
{
$id = $this->getHtmlId();
$div = new divSelectBox($id);
$div->setHeight($this->height);
$headers = array_values($this->displayed_values);
$headers[] = '';
$div->setHeaders($headers);
// Updates and get display values
$displays = $this->getDisplayValues();
foreach ($displays as $key => $display_item) {
$fields = [];
foreach (array_keys($this->displayed_values) as $field) {
if (isset($display_item[$field][0]) && !empty($display_item[$field][0])) {
$display = $display_item[$field][0];
if ($field == 'pgpKeySize') {
// Remove useless 0 in front of the number
$display = pgpKeySelect::pgpSize($display);
} elseif ($field == 'pgpKeyCreateTime') {
$display = pgpKeySelect::pgpDate($display);
} elseif ($field == 'pgpKeyExpireTime') {
$display = pgpKeySelect::pgpDate($display);
} elseif ($field == 'pgpRevoked') {
$display = pgpKeySelect::pgpRevoked($display);
} elseif ($field == 'pgpDisabled') {
$display = pgpKeySelect::pgpRevoked($display);
}
$fields[] = ['string' => $display];
} else {
$display = '';
if (($field == 'pgpRevoked') || ($field == 'pgpDisabled')) {
$display = pgpKeySelect::pgpRevoked($display);
}
$fields[] = ['string' => $display];
}
}
$img = '<input type="image" src="geticon.php?context=actions&icon=edit-delete&size=16" name="'.$id.'_del_'.$key.'" class="center"/>&nbsp;';
$fields[] = ['html' => $img, 'attach' => 'style="border-right:0px;width:20px;"'];
$div->addEntry($fields);
}
$smarty = get_smarty();
$smarty->assign("div_$id", $div->drawList());
return '{$div_'.$id.'}'."\n";
}
public function htmlIds (): array
{
$id = $this->getHtmlId();
$ids = ['add'.$id.'_dialog'];
$nb_values = count($this->value);
for ($i = 0; $i < $nb_values; ++$i) {
$ids[] = $id.'_del_'.$i;
}
return $ids;
}
function renderButtons ()
{
$id = $this->getHtmlId();
$buttons = '<input type="submit" formnovalidate="formnovalidate" name="add'.$id.'_dialog" id="add'.$id.'_dialog"'.
($this->disabled ? ' disabled="disabled"' : '').
' value="{msgPool type=addButton}"/>';
return $buttons;
}
}
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2013-2016 FusionDirectory
Copyright (C) 2013-2019 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -18,126 +18,70 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
class pgpKeySelect extends simpleSelectManagement
class pgpKeySelect extends selectManagement
{
protected $objectTypes = [];
protected $autoFilter = FALSE;
/* Create pqpKey object type */
static function plInfo (): array
{
return [
'plShortName' => _('PGP Key selection'),
'plDescription' => _('PGP Key selection for a user'),
'plObjectType' => ['pgpKey' => [
'name' => _('PGP Key'),
'description' => _('PGP Key information'),
'filter' => '(objectClass=pgpKeyInfo)',
'icon' => 'geticon.php?context=applications&icon=gpg&size=16',
'mainAttr' => 'pgpCertID',
'ou' => static::getPgpKeysOu(),
]],
'plProvidedAcls' => static::getPgpFields(),
];
}
protected $departmentBrowser = FALSE;
protected $departmentRootVisible = FALSE;
protected $baseMode = FALSE;
protected $multiSelect = TRUE;
/* Default columns */
public static $columns = [
['ObjectTypeColumn', []],
['LinkColumn', ['attributes' => 'pgpCertID', 'label' => 'Cert ID']],
['LinkColumn', ['attributes' => 'pgpKeyID', 'label' => 'Key ID']],
['LinkColumn', ['attributes' => 'pgpUserID', 'label' => 'User ID']],
['LinkColumn', ['attributes' => 'pgpKeyCreateTime', 'label' => 'Creation time']],
['LinkColumn', ['attributes' => 'pgpKeyExpireTime', 'label' => 'Expiration']],
['LinkColumn', ['attributes' => 'pgpKeyType', 'label' => 'Algorithm']],
['LinkColumn', ['attributes' => 'pgpKeySize', 'label' => 'Size']],
['LinkColumn', ['attributes' => 'pgpRevoked', 'label' => 'Revoked']],
['LinkColumn', ['attributes' => 'pgpDisabled', 'label' => 'Disabled']],
];
function __construct ()
{
$this->listXMLPath = get_template_path('pgpKeySelect-list.xml', TRUE, dirname(__FILE__));
parent::__construct();
}
/* Disable and hide configuration system */
protected $skipConfiguration = TRUE;
function configureHeadpage ()
protected function setUpListing ()
{
parent::configureHeadpage();
$this->headpage->registerElementFilter('pgpDate', 'pgpKeySelect::filterPgpDate');
$this->headpage->registerElementFilter('pgpSize', 'pgpKeySelect::filterPgpSize');
$this->headpage->registerElementFilter('pgpDisabled', 'pgpKeySelect::filterPgpRevoked');
$this->headpage->registerElementFilter('pgpRevoked', 'pgpKeySelect::filterPgpRevoked');
/* Set baseMode to FALSE */
$this->listing = new managementListing($this, FALSE, $this->multiSelect);
}
/* Build the filter */
function configureFilter ()
public static function getPgpKeysOu (): string
{
global $config;
$ldap = $config->get_ldap_link();
$ldap->cat('cn=pgpServerInfo,'.$config->current['BASE']);
if ($pgpServerInfo = $ldap->fetch()) {
$this->storagePoints = [
preg_replace(
'/'.$config->current['BASE'].'$/', '',
$pgpServerInfo['pgpBaseKeySpaceDN'][0]
)
];
return preg_replace(
'/'.$config->current['BASE'].'$/', '',
$pgpServerInfo['pgpBaseKeySpaceDN'][0]
);
} else {
msg_dialog::display(_('Configuration error'), _('You need to configure GPG base dn through the addons section first'));
return '';
}
$this->filter->setObjectStorage($this->storagePoints);
$this->filter->category = 'user';
$attributes = ['pgpCertID', 'pgpKeyID', 'pgpUserID', 'pgpKeyCreateTime', 'pgpKeyExpireTime', 'pgpKeyType', 'pgpKeySize'];
$this->filter->query[0]['backend'] = 'LDAPBlacklist';
$this->filter->query[0]['filter'] = '$NAME';
$this->filter->query[0]['attribute'] = $attributes;
$this->filter->elements['NAME']['set'] = '(|('.join('=*$*)(', $attributes).'=*$*))';
$this->filter->elements['NAME']['autocomplete']['filter'] = '(|('.join('=*$NAME*)(', $attributes).'=*$NAME*))';
$this->filter->elements['NAME']['autocomplete']['attribute'] = $attributes;
}
static function filterSingleValue ($value, $func)
{
if (is_array($value)) {
$value = reset($value);
}
if (($value === FALSE) || ($value === '')) {
return '&nbsp;';
}
$return = static::$func($value);
return ($return === '' ? '&nbsp;' : $return);
}
static function filterPgpDate ($value = [])
{
return static::filterSingleValue($value, 'pgpDate');
}
static function filterPgpSize ($value = [])
{
return static::filterSingleValue($value, 'pgpSize');
}
static function filterPgpRevoked ($value = [])
{
if (empty($value)) {
return 'No';
}
return static::pgpRevoked($value[0]);
}
static function pgpDate ($date)
{
if (empty($date)) {
return $date;
}
return DateTime::createFromFormat('Ymd', substr($date, 0, 8))->format('d.m.Y');
}
static function pgpSize ($size)
{
// Remove useless 0 at the beginning
return strval(intval($size));
}
static function pgpRevoked ($value)
{
// Remove useless 0 at the beginning
return ($value == 0 ? 'No' : 'Yes');
}
}
class PgpKeySelectDialog extends GenericSelectDialog
{
protected $dialogClass = 'pgpKeySelect';
}
class PgpKeyAttribute extends GenericDialogAttribute
{
protected $dialogClass = 'PgpKeySelectDialog';
protected $height = 200;
protected $displayed_values;
function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = [], $acl = '')
public static function getPgpFields (): array
{
parent::__construct($label, $description, $ldapName, $required, $defaultValue, 'dn', NULL, $acl);
$this->displayed_values = [
return [
'pgpCertID' => _('Cert ID'),
'pgpKeyID' => _('Key ID'),
'pgpUserID' => _('User ID'),
......@@ -150,109 +94,22 @@ class PgpKeyAttribute extends GenericDialogAttribute
];
}
function addValue (string $dn, $entry = NULL)
{
if (!in_array($dn, $this->value)) {
$this->value[] = $dn;
$this->displays[] = $entry;
}
}
protected function fillDisplayValue ($i)
{
global $config;
$ldap = $config->get_ldap_link();
$value = $this->value[$i];
$ldap->cat($value);
if (!$ldap->count()) {
unset($this->value[$i]);
} else {
$attrs = $ldap->fetch();
$this->displays[$i] = $attrs;
}
}
function loadPostValue ()
{
if ($this->isVisible()) {
$id = $this->getHtmlId();
foreach ($_POST as $name => $value) {
if (preg_match('/^'.$id.'_del_/', $name)) {
$key = preg_replace('/^'.$id.'_del_/', '', $name);
$key = preg_replace('/_[xy]$/', '', $key);
$this->removeValue($key);
break;
}
}
if (isset($_POST['add'.$this->getLdapName().'_dialog'])) {
$this->plugin->openDialog(new $this->dialogClass($this->plugin, $this));
}
}
}
function renderOnlyFormInput (): string
static function pgpDate ($date)
{
$id = $this->getHtmlId();
$div = new divSelectBox($id);
$div->setHeight($this->height);
$headers = array_values($this->displayed_values);
$headers[] = '';
$div->setHeaders($headers);
// Updates and get display values
$displays = $this->getDisplayValues();
foreach ($displays as $key => $display_item) {
$fields = [];
foreach (array_keys($this->displayed_values) as $field) {
if (isset($display_item[$field][0]) && !empty($display_item[$field][0])) {
$display = $display_item[$field][0];
if ($field == 'pgpKeySize') {
// Remove useless 0 in front of the number
$display = pgpKeySelect::pgpSize($display);
} elseif ($field == 'pgpKeyCreateTime') {
$display = pgpKeySelect::pgpDate($display);
} elseif ($field == 'pgpKeyExpireTime') {
$display = pgpKeySelect::pgpDate($display);
} elseif ($field == 'pgpRevoked') {
$display = pgpKeySelect::pgpRevoked($display);
} elseif ($field == 'pgpDisabled') {
$display = pgpKeySelect::pgpRevoked($display);
}
$fields[] = ['string' => $display];
} else {
$display = '';
if (($field == 'pgpRevoked') || ($field == 'pgpDisabled')) {
$display = pgpKeySelect::pgpRevoked($display);
}
$fields[] = ['string' => $display];
}
}
$img = '<input type="image" src="geticon.php?context=actions&icon=edit-delete&size=16" name="'.$id.'_del_'.$key.'" class="center"/>&nbsp;';
$fields[] = ['html' => $img, 'attach' => 'style="border-right:0px;width:20px;"'];
$div->addEntry($fields);
if (empty($date)) {
return $date;
}
$smarty = get_smarty();
$smarty->assign("div_$id", $div->drawList());
return '{$div_'.$id.'}'."\n";
return DateTime::createFromFormat('Ymd', substr($date, 0, 8))->format('d.m.Y');
}
public function htmlIds (): array
static function pgpSize ($size)
{
$id = $this->getHtmlId();
$ids = ['add'.$id.'_dialog'];
$nb_values = count($this->value);
for ($i = 0; $i < $nb_values; ++$i) {
$ids[] = $id.'_del_'.$i;
}
return $ids;
// Remove useless 0 at the beginning
return strval(intval($size));
}
function renderButtons ()
static function pgpRevoked ($value)
{
$id = $this->getHtmlId();
$buttons = '<input type="submit" formnovalidate="formnovalidate" name="add'.$id.'_dialog" id="add'.$id.'_dialog"'.
($this->disabled ? ' disabled="disabled"' : '').
' value="{msgPool type=addButton}"/>';
return $buttons;
return ($value == 0 ? 'No' : 'Yes');
}
}
<?xml version="1.0" encoding="UTF-8"?>
<list>
<definition>
<departmentBrowser>false</departmentBrowser>
<departmentRootVisible>false</departmentRootVisible>
<baseMode>false</baseMode>
<multiSelect>true</multiSelect>
<template>simple-select-list.tpl</template>
<module>simple</module>
<label>Please select the desired entries</label>
<defaultSortColumn>1</defaultSortColumn>
<objectType>
<label>PGP Key</label>
<objectClass>pgpKeyInfo</objectClass>
<category>user</category>
<class>gpgAccount</class>
<image>geticon.php?context=status&amp;icon=dialog-password&amp;size=16</image>
</objectType>
</definition>
<table>
<layout>|20px;c||||||||||</layout>
<department>
<value>%{filter:departmentType(row,dn)}</value>
</department>
<department>
<value>%{filter:departmentLink(row,dn,description)}</value>
<span>2</span>
</department>
<column>
<value>%{filter:objectType(row,dn)}</value>
</column>
<column>
<label>Cert ID</label>
<sortAttribute>pgpCertID</sortAttribute>
<sortType>string</sortType>
<value>%{filter:selectLink(pid,row,dn,pgpCertID)}</value>
<export>true</export>
</column>
<column>
<label>Key ID</label>
<sortAttribute>pgpKeyID</sortAttribute>
<sortType>string</sortType>
<value>%{filter:selectLink(pid,row,dn,pgpKeyID)}</value>
<export>true</export>
</column>
<column>
<label>User</label>
<sortAttribute>pgpUserID</sortAttribute>
<sortType>string</sortType>
<value>%{filter:selectLink(pid,row,dn,pgpUserID)}</value>
<export>true</export>
</column>
<column>
<label>Creation date</label>
<sortAttribute>pgpKeyCreateTime</sortAttribute>
<sortType>date</sortType>
<value>%{filter:pgpDate(pgpKeyCreateTime)}</value>
<export>true</export>
</column>
<column>
<label>Expiration date</label>
<sortAttribute>pgpKeyExpireTime</sortAttribute>
<sortType>date</sortType>
<value>%{filter:pgpDate(pgpKeyExpireTime)}</value>
<export>true</export>
</column>
<column>
<label>Type</label>
<sortAttribute>pgpKeyType</sortAttribute>
<sortType>string</sortType>
<value>%{pgpKeyType}</value>
<export>true</export>
</column>
<column>
<label>Size</label>
<sortAttribute>pgpKeySize</sortAttribute>
<sortType>integer</sortType>
<value>%{filter:pgpSize(pgpKeySize)}</value>
<export>true</export>
</column>
<column>
<label>Revoked</label>
<sortAttribute>pgpRevoked</sortAttribute>
<sortType>string</sortType>
<value>%{filter:pgpRevoked(pgpRevoked)}</value>
<export>true</export>
</column>
<column>
<label>Disabled</label>
<sortAttribute>pgpDisabled</sortAttribute>
<sortType>string</sortType>
<value>%{filter:pgpDisabled(pgpDisabled)}</value>
<export>true</export>
</column>
</table>
<actionmenu>
</actionmenu>
<actiontriggers snapshot="false" copypaste="false">
</actiontriggers>
</list>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment