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; $model->name], ['class' => 'btn btn-primary']) ?> $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;
$model, + 'model' => $model, 'attributes' => [ [ 'attribute' => 'name', - 'label' => Yii::t('user', 'Role Name'), + 'label' => Yii::t('user', 'Role Name'), ], [ 'attribute' => 'description', - 'label' => Yii::t('user', 'Role Description'), + 'label' => Yii::t('user', 'Role Description'), ], [ 'attribute' => 'ruleName', - 'label' => Yii::t('user', 'Rule Name'), + 'label' => Yii::t('user', 'Rule Name'), ], ], ]) ?> @@ -53,44 +54,54 @@ $this->params['breadcrumbs'][] = $this->title;
- name !== 'admin'): ?> + name !== 'admin') : ?> -

- +

+ - field($itemsForm, 'roles')->widget( - Select2::classname(), [ - 'name' => 'childrenRoles', - 'data' => $roles, - 'size' => Select2::SMALL, - 'options' => ['placeholder' => Yii::t('user', 'Select role...'), 'multiple' => true], - 'pluginOptions' => [ - 'allowClear' => true - ], - ])->label(false); - ?> + field($itemsForm, 'roles')->widget( + Select2::class, + [ + 'name' => 'childrenRoles', + 'data' => $roles, + 'size' => Select2::SMALL, + 'options' => [ + 'placeholder' => Yii::t('user', 'Select role...'), + 'multiple' => true, + ], + 'pluginOptions' => [ + 'allowClear' => true + ], + ] + )->label(false); + ?> -

+

- field($itemsForm, 'permissions')->widget( - Select2::classname(), [ - 'name' => 'childrenPermissions', - 'data' => $permissions, - 'size' => Select2::SMALL, - 'options' => ['placeholder' => Yii::t('user', 'Select permission...'), 'multiple' => true], + field($itemsForm, 'permissions')->widget( + Select2::class, + [ + 'name' => 'childrenPermissions', + 'data' => $permissions, + 'size' => Select2::SMALL, + 'options' => [ + 'placeholder' => Yii::t('user', 'Select permission...'), + 'multiple' => true + ], 'pluginOptions' => [ 'allowClear' => true ], - ])->label(false); - ?> + ] + )->label(false); + ?> -
- 'btn btn-primary']) ?> -
+
+ 'btn btn-primary']) ?> +
- + - +
diff --git a/console/controllers/ModuleController.php b/console/controllers/ModuleController.php new file mode 100644 index 0000000..a39de01 --- /dev/null +++ b/console/controllers/ModuleController.php @@ -0,0 +1,43 @@ +_service = $service; + } + + /** + * Activate module and apply it migration if needed + * @param $name + */ + public function actionActivate($name) + { + $module = ModuleRecord::find()->andWhere(['name' => $name])->one(); + + if ($module || $module->isDisabled()) { + $this->_service->enable($module); + } + } +} diff --git a/console/controllers/PermissionController.php b/console/controllers/PermissionController.php new file mode 100644 index 0000000..260dcbb --- /dev/null +++ b/console/controllers/PermissionController.php @@ -0,0 +1,39 @@ +_service = $service; + } + + /** + * Create permission + * @param $name + * @param null $description + */ + public function actionAdd($name, $description = null) : void + { + $this->_service->create($name, $description); + } +} diff --git a/console/controllers/UserController.php b/console/controllers/UserController.php index ab06dbb..fdea534 100644 --- a/console/controllers/UserController.php +++ b/console/controllers/UserController.php @@ -31,8 +31,6 @@ class UserController extends Controller $password = $this->prompt('Password:', ['required' => true]); $role = $this->select('Role:', ArrayHelper::map(Yii::$app->authManager->getRoles(), 'name', 'description')); - //$this->findModel($username, $email); - $form = new UserForm(); $form->username = $username; $form->email = $email; @@ -50,12 +48,6 @@ class UserController extends Controller foreach ($form->errors as $error) { $this->stdout(is_string($error) ? $error : $error[0] . PHP_EOL); } - //$this->stdout(print_r($form->errors, true) . PHP_EOL); - - - /* - $user = User::create($username, $email, $phone, $password); - $user->save();*/ } public function actionAddAdmin($username, $email, $password) diff --git a/core/entities/Settings.php b/core/entities/Settings.php index c88f67a..e9c1ac7 100644 --- a/core/entities/Settings.php +++ b/core/entities/Settings.php @@ -6,6 +6,10 @@ namespace core\entities; +use Yii; +use yii\db\ActiveRecord; +use yii\behaviors\TimestampBehavior; + /** * @property integer $id * @property string $type @@ -17,55 +21,52 @@ namespace core\entities; * @property integer $updated_at */ -use Yii; -use yii\db\ActiveRecord; -use yii\behaviors\TimestampBehavior; - class Settings extends ActiveRecord { - public static function tableName(): string - { - return '{{%settings}}'; - } + public static function tableName(): string + { + return '{{%settings}}'; + } + + public function attributeLabels() + { + return [ + 'id' => Yii::t('main', 'ID'), + 'type' => Yii::t('main', 'Type'), + 'section' => Yii::t('main', 'Section'), + 'key' => Yii::t('main', 'Key'), + 'value' => Yii::t('main', 'Value'), + 'active' => Yii::t('main', 'Active'), + 'created_at' => Yii::t('main', 'Created At'), + 'updated_at' => Yii::t('main', 'Updated At'), + ]; + } - public function attributeLabels() - { - return [ - 'id' => Yii::t('main', 'ID'), - 'type' => Yii::t('main', 'Type'), - 'section' => Yii::t('main', 'Section'), - 'key' => Yii::t('main', 'Key'), - 'value' => Yii::t('main', 'Value'), - 'active' => Yii::t('main', 'Active'), - 'created_at' => Yii::t('main', 'Created At'), - 'updated_at' => Yii::t('main', 'Updated At'), - ]; - } + public static function create($type, $section, $key, $value, $active): self + { + $settings = new static(); + $settings->type = $type; + $settings->section = $section; + $settings->key = $key; + $settings->value = $value; + $settings->active = $active; - public static function create($type, $section, $key, $value, $active): self - { - $settings = new static(); - $settings->type = $type; - $settings->section = $section; - $settings->key = $key; - $settings->value = $value; - $settings->active = $active; - return $settings; - } + return $settings; + } - public function edit($type, $section, $key, $value, $active): void - { - $this->type = $type; - $this->section = $section; - $this->key = $key; - $this->value = $value; - $this->active = $active; - } + public function edit($type, $section, $key, $value, $active): void + { + $this->type = $type; + $this->section = $section; + $this->key = $key; + $this->value = $value; + $this->active = $active; + } - public function behaviors(): array - { - return [ - TimestampBehavior::class, - ]; - } -} \ No newline at end of file + public function behaviors(): array + { + return [ + TimestampBehavior::class, + ]; + } +} diff --git a/core/services/SettingsService.php b/core/services/SettingsService.php index 3cee2a4..4ac7cc3 100644 --- a/core/services/SettingsService.php +++ b/core/services/SettingsService.php @@ -8,11 +8,11 @@ use core\repositories\SettingsRepository; class SettingsService { - private $repository; + private $_repository; public function __construct(SettingsRepository $repository) { - $this->repository = $repository; + $this->_repository = $repository; } public function create(SettingsForm $form): Settings @@ -24,26 +24,27 @@ class SettingsService $form->value, $form->active ); - $this->repository->save($settings); + $this->_repository->save($settings); + return $settings; } public function edit($id, SettingsForm $form): void { - $settings = $this->repository->get($id); + $settings = $this->_repository->get($id); $settings->edit( - $form->type, - $form->section, - $form->key, - $form->value, - $form->active + $form->type, + $form->section, + $form->key, + $form->value, + $form->active ); - $this->repository->save($settings); + $this->_repository->save($settings); } public function remove($id): void { - $settings = $this->repository->get($id); - $this->repository->remove($settings); + $settings = $this->_repository->get($id); + $this->_repository->remove($settings); } -} \ No newline at end of file +} diff --git a/core/services/user/UserManageService.php b/core/services/user/UserManageService.php index d710103..0cdd6c2 100644 --- a/core/services/user/UserManageService.php +++ b/core/services/user/UserManageService.php @@ -19,13 +19,13 @@ class UserManageService */ //private $newsletter; - /** - * UserManageService constructor. - * - * @param UserRepository $repository - * @param RoleManager $roles - * @param TransactionManager $transaction - */ + /** + * UserManageService constructor. + * + * @param UserRepository $repository + * @param RoleManager $roles + * @param TransactionManager $transaction + */ public function __construct( UserRepository $repository, RoleManager $roles, @@ -33,8 +33,8 @@ class UserManageService //Newsletter $newsletter ) { - $this->repository = $repository; - $this->roles = $roles; + $this->repository = $repository; + $this->roles = $roles; $this->transaction = $transaction; //$this->newsletter = $newsletter; } @@ -51,6 +51,7 @@ class UserManageService $this->roles->assign($user->id, $form->role); //$this->newsletter->subscribe($user->email); }); + return $user; } @@ -70,11 +71,11 @@ class UserManageService public function setBackendLanguage($language): void { - if (in_array($language, array_keys(\Yii::$app->params['backendTranslatedLanguages']))) { - $user = $this->repository->get(\Yii::$app->user->id); - $user->backend_language = $language; - $this->repository->save($user); - } + if (in_array($language, array_keys(\Yii::$app->params['backendTranslatedLanguages']))) { + $user = $this->repository->get(\Yii::$app->user->id); + $user->backend_language = $language; + $this->repository->save($user); + } } public function assignRole($id, $role): void diff --git a/frontend/config/main.php b/frontend/config/main.php index cd2679f..d1dbb2f 100644 --- a/frontend/config/main.php +++ b/frontend/config/main.php @@ -7,121 +7,121 @@ $params = array_merge( ); return [ - 'id' => 'app-frontend', - 'language' => 'ru', - 'basePath' => dirname(__DIR__), - 'bootstrap' => [ - 'log', - 'common\bootstrap\SetUp', - 'frontend\bootstrap\SetUp', + 'id' => 'app-frontend', + 'language' => 'ru', + 'basePath' => dirname(__DIR__), + 'bootstrap' => [ + 'log', + 'common\bootstrap\SetUp', + 'frontend\bootstrap\SetUp', ], - 'aliases' => [ - '@staticRoot' => $params['staticPath'], - '@static' => $params['staticHostInfo'], + 'aliases' => [ + '@staticRoot' => $params['staticPath'], + '@static' => $params['staticHostInfo'], ], 'controllerNamespace' => 'frontend\controllers', - 'components' => [ - 'request' => [ - 'baseUrl' => '', - 'csrfParam' => '_csrf-frontend', - 'cookieValidationKey' => $params['cookieValidationKey'], - ], - 'user' => [ - 'identityClass' => 'common\auth\Identity', - 'enableAutoLogin' => true, - 'identityCookie' => ['name' => '_identity', 'httpOnly' => true, 'domain' => $params['cookieDomain']], - 'loginUrl' => ['auth/auth/login'], - ], - 'session' => [ - 'name' => '_session', - 'class' => 'yii\web\DbSession', - 'writeCallback' => function($session){ - return [ - 'user_id' => Yii::$app->user->id - ]; - }, - 'cookieParams' => [ - 'domain' => $params['cookieDomain'], - 'httpOnly' => true, - ], - ], - 'log' => [ + 'components' => [ + 'request' => [ + 'baseUrl' => '', + 'csrfParam' => '_csrf-frontend', + 'cookieValidationKey' => $params['cookieValidationKey'], + ], + 'user' => [ + 'identityClass' => 'common\auth\Identity', + 'enableAutoLogin' => true, + 'identityCookie' => ['name' => '_identity', 'httpOnly' => true, 'domain' => $params['cookieDomain']], + 'loginUrl' => ['auth/auth/login'], + ], + 'session' => [ + 'name' => '_session', + 'class' => 'yii\web\DbSession', + 'writeCallback' => function ($session) { + return [ + 'user_id' => Yii::$app->user->id + ]; + }, + 'cookieParams' => [ + 'domain' => $params['cookieDomain'], + 'httpOnly' => true, + ], + ], + 'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, - 'targets' => [ + 'targets' => [ [ - 'class' => 'yii\log\FileTarget', + 'class' => 'yii\log\FileTarget', 'levels' => ['error', 'warning'], ], ], ], - 'errorHandler' => [ + 'errorHandler' => [ 'errorAction' => 'site/error', ], - 'backendUrlManager' => require __DIR__ . '/../../backend/config/urlManager.php', - 'frontendUrlManager' => require __DIR__ . '/urlManager.php', - 'urlManager' => function () { - return Yii::$app->get('frontendUrlManager'); - }, - 'i18n' => [ - 'translations' => [ - 'post' => [ - 'class' => 'yii\i18n\PhpMessageSource', - 'basePath' => '@frontend/messages', - ], - 'slider' => [ - 'class' => 'yii\i18n\PhpMessageSource', - 'basePath' => '@frontend/messages', - ], - 'auth' => [ - 'class' => 'yii\i18n\PhpMessageSource', - 'basePath' => '@frontend/messages', - ], - 'user' => [ - 'class' => 'yii\i18n\PhpMessageSource', - 'basePath' => '@frontend/messages', - ], - 'main' => [ - 'class' => 'yii\i18n\PhpMessageSource', - 'basePath' => '@frontend/messages', - ], - ], - ], - 'view' => [ - 'theme' => [ - 'basePath' => '@webroot/themes/sport', - 'baseUrl' => '@web/themes/sport', - 'pathMap' => [ - '@frontend/views' => '@webroot/themes/sport', - '@frontend/widgets' => '@webroot/themes/sport/widgets', - ], - ], - ], - 'assetManager' => [ - 'bundles' => [ - 'yii\web\JqueryAsset' => [ - 'sourcePath' => '@frontend/assets/libs/jquery321', // do not publish the bundle - 'js' => [ - YII_ENV_DEV ? 'jquery-3.2.1.js' : 'jquery-3.2.1.min.js' - ], - ], - 'yii\bootstrap\BootstrapAsset' => [ - 'sourcePath' => '@frontend/assets/libs/bootstrap4/css', // do not publish the bundle - 'css' => [ - YII_ENV_DEV ? 'bootstrap.css' : 'bootstrap.min.css' - ], - ], - 'yii\bootstrap\BootstrapPluginAsset' => [ - 'sourcePath' => '@frontend/assets/libs/bootstrap4/js', // do not publish the bundle - 'js' => [ - YII_ENV_DEV ? 'bootstrap.js' : 'bootstrap.min.js' - ], - 'depends' => [ - 'yii\web\JqueryAsset', - 'yii\bootstrap\BootstrapAsset', - ], - ], - ], - ], + 'backendUrlManager' => require __DIR__ . '/../../backend/config/urlManager.php', + 'frontendUrlManager' => require __DIR__ . '/urlManager.php', + 'urlManager' => function () { + return Yii::$app->get('frontendUrlManager'); + }, + 'i18n' => [ + 'translations' => [ + 'post' => [ + 'class' => 'yii\i18n\PhpMessageSource', + 'basePath' => '@frontend/messages', + ], + 'slider' => [ + 'class' => 'yii\i18n\PhpMessageSource', + 'basePath' => '@frontend/messages', + ], + 'auth' => [ + 'class' => 'yii\i18n\PhpMessageSource', + 'basePath' => '@frontend/messages', + ], + 'user' => [ + 'class' => 'yii\i18n\PhpMessageSource', + 'basePath' => '@frontend/messages', + ], + 'main' => [ + 'class' => 'yii\i18n\PhpMessageSource', + 'basePath' => '@frontend/messages', + ], + ], + ], + 'view' => [ + 'theme' => [ + 'basePath' => '@webroot/themes/sport', + 'baseUrl' => '@web/themes/sport', + 'pathMap' => [ + '@frontend/views' => '@webroot/themes/sport', + '@frontend/widgets' => '@webroot/themes/sport/widgets', + ], + ], + ], + 'assetManager' => [ + 'bundles' => [ + 'yii\web\JqueryAsset' => [ + 'sourcePath' => '@frontend/assets/libs/jquery321', // do not publish the bundle + 'js' => [ + YII_ENV_DEV ? 'jquery-3.2.1.js' : 'jquery-3.2.1.min.js' + ], + ], + 'yii\bootstrap\BootstrapAsset' => [ + 'sourcePath' => '@frontend/assets/libs/bootstrap4/css', // do not publish the bundle + 'css' => [ + YII_ENV_DEV ? 'bootstrap.css' : 'bootstrap.min.css' + ], + ], + 'yii\bootstrap\BootstrapPluginAsset' => [ + 'sourcePath' => '@frontend/assets/libs/bootstrap4/js', // do not publish the bundle + 'js' => [ + YII_ENV_DEV ? 'bootstrap.js' : 'bootstrap.min.js' + ], + 'depends' => [ + 'yii\web\JqueryAsset', + 'yii\bootstrap\BootstrapAsset', + ], + ], + ], + ], ], - 'params' => $params, + 'params' => $params, ]; diff --git a/frontend/controllers/ContactController.php b/frontend/controllers/ContactController.php index 4a14f1c..b2a160c 100644 --- a/frontend/controllers/ContactController.php +++ b/frontend/controllers/ContactController.php @@ -1,4 +1,5 @@ service = $service; + $this->_service = $service; } public function actionIndex() @@ -23,13 +24,15 @@ class ContactController extends FrontendController $form = new ContactForm(); if ($form->load(Yii::$app->request->post()) && $form->validate()) { try { - $this->service->send($form); + $this->_service->send($form); Yii::$app->session->setFlash('success', Yii::t('main', 'Thank you for contacting us. We will respond to you as soon as possible.')); + return $this->goHome(); } catch (\Exception $e) { Yii::$app->errorHandler->logException($e); Yii::$app->session->setFlash('error', Yii::t('main', 'There was an error sending your message.')); } + return $this->refresh(); } diff --git a/frontend/controllers/SiteController.php b/frontend/controllers/SiteController.php index 3500b86..bc5bc69 100644 --- a/frontend/controllers/SiteController.php +++ b/frontend/controllers/SiteController.php @@ -1,4 +1,5 @@ newletter = $newsletter; - } + public function __construct(string $id, $module, Newsletter $newsletter, array $config = []) + { + parent::__construct($id, $module, $config); + $this->newletter = $newsletter; + } - /** + /** * @inheritdoc */ public function behaviors() @@ -29,21 +31,21 @@ class SiteController extends FrontendController return [ 'access' => [ 'class' => AccessControl::class, - 'only' => ['subscribe'], + 'only' => ['subscribe'], 'rules' => [ - [ - 'actions' => ['subscribe'], - 'allow' => true, - ], + [ + 'actions' => ['subscribe'], + 'allow' => true, + ], ], ], - 'verbs' => [ - 'class' => VerbFilter::class, + 'verbs' => [ + 'class' => VerbFilter::class, 'actions' => [ 'logout' => ['post'], ], ], - SiteAccess::class, + SiteAccess::class, ]; } @@ -53,13 +55,13 @@ class SiteController extends FrontendController public function actions() { return [ - 'error' => [ + 'error' => [ 'class' => 'yii\web\ErrorAction', ], 'captcha' => [ - 'class' => 'yii\captcha\CaptchaAction', + 'class' => 'yii\captcha\CaptchaAction', 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, - 'transparent' => true, + 'transparent' => true, ], ]; } @@ -71,24 +73,25 @@ class SiteController extends FrontendController */ public function actionIndex() { - $this->layout = 'home'; + $this->layout = 'home'; + return $this->render('index'); } - public function actionSubscribe() - { - Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; - $form = new SubscribeForm(); - if(Yii::$app->request->isAjax && $form->load(Yii::$app->request->post())){ - try { - $this->newletter->subscribe( $form->email ); - return [ 'result' => 'success' ]; - } - catch (\RuntimeException $e) { - return [ 'result' => 'error', 'message' => $e->getMessage() ]; - } - } - return ['result' => 'error', 'message' => 'Request error']; - } + public function actionSubscribe() + { + Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; + $form = new SubscribeForm(); + if (Yii::$app->request->isAjax && $form->load(Yii::$app->request->post())) { + try { + $this->newletter->subscribe($form->email); + + return ['result' => 'success']; + } catch (\RuntimeException $e) { + return ['result' => 'error', 'message' => $e->getMessage()]; + } + } + return ['result' => 'error', 'message' => 'Request error']; + } } diff --git a/frontend/controllers/account/ProfileController.php b/frontend/controllers/account/ProfileController.php index 6e1d607..6dc9994 100644 --- a/frontend/controllers/account/ProfileController.php +++ b/frontend/controllers/account/ProfileController.php @@ -6,7 +6,6 @@ namespace frontend\controllers\account; - use frontend\components\FrontendController; use core\services\user\ProfileService; use core\forms\user\ProfileEditForm; @@ -17,14 +16,14 @@ use Yii; class ProfileController extends FrontendController { - public $layout = 'profile'; + public $layout = 'profile'; - private $service; + private $_service; public function __construct($id, $module, ProfileService $service, $config = []) { parent::__construct($id, $module, $config); - $this->service = $service; + $this->_service = $service; } public function behaviors(): array @@ -32,22 +31,22 @@ class ProfileController extends FrontendController return [ 'access' => [ 'class' => AccessControl::class, - 'only' => ['index'], + 'only' => ['index'], 'rules' => [ [ 'actions' => ['edit', 'social'], - 'allow' => true, - 'roles' => ['@'], + 'allow' => true, + 'roles' => ['@'], ], ], ], ]; } - /** - * @return string|\yii\web\Response - * @throws NotFoundHttpException - */ + /** + * @return string|\yii\web\Response + * @throws NotFoundHttpException + */ public function actionEdit() { $user = $this->findModel(Yii::$app->user->id); @@ -55,17 +54,19 @@ class ProfileController extends FrontendController $form = new ProfileEditForm($user); if ($form->load(Yii::$app->request->post()) && $form->validate()) { try { - $this->service->edit($user->id, $form); + $this->_service->edit($user->id, $form); Yii::$app->session->setFlash('success', Yii::t('user', 'Profile is saved.')); + return $this->redirect(['/account/profile/edit']); } catch (\DomainException $e) { Yii::$app->errorHandler->logException($e); Yii::$app->session->setFlash('error', $e->getMessage()); } } + return $this->render('edit', [ 'model' => $form, - 'user' => $user, + 'user' => $user, ]); } @@ -77,7 +78,9 @@ class ProfileController extends FrontendController /** * 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 */ @@ -89,4 +92,4 @@ class ProfileController extends FrontendController throw new NotFoundHttpException('The requested page does not exist.'); } } -} \ No newline at end of file +} diff --git a/frontend/controllers/auth/AuthController.php b/frontend/controllers/auth/AuthController.php index 6593c98..8dc3a41 100644 --- a/frontend/controllers/auth/AuthController.php +++ b/frontend/controllers/auth/AuthController.php @@ -1,4 +1,5 @@ service = $service; + $this->_service = $service; } - public function behaviors(): array - { - 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'], - ], - ], - ], - ]; - } + public function behaviors(): array + { + 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'], + ], + ], + ], + ]; + } /** * @return mixed @@ -57,8 +58,9 @@ class AuthController extends Controller $form = new LoginForm(); if ($form->load(Yii::$app->request->post()) && $form->validate()) { try { - $user = $this->service->auth($form); + $user = $this->_service->auth($form); Yii::$app->user->login(new Identity($user), $form->rememberMe ? Yii::$app->params['user.rememberMeDuration'] : 0); + return $this->goBack(); } catch (\DomainException $e) { Yii::$app->errorHandler->logException($e); diff --git a/frontend/controllers/auth/NetworkController.php b/frontend/controllers/auth/NetworkController.php index 54edaf7..a00c196 100644 --- a/frontend/controllers/auth/NetworkController.php +++ b/frontend/controllers/auth/NetworkController.php @@ -12,12 +12,12 @@ use yii\web\Controller; class NetworkController extends Controller { - private $service; + private $_service; public function __construct($id, $module, NetworkService $service, $config = []) { parent::__construct($id, $module, $config); - $this->service = $service; + $this->_service = $service; } public function actions() @@ -37,11 +37,11 @@ class NetworkController extends Controller $identity = ArrayHelper::getValue($attributes, 'id'); try { - $user = $this->service->auth($network, $identity); + $user = $this->_service->auth($network, $identity); Yii::$app->user->login(new Identity($user), Yii::$app->params['user.rememberMeDuration']); } catch (\DomainException $e) { Yii::$app->errorHandler->logException($e); Yii::$app->session->setFlash('error', $e->getMessage()); } } -} \ No newline at end of file +} diff --git a/frontend/controllers/auth/ResetController.php b/frontend/controllers/auth/ResetController.php index 966b4bc..cc2524b 100644 --- a/frontend/controllers/auth/ResetController.php +++ b/frontend/controllers/auth/ResetController.php @@ -12,12 +12,12 @@ class ResetController extends Controller { public $layout = 'auth'; - private $service; + private $_service; public function __construct($id, $module, PasswordResetService $service, $config = []) { parent::__construct($id, $module, $config); - $this->service = $service; + $this->_service = $service; } /** @@ -28,7 +28,7 @@ class ResetController extends Controller $form = new PasswordResetRequestForm(); if ($form->load(Yii::$app->request->post()) && $form->validate()) { try { - $this->service->request($form); + $this->_service->request($form); Yii::$app->session->setFlash('success', Yii::t('auth', 'Check your email for further instructions.')); return $this->goHome(); } catch (\DomainException $e) { @@ -50,7 +50,7 @@ class ResetController extends Controller public function actionConfirm($token) { try { - $this->service->validateToken($token); + $this->_service->validateToken($token); } catch (\DomainException $e) { throw new BadRequestHttpException($e->getMessage()); } @@ -58,7 +58,7 @@ class ResetController extends Controller $form = new ResetPasswordForm(); if ($form->load(Yii::$app->request->post()) && $form->validate()) { try { - $this->service->reset($token, $form); + $this->_service->reset($token, $form); Yii::$app->session->setFlash('success', Yii::t('auth', 'New password saved.')); } catch (\DomainException $e) { Yii::$app->errorHandler->logException($e); diff --git a/frontend/controllers/auth/SignupController.php b/frontend/controllers/auth/SignupController.php index 9fec081..5c1416a 100644 --- a/frontend/controllers/auth/SignupController.php +++ b/frontend/controllers/auth/SignupController.php @@ -1,4 +1,5 @@ service = $service; + $this->_service = $service; } public function behaviors(): array { return [ 'access' => [ - 'class' => AccessControl::className(), + 'class' => AccessControl::class, //'only' => ['index'], 'rules' => [ [ 'actions' => ['request', 'confirm'], - 'allow' => true, - 'roles' => ['?'], + 'allow' => true, + 'roles' => ['?'], ], ], ], @@ -44,10 +45,11 @@ class SignupController extends Controller $form = new SignupForm(); if ($form->load(Yii::$app->request->post()) && $form->validate()) { try { - $this->service->signup($form); + $this->_service->signup($form); Yii::$app->session->setFlash('success', Yii::t('auth', 'Check your email for further instructions.')); + //return $this->goHome(); - return $this->redirect(['auth/auth/login']); + return $this->redirect(['auth/auth/login']); } catch (\DomainException $e) { Yii::$app->errorHandler->logException($e); Yii::$app->session->setFlash('error', $e->getMessage()); @@ -61,18 +63,21 @@ class SignupController extends Controller /** * @param $token + * * @return mixed */ public function actionConfirm($token) { try { - $this->service->confirm($token); + $this->_service->confirm($token); Yii::$app->session->setFlash('success', Yii::t('auth', 'Your email is confirmed.')); + return $this->redirect(['auth/auth/login']); } catch (\DomainException $e) { Yii::$app->errorHandler->logException($e); Yii::$app->session->setFlash('error', $e->getMessage()); } + return $this->goHome(); } } diff --git a/setup.php b/setup.php index 55e5abc..478a478 100644 --- a/setup.php +++ b/setup.php @@ -60,6 +60,10 @@ class Setup 'Admin account complete' => 'Аккаунт администратора создан', 'Prepare MySQL tables' => 'Подготовка MySQL таблиц', 'Complete' => 'Готово', + 'Server HTTP configuration' => 'Конфигурация HTTP сервера', + 'Does your server use Apache? [y]: ' => 'Ваш сервер использует Apache? [y]: ', + 'Creating permissions' => 'Создание разрешений', + 'Activating modules' => 'Активация модулей', ], ]; @@ -95,6 +99,16 @@ class Setup // setup domain data $this->setConfigDomains(); + // install modules + $this->activateSystemModules(); + + // install system permissions + $this->addPermissions(); + + + // apache htaccess + $this->apache(); + echo '---------------------' . PHP_EOL; echo Console::log($this->l('Installation complete'), 'yellow') . PHP_EOL; echo PHP_EOL; @@ -104,7 +118,7 @@ class Setup echo '---------------------' . PHP_EOL; } - private function selectType() + private function selectType() : void { echo '---------------------' . PHP_EOL; echo Console::log($this->l('Select initialization type'), 'white') . PHP_EOL; @@ -114,7 +128,7 @@ class Setup $this->_type = readline($this->l('Type [p]: ')) ?: 'p'; } - private function selectLanguage() + private function selectLanguage() : void { echo '---------------------' . PHP_EOL; echo Console::log($this->l('Select installer language'), 'white') . PHP_EOL; @@ -124,7 +138,7 @@ class Setup $this->_language = readline($this->l('Language [en]: ')) ?: 'en'; } - private function setupMySQL(): bool + private function setupMySQL() : bool { echo '---------------------' . PHP_EOL; echo Console::log($this->l('MySQL settings'), 'white') . PHP_EOL; @@ -141,7 +155,7 @@ class Setup return $this->checkDatabaseConnection(); } - private function checkDatabaseConnection(): bool + private function checkDatabaseConnection() : bool { try { $this->_db_connection = new \PDO('mysql:host=' . $this->_db_host . ';dbname=' . $this->_db_name, $this->_db_user, $this->_db_pass); @@ -152,7 +166,7 @@ class Setup } } - private function setConfigMySQL() + private function setConfigMySQL() : void { $mysql_line = 'mysql:host=' . $this->_db_host . ';dbname=' . $this->_db_name; $file = __DIR__ . '/common/config/main-local.php'; @@ -163,7 +177,7 @@ class Setup file_put_contents($file, $content); } - private function setConfigDomains() + private function setConfigDomains() : void { echo '---------------------' . PHP_EOL; echo Console::log($this->l('Set your HTTP protocol (http/https)'), 'white') . PHP_EOL; @@ -196,7 +210,7 @@ class Setup file_put_contents($file, $content); } - private function addAdmin(): void + private function addAdmin() : void { echo '---------------------' . PHP_EOL; echo Console::log($this->l('Create admin account'), 'white') . PHP_EOL; @@ -227,13 +241,13 @@ class Setup echo Console::log($this->l('Admin account complete'), 'green') . PHP_EOL; } - private function trueEmail($email): bool + private function trueEmail($email) : bool { $email = filter_var($email, FILTER_VALIDATE_EMAIL); return $email ? true : false; } - private function runMigrations(): void + private function runMigrations() : void { echo '---------------------' . PHP_EOL; echo Console::log($this->l('Prepare MySQL tables'), 'white') . PHP_EOL; @@ -242,6 +256,141 @@ class Setup echo Console::log($this->l('Complete'), 'green') . PHP_EOL; } + private function apache() : void + { + echo '---------------------' . PHP_EOL; + echo Console::log($this->l('Server HTTP configuration'), 'white') . PHP_EOL; + echo '---------------------' . PHP_EOL; + $apache = readline($this->l('Does your server use Apache? [yes]: ')) ?: 'y'; + if ($apache == 'y' || $apache == 'yes') { + $this->prepareHtaccess(); + } + } + + private function prepareHtaccess() : void + { + // main +$ssh_rules = <<_domain}%{REQUEST_URI} [L,R=301] + +RewriteCond %{HTTPS} on +RewriteCond %{HTTP_HOST} ^(www\.)+(.*)$ [NC] +RewriteRule (.*) https://{$this->_domain}%{REQUEST_URI} [L,R=301] +SSH; + + $ssh_rules = $this->_http_protocol == 'https' ? $ssh_rules : ''; + +$main_htacces = << + Options +FollowSymlinks + RewriteEngine On + + + + + {$ssh_rules} + + #static + RewriteCond %{HTTP_HOST} ^static.{$this->_domain} + RewriteRule ^(.*)$ zxcms/static/$1 [L] + + #RewriteCond %{HTTP_HOST} ^static.{$this->_domain} + #RewriteRule ^(.*)$ $1 [L] + + RewriteCond %{HTTP_HOST} ^admin.{$this->_domain} + RewriteRule ^(.*)$ zxcms/backend/web/$1 [L] + + # if /admin - backend + RewriteCond %{HTTP_HOST} ^admin.{$this->_domain} + RewriteRule ^assets/(.*)$ zxcms/backend/web/assets/$1 [L] + + RewriteCond %{HTTP_HOST} ^admin.{$this->_domain} + RewriteRule ^css/(.*)$ zxcms/backend/web/css/$1 [L] + + RewriteCond %{HTTP_HOST} ^admin.{$this->_domain} + RewriteRule ^js/(.*)$ zxcms/backend/web/js/$1 [L] + + RewriteCond %{REQUEST_URI} !^/zxcms/backend/web/(assets|js|css)/ + RewriteCond %{HTTP_HOST} ^admin.{$this->_domain} + RewriteRule ^.*$ zxcms/backend/web/index.php [L] + + #RewriteCond %{REQUEST_URI} !^/zxcms/static + RewriteCond %{REQUEST_URI} ^/(assets|css|js|images) + RewriteRule ^assets/(.*)$ zxcms/frontend/web/assets/$1 [L] + RewriteRule ^css/(.*)$ zxcms/frontend/web/css/$1 [L] + RewriteRule ^js/(.*)$ zxcms/frontend/web/js/$1 [L] + RewriteRule ^images/(.*)$ zxcms/frontend/web/images/$1 [L] + RewriteRule ^(.*)$ zxcms/frontend/web/$1 [L] + + #RewriteCond %{REQUEST_URI} !^/zxcms/static + RewriteCond %{REQUEST_URI} !^/zxcms/(frontend|backend)/web/(assets|css|js)/ + RewriteCond %{REQUEST_URI} !index.php + RewriteCond %{REQUEST_FILENAME} !-f [OR] + RewriteCond %{REQUEST_FILENAME} !-d + RewriteRule ^.*$ zxcms/frontend/web/index.php + +MH; + + file_put_contents(__DIR__ . '/../.htaccess', $main_htacces); + + // backend, frontend +$bf_htaccess = <<_http_protocol}://{$this->_domain}/site/error [R=404,L] + +ErrorDocument 404 {$this->_http_protocol}://{$this->_domain}/site/error +SH; + file_put_contents(__DIR__ . '/frontend/web/.htaccess', $static_htaccess); + } + + private function addPermissions() : void + { + echo '---------------------' . PHP_EOL; + echo Console::log($this->l('Creating permissions'), 'white') . PHP_EOL; + echo '---------------------' . PHP_EOL; + $systemPermissions = [ + 'SettingsManagement' => 'Settings Management', + 'MenuManagement' => 'Menu Management', + 'ModuleManagement' => 'Modules Management', + 'UserManagement' => 'Users Management', + 'Dashboard' => 'Dashboard', + 'SliderManagement' => 'Slider Management', + ]; + foreach ($systemPermissions as $name => $description) { + shell_exec('php ' . __DIR__ . '/yii permission/add "' . $name . '" "' . $description . '"'); + } + echo Console::log($this->l('Complete'), 'green') . PHP_EOL; + } + + private function activateSystemModules(): void + { + Console::log($this->l('Activating modules: '), 'white'); + $systemModules = [ + 'languages', 'pages', 'forms', 'links' + ]; + foreach ($systemModules as $name) { + shell_exec('php ' . __DIR__ . '/yii module/add "' . $name . '"'); + } + echo Console::log($this->l('Complete'), 'green') . PHP_EOL; + } + private function l($str): string { return isset($this->_l[$this->_language]) && isset($this->_l[$this->_language][$str]) ? $this->_l[$this->_language][$str] : $str;