move some methods into trait, because extensions may need them

This commit is contained in:
Uwe Steinmann 2026-01-31 16:06:51 +01:00
parent 4ecd84ee1c
commit 86df898297
2 changed files with 147 additions and 126 deletions

143
inc/inc.TraitRestapi.php Normal file
View File

@ -0,0 +1,143 @@
<?php
/**
* Some function for the rest api
*
* @category DMS
* @package SeedDMS
* @license GPL 2
* @version @version@
* @author Uwe Steinmann <uwe@steinmann.cx>
* @copyright Copyright (C) 2026 Uwe Steinmann
* @version Release: @package_version@
*/
trait SeedDMS_RestapiTrait { /* {{{ */
protected function __getAttributesData($obj) { /* {{{ */
$attributes = $obj->getAttributes();
$attrvalues = array();
if($attributes) {
foreach($attributes as $attrdefid=>$attribute) {
$attrdef = $attribute->getAttributeDefinition();
$attrvalues[] = array(
'id'=>(int) $attrdef->getId(),
'name'=>$attrdef->getName(),
'value'=>$attribute->getValue()
);
}
}
return $attrvalues;
} /* }}} */
protected function __getDocumentData($document) { /* {{{ */
$cats = $document->getCategories();
$tmp = [];
foreach($cats as $cat) {
$tmp[] = $this->__getCategoryData($cat);
}
$data = array(
'type'=>'document',
'id'=>(int)$document->getId(),
'date'=>date('Y-m-d H:i:s', $document->getDate()),
'name'=>$document->getName(),
'comment'=>$document->getComment(),
'keywords'=>$document->getKeywords(),
'categories'=>$tmp,
'owner'=>(int)$document->getOwner()->getId()
);
return $data;
} /* }}} */
protected function __getLatestVersionData($lc) { /* {{{ */
$document = $lc->getDocument();
$data = array(
'type'=>'document',
'id'=>(int)$document->getId(),
'date'=>date('Y-m-d H:i:s', $document->getDate()),
'name'=>$document->getName(),
'comment'=>$document->getComment(),
'keywords'=>$document->getKeywords(),
'ownerid'=>(int) $document->getOwner()->getID(),
'islocked'=>$document->isLocked(),
'sequence'=>$document->getSequence(),
'expires'=>$document->getExpires() ? date('Y-m-d H:i:s', $document->getExpires()) : "",
'mimetype'=>$lc->getMimeType(),
'filetype'=>$lc->getFileType(),
'origfilename'=>$lc->getOriginalFileName(),
'version'=>$lc->getVersion(),
'version_comment'=>$lc->getComment(),
'version_date'=>date('Y-m-d H:i:s', $lc->getDate()),
'size'=>(int) $lc->getFileSize(),
);
$cats = $document->getCategories();
if($cats) {
$c = array();
foreach($cats as $cat) {
$c[] = array('id'=>(int)$cat->getID(), 'name'=>$cat->getName());
}
$data['categories'] = $c;
}
$attributes = $this->__getAttributesData($document);
if($attributes) {
$data['attributes'] = $attributes;
}
$attributes = $this->__getAttributesData($lc);
if($attributes) {
$data['version_attributes'] = $attributes;
}
return $data;
} /* }}} */
protected function __getDocumentVersionData($lc) { /* {{{ */
$data = array(
'id'=>(int) $lc->getId(),
'version'=>$lc->getVersion(),
'date'=>date('Y-m-d H:i:s', $lc->getDate()),
'mimetype'=>$lc->getMimeType(),
'filetype'=>$lc->getFileType(),
'origfilename'=>$lc->getOriginalFileName(),
'size'=>(int) $lc->getFileSize(),
'comment'=>$lc->getComment(),
);
return $data;
} /* }}} */
protected function __getDocumentFileData($file) { /* {{{ */
$data = array(
'id'=>(int)$file->getId(),
'name'=>$file->getName(),
'date'=>$file->getDate(),
'mimetype'=>$file->getMimeType(),
'comment'=>$file->getComment(),
);
return $data;
} /* }}} */
protected function __getDocumentLinkData($link) { /* {{{ */
$data = array(
'id'=>(int)$link->getId(),
'target'=>$this->__getDocumentData($link->getTarget()),
'public'=>(boolean)$link->isPublic(),
);
return $data;
} /* }}} */
protected function __getFolderData($folder) { /* {{{ */
$data = array(
'type'=>'folder',
'id'=>(int)$folder->getID(),
'name'=>$folder->getName(),
'comment'=>$folder->getComment(),
'date'=>date('Y-m-d H:i:s', $folder->getDate()),
'owner'=>(int)$folder->getOwner()->getId()
);
$attributes = $this->__getAttributesData($folder);
if($attributes) {
$data['attributes'] = $attributes;
}
return $data;
} /* }}} */
} /* }}} */

View File

@ -16,6 +16,7 @@ require_once("../inc/inc.ClassNotificationService.php");
require_once("../inc/inc.ClassEmailNotify.php");
require_once("../inc/inc.Notification.php");
require_once("../inc/inc.ClassController.php");
require_once("../inc/inc.TraitRestapi.php");
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
@ -44,6 +45,8 @@ final class JsonRenderer { /* {{{ */
} /* }}} */
final class SeedDMS_RestapiController { /* {{{ */
use SeedDMS_RestapiTrait;
protected $container;
protected $renderer;
@ -53,131 +56,6 @@ final class SeedDMS_RestapiController { /* {{{ */
$this->renderer = $renderer;
}
protected function __getAttributesData($obj) { /* {{{ */
$attributes = $obj->getAttributes();
$attrvalues = array();
if($attributes) {
foreach($attributes as $attrdefid=>$attribute) {
$attrdef = $attribute->getAttributeDefinition();
$attrvalues[] = array(
'id'=>(int) $attrdef->getId(),
'name'=>$attrdef->getName(),
'value'=>$attribute->getValue()
);
}
}
return $attrvalues;
} /* }}} */
protected function __getDocumentData($document) { /* {{{ */
$cats = $document->getCategories();
$tmp = [];
foreach($cats as $cat) {
$tmp[] = $this->__getCategoryData($cat);
}
$data = array(
'type'=>'document',
'id'=>(int)$document->getId(),
'date'=>date('Y-m-d H:i:s', $document->getDate()),
'name'=>$document->getName(),
'comment'=>$document->getComment(),
'keywords'=>$document->getKeywords(),
'categories'=>$tmp,
'owner'=>(int)$document->getOwner()->getId()
);
return $data;
} /* }}} */
protected function __getLatestVersionData($lc) { /* {{{ */
$document = $lc->getDocument();
$data = array(
'type'=>'document',
'id'=>(int)$document->getId(),
'date'=>date('Y-m-d H:i:s', $document->getDate()),
'name'=>$document->getName(),
'comment'=>$document->getComment(),
'keywords'=>$document->getKeywords(),
'ownerid'=>(int) $document->getOwner()->getID(),
'islocked'=>$document->isLocked(),
'sequence'=>$document->getSequence(),
'expires'=>$document->getExpires() ? date('Y-m-d H:i:s', $document->getExpires()) : "",
'mimetype'=>$lc->getMimeType(),
'filetype'=>$lc->getFileType(),
'origfilename'=>$lc->getOriginalFileName(),
'version'=>$lc->getVersion(),
'version_comment'=>$lc->getComment(),
'version_date'=>date('Y-m-d H:i:s', $lc->getDate()),
'size'=>(int) $lc->getFileSize(),
);
$cats = $document->getCategories();
if($cats) {
$c = array();
foreach($cats as $cat) {
$c[] = array('id'=>(int)$cat->getID(), 'name'=>$cat->getName());
}
$data['categories'] = $c;
}
$attributes = $this->__getAttributesData($document);
if($attributes) {
$data['attributes'] = $attributes;
}
$attributes = $this->__getAttributesData($lc);
if($attributes) {
$data['version_attributes'] = $attributes;
}
return $data;
} /* }}} */
protected function __getDocumentVersionData($lc) { /* {{{ */
$data = array(
'id'=>(int) $lc->getId(),
'version'=>$lc->getVersion(),
'date'=>date('Y-m-d H:i:s', $lc->getDate()),
'mimetype'=>$lc->getMimeType(),
'filetype'=>$lc->getFileType(),
'origfilename'=>$lc->getOriginalFileName(),
'size'=>(int) $lc->getFileSize(),
'comment'=>$lc->getComment(),
);
return $data;
} /* }}} */
protected function __getDocumentFileData($file) { /* {{{ */
$data = array(
'id'=>(int)$file->getId(),
'name'=>$file->getName(),
'date'=>$file->getDate(),
'mimetype'=>$file->getMimeType(),
'comment'=>$file->getComment(),
);
return $data;
} /* }}} */
protected function __getDocumentLinkData($link) { /* {{{ */
$data = array(
'id'=>(int)$link->getId(),
'target'=>$this->__getDocumentData($link->getTarget()),
'public'=>(boolean)$link->isPublic(),
);
return $data;
} /* }}} */
protected function __getFolderData($folder) { /* {{{ */
$data = array(
'type'=>'folder',
'id'=>(int)$folder->getID(),
'name'=>$folder->getName(),
'comment'=>$folder->getComment(),
'date'=>date('Y-m-d H:i:s', $folder->getDate()),
'owner'=>(int)$folder->getOwner()->getId()
);
$attributes = $this->__getAttributesData($folder);
if($attributes) {
$data['attributes'] = $attributes;
}
return $data;
} /* }}} */
protected function __getGroupData($u) { /* {{{ */
$data = array(
'type'=>'group',
@ -3144,7 +3022,7 @@ class RestapiAuthMiddleware implements MiddlewareInterface { /* {{{ */
/* We cannot handle Basic authentication, so skip it */
if (substr($environment['HTTP_AUTHORIZATION'], 0, 6) != 'Basic ') {
$logger->log("Authorization key: ".$environment['HTTP_AUTHORIZATION'], PEAR_LOG_DEBUG);
if($settings->_apiKey == $environment['HTTP_AUTHORIZATION']) {
if(($settings->_apiKey == $environment['HTTP_AUTHORIZATION']) || ('Bearer '.$settings->_apiKey == $environment['HTTP_AUTHORIZATION'])) {
if(!($userobj = $dms->getUser($settings->_apiUserId))) {
$response = $this->responsefactory->createResponse();
$response = $response->withHeader('Content-Type', 'application/json');