From 13510d3f91d4ed3b32d47abd65a48b56724c2651 Mon Sep 17 00:00:00 2001 From: Basil Suter Date: Sat, 11 Mar 2017 13:31:35 +0100 Subject: [PATCH] removed session serializer in order to add closures #1228, this fixes #1169 --- modules/admin/src/components/AdminMenu.php | 2 +- .../src/controllers/NgrestController.php | 80 ------------------- modules/admin/src/ngrest/NgRest.php | 15 ---- modules/admin/src/ngrest/base/Api.php | 59 ++++++++++---- modules/admin/src/ngrest/base/Controller.php | 50 +++++------- modules/admin/src/ngrest/base/NgRestModel.php | 27 ++++++- .../admin/src/ngrest/render/RenderCrud.php | 10 ++- modules/admin/src/resources/js/controllers.js | 2 +- .../admin/src/views/ngrest/render/crud.php | 1 + 9 files changed, 102 insertions(+), 144 deletions(-) delete mode 100644 modules/admin/src/controllers/NgrestController.php diff --git a/modules/admin/src/components/AdminMenu.php b/modules/admin/src/components/AdminMenu.php index 30d55801e..ba642489b 100644 --- a/modules/admin/src/components/AdminMenu.php +++ b/modules/admin/src/components/AdminMenu.php @@ -221,7 +221,7 @@ public function getItems() */ public function getApiDetail($api = null) { - $items = $this->items; + $items = $this->getItems(); $key = array_search($api, array_column($items, 'permssionApiEndpoint')); diff --git a/modules/admin/src/controllers/NgrestController.php b/modules/admin/src/controllers/NgrestController.php deleted file mode 100644 index 86252acfe..000000000 --- a/modules/admin/src/controllers/NgrestController.php +++ /dev/null @@ -1,80 +0,0 @@ - - */ -final class NgrestController extends Controller -{ - public $disablePermissionCheck = true; - - public $enableCsrfValidation = false; - - public function behaviors() - { - $behaviors = parent::behaviors(); - $behaviors[] = [ - 'class' => 'yii\filters\ContentNegotiator', - 'only' => ['callback'], - 'formats' => [ - 'application/json' => Response::FORMAT_JSON, - ], - ]; - return $behaviors; - } - - public function actionRender() - { - $config = NgRest::findConfig(Yii::$app->request->post('ngrestConfigHash', false)); - - $render = new RenderActiveWindow(); - - $render->setItemId(Yii::$app->request->post('itemId', false)); - $render->setActiveWindowHash(Yii::$app->request->post('activeWindowHash', false)); - - $ngrest = new NgRest($config); - - return $ngrest->render($render); - } - - public function actionCallback() - { - $config = NgRest::findConfig(Yii::$app->request->get('ngrestConfigHash', false)); - - $render = new RenderActiveWindowCallback(); - - $ngrest = new NgRest($config); - - return $ngrest->render($render); - } - - public function actionExportDownload($key) - { - $sessionkey = Yii::$app->session->get('tempNgRestKey'); - $fileName = Yii::$app->session->get('tempNgRestFileName'); - - if ($sessionkey !== base64_decode($key)) { - throw new Exception('Invalid Export download key.'); - } - - $content = FileHelper::getFileContent('@runtime/'.$sessionkey.'.tmp'); - - Yii::$app->session->remove('tempNgRestKey'); - Yii::$app->session->remove('tempNgRestFileName'); - @unlink(Yii::getAlias('@runtime/'.$sessionkey.'.tmp')); - - return Yii::$app->response->sendContentAsFile($content, $fileName . '-export-'.date("Y-m-d-H-i").'.csv', ['mimeType' => 'application/csv']); - } -} diff --git a/modules/admin/src/ngrest/NgRest.php b/modules/admin/src/ngrest/NgRest.php index d119ec20a..d9db6d37c 100644 --- a/modules/admin/src/ngrest/NgRest.php +++ b/modules/admin/src/ngrest/NgRest.php @@ -46,21 +46,6 @@ public function render(RenderInterface $render) $this->render->setConfig($this->config); return $this->render->render(); } - - public static function findConfig($ngRestConfigHash) - { - // decode the session, find the hash, if yes return the - $session = Yii::$app->session->get($ngRestConfigHash); - // valid session usnerialize and return - if ($session) { - return unserialize($session); - } - } - - public function __destruct() - { - yii::$app->session->set($this->config->hash, serialize($this->config)); - } public static function createPluginObject($className, $name, $alias, $i18n, $args = []) { diff --git a/modules/admin/src/ngrest/base/Api.php b/modules/admin/src/ngrest/base/Api.php index 5cf8bf8fe..5613435f2 100644 --- a/modules/admin/src/ngrest/base/Api.php +++ b/modules/admin/src/ngrest/base/Api.php @@ -15,6 +15,10 @@ use luya\admin\base\RestActiveController; use luya\admin\components\AdminUser; use luya\admin\models\UserOnline; +use luya\admin\ngrest\render\RenderActiveWindow; +use luya\admin\ngrest\render\RenderActiveWindowCallback; +use luya\admin\ngrest\NgRest; +use luya\cms\admin\helpers\MenuHelper; /** * The RestActiveController for all NgRest implementations. @@ -23,20 +27,6 @@ */ class Api extends RestActiveController { - /** - * @inheritdoc - */ - public function actions() - { - $actions = parent::actions(); - $actions['view']['class'] = 'luya\admin\ngrest\base\actions\ViewAction'; - $actions['index']['class'] = 'luya\admin\ngrest\base\actions\IndexAction'; - $actions['create']['class'] = 'luya\admin\ngrest\base\actions\CreateAction'; - $actions['update']['class'] = 'luya\admin\ngrest\base\actions\UpdateAction'; - $actions['delete']['class'] = 'luya\admin\ngrest\base\actions\DeleteAction'; - return $actions; - } - /** * @var string Defines the related model for the NgRest Controller. The full qualiefied model name * is required. @@ -76,6 +66,20 @@ public function init() } } + /** + * @inheritdoc + */ + public function actions() + { + $actions = parent::actions(); + $actions['view']['class'] = 'luya\admin\ngrest\base\actions\ViewAction'; + $actions['index']['class'] = 'luya\admin\ngrest\base\actions\IndexAction'; + $actions['create']['class'] = 'luya\admin\ngrest\base\actions\CreateAction'; + $actions['update']['class'] = 'luya\admin\ngrest\base\actions\UpdateAction'; + $actions['delete']['class'] = 'luya\admin\ngrest\base\actions\DeleteAction'; + return $actions; + } + private $_model = null; /** @@ -195,6 +199,26 @@ public function actionFilter($filterName) ]); } + public function actionActiveWindowCallback() + { + $config = $this->model->getNgRestConfig(); + $render = new RenderActiveWindowCallback(); + $ngrest = new NgRest($config); + + return $ngrest->render($render); + } + + public function actionActiveWindowRender() + { + $config = $this->model->getNgRestConfig(); + $render = new RenderActiveWindow(); + $render->setItemId(Yii::$app->request->post('itemId', false)); + $render->setActiveWindowHash(Yii::$app->request->post('activeWindowHash', false)); + $ngrest = new NgRest($config); + + return $ngrest->render($render); + } + /** * Prepare a temp file to * @todo added very basic csv support, must be stored as class, just a temp solution @@ -262,11 +286,16 @@ public function actionExport() $store = FileHelper::writeFile('@runtime/'.$key.'.tmp', $tempData); + $menu = Yii::$app->adminmenu->getApiDetail($this->model->ngRestApiEndpoint()); + + $route = $menu['route']; + $route = str_replace("/index", "/export-download", $route); + if ($store) { Yii::$app->session->set('tempNgRestFileName', Inflector::slug($this->model->tableName())); Yii::$app->session->set('tempNgRestKey', $key); return [ - 'url' => Url::toRoute(['/admin/ngrest/export-download', 'key' => base64_encode($key)]), + 'url' => Url::toRoute(['/'.$route, 'key' => base64_encode($key)]), ]; } diff --git a/modules/admin/src/ngrest/base/Controller.php b/modules/admin/src/ngrest/base/Controller.php index 580d8bfae..22f9b7909 100644 --- a/modules/admin/src/ngrest/base/Controller.php +++ b/modules/admin/src/ngrest/base/Controller.php @@ -7,6 +7,7 @@ use yii\base\InvalidConfigException; use luya\admin\ngrest\NgRest; use luya\admin\ngrest\render\RenderCrud; +use luya\helpers\FileHelper; /** * Base Controller for all NgRest Controllers. @@ -60,18 +61,6 @@ public function actionIndex($inline = false, $relation = false, $arrayIndex = fa $apiEndpoint = $this->model->ngRestApiEndpoint(); $config = $this->model->getNgRestConfig(); - - /* - // partial concept integration for - $configClass = $this->module->getLinkedNgRestConfig($apiEndpoint); - - if ($configClass) { - // $class = Yii::createObject($configClass, ['apiEndpoint' => '', 'primaryKey' => '..']); - $config = false; - } else { - $config = $this->model->getNgRestConfig(); - } - */ if (!$config) { throw new Exception("Provided NgRest config for controller '' is invalid."); @@ -81,34 +70,39 @@ public function actionIndex($inline = false, $relation = false, $arrayIndex = fa $config->relationCall = ['id' => $relation, 'arrayIndex' => $arrayIndex, 'modelClass' => $modelClass]; } - // apply config informations - $config->filters = $this->model->ngRestFilters(); - $config->defaultOrder = $this->model->ngRestListOrder(); - $userSortSettings = Yii::$app->adminuser->identity->setting->get('ngrestorder.admin/'.$apiEndpoint, false); if ($userSortSettings && is_array($userSortSettings)) { - $config->defaultOrder = [$userSortSettings['field'] => $userSortSettings['sort']]; + $config->defaultOrder = [$userSortSettings['field'] => $userSortSettings['sort']]; } - $config->attributeGroups = $this->model->ngRestAttributeGroups(); - $config->groupByField = $this->model->ngRestGroupByField(); - - $rel = []; - foreach ($this->model->ngRestRelations() as $key => $item) { - $rel[] = ['label' => $item['label'], 'apiEndpoint' => $item['apiEndpoint'], 'arrayIndex' => $key, 'modelClass' => base64_encode($this->model->className())]; - } - $config->inline = (int) $inline; - $config->relations = $rel; - $config->tableName = $this->model->tableName(); $ngrest = new NgRest($config); - $crud = new RenderCrud(); if ($relation) { $crud->viewFile = '@admin/views/ngrest/render/crud_relation.php'; } return $ngrest->render($crud); } + + + + public function actionExportDownload($key) + { + $sessionkey = Yii::$app->session->get('tempNgRestKey'); + $fileName = Yii::$app->session->get('tempNgRestFileName'); + + if ($sessionkey !== base64_decode($key)) { + throw new Exception('Invalid Export download key.'); + } + + $content = FileHelper::getFileContent('@runtime/'.$sessionkey.'.tmp'); + + Yii::$app->session->remove('tempNgRestKey'); + Yii::$app->session->remove('tempNgRestFileName'); + @unlink(Yii::getAlias('@runtime/'.$sessionkey.'.tmp')); + + return Yii::$app->response->sendContentAsFile($content, $fileName . '-export-'.date("Y-m-d-H-i").'.csv', ['mimeType' => 'application/csv']); + } } diff --git a/modules/admin/src/ngrest/base/NgRestModel.php b/modules/admin/src/ngrest/base/NgRestModel.php index a5b988106..06a895d31 100644 --- a/modules/admin/src/ngrest/base/NgRestModel.php +++ b/modules/admin/src/ngrest/base/NgRestModel.php @@ -493,7 +493,12 @@ public function ngRestConfigDefine(ConfigBuilder $config, $assignedType, array $ public function getNgRestConfig() { if ($this->_config == null) { - $config = Yii::createObject(['class' => Config::class, 'apiEndpoint' => static::ngRestApiEndpoint(), 'primaryKey' => $this->getNgRestPrimaryKey()]); + //$config = Yii::createObject(['class' => Config::class, 'apiEndpoint' => static::ngRestApiEndpoint(), 'primaryKey' => $this->getNgRestPrimaryKey()]); + + $config = new Config(); + $config->apiEndpoint = static::ngRestApiEndpoint(); + $config->primaryKey = $this->getNgRestPrimaryKey(); + $configBuilder = new ConfigBuilder(static::class); $this->ngRestConfig($configBuilder); $config->setConfig($configBuilder->getConfig()); @@ -501,6 +506,26 @@ public function getNgRestConfig() $config->appendFieldOption($fieldName, 'i18n', true); } + + + // COPY FROM NgRestController to Builder of the config + // ensure what must be removed and added from outside + $config->filters = $this->ngRestFilters(); + $config->defaultOrder = $this->ngRestListOrder(); + + + $config->attributeGroups = $this->ngRestAttributeGroups(); + $config->groupByField = $this->ngRestGroupByField(); + + $rel = []; + foreach ($this->ngRestRelations() as $key => $item) { + $rel[] = ['label' => $item['label'], 'apiEndpoint' => $item['apiEndpoint'], 'arrayIndex' => $key, 'modelClass' => base64_encode($this->model->className())]; + } + + + $config->relations = $rel; + $config->tableName = $this->tableName(); + $this->_config = $config; } diff --git a/modules/admin/src/ngrest/render/RenderCrud.php b/modules/admin/src/ngrest/render/RenderCrud.php index 89e15cfbc..d62de3d9f 100644 --- a/modules/admin/src/ngrest/render/RenderCrud.php +++ b/modules/admin/src/ngrest/render/RenderCrud.php @@ -59,13 +59,17 @@ public function render() 'canDelete' => $this->can(Auth::CAN_DELETE), //'crud' => $this, 'config' => $this->config, - 'activeWindowCallbackUrl' => 'admin/ngrest/callback', + 'activeWindowRenderUrl' => $this->getRestUrl('active-window-render'), + 'activeWindowCallbackUrl' => $this->getRestUrl('active-window-callback'), ), $this); } - public function getRestUrl() + public function getRestUrl($append = null) { - return 'admin/'.$this->config->apiEndpoint; + if ($append) { + $append = '/' . ltrim($append, '/'); + } + return 'admin/'.$this->config->apiEndpoint . $append; } public function getPrimaryKey() diff --git a/modules/admin/src/resources/js/controllers.js b/modules/admin/src/resources/js/controllers.js index e11aa0928..69ba4ea7c 100644 --- a/modules/admin/src/resources/js/controllers.js +++ b/modules/admin/src/resources/js/controllers.js @@ -267,7 +267,7 @@ } $scope.getActiveWindow = function (activeWindowId, id, $event) { - $http.post('admin/ngrest/render', $.param({ itemId : id, activeWindowHash : activeWindowId , ngrestConfigHash : $scope.config.ngrestConfigHash }), { + $http.post($scope.config.activeWindowRenderUrl, $.param({ itemId : id, activeWindowHash : activeWindowId , ngrestConfigHash : $scope.config.ngrestConfigHash }), { headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'} }) .success(function(data) { diff --git a/modules/admin/src/views/ngrest/render/crud.php b/modules/admin/src/views/ngrest/render/crud.php index 2cf0b0296..a013254a4 100644 --- a/modules/admin/src/views/ngrest/render/crud.php +++ b/modules/admin/src/views/ngrest/render/crud.php @@ -18,6 +18,7 @@ $scope.config.update = context->getFieldsJson('update'); ?>; $scope.config.ngrestConfigHash = 'hash; ?>'; $scope.config.activeWindowCallbackUrl = ''; + $scope.config.activeWindowRenderUrl = ''; $scope.config.pk = 'context->getPrimaryKey(); ?>'; $scope.config.inline = inline; ?>; $scope.config.orderBy = 'getDefaultOrderDirection() . $config->getDefaultOrderField(); ?>';