From 5a5a22e0d5a9a74b9c81796dfba014e3fd66be09 Mon Sep 17 00:00:00 2001 From: paolo Date: Tue, 21 Jun 2016 11:10:55 +0200 Subject: [PATCH] - Removed body tag from footer.php and header.php - Added permission fs/dms to dump.sql and fhcomplete.php - Added FilesystemLib to read and write from/in filesystem - Added FS_Model to manage filesystem with the same permission system of DB_Model - Added more models to handle the Dms - Modified Dms controller to use the new models --- application/config/fhcomplete.php | 2 + .../controllers/api/v1/content/Dms.php | 205 +++++------------- application/core/FS_Model.php | 170 +++++++++++++++ application/libraries/FilesystemLib.php | 148 +++++++++++++ application/models/content/DmsFS_model.php | 13 ++ .../models/content/DmsVersion_model.php | 53 +++++ application/models/content/Dms_model.php | 45 ++-- application/views/templates/footer.php | 3 +- application/views/templates/header.php | 4 +- tests/codeception/_data/dump.sql | 2 + 10 files changed, 463 insertions(+), 182 deletions(-) create mode 100644 application/core/FS_Model.php create mode 100644 application/libraries/FilesystemLib.php create mode 100644 application/models/content/DmsFS_model.php create mode 100644 application/models/content/DmsVersion_model.php diff --git a/application/config/fhcomplete.php b/application/config/fhcomplete.php index a5d5f2c2f..380a629e9 100755 --- a/application/config/fhcomplete.php +++ b/application/config/fhcomplete.php @@ -231,6 +231,8 @@ $config['fhc_acl'] = array 'wawi.tbl_rechnungsbetrag' => 'basis/rechnungsbetrag', 'wawi.tbl_rechnungstyp' => 'basis/rechnungstyp', 'wawi.tbl_zahlungstyp' => 'basis/zahlungstyp', + + DMS_PATH => 'fs/dms', 'public.tbl_sprache' => 'admin' ); diff --git a/application/controllers/api/v1/content/Dms.php b/application/controllers/api/v1/content/Dms.php index 6d37bc5db..5800832f1 100644 --- a/application/controllers/api/v1/content/Dms.php +++ b/application/controllers/api/v1/content/Dms.php @@ -23,9 +23,13 @@ class Dms extends APIv1_Controller { parent::__construct(); // Load model PersonModel - $this->load->model('content/dms_model', 'DmsModel'); - // Load set the uid of the model to let to check the permissions + $this->load->model('content/Dms_model', 'DmsModel'); + $this->load->model('content/DmsVersion_model', 'DmsVersionModel'); + $this->load->model('content/DmsFS_model', 'DmsFSModel'); + // Set the uid of the model to let to check the permissions $this->DmsModel->setUID($this->_getUID()); + $this->DmsVersionModel->setUID($this->_getUID()); + $this->DmsFSModel->setUID($this->_getUID()); } /** @@ -41,9 +45,52 @@ class Dms extends APIv1_Controller $result = $this->_getDms($dms_id, $version); if (is_object($result) && $result->error == EXIT_SUCCESS && is_array($result->retval) && count($result->retval) > 0) { - if (($fileContent = $this->_readFile($result->retval[0]->filename)) != false) + $resultFS = $this->DmsFSModel->read($result->retval[0]->filename); + if (is_object($resultFS) && $resultFS->error == EXIT_SUCCESS) { - $result->retval[0]->file_content = $fileContent; + $result->retval[0]->file_content = $resultFS->retval; + } + } + + $this->response($result, REST_Controller::HTTP_OK); + } + else + { + $this->response(); + } + } + + /** + * + */ + public function postDms() + { + $dms = $this->_parseData($this->post()); + + if ($this->_validate($dms)) + { + $result = null; + + if (isset($dms['dms_id'])) + { + if ($this->_saveFileOnUpdate($dms)) + { + $result = $this->DmsModel->update($dms['dms_id'], $this->DmsModel->filterFields($dms)); + if ($result->error == EXIT_SUCCESS) + { + $result = $this->DmsVersionModel->update(array($dms['dms_id'], $dms['version']), $this->DmsVersionModel->filterFields($dms)); + } + } + } + else + { + if (($filename = $this->_saveFileOnInsert($dms)) !== false) + { + $result = $this->DmsModel->insert($this->DmsModel->filterFields($dms)); + if ($result->error == EXIT_SUCCESS) + { + $result = $this->DmsVersionModel->insert($this->DmsVersionModel->filterFields($dms, $result->retval, $filename)); + } } } @@ -89,102 +136,6 @@ class Dms extends APIv1_Controller return $result; } - /** - * - */ - public function postDms() - { - $dms = $this->_parseData($this->post()); - if ($this->_validate($dms)) - { - if (isset($dms['dms_id'])) - { - if ($this->_saveFileOnUpdate($dms)) - { - $result = $this->DmsModel->update($dms['dms_id'], $this->_dmsFieldsArray($dms)); - if ($result->error == EXIT_SUCCESS) - { - $result = $this->DmsModel->updateDmsVersion($dms['dms_id'], $this->_dmsVersionFieldsArray($dms)); - } - } - } - else - { - if (($fileName = $this->_saveFileOnInsert($dms)) !== false) - { - $result = $this->DmsModel->insert($this->_dmsFieldsArray($dms)); - if ($result->error == EXIT_SUCCESS) - { - $result = $this->DmsModel->insertDmsVersion($this->_dmsVersionFieldsArray($dms, $result->retval, $fileName)); - } - } - } - - $this->response($result, REST_Controller::HTTP_OK); - } - else - { - $this->response(); - } - } - - /** - * - */ - private function _dmsFieldsArray($dms) - { - $fieldsArray = array('oe_kurzbz', 'dokument_kurzbz', 'kategorie_kurzbz'); - $returnArray = array(); - - foreach ($fieldsArray as $value) - { - if (isset($dms[$value])) - { - $returnArray[$value] = $dms[$value]; - } - } - - return $returnArray; - } - - /** - * - */ - private function _dmsVersionFieldsArray($dms, $dms_id = null, $fileName = null) - { - $fieldsArray = array( - 'version', - 'mimetype', - 'name', - 'beschreibung', - 'letzterzugriff', - 'insertamum', - 'insertvon', - 'updateamum', - 'updatevon' - ); - $returnArray = array(); - - foreach ($fieldsArray as $value) - { - if (isset($dms[$value])) - { - $returnArray[$value] = $dms[$value]; - } - } - - if (isset($dms_id)) - { - $returnArray['dms_id'] = $dms_id; - } - if (isset($fileName)) - { - $returnArray['filename'] = $fileName; - } - - return $returnArray; - } - /** * */ @@ -193,19 +144,11 @@ class Dms extends APIv1_Controller if(isset($dms['version'])) { $result = $this->_getDms($dms['dms_id'], $dms['version']); - } - else - { - $result = $this->_getDms($dms['dms_id']); - } - if (is_object($result) && $result->error == EXIT_SUCCESS && is_array($result->retval) && count($result->retval) > 0) - { - $fileName = DMS_PATH . $result->retval[0]->filename; - - if (($fileContent = base64_decode($dms['file_content']))) + if (is_object($result) && $result->error == EXIT_SUCCESS && is_array($result->retval) && count($result->retval) > 0) { - if (file_put_contents($fileName, $fileContent)) + $result = $this->DmsFSModel->write($result->retval[0]->filename, $dms['file_content']); + if (is_object($result) && $result->error == EXIT_SUCCESS) { return true; } @@ -220,47 +163,17 @@ class Dms extends APIv1_Controller */ private function _saveFileOnInsert($dms) { - $fileName = uniqid() . '.' . pathinfo($dms['name'], PATHINFO_EXTENSION); - $FileNamePath = DMS_PATH . $fileName; + $filename = uniqid() . '.' . pathinfo($dms['name'], PATHINFO_EXTENSION); - if (($fileContent = base64_decode($dms['file_content']))) + $result = $this->DmsFSModel->write($filename, $dms['file_content']); + if (is_object($result) && $result->error == EXIT_SUCCESS) { - if ($fileHandle = fopen($FileNamePath, 'w')) - { - if(fwrite($fileHandle, $fileContent)) - { - fclose($fileHandle); - return $fileName; - } - } + return $filename; } return false; } - /** - * - */ - private function _readFile($fileName) - { - $fileNamePath = DMS_PATH . $fileName; - if (file_exists($fileNamePath)) - { - if ($fileHandle = fopen($fileNamePath, 'r')) - { - $cTmpHEX = ''; - while (!feof($fileHandle)) - { - $cTmpHEX .= fread($fileHandle, 8192); - } - fclose($fileHandle); - return base64_encode($cTmpHEX); - } - } - - return false; - } - private function _validate($dms = NULL) { if (!isset($dms['file_content']) || (isset($dms['file_content']) && $dms['file_content'] == '')) @@ -274,4 +187,4 @@ class Dms extends APIv1_Controller return true; } -} +} \ No newline at end of file diff --git a/application/core/FS_Model.php b/application/core/FS_Model.php new file mode 100644 index 000000000..1bcaaa251 --- /dev/null +++ b/application/core/FS_Model.php @@ -0,0 +1,170 @@ +load->library('FilesystemLib'); + $this->acl = $this->config->item('fhc_acl'); + $this->filepath = $filepath; + } + + /** --------------------------------------------------------------- + * Read data from file system + * + * @return array + */ + public function read($filename) + { + // Check Class-Attributes + if (is_null($this->filepath)) + return $this->_error(lang('fhc_'.FHC_ERROR), FHC_MODEL_ERROR); + + // Check method parameters + if (is_null($filename)) + return $this->_error(lang('fhc_'.FHC_ERROR), FHC_MODEL_ERROR); + + // Check rights + if (! $this->fhc_db_acl->isBerechtigt($this->acl[$this->filepath], 's')) + return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->acl[$this->filepath], FHC_MODEL_ERROR); + + if (!is_null($data = $this->filesystemlib->read($this->filepath, $filename))) + { + return $this->_success(base64_encode($data)); + } + else + { + return $this->_error(lang('fhc_'.FHC_ERROR), FHC_MODEL_ERROR); + } + } + + /** --------------------------------------------------------------- + * Writing data to file system + * + * @param string $fileContent File content + * @return object + */ + public function write($filename, $content) + { + // Check Class-Attributes + if (is_null($this->filepath)) + return $this->_error(lang('fhc_'.FHC_ERROR), FHC_MODEL_ERROR); + + // Check method parameters + if (is_null($filename)) + return $this->_error(lang('fhc_'.FHC_ERROR), FHC_MODEL_ERROR); + if (is_null($content)) + return $this->_error(lang('fhc_'.FHC_ERROR), FHC_MODEL_ERROR); + + // Check rights + if (! $this->fhc_db_acl->isBerechtigt($this->acl[$this->filepath], 'i')) + return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->acl[$this->filepath], FHC_MODEL_ERROR); + + if ($this->filesystemlib->write($this->filepath, $filename, base64_decode($content)) === true) + { + return $this->_success(FHC_SUCCESS); + } + else + { + return $this->_error(lang('fhc_'.FHC_ERROR), FHC_MODEL_ERROR); + } + } + + /** --------------------------------------------------------------- + * Append data to a file + * + * @param array $data File content + * @return array + */ + public function append($filename, $content) + { + // Check Class-Attributes + if (is_null($this->filepath)) + return $this->_error(lang('fhc_'.FHC_ERROR), FHC_MODEL_ERROR); + + // Check method parameters + if (is_null($filename)) + return $this->_error(lang('fhc_'.FHC_ERROR), FHC_MODEL_ERROR); + if (is_null($content)) + return $this->_error(lang('fhc_'.FHC_ERROR), FHC_MODEL_ERROR); + + // Check rights + if (! $this->fhc_db_acl->isBerechtigt($this->acl[$this->filepath], 'i')) + return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->acl[$this->filepath], FHC_MODEL_ERROR); + + if ($this->filesystemlib->append($this->filepath, $filename, base64_decode($content)) === true) + { + return $this->_success(FHC_SUCCESS); + } + else + { + return $this->_error(lang('fhc_'.FHC_ERROR), FHC_MODEL_ERROR); + } + } + + /** --------------------------------------------------------------- + * Delete data from file system + * + * @param string $id Primary Key for DELETE + * @return array + */ + public function remove($filename) + { + // Check Class-Attributes + if (is_null($this->filepath)) + return $this->_error(lang('fhc_'.FHC_ERROR), FHC_MODEL_ERROR); + + // Check method parameters + if (is_null($filename)) + return $this->_error(lang('fhc_'.FHC_ERROR), FHC_MODEL_ERROR); + + // Check rights + if (! $this->fhc_db_acl->isBerechtigt($this->acl[$this->filepath], 'd')) + return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->acl[$this->filepath], FHC_MODEL_ERROR); + + if ($this->filesystemlib->remove($this->filepath, $filename) === true) + { + return $this->_success(FHC_SUCCESS); + } + else + { + return $this->_error(lang('fhc_'.FHC_ERROR), FHC_MODEL_ERROR); + } + } + + /** --------------------------------------------------------------- + * Rename a file + * + * @param string $id Primary Key for DELETE + * @return array + */ + public function rename($filename, $newFilename) + { + // Check Class-Attributes + if (is_null($this->filepath)) + return $this->_error(lang('fhc_'.FHC_ERROR), FHC_MODEL_ERROR); + + // Check method parameters + if (is_null($filename)) + return $this->_error(lang('fhc_'.FHC_ERROR), FHC_MODEL_ERROR); + if (is_null($newFilename)) + return $this->_error(lang('fhc_'.FHC_ERROR), FHC_MODEL_ERROR); + + // Check rights + if (! $this->fhc_db_acl->isBerechtigt($this->acl[$this->filepath], 'u')) + return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->acl[$this->filepath], FHC_MODEL_ERROR); + + if ($this->filesystemlib->rename($this->filepath, $filename, $this->filepath, $newFilename) === true) + { + return $this->_success(FHC_SUCCESS); + } + else + { + return $this->_error(lang('fhc_'.FHC_ERROR), FHC_MODEL_ERROR); + } + } +} \ No newline at end of file diff --git a/application/libraries/FilesystemLib.php b/application/libraries/FilesystemLib.php new file mode 100644 index 000000000..0b1dbc5d0 --- /dev/null +++ b/application/libraries/FilesystemLib.php @@ -0,0 +1,148 @@ +checkParameters($filepath, $filename)) + { + $resource = $filepath . DIRECTORY_SEPARATOR . $filename; + if (file_exists($resource) && $fileHandle = fopen($resource, 'r')) + { + $result = ''; + while (!feof($fileHandle)) + { + $result .= fread($fileHandle, 8192); + } + fclose($fileHandle); + } + } + + return $result; + } + + /* + * + */ + public function write($filepath, $filename, $content) + { + $result = null; + + if ($this->checkParameters($filepath, $filename) && isset($content)) + { + $resource = $filepath . DIRECTORY_SEPARATOR . $filename; + if (is_writable($filepath) && $fileHandle = fopen($resource, 'w')) + { + if (fwrite($fileHandle, $content) !== false) + { + $result = true; + } + fclose($fileHandle); + } + } + + return $result; + } + + /* + * + */ + public function append($filepath, $filename, $content) + { + $result = null; + + if ($this->checkParameters($filepath, $filename) && isset($content)) + { + $resource = $filepath . DIRECTORY_SEPARATOR . $filename; + if (is_writable($resource) && $fileHandle = fopen($resource, 'a')) + { + if (fwrite($fileHandle, $content) !== false) + { + $result = true; + } + fclose($fileHandle); + } + } + + return $result; + } + + /* + * + */ + public function remove($filepath, $filename) + { + $result = null; + + if ($this->checkParameters($filepath, $filename)) + { + if (is_writable($filepath)) + { + $resource = $filepath . DIRECTORY_SEPARATOR . $filename; + $result = unlink($resource); + } + } + + return $result; + } + + /* + * + */ + public function rename($filepath, $filename, $newFilepath, $newFilename) + { + $result = null; + + if ($this->checkParameters($filepath, $filename) && $this->checkParameters($newFilepath, $newFilename)) + { + $resource = $filepath . DIRECTORY_SEPARATOR . $filename; + if (is_writable($filepath) && is_writable($newFilepath) && file_exists($resource)) + { + $destination = $newFilepath . DIRECTORY_SEPARATOR . $newFilename; + $result = rename($resource, $destination); + } + } + + return $result; + } +} \ No newline at end of file diff --git a/application/models/content/DmsFS_model.php b/application/models/content/DmsFS_model.php new file mode 100644 index 000000000..38a72d853 --- /dev/null +++ b/application/models/content/DmsFS_model.php @@ -0,0 +1,13 @@ +filepath = DMS_PATH; + } +} \ No newline at end of file diff --git a/application/models/content/DmsVersion_model.php b/application/models/content/DmsVersion_model.php new file mode 100644 index 000000000..5cc3b556e --- /dev/null +++ b/application/models/content/DmsVersion_model.php @@ -0,0 +1,53 @@ +dbTable = 'campus.tbl_dms_version'; + $this->pk = array('dms_id', 'version'); + $this->hasSequence = false; + } + + /** + * + */ + public function filterFields($dms, $dms_id = null, $fileName = null) + { + $fieldsArray = array( + 'version', + 'mimetype', + 'name', + 'beschreibung', + 'letzterzugriff', + 'insertamum', + 'insertvon', + 'updateamum', + 'updatevon' + ); + $returnArray = array(); + + foreach ($fieldsArray as $value) + { + if (isset($dms[$value])) + { + $returnArray[$value] = $dms[$value]; + } + } + + if (isset($dms_id)) + { + $returnArray['dms_id'] = $dms_id; + } + if (isset($fileName)) + { + $returnArray['filename'] = $fileName; + } + + return $returnArray; + } +} \ No newline at end of file diff --git a/application/models/content/Dms_model.php b/application/models/content/Dms_model.php index 55af1371e..e9db6d903 100644 --- a/application/models/content/Dms_model.php +++ b/application/models/content/Dms_model.php @@ -12,39 +12,22 @@ class Dms_model extends DB_Model $this->pk = 'dms_id'; } - public function insertDmsVersion($data) + /** + * + */ + public function filterFields($dms) { - $tableName = 'campus.tbl_dms_version'; + $fieldsArray = array('oe_kurzbz', 'dokument_kurzbz', 'kategorie_kurzbz'); + $returnArray = array(); - // Check rights - if (! $this->fhc_db_acl->isBerechtigt($this->acl[$tableName], 'i')) - return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->acl[$tableName], FHC_MODEL_ERROR); - - // DB-INSERT - if ($this->db->insert($tableName, $data)) - return $this->_success($this->db->insert_id()); - else - return $this->_error($this->db->error(), FHC_DB_ERROR); - } - - public function updateDmsVersion($id, $data) - { - $tableName = 'campus.tbl_dms_version'; + foreach ($fieldsArray as $value) + { + if (isset($dms[$value])) + { + $returnArray[$value] = $dms[$value]; + } + } - // Check Class-Attributes - if (is_null($this->pk)) - return $this->_error(lang('fhc_'.FHC_NOPK), FHC_MODEL_ERROR); - - // Check rights - if (! $this->fhc_db_acl->isBerechtigt($this->acl[$tableName], 'u')) - return $this->_error(lang('fhc_'.FHC_NORIGHT).' -> '.$this->acl[$tableName], FHC_MODEL_ERROR); - - // DB-UPDATE - $this->db->where('dms_id', $id); - - if ($this->db->update($tableName, $data)) - return $this->_success($id); - else - return $this->_error($this->db->error(), FHC_DB_ERROR); + return $returnArray; } } \ No newline at end of file diff --git a/application/views/templates/footer.php b/application/views/templates/footer.php index 308b1d01b..62d09b822 100644 --- a/application/views/templates/footer.php +++ b/application/views/templates/footer.php @@ -1,2 +1 @@ - - + \ No newline at end of file diff --git a/application/views/templates/header.php b/application/views/templates/header.php index 4c4909ece..6fb67c6ef 100644 --- a/application/views/templates/header.php +++ b/application/views/templates/header.php @@ -57,6 +57,4 @@ if ($tablesort) - - - + \ No newline at end of file diff --git a/tests/codeception/_data/dump.sql b/tests/codeception/_data/dump.sql index 552d65f75..3090a1504 100644 --- a/tests/codeception/_data/dump.sql +++ b/tests/codeception/_data/dump.sql @@ -869,6 +869,7 @@ INSERT INTO system.tbl_berechtigung (berechtigung_kurzbz, beschreibung) VALUES(' INSERT INTO system.tbl_berechtigung (berechtigung_kurzbz, beschreibung) VALUES('lehre/reservierung', ''); INSERT INTO system.tbl_berechtigung (berechtigung_kurzbz, beschreibung) VALUES('lehre/reihungstest', ''); INSERT INTO system.tbl_berechtigung (berechtigung_kurzbz, beschreibung) VALUES('wawi/inventar:begrenzt', ''); +INSERT INTO system.tbl_berechtigung (berechtigung_kurzbz, beschreibung) VALUES('fs/dms', ''); -- INSERT link between user admin and permissions INSERT INTO system.tbl_rolleberechtigung (berechtigung_kurzbz, rolle_kurzbz, art) VALUES('basis/archiv', 'admin', 'suid'); @@ -1092,6 +1093,7 @@ INSERT INTO system.tbl_rolleberechtigung (berechtigung_kurzbz, rolle_kurzbz, art INSERT INTO system.tbl_rolleberechtigung (berechtigung_kurzbz, rolle_kurzbz, art) VALUES('lehre/reservierung', 'admin', 'suid'); INSERT INTO system.tbl_rolleberechtigung (berechtigung_kurzbz, rolle_kurzbz, art) VALUES('lehre/reihungstest', 'admin', 'suid'); INSERT INTO system.tbl_rolleberechtigung (berechtigung_kurzbz, rolle_kurzbz, art) VALUES('wawi/inventar:begrenzt', 'admin', 'suid'); +INSERT INTO system.tbl_rolleberechtigung (berechtigung_kurzbz, rolle_kurzbz, art) VALUES('fs/dms', 'admin', 'suid'); -- EMPTY public.tbl_statistik DELETE FROM public.tbl_statistik;