diff --git a/Yii.php b/Yii.php new file mode 100644 index 0000000..9540874 --- /dev/null +++ b/Yii.php @@ -0,0 +1,47 @@ +authService = $service; + $this->_auth_service = $service; } /** @@ -25,27 +26,27 @@ class AuthController extends Controller public function behaviors() { return [ - 'access' => [ - 'class' => AccessControl::class, - 'rules' => [ - [ - 'actions' => ['login'], - 'allow' => true, - 'roles' => ['?'], - ], - [ - 'actions' => ['logout'], - 'allow' => true, - 'roles' => ['@'], - ], - [ // all the action are accessible to admin - 'allow' => true, - 'roles' => ['admin'], - ], - ], - ], - 'verbs' => [ - 'class' => VerbFilter::class, + 'access' => [ + 'class' => AccessControl::class, + 'rules' => [ + [ + 'actions' => ['login'], + 'allow' => true, + 'roles' => ['?'], + ], + [ + 'actions' => ['logout'], + 'allow' => true, + 'roles' => ['@'], + ], + [ // all the action are accessible to admin + 'allow' => true, + 'roles' => ['admin'], + ], + ], + ], + 'verbs' => [ + 'class' => VerbFilter::class, 'actions' => [ 'logout' => ['post'], ], @@ -67,8 +68,9 @@ class AuthController extends Controller $form = new LoginForm(); if ($form->load(Yii::$app->request->post()) && $form->validate()) { try { - $user = $this->authService->auth($form); + $user = $this->_auth_service->auth($form); Yii::$app->user->login(new Identity($user), $form->rememberMe ? 3600 * 24 * 30 : 0); + return $this->goBack(); } catch (\DomainException $e) { Yii::$app->errorHandler->logException($e); diff --git a/backend/controllers/MenuController.php b/backend/controllers/MenuController.php index 66a4d2a..910d399 100644 --- a/backend/controllers/MenuController.php +++ b/backend/controllers/MenuController.php @@ -23,257 +23,233 @@ use Yii; class MenuController extends Controller { - public $menu_service; - public $menu_item_service; - - public function __construct( string $id, $module, MenuManageService $menu_service, MenuItemManageService $menu_item_service, array $config = [] ) { - parent::__construct( $id, $module, $config ); - - $this->menu_service = $menu_service; - $this->menu_item_service = $menu_item_service; - } - - public function behaviors(): array - { - return [ - 'access' => [ - 'class' => AccessControl::class, - 'rules' => [ - [ - 'allow' => true, - 'roles' => ['MenuManagement'], - ], - [ // all the action are accessible to admin - 'allow' => true, - 'roles' => ['admin'], - ], - ], - ], - 'verbs' => [ - 'class' => VerbFilter::class, - 'actions' => [ - 'delete' => ['POST'], - 'delete-menu-item' => ['POST'], - ], - ], - ]; - } - - public function actionIndex($id = null) - { - $menus = []; // menu list - $menu_records = Menu::find()->all(); - foreach ($menu_records as $menu_record) { - $menus[$menu_record->id] = isset($menu_record->translation) && $menu_record->translation->name ? $menu_record->translation->name : $menu_record->findTranslation(Yii::$app->params['defaultLanguage'])->name; - } - - $form = new MenuSelectForm(); - if ($form->load(Yii::$app->request->get()) && $form->validate()) { - return $this->redirect(['menu/index', 'id' => $form->id]); - } - elseif ($id) { - $this->createMenuItem(); // create menu item if MenuItemForm sent - - $menu = $this->findModel($id); - - $creatorWidgets = $this->getCreatorWidgets($menu->id); - - return $this->render('menu', [ - 'model' => $form, - 'menus' => $menus, - 'menu' => $menu, - 'creator' => $creatorWidgets, - ]); - } - else { - return $this->render('select_menu', [ - 'model' => $form, - 'menus' => $menus, - ]); - } - } - - public function actionCreate() - { - $form = new MenuForm(); - if ($form->load(Yii::$app->request->post()) && $form->validate()) { - try { - $menu = $this->menu_service->create($form); - return $this->redirect(['index', 'id' => $menu->id]); - } catch (\DomainException $e) { - Yii::$app->errorHandler->logException($e); - Yii::$app->session->setFlash('error', $e->getMessage()); - } - } - return $this->render('create', [ - 'model' => $form, - ]); - } - - public function actionUpdate($id) - { - $menu = $this->findModel($id); - - $form = new MenuForm($menu); - if ($form->load(Yii::$app->request->post()) && $form->validate()) { - try { - $this->menu_service->edit($menu->id, $form); - return $this->redirect(['index', 'id' => $menu->id]); - } catch (\DomainException $e) { - Yii::$app->errorHandler->logException($e); - Yii::$app->session->setFlash('error', $e->getMessage()); - } - } - return $this->render('update', [ - 'model' => $form, - 'menu' => $menu, - ]); - } - - public function actionDelete($id) - { - try { - $this->menu_service->remove($id); - } catch (\DomainException $e) { - Yii::$app->errorHandler->logException($e); - Yii::$app->session->setFlash('error', $e->getMessage()); - } - return $this->redirect(['index']); - } - - public function actionSaveItem($id) - { - $item = $this->findItemModel($id); - $form = new MenuItemForm($item); - if ($form->load(Yii::$app->request->post()) && $form->validate()) { - try { - $this->menu_item_service->edit($item->id, $form); - return $this->redirect(['index', 'id' => $item->menu_id]); - } catch (\DomainException $e) { - Yii::$app->errorHandler->logException($e); - Yii::$app->session->setFlash('error', $e->getMessage()); - } - } - return $this->redirect(['index', 'id' => $item->menu_id]); - } - - public function actionDeleteMenuItem() - { - Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; - if (Yii::$app->request->isAjax) { - try { - $id = Yii::$app->request->post('id'); - $item = $this->findItemModel($id); - - //$this->deleteItem($item); - $item->delete(); - - Yii::$app->session->setFlash('success', Yii::t('menu', 'Menu Item Deleted')); - - return [ 'result' => 'success' ]; - } - catch (\RuntimeException $e) { - return [ 'result' => 'error', 'message' => $e->getMessage() ]; - } - } - return ['result' => 'error', 'message' => 'Request error']; - } - - /* - public function actionSaveMenuItemData() - { - Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; - if (Yii::$app->request->isAjax) { - try { - $id = Yii::$app->request->post('id'); - $name = Yii::$app->request->post('name'); - $title = Yii::$app->request->post('title'); - $style = Yii::$app->request->post('style'); - $css = Yii::$app->request->post('css'); - $url = Yii::$app->request->post('url'); - $target = Yii::$app->request->post('target'); - - $item = $this->findItemModel($id); - $item->name = $name; - $item->title_attr = $title; - $item->style = $style; - $item->css = $css; - $item->url = $url; - $item->target = $target; - $item->save(); - - Yii::$app->session->setFlash('success', Yii::t('menu', 'Menu Item Saved')); - - return [ 'result' => 'success' ]; - } - catch (\RuntimeException $e) { - return [ 'result' => 'error', 'message' => $e->getMessage() ]; - } - } - return ['result' => 'error', 'message' => 'Request error']; - } - */ - - public function actionSaveMenuItems() - { - $json = Yii::$app->request->post('json'); - Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; - if (Yii::$app->request->isAjax) { - try { - $order = []; - $items = Json::decode($json, true); - foreach ($items as $item) { - $order[$item[1]] = isset($order[$item[1]]) ? $order[$item[1]]+1 : 0; - $this->menu_item_service->setPosition($item, $order[$item[1]]); - } - Yii::$app->session->setFlash('success', Yii::t('menu', 'Menu Saved')); - return [ 'result' => 'success' ]; - } - catch (\RuntimeException $e) { - return [ 'result' => 'error', 'message' => $e->getMessage() ]; - } - } - return ['result' => 'error', 'message' => 'Request error']; - } - - private function getCreatorWidgets($menu_id): array - { - $widgets = []; - $modules = ModuleRecord::find()->active()->all(); - foreach ($modules as $module) { - if (method_exists($module->class, 'getMenuItemCreator')) { - $module_widgets = call_user_func_array($module->class . '::getMenuItemCreator', [$menu_id]); - $widgets = is_array($module_widgets) ? array_merge($widgets, $module_widgets) : $widgets; - } - } - return $widgets; - } - - private function createMenuItem() - { - $form = new MenuItemForm(); - if ($form->load(Yii::$app->request->post()) && $form->validate()) { - try { - $this->menu_item_service->create($form); - } catch (\DomainException $e) { - Yii::$app->errorHandler->logException($e); - } - } - } - - protected function findModel($id): Menu - { - if (($model = Menu::findOne($id)) !== null) { - return $model; - } - throw new NotFoundHttpException('The requested menu does not exist.'); - } - - protected function findItemModel($id): MenuItem - { - if (($model = MenuItem::findOne($id)) !== null) { - return $model; - } - throw new NotFoundHttpException('The requested menu item does not exist.'); - } -} \ No newline at end of file + public $menu_service; + public $menu_item_service; + + public function __construct( + string $id, + $module, + MenuManageService $menu_service, + MenuItemManageService $menu_item_service, + array $config = [] + ) { + parent::__construct($id, $module, $config); + + $this->menu_service = $menu_service; + $this->menu_item_service = $menu_item_service; + } + + public function behaviors(): array + { + return [ + 'access' => [ + 'class' => AccessControl::class, + 'rules' => [ + [ + 'allow' => true, + 'roles' => ['MenuManagement'], + ], + [ // all the action are accessible to admin + 'allow' => true, + 'roles' => ['admin'], + ], + ], + ], + 'verbs' => [ + 'class' => VerbFilter::class, + 'actions' => [ + 'delete' => ['POST'], + 'delete-menu-item' => ['POST'], + ], + ], + ]; + } + + public function actionIndex($id = null) + { + $menus = []; // menu list + $menu_records = Menu::find()->all(); + foreach ($menu_records as $menu_record) { + $menus[$menu_record->id] = isset($menu_record->translation) && $menu_record->translation->name ? $menu_record->translation->name : $menu_record->findTranslation(Yii::$app->params['defaultLanguage'])->name; + } + + $form = new MenuSelectForm(); + if ($form->load(Yii::$app->request->get()) && $form->validate()) { + return $this->redirect(['menu/index', 'id' => $form->id]); + } elseif ($id) { + $this->createMenuItem(); // create menu item if MenuItemForm sent + + $menu = $this->findModel($id); + + $creatorWidgets = $this->getCreatorWidgets($menu->id); + + return $this->render('menu', [ + 'model' => $form, + 'menus' => $menus, + 'menu' => $menu, + 'creator' => $creatorWidgets, + ]); + } else { + return $this->render('select_menu', [ + 'model' => $form, + 'menus' => $menus, + ]); + } + } + + public function actionCreate() + { + $form = new MenuForm(); + if ($form->load(Yii::$app->request->post()) && $form->validate()) { + try { + $menu = $this->menu_service->create($form); + + return $this->redirect(['index', 'id' => $menu->id]); + } catch (\DomainException $e) { + Yii::$app->errorHandler->logException($e); + Yii::$app->session->setFlash('error', $e->getMessage()); + } + } + + return $this->render('create', [ + 'model' => $form, + ]); + } + + public function actionUpdate($id) + { + $menu = $this->findModel($id); + + $form = new MenuForm($menu); + if ($form->load(Yii::$app->request->post()) && $form->validate()) { + try { + $this->menu_service->edit($menu->id, $form); + + return $this->redirect(['index', 'id' => $menu->id]); + } catch (\DomainException $e) { + Yii::$app->errorHandler->logException($e); + Yii::$app->session->setFlash('error', $e->getMessage()); + } + } + + return $this->render('update', [ + 'model' => $form, + 'menu' => $menu, + ]); + } + + public function actionDelete($id) + { + try { + $this->menu_service->remove($id); + } catch (\DomainException $e) { + Yii::$app->errorHandler->logException($e); + Yii::$app->session->setFlash('error', $e->getMessage()); + } + + return $this->redirect(['index']); + } + + public function actionSaveItem($id) + { + $item = $this->findItemModel($id); + $form = new MenuItemForm($item); + if ($form->load(Yii::$app->request->post()) && $form->validate()) { + try { + $this->menu_item_service->edit($item->id, $form); + + return $this->redirect(['index', 'id' => $item->menu_id]); + } catch (\DomainException $e) { + Yii::$app->errorHandler->logException($e); + Yii::$app->session->setFlash('error', $e->getMessage()); + } + } + + return $this->redirect(['index', 'id' => $item->menu_id]); + } + + public function actionDeleteMenuItem() + { + Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; + if (Yii::$app->request->isAjax) { + try { + $id = Yii::$app->request->post('id'); + $item = $this->findItemModel($id); + $item->delete(); + + Yii::$app->session->setFlash('success', Yii::t('menu', 'Menu Item Deleted')); + + return ['result' => 'success']; + } catch (\RuntimeException $e) { + return ['result' => 'error', 'message' => $e->getMessage()]; + } + } + + return ['result' => 'error', 'message' => 'Request error']; + } + + public function actionSaveMenuItems() + { + $json = Yii::$app->request->post('json'); + Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; + if (Yii::$app->request->isAjax) { + try { + $order = []; + $items = Json::decode($json, true); + foreach ($items as $item) { + $order[$item[1]] = isset($order[$item[1]]) ? $order[$item[1]] + 1 : 0; + $this->menu_item_service->setPosition($item, $order[$item[1]]); + } + Yii::$app->session->setFlash('success', Yii::t('menu', 'Menu Saved')); + + return ['result' => 'success']; + } catch (\RuntimeException $e) { + return ['result' => 'error', 'message' => $e->getMessage()]; + } + } + + return ['result' => 'error', 'message' => 'Request error']; + } + + private function getCreatorWidgets($menu_id): array + { + $widgets = []; + $modules = ModuleRecord::find()->active()->all(); + foreach ($modules as $module) { + if (method_exists($module->class, 'getMenuItemCreator')) { + $module_widgets = call_user_func_array($module->class . '::getMenuItemCreator', [$menu_id]); + $widgets = is_array($module_widgets) ? array_merge($widgets, $module_widgets) : $widgets; + } + } + + return $widgets; + } + + private function createMenuItem() + { + $form = new MenuItemForm(); + if ($form->load(Yii::$app->request->post()) && $form->validate()) { + try { + $this->menu_item_service->create($form); + } catch (\DomainException $e) { + Yii::$app->errorHandler->logException($e); + } + } + } + + protected function findModel($id): Menu + { + if (($model = Menu::findOne($id)) !== null) { + return $model; + } + throw new NotFoundHttpException('The requested menu does not exist.'); + } + + protected function findItemModel($id): MenuItem + { + if (($model = MenuItem::findOne($id)) !== null) { + return $model; + } + throw new NotFoundHttpException('The requested menu item does not exist.'); + } +} diff --git a/backend/controllers/ModuleController.php b/backend/controllers/ModuleController.php index 230ee87..47b16d6 100644 --- a/backend/controllers/ModuleController.php +++ b/backend/controllers/ModuleController.php @@ -6,7 +6,6 @@ namespace backend\controllers; - use core\entities\ModuleRecord; use core\services\ModuleService; use yii\web\Controller; @@ -16,77 +15,80 @@ use yii\web\NotFoundHttpException; class ModuleController extends Controller { - private $service; + private $_service; + + public function __construct(string $id, $module, ModuleService $service, array $config = []) + { + parent::__construct($id, $module, $config); + $this->_service = $service; + } + + public function behaviors(): array + { + return [ + 'access' => [ + 'class' => AccessControl::class, + 'rules' => [ + [ + 'actions' => ['list', 'disable', 'enable', 'delete'], + 'allow' => true, + 'roles' => ['ModuleManagement'], + ], + [ // all the action are accessible to admin + 'allow' => true, + 'roles' => ['admin'], + ], + ], + ], + 'verbs' => [ + 'class' => VerbFilter::class, + 'actions' => [ + 'delete' => ['POST'], + 'disable' => ['POST'], + 'enable' => ['POST'], + ], + ], + ]; + } + + public function actionList() + { + $modules = \Yii::$app->moduleManager->getModules(); - public function __construct( string $id, $module, ModuleService $service, array $config = [] ) { - parent::__construct( $id, $module, $config ); - $this->service = $service; - } + return $this->render('list', [ + 'modules' => $modules, + ]); + } - public function behaviors(): array - { - return [ - 'access' => [ - 'class' => AccessControl::class, - 'rules' => [ - [ - 'actions' => ['list', 'disable', 'enable', 'delete'], - 'allow' => true, - 'roles' => ['ModuleManagement'], - ], - [ // all the action are accessible to admin - 'allow' => true, - 'roles' => ['admin'], - ], - ], - ], - 'verbs' => [ - 'class' => VerbFilter::class, - 'actions' => [ - 'delete' => ['POST'], - 'disable' => ['POST'], - 'enable' => ['POST'], - ], - ], - ]; - } + public function actionDelete($id) + { + $module = $this->findModel($id); + $this->_service->delete($module); - public function actionList() - { - //$modules = ModuleRecord::find()->all(); - $modules = \Yii::$app->moduleManager->getModules(); + return $this->redirect(['module/list']); + } - return $this->render('list', [ - 'modules' => $modules, - ]); - } + public function actionDisable($id) + { + $module = $this->findModel($id); + $this->_service->disable($module); - public function actionDelete($id) - { - $module = $this->findModel($id); - $this->service->delete($module); - return $this->redirect(['module/list']); - } + return $this->redirect(['module/list']); + } - public function actionDisable($id) - { - $module = $this->findModel($id); - $this->service->disable($module); - return $this->redirect(['module/list']); - } + public function actionEnable($id) + { + $module = $this->findModel($id); + $this->_service->enable($module); - public function actionEnable($id) - { - $module = $this->findModel($id); - $this->service->enable($module); - return $this->redirect(['module/list']); - } + return $this->redirect(['module/list']); + } - protected function findModel($id): ModuleRecord - { - if (($model = ModuleRecord::findOne($id)) !== null) { - return $model; - } - throw new NotFoundHttpException('The requested module does not exist.'); - } -} \ No newline at end of file + protected function findModel($id): ModuleRecord + { + if (($model = ModuleRecord::findOne($id)) !== null) { + return $model; + } + throw new NotFoundHttpException('The requested module does not exist.'); + } +} diff --git a/backend/controllers/PermissionController.php b/backend/controllers/PermissionController.php index 5d72a46..1107e4f 100644 --- a/backend/controllers/PermissionController.php +++ b/backend/controllers/PermissionController.php @@ -19,12 +19,12 @@ use yii\filters\AccessControl; class PermissionController extends Controller { - private $permission; + private $_permission; public function __construct($id, $module, PermissionManager $permission, $config = []) { parent::__construct($id, $module, $config); - $this->permission = $permission; + $this->_permission = $permission; } /** @@ -34,15 +34,15 @@ class PermissionController extends Controller { return [ [ - 'class' => TimestampBehavior::className(), + 'class' => TimestampBehavior::class, ], 'access' => [ - 'class' => AccessControl::className(), + 'class' => AccessControl::class, 'rules' => [ [ - 'actions' => ['create','view','index', 'update', 'delete'], - 'allow' => true, - 'roles' => ['UserManagement'], + 'actions' => ['create', 'view', 'index', 'update', 'delete'], + 'allow' => true, + 'roles' => ['UserManagement'], ], [ // all the action are accessible to admin 'allow' => true, @@ -50,8 +50,8 @@ class PermissionController extends Controller ], ], ], - 'verbs' => [ - 'class' => VerbFilter::className(), + 'verbs' => [ + 'class' => VerbFilter::class, 'actions' => [ 'delete' => ['POST'], ], @@ -61,19 +61,19 @@ class PermissionController extends Controller public function actionIndex() { - $data = array_map(function (Permission $permission){ + $data = array_map(function (Permission $permission) { return [ - 'name' => $permission->name, + 'name' => $permission->name, 'description' => $permission->description, ]; - }, $this->permission->getPermissions()); + }, $this->_permission->getPermissions()); $dataProvider = new ArrayDataProvider([ - 'allModels' => $data, + 'allModels' => $data, 'pagination' => [ 'pageSize' => 20, ], - 'sort' => [ + 'sort' => [ 'attributes' => ['name', 'description'], ], ]); @@ -86,13 +86,15 @@ class PermissionController extends Controller $form = new RbacCreatePermissionForm(); if ($form->load(Yii::$app->request->post()) && $form->validate()) { try { - $this->permission->create($form->name, $form->description, $form->rule_name, $form->data); + $this->_permission->create($form->name, $form->description, $form->rule_name, $form->data); + return $this->redirect(['view', 'id' => $form->name]); } catch (\DomainException $e) { Yii::$app->errorHandler->logException($e); Yii::$app->session->setFlash('error', $e->getMessage()); } } + return $this->render('create', [ 'model' => $form, ]); @@ -105,13 +107,15 @@ class PermissionController extends Controller $form = new RbacEditPermissionForm($permission); if ($form->load(Yii::$app->request->post()) && $form->validate()) { try { - $this->permission->update($permission->name, $form->name, $form->description, $form->rule_name, $form->data); + $this->_permission->update($permission->name, $form->name, $form->description, $form->rule_name, $form->data); + return $this->redirect(['view', 'id' => $form->name]); } catch (\DomainException $e) { Yii::$app->errorHandler->logException($e); Yii::$app->session->setFlash('error', $e->getMessage()); } } + return $this->render('update', [ 'model' => $form, ]); @@ -119,7 +123,8 @@ class PermissionController extends Controller public function actionDelete($id) { - $this->permission->delete($id); + $this->_permission->delete($id); + return $this->redirect(['index']); } @@ -132,6 +137,6 @@ class PermissionController extends Controller protected function findModel($id) { - return $this->permission->getPermission($id); + return $this->_permission->getPermission($id); } -} \ No newline at end of file +} diff --git a/backend/controllers/RoleController.php b/backend/controllers/RoleController.php index c243c19..26133d4 100644 --- a/backend/controllers/RoleController.php +++ b/backend/controllers/RoleController.php @@ -20,14 +20,14 @@ use Yii; class RoleController extends Controller { - private $role; - private $permission; + private $_role; + private $_permission; public function __construct($id, $module, RoleManager $role, PermissionManager $permission, $config = []) { parent::__construct($id, $module, $config); - $this->role = $role; - $this->permission = $permission; + $this->_role = $role; + $this->_permission = $permission; } /** @@ -37,15 +37,15 @@ class RoleController extends Controller { return [ [ - 'class' => TimestampBehavior::className(), + 'class' => TimestampBehavior::class, ], 'access' => [ - 'class' => AccessControl::className(), + 'class' => AccessControl::class, 'rules' => [ [ - 'actions' => ['create','view','index', 'update', 'delete'], - 'allow' => true, - 'roles' => ['UserManagement'], + 'actions' => ['create', 'view', 'index', 'update', 'delete'], + 'allow' => true, + 'roles' => ['UserManagement'], ], [ // all the action are accessible to admin 'allow' => true, @@ -53,8 +53,8 @@ class RoleController extends Controller ], ], ], - 'verbs' => [ - 'class' => VerbFilter::className(), + 'verbs' => [ + 'class' => VerbFilter::class, 'actions' => [ 'delete' => ['POST'], ], @@ -64,14 +64,14 @@ class RoleController extends Controller public function actionIndex() { - $data = $this->role->getRolesListArray(); + $data = $this->_role->getRolesListArray(); $dataProvider = new ArrayDataProvider([ - 'allModels' => $data, + 'allModels' => $data, 'pagination' => [ 'pageSize' => 20, ], - 'sort' => [ + 'sort' => [ 'attributes' => ['name', 'description'], ], ]); @@ -84,13 +84,15 @@ class RoleController extends Controller $form = new RbacCreateRoleForm(); if ($form->load(Yii::$app->request->post()) && $form->validate()) { try { - $this->role->create($form->name, $form->description, $form->rule_name, $form->data); + $this->_role->create($form->name, $form->description, $form->rule_name, $form->data); + return $this->redirect(['view', 'id' => $form->name]); } catch (\DomainException $e) { Yii::$app->errorHandler->logException($e); Yii::$app->session->setFlash('error', $e->getMessage()); } } + return $this->render('create', [ 'model' => $form, ]); @@ -103,13 +105,15 @@ class RoleController extends Controller $form = new RbacEditRoleForm($role); if ($form->load(Yii::$app->request->post()) && $form->validate()) { try { - $this->role->update($role->name, $form->name, $form->description, $form->rule_name, $form->data); + $this->_role->update($role->name, $form->name, $form->description, $form->rule_name, $form->data); + return $this->redirect(['view', 'id' => $form->name]); } catch (\DomainException $e) { Yii::$app->errorHandler->logException($e); Yii::$app->session->setFlash('error', $e->getMessage()); } } + return $this->render('update', [ 'model' => $form, ]); @@ -118,46 +122,44 @@ class RoleController extends Controller public function actionDelete($id) { try { - $this->role->delete($id); - } - catch (\DomainException $e) - { + $this->_role->delete($id); + } catch (\DomainException $e) { Yii::$app->errorHandler->logException($e); Yii::$app->session->setFlash('error', $e->getMessage()); } + return $this->redirect(['index']); } public function actionView($id) { - $currentRole = $this->role->getRole($id); - $rolesSelectArray = array_diff_assoc($this->role->getRolesSelectArray(), [$currentRole->name => $currentRole->description]); + $currentRole = $this->_role->getRole($id); + $rolesSelectArray = array_diff_assoc($this->_role->getRolesSelectArray(), [$currentRole->name => $currentRole->description]); $itemsForm = new RbacUpdateChildren(); if ($itemsForm->load(Yii::$app->request->post()) && $itemsForm->validate()) { - $this->role->saveChildren($id, $itemsForm->roles, $itemsForm->permissions); + $this->_role->saveChildren($id, $itemsForm->roles, $itemsForm->permissions); Yii::$app->session->setFlash('success', Yii::t('user', 'Children roles and permissions for "{role}" is updated.', ['role' => $currentRole->description])); } - $rolesSelected = $this->role->getRolesSelectArrayByRole($id); + $rolesSelected = $this->_role->getRolesSelectArrayByRole($id); - $permissionsSelectArray = $this->permission->getPermissionsSelectArray(); - $permissionsSelected = $this->permission->getPermissionsSelectArrayByRole($id); + $permissionsSelectArray = $this->_permission->getPermissionsSelectArray(); + $permissionsSelected = $this->_permission->getPermissionsSelectArrayByRole($id); - $itemsForm->roles = $rolesSelected; + $itemsForm->roles = $rolesSelected; $itemsForm->permissions = $permissionsSelected; return $this->render('view', [ - 'model' => $this->findModel($id), - 'roles' => $rolesSelectArray, - 'permissions' => $permissionsSelectArray, - 'permissionsSelected' => $permissionsSelected, - 'itemsForm' => $itemsForm, + 'model' => $this->findModel($id), + 'roles' => $rolesSelectArray, + 'permissions' => $permissionsSelectArray, + 'itemsForm' => $itemsForm, ]); } protected function findModel($id) { - return $this->role->getRole($id); + return $this->_role->getRole($id); } -} \ No newline at end of file +} diff --git a/backend/controllers/SiteController.php b/backend/controllers/SiteController.php index e7ee893..9e0f61e 100644 --- a/backend/controllers/SiteController.php +++ b/backend/controllers/SiteController.php @@ -1,31 +1,30 @@ service = $service; - } + public function __construct(string $id, $module, UserManageService $service, array $config = []) + { + parent::__construct($id, $module, $config); + $this->_service = $service; + } - /** + /** * @inheritdoc */ public function behaviors() @@ -36,23 +35,23 @@ class SiteController extends Controller 'rules' => [ [ 'actions' => ['error'], - 'allow' => true, + 'allow' => true, ], [ 'actions' => ['index', 'search', 'language'], + 'allow' => true, + 'roles' => ['Dashboard'], + ], + [ // all the action are accessible to admin 'allow' => true, - 'roles' => ['Dashboard'], + 'roles' => ['admin'], ], - [ // all the action are accessible to admin - 'allow' => true, - 'roles' => ['admin'], - ], ], ], - 'verbs' => [ - 'class' => VerbFilter::class, + 'verbs' => [ + 'class' => VerbFilter::class, 'actions' => [ - 'logout' => ['post'], + 'logout' => ['post'], 'language' => ['post'], ], ], @@ -83,50 +82,53 @@ class SiteController extends Controller public function actionSearch() { - $form = new SearchForm(); - - if ($form->load(Yii::$app->request->post()) && $form->validate()) { - try { - $query = Search::find() - ->andWhere(['LIKE', 'title', $form->query]) - ->orWhere(['LIKE', 'content', $form->query]); - - $provider = new ActiveDataProvider([ - 'query' => $query, - 'pagination' => [ - 'pageSize' => 10, - ], - 'sort' => [], - ]); - - return $this->render('search', [ - 'provider' => $provider, - 'form' => $form - ]); - - //$page = $this->service->create($form); - //return $this->redirect(['view', 'id' => $page->id]); - } catch (\DomainException $e) { - //Yii::$app->errorHandler->logException($e); - //Yii::$app->session->setFlash('error', $e->getMessage()); - } - } - return ''; + $form = new SearchForm(); + + if ($form->load(Yii::$app->request->post()) && $form->validate()) { + try { + $query = Search::find() + ->andWhere(['LIKE', 'title', $form->query]) + ->orWhere(['LIKE', 'content', $form->query]); + + $provider = new ActiveDataProvider([ + 'query' => $query, + 'pagination' => [ + 'pageSize' => 10, + ], + 'sort' => [], + ]); + + return $this->render('search', [ + 'provider' => $provider, + 'form' => $form + ]); + + //$page = $this->service->create($form); + //return $this->redirect(['view', 'id' => $page->id]); + } catch (\DomainException $e) { + //Yii::$app->errorHandler->logException($e); + //Yii::$app->session->setFlash('error', $e->getMessage()); + } + } + + return ''; } public function actionLanguage($language) { if ($language && in_array($language, array_keys(Yii::$app->params['backendTranslatedLanguages']))) { - $this->service->setBackendLanguage($language); - } - return $this->redirect(Yii::$app->request->referrer); + $this->_service->setBackendLanguage($language); + } + + return $this->redirect(Yii::$app->request->referrer); } - public function beforeAction($action) - { - if ($action->id === 'error') { - $this->layout = 'error'; - } - return parent::beforeAction($action); - } + public function beforeAction($action) + { + if ($action->id === 'error') { + $this->layout = 'error'; + } + + return parent::beforeAction($action); + } } diff --git a/backend/controllers/SliderController.php b/backend/controllers/SliderController.php index 9321d2b..9c48d79 100644 --- a/backend/controllers/SliderController.php +++ b/backend/controllers/SliderController.php @@ -12,15 +12,14 @@ use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; use yii\filters\AccessControl; - class SliderController extends Controller { - private $service; + private $_service; public function __construct($id, $module, SliderService $service, $config = []) { parent::__construct($id, $module, $config); - $this->service = $service; + $this->_service = $service; } /** @@ -29,22 +28,22 @@ class SliderController extends Controller public function behaviors() { return [ - 'access' => [ - 'class' => AccessControl::class, - 'rules' => [ - [ - 'actions' => ['create', 'view', 'index', 'update', 'delete'], - 'allow' => true, - 'roles' => ['SliderManagement'], - ], - [ // all the action are accessible to admin - 'allow' => true, - 'roles' => ['admin'], - ], - ], - ], - 'verbs' => [ - 'class' => VerbFilter::class, + 'access' => [ + 'class' => AccessControl::class, + 'rules' => [ + [ + 'actions' => ['create', 'view', 'index', 'update', 'delete'], + 'allow' => true, + 'roles' => ['SliderManagement'], + ], + [ // all the action are accessible to admin + 'allow' => true, + 'roles' => ['admin'], + ], + ], + ], + 'verbs' => [ + 'class' => VerbFilter::class, 'actions' => [ 'delete' => ['POST'], ], @@ -56,30 +55,30 @@ class SliderController extends Controller { //$searchModel = new UserSearch(); //$dataProvider = $searchModel->search(Yii::$app->request->queryParams); - $query = Slider::find(); - $dataProvider = new ActiveDataProvider([ - 'query' => $query, - 'pagination' => [ - 'pageSize' => 20, - ], - 'sort' => [ - 'defaultOrder' => [ - 'id' => SORT_ASC, - ] - ], - ]); + $query = Slider::find(); + $dataProvider = new ActiveDataProvider([ + 'query' => $query, + 'pagination' => [ + 'pageSize' => 20, + ], + 'sort' => [ + 'defaultOrder' => [ + 'id' => SORT_ASC, + ] + ], + ]); return $this->render('index', [ 'dataProvider' => $dataProvider, ]); } - /** - * @param $id - * - * @return string - * @throws NotFoundHttpException - */ + /** + * @param $id + * + * @return string + * @throws NotFoundHttpException + */ public function actionView($id) { return $this->render('view', [ @@ -94,57 +93,62 @@ class SliderController extends Controller */ public function actionCreate() { - $form = new SliderForm(); + $form = new SliderForm(); $form->scenario = Slider::SCENARIO_CREATE; if ($form->load(Yii::$app->request->post()) && $form->validate()) { try { - $slider = $this->service->create($form); + $slider = $this->_service->create($form); + return $this->redirect(['view', 'id' => $slider->id]); } catch (\DomainException $e) { Yii::$app->errorHandler->logException($e); Yii::$app->session->setFlash('error', $e->getMessage()); } } + return $this->render('create', [ 'model' => $form, ]); } - /** - * @param $id - * - * @return string|\yii\web\Response - * @throws NotFoundHttpException - */ + /** + * @param $id + * + * @return string|\yii\web\Response + * @throws NotFoundHttpException + */ public function actionUpdate($id) { $slider = $this->findModel($id); - $form = new SliderForm($slider); - $form->scenario = Slider::SCENARIO_UPDATE; + $form = new SliderForm($slider); + $form->scenario = Slider::SCENARIO_UPDATE; if ($form->load(Yii::$app->request->post()) && $form->validate()) { try { - $this->service->edit($slider->id, $form); + $this->_service->edit($slider->id, $form); + return $this->redirect(['view', 'id' => $slider->id]); } catch (\DomainException $e) { Yii::$app->errorHandler->logException($e); Yii::$app->session->setFlash('error', $e->getMessage()); } } + return $this->render('update', [ - 'model' => $form, + 'model' => $form, 'slider' => $slider, ]); } - /** - * @param $id - * - * @return \yii\web\Response - */ + /** + * @param $id + * + * @return \yii\web\Response + */ public function actionDelete($id) { - $this->service->remove($id); + $this->_service->remove($id); + return $this->redirect(['index']); } diff --git a/backend/controllers/UserController.php b/backend/controllers/UserController.php index cc4e60b..d07a34b 100644 --- a/backend/controllers/UserController.php +++ b/backend/controllers/UserController.php @@ -20,14 +20,14 @@ use yii\web\UploadedFile; */ class UserController extends Controller { - private $service; - private $profile_service; + private $_service; + private $_profile_service; public function __construct($id, $module, UserManageService $service, ProfileService $profile_service, $config = []) { parent::__construct($id, $module, $config); - $this->service = $service; - $this->profile_service = $profile_service; + $this->_service = $service; + $this->_profile_service = $profile_service; } /** @@ -36,27 +36,27 @@ class UserController extends Controller public function behaviors() { return [ - 'access' => [ - 'class' => AccessControl::class, - 'rules' => [ - [ - 'actions' => ['create','view','index', 'update', 'delete'], - 'allow' => true, - 'roles' => ['UserManagement'], - ], - [ - 'actions' => ['profile'], - 'allow' => true, - 'roles' => ['@'], - ], - [ // all the action are accessible to admin - 'allow' => true, - 'roles' => ['admin'], - ], - ], - ], - 'verbs' => [ - 'class' => VerbFilter::class, + 'access' => [ + 'class' => AccessControl::class, + 'rules' => [ + [ + 'actions' => ['create', 'view', 'index', 'update', 'delete'], + 'allow' => true, + 'roles' => ['UserManagement'], + ], + [ + 'actions' => ['profile'], + 'allow' => true, + 'roles' => ['@'], + ], + [ // all the action are accessible to admin + 'allow' => true, + 'roles' => ['admin'], + ], + ], + ], + 'verbs' => [ + 'class' => VerbFilter::class, 'actions' => [ 'delete' => ['POST'], ], @@ -70,21 +70,21 @@ class UserController extends Controller */ public function actionIndex() { - $searchModel = new UserSearch(); + $searchModel = new UserSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', [ - 'searchModel' => $searchModel, + 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); } - /** - * @param $id - * - * @return string - * @throws NotFoundHttpException - */ + /** + * @param $id + * + * @return string + * @throws NotFoundHttpException + */ public function actionView($id) { return $this->render('view', [ @@ -102,24 +102,26 @@ class UserController extends Controller $form = new UserForm(); if ($form->load(Yii::$app->request->post()) && $form->validate()) { try { - $user = $this->service->create($form); + $user = $this->_service->create($form); + return $this->redirect(['view', 'id' => $user->id]); } catch (\DomainException $e) { Yii::$app->errorHandler->logException($e); Yii::$app->session->setFlash('error', $e->getMessage()); } } + return $this->render('create', [ 'model' => $form, ]); } - /** - * @param $id - * - * @return string|\yii\web\Response - * @throws NotFoundHttpException - */ + /** + * @param $id + * + * @return string|\yii\web\Response + * @throws NotFoundHttpException + */ public function actionUpdate($id) { $user = $this->findModel($id); @@ -127,58 +129,66 @@ class UserController extends Controller $form = new UserForm($user); if ($form->load(Yii::$app->request->post()) && $form->validate()) { try { - $this->service->edit($user->id, $form); + $this->_service->edit($user->id, $form); + return $this->redirect(['view', 'id' => $user->id]); } catch (\DomainException $e) { Yii::$app->errorHandler->logException($e); Yii::$app->session->setFlash('error', $e->getMessage()); } } + return $this->render('update', [ 'model' => $form, - 'user' => $user, + 'user' => $user, ]); } /** * Deletes an existing User model. * If deletion is successful, the browser will be redirected to the 'index' page. + * * @param integer $id + * * @return mixed */ public function actionDelete($id) { - $this->service->remove($id); + $this->_service->remove($id); + return $this->redirect(['index']); } - public function actionProfile() - { - $user = $this->findModel(Yii::$app->user->id); - $form = new ProfileEditForm($user); - - if ($form->load(Yii::$app->request->post()) && $form->validate()) { - try { - $form->user_pic = UploadedFile::getInstance($form, 'user_pic'); - $this->profile_service->edit(Yii::$app->user->id, $form); - Yii::$app->session->setFlash('success', Yii::t('user', 'Profile is saved.')); - return $this->redirect(['/user/profile']); - } catch (\DomainException $e) { - Yii::$app->errorHandler->logException($e); - Yii::$app->session->setFlash('error', $e->getMessage()); - } - } - - return $this->render('profile', [ - 'model' => $form, - 'user' => $user, - ]); - } + public function actionProfile() + { + $user = $this->findModel(Yii::$app->user->id); + $form = new ProfileEditForm($user); + + if ($form->load(Yii::$app->request->post()) && $form->validate()) { + try { + $form->user_pic = UploadedFile::getInstance($form, 'user_pic'); + $this->_profile_service->edit(Yii::$app->user->id, $form); + Yii::$app->session->setFlash('success', Yii::t('user', 'Profile is saved.')); + + return $this->redirect(['/user/profile']); + } catch (\DomainException $e) { + Yii::$app->errorHandler->logException($e); + Yii::$app->session->setFlash('error', $e->getMessage()); + } + } + + return $this->render('profile', [ + 'model' => $form, + 'user' => $user, + ]); + } /** * Finds the User model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. + * * @param integer $id + * * @return User the loaded model * @throws NotFoundHttpException if the model cannot be found */ diff --git a/backend/controllers/settings/ListController.php b/backend/controllers/settings/ListController.php index da83f0f..4c454fa 100644 --- a/backend/controllers/settings/ListController.php +++ b/backend/controllers/settings/ListController.php @@ -6,7 +6,6 @@ namespace backend\controllers\settings; - use backend\forms\SettingsSearch; use core\entities\Settings; use core\forms\SettingsForm; @@ -20,131 +19,137 @@ use yii\web\NotFoundHttpException; class ListController extends Controller { - private $service; - - public function __construct( string $id, $module, SettingsService $service, array $config = [] ) { - parent::__construct( $id, $module, $config ); - $this->service = $service; - } - - public function behaviors() - { - return [ - 'verbs' => [ - 'class' => VerbFilter::class, - 'actions' => [ - 'delete' => ['POST'], - ], - ], - 'access' => [ - 'class' => AccessControl::class, - 'rules' => [ - [ - 'actions' => ['create','view','index', 'update', 'delete', 'toggle'], - 'allow' => true, - 'roles' => ['SettingsManagement'], - ], - [ // all the action are accessible to admin - 'allow' => true, - 'roles' => ['admin'], - ], - ], - ], - ]; - } - - public function actions() - { - return [ - 'toggle' => [ - 'class' => ToggleAction::class, - 'modelClass' => Settings::class, - //'setFlash' => true, - ] - ]; - } - - public function actionIndex() - { - $searchModel = new SettingsSearch(); - $dataProvider = $searchModel->search(Yii::$app->request->queryParams); - return $this->render( - 'index', - [ - 'searchModel' => $searchModel, - 'dataProvider' => $dataProvider, - ] - ); - } - - public function actionView($id) - { - return $this->render( - 'view', - [ - 'model' => $this->findModel($id), - ] - ); - } - - public function actionCreate() - { - $form = new SettingsForm(); - if ($form->load(Yii::$app->request->post()) && $form->validate()) { - try { - $settings = $this->service->create($form); - return $this->redirect(['view', 'id' => $settings->id]); - } catch (\DomainException $e) { - Yii::$app->errorHandler->logException($e); - Yii::$app->session->setFlash('error', $e->getMessage()); - } - } - else { - $form->active = 1; - } - return $this->render( - 'create', - [ - 'model' => $form, - ] - ); - } - - public function actionUpdate($id) - { - $settings = $this->findModel($id); - - $form = new SettingsForm($settings); - if ($form->load(Yii::$app->request->post()) && $form->validate()) { - try { - $this->service->edit($settings->id, $form); - return $this->redirect(['view', 'id' => $settings->id]); - } catch (\DomainException $e) { - Yii::$app->errorHandler->logException($e); - Yii::$app->session->setFlash('error', $e->getMessage()); - } - } - return $this->render( - 'update', - [ - 'model' => $form, - 'settings' => $settings, - ] - ); - } - - public function actionDelete($id) - { - $this->service->remove($id); - return $this->redirect(['index']); - } - - protected function findModel($id) - { - if (($model = Settings::findOne($id)) !== null) { - return $model; - } else { - throw new NotFoundHttpException('The requested page does not exist.'); - } - } -} \ No newline at end of file + private $_service; + + public function __construct(string $id, $module, SettingsService $service, array $config = []) + { + parent::__construct($id, $module, $config); + $this->_service = $service; + } + + public function behaviors() + { + return [ + 'verbs' => [ + 'class' => VerbFilter::class, + 'actions' => [ + 'delete' => ['POST'], + ], + ], + 'access' => [ + 'class' => AccessControl::class, + 'rules' => [ + [ + 'actions' => ['create', 'view', 'index', 'update', 'delete', 'toggle'], + 'allow' => true, + 'roles' => ['SettingsManagement'], + ], + [ // all the action are accessible to admin + 'allow' => true, + 'roles' => ['admin'], + ], + ], + ], + ]; + } + + public function actions() + { + return [ + 'toggle' => [ + 'class' => ToggleAction::class, + 'modelClass' => Settings::class, + //'setFlash' => true, + ] + ]; + } + + public function actionIndex() + { + $searchModel = new SettingsSearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render( + 'index', + [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ] + ); + } + + public function actionView($id) + { + return $this->render( + 'view', + [ + 'model' => $this->findModel($id), + ] + ); + } + + public function actionCreate() + { + $form = new SettingsForm(); + if ($form->load(Yii::$app->request->post()) && $form->validate()) { + try { + $settings = $this->_service->create($form); + + return $this->redirect(['view', 'id' => $settings->id]); + } catch (\DomainException $e) { + Yii::$app->errorHandler->logException($e); + Yii::$app->session->setFlash('error', $e->getMessage()); + } + } else { + $form->active = 1; + } + + return $this->render( + 'create', + [ + 'model' => $form, + ] + ); + } + + public function actionUpdate($id) + { + $settings = $this->findModel($id); + + $form = new SettingsForm($settings); + if ($form->load(Yii::$app->request->post()) && $form->validate()) { + try { + $this->_service->edit($settings->id, $form); + + return $this->redirect(['view', 'id' => $settings->id]); + } catch (\DomainException $e) { + Yii::$app->errorHandler->logException($e); + Yii::$app->session->setFlash('error', $e->getMessage()); + } + } + + return $this->render( + 'update', + [ + 'model' => $form, + 'settings' => $settings, + ] + ); + } + + public function actionDelete($id) + { + $this->_service->remove($id); + + return $this->redirect(['index']); + } + + protected function findModel($id) + { + if (($model = Settings::findOne($id)) !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } +} diff --git a/backend/views/role/view.php b/backend/views/role/view.php index 60fea59..163847b 100644 --- a/backend/views/role/view.php +++ b/backend/views/role/view.php @@ -1,4 +1,5 @@ title = $model->name; +$this->title = $model->name; $this->params['breadcrumbs'][] = ['label' => Yii::t('user', 'Roles'), 'url' => ['index']]; $this->params['breadcrumbs'][] = $this->title; ?> @@ -23,9 +24,9 @@ $this->params['breadcrumbs'][] = $this->title; = Html::a(Yii::t('buttons', 'Edit'), ['update', 'id' => $model->name], ['class' => 'btn btn-primary']) ?> = Html::a(Yii::t('buttons', 'Delete'), ['delete', 'id' => $model->name], [ 'class' => 'btn btn-danger', - 'data' => [ + 'data' => [ 'confirm' => Yii::t('buttons', 'Are you sure you want to delete this item?'), - 'method' => 'post', + 'method' => 'post', ], ]) ?>
@@ -33,19 +34,19 @@ $this->params['breadcrumbs'][] = $this->title;