From d93df2c49b9c6c96aa6a09d99b7a8ade2bece65a Mon Sep 17 00:00:00 2001 From: jhaniot Date: Tue, 3 Oct 2017 11:42:23 +0300 Subject: [PATCH] Enable/Disable Applications migration+behaviour --- controllers/ApplicationController.php | 14 +++++++-- .../m171003_081916_create_config_table.php | 30 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 migrations/m171003_081916_create_config_table.php diff --git a/controllers/ApplicationController.php b/controllers/ApplicationController.php index 43271a3..7aa9adb 100644 --- a/controllers/ApplicationController.php +++ b/controllers/ApplicationController.php @@ -22,6 +22,7 @@ class ApplicationController extends Controller */ public function behaviors() { + return [ 'verbs' => [ 'class' => VerbFilter::className(), @@ -33,12 +34,21 @@ public function behaviors() 'class' => AccessControl::className(), 'rules' => [ [ - 'actions' => ['my-application', 'apply', 'delete-my-application', 'my-delete'], + 'actions' => ['my-application'], 'allow' => true, 'roles' => ['@'], - 'matchCallback' => function ($rule, $action) { + 'matchCallback' => function ($rule, $action) { return false === \Yii::$app->user->identity->isAdmin(); } + ], + [ + 'actions' => ['apply', 'delete-my-application', 'my-delete'], + 'allow' => true, + 'roles' => ['@'], + 'matchCallback' => function ($rule, $action) { + return (false === \Yii::$app->user->identity->isAdmin()) && + (1 == \Yii::$app->db->createCommand('SELECT enable_applications FROM config WHERE id=1')->queryColumn()[0]); + } ], [ 'actions' => ['index', 'delete'], diff --git a/migrations/m171003_081916_create_config_table.php b/migrations/m171003_081916_create_config_table.php new file mode 100644 index 0000000..60fd95b --- /dev/null +++ b/migrations/m171003_081916_create_config_table.php @@ -0,0 +1,30 @@ +createTable('{{%config}}', [ + 'id' => $this->primaryKey(), + 'enable_applications' => $this->boolean()->notNull()->defaultValue(false) + ]); + + $this->insert('{{%config}}', ['id' => 1, 'enable_applications' => TRUE]); + } + + /** + * @inheritdoc + */ + public function safeDown() + { + $this->dropTable('config'); + } +}