Skip to content
This repository was archived by the owner on Sep 19, 2023. It is now read-only.

Commit e715d97

Browse files
authored
Merge pull request #43 from justcoded/develop
removed base module, added app key generator
2 parents 49c3ad7 + 03948a7 commit e715d97

28 files changed

+282
-226
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
APP_ENV=dev
22
APP_DEBUG=true
3-
APP_KEY=wUZvVVKJyHFGDB9qK_Lop4QE1vwb4bYU
3+
APP_KEY=
44

55
DB_HOST=127.0.0.1
66
DB_NAME=yii2_starter

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ CHANGELOG for Yii2 STARTER PROJECT TEMPLATE
33

44
*should be replaced with real project changelog later*
55

6+
Develop
7+
---------------------
8+
* Added APP_KEY hash generator console command.
9+
* Base module has been removed
10+
611
v0.9.6
712
---------------------
813
* Fix register page, controller missed use statement for RegisterForm.

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ APP_DEBUG=true
9999
APP_KEY=wUZvVVKJyHFGDB9qK_Lop4QE1vwb4bYU
100100
```
101101

102-
*`APP_KEY` is used as cookie verification key. Unfortunately there are no post install composer script to generate it automatically*
102+
*`APP_KEY` is used as CSRF token (cookie verification key). In order to set or change it, run:
103+
104+
```bash
105+
php yii security/app-key
106+
```
103107

104108
### Database
105109

app/console/Controller.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace app\console;
44

5-
5+
use yii\console\ExitCode;
66
use yii\helpers\Console;
77

88
abstract class Controller extends \yii\console\Controller
@@ -34,22 +34,25 @@ protected function line($string = '')
3434
* Prints a yellow line to STDOUT.
3535
*
3636
* @param string $string the string to print
37-
* @return int|bool Number of bytes printed or false on error
37+
* @return int Exit code status
3838
*/
3939
protected function warning($string)
4040
{
41-
return $this->line($string, Console::FG_YELLOW);
41+
$this->line($string, Console::FG_YELLOW);
42+
43+
return ExitCode::TEMPFAIL;
4244
}
4345

4446
/**
4547
* Prints a yellow line to STDOUT.
4648
*
4749
* @param string $string the string to print
48-
* @return int|bool Number of bytes printed or false on error
50+
* @return int Exit code status
4951
*/
5052
protected function success($string)
5153
{
52-
return $this->line($string, Console::FG_GREEN);
53-
}
54+
$this->line($string, Console::FG_GREEN);
5455

55-
}
56+
return ExitCode::OK;
57+
}
58+
}

app/console/SecurityController.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace app\console;
4+
5+
use Yii;
6+
use yii\base\Exception;
7+
8+
/**
9+
* Class GenerateController
10+
*
11+
* @package app\console\controllers
12+
*/
13+
class SecurityController extends Controller
14+
{
15+
/**
16+
* Action Generate
17+
*
18+
* @return bool|int
19+
* @throws Exception
20+
*/
21+
public function actionAppKey()
22+
{
23+
$key = Yii::$app->security->generateRandomString();
24+
$envPath = Yii::getAlias('@srcPath/.env');
25+
26+
if (file_exists($envPath)) {
27+
$envData = file_get_contents($envPath);
28+
29+
if (! preg_match('/APP_KEY=(\w+)?/', $envData)) {
30+
return $this->warning('No APP_KEY has been found in your .env file');
31+
}
32+
33+
$updatedEnvData = preg_replace('/APP_KEY=(\w+)?/', "APP_KEY={$key}", $envData);
34+
if (! $updatedEnvData) {
35+
return $this->stderr('Error during the app key setup');
36+
}
37+
38+
file_put_contents($envPath, $updatedEnvData);
39+
40+
return $this->success('The application key has been successfully set');
41+
}
42+
43+
return $this->warning('The .env file not found. Do you need to create one?');
44+
}
45+
}

app/modules/admin/forms/UserForm.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class UserForm extends User
2929
public function rules()
3030
{
3131
return array_merge(parent::rules(), [
32-
['password', 'safe'],
32+
[['password', 'passwordRepeat'], 'string'],
3333
[
3434
'passwordRepeat',
3535
'required',
@@ -53,11 +53,19 @@ public function beforeSave($insert)
5353
$this->setPassword($this->password);
5454
}
5555

56-
$this->updateUserRoles($this->roles);
57-
5856
return parent::beforeSave($insert);
5957
}
60-
58+
59+
/**
60+
* @inheritDoc
61+
*/
62+
public function afterSave($insert, $changedAttributes)
63+
{
64+
parent::afterSave($insert, $changedAttributes);
65+
66+
$this->updateUserRoles($this->roles);
67+
}
68+
6169
/**
6270
* @inheritdoc
6371
*/

app/modules/base/console/MigrateController.php

Lines changed: 0 additions & 108 deletions
This file was deleted.

app/modules/base/db/Migration.php renamed to app/traits/migrations/CreateTableOptions.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
2-
namespace app\modules\base\db;
2+
namespace app\traits\migrations;
33

4-
class Migration extends \yii\db\Migration
4+
trait CreateTableOptions
55
{
66
/**
77
* Prepare table options based on DB driver
88
*
99
* @return null|string
1010
*/
11-
protected function tableOptions()
11+
protected function createTableOptions()
1212
{
1313
$tableOptions = null;
1414
if ($this->db->driverName === 'mysql') {

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"prefer-stable": true,
99
"require": {
1010
"php": ">=7.0",
11-
"yiisoft/yii2": "~2.0.12",
11+
"yiisoft/yii2": "~2.0.22",
1212
"yiisoft/yii2-bootstrap": "~2.0.0",
1313
"yiisoft/yii2-swiftmailer": "~2.0.0",
1414
"yiisoft/yii2-faker": "~2.0.0",
@@ -51,12 +51,12 @@
5151
"yii\\composer\\Installer::postInstall",
5252
"@php tests/_phpstorm.php"
5353
],
54-
"post-update-cmd": [
55-
"@php tests/_phpstorm.php"
56-
],
5754
"post-create-project-cmd": [
5855
"yii\\composer\\Installer::postCreateProject",
59-
"yii\\composer\\Installer::postInstall"
56+
"yii\\composer\\Installer::postInstall",
57+
"cp -n .env.example .env",
58+
"cp -n public/.htaccess.example public/.htaccess",
59+
"php yii security/app-key"
6060
]
6161
},
6262
"extra": {

config/app-console.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
11
<?php
22

3-
\Yii::setAlias('@app', dirname(__DIR__) . '/app');
4-
\Yii::setAlias('@webroot', dirname(__DIR__) . '/public');
3+
\Yii::setAlias('@srcPath', dirname(__DIR__));
4+
\Yii::setAlias('@app', '@srcPath/app');
5+
\Yii::setAlias('@webroot', '@srcPath/public');
56

67
$db = require __DIR__ . '/db.php';
78
$settings = require __DIR__ . '/settings.php';
89

910
$config = [
10-
'id' => 'main-console',
11-
'basePath' => dirname(__DIR__) . '/app',
12-
'runtimePath' => dirname(__DIR__) . '/runtime',
13-
'vendorPath' => dirname(__DIR__) . '/vendor',
11+
'id' => 'main-console',
12+
'basePath' => '@app',
13+
'runtimePath' => '@srcPath/runtime',
14+
'vendorPath' => '@srcPath/vendor',
1415
'controllerNamespace' => 'app\\console',
1516
'bootstrap' => ['log', 'settings'],
1617
'aliases' => [
17-
'@config'=> dirname(__DIR__) . '/config',
18-
'@migrations' => dirname(__DIR__) . '/database/migrations',
19-
'@fixtures' => dirname(__DIR__) . '/database/fixtures',
18+
'@config'=> '@srcPath/config',
19+
'@migrations' => '@srcPath/database/migrations',
20+
'@fixtures' => '@srcPath/database/fixtures',
2021
'@app/fixtures' => '@fixtures',
2122
],
2223
'controllerMap' => [
2324
'migrate' => [
24-
'class' => \app\modules\base\console\MigrateController::class,
25+
'class' => \yii\console\controllers\MigrateController::class,
26+
'templateFile' => '@migrations/templates/migration.php',
27+
'generatorTemplateFiles' => [
28+
'create_table' => '@migrations/templates/createTableMigration.php',
29+
'drop_table' => '@migrations/templates/dropTableMigration.php',
30+
'add_column' => '@migrations/templates/addColumnMigration.php',
31+
'drop_column' => '@migrations/templates/dropColumnMigration.php',
32+
'create_junction' => '@migrations/templates/createTableMigration.php',
33+
],
2534
'migrationPath' => [
2635
'@migrations',
2736
'@yii/rbac/migrations',

0 commit comments

Comments
 (0)