1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\components; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @link http://www.diemeisterei.de/ |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) 2015 diemeisterei GmbH, Stuttgart |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
use dektrium\user\models\User as UserModel; |
15
|
|
|
use yii\base\Component; |
16
|
|
|
use yii\helpers\Html; |
17
|
|
|
|
18
|
|
|
class Helper extends Component |
19
|
|
|
{ |
20
|
|
|
public static function checkApplication() |
21
|
|
|
{ |
22
|
|
|
if (\Yii::$app->user->can('Admin')) { |
23
|
|
|
self::checkPassword(getenv('APP_ADMIN_PASSWORD')); |
24
|
|
|
self::checkUserSetup(); |
25
|
|
|
self::checkPagesSetup(); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
View Code Duplication |
private static function checkUserSetup() |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
if (UserModel::find()->where('id != 1')->count() == 0) { |
32
|
|
|
$link = Html::a('user module', ['/user/admin/create']); |
33
|
|
|
\Yii::$app->session->addFlash( |
34
|
|
|
'warning', |
35
|
|
|
"There is no additional user registered, visit {$link} to create an editor." |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
View Code Duplication |
private static function checkPagesSetup() |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
if (!\Yii::$app->getModule('pages')->getLocalizedRootNode()) { |
43
|
|
|
$link = Html::a('pages module', ['/pages']); |
44
|
|
|
\Yii::$app->session->addFlash( |
45
|
|
|
'warning', |
46
|
|
|
"There is no navigation root node, visit {$link} to create a root node." |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Password check |
53
|
|
|
* Based upon http://stackoverflow.com/a/10753064 |
54
|
|
|
* |
55
|
|
|
* @param $pwd |
56
|
|
|
*/ |
57
|
|
|
private static function checkPassword($pwd) |
58
|
|
|
{ |
59
|
|
|
$errors = []; |
60
|
|
|
|
61
|
|
|
if (strlen($pwd) < 8) { |
62
|
|
|
$errors[] = "Password too short!"; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (!preg_match("#[0-9]+#", $pwd)) { |
66
|
|
|
$errors[] = "Password must include at least one number!"; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (!preg_match("#[a-zA-Z]+#", $pwd)) { |
70
|
|
|
$errors[] = "Password must include at least one letter!"; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (count($errors) > 0) { |
74
|
|
|
$msg = implode('<br/>', $errors); |
75
|
|
|
\Yii::$app->session->addFlash( |
76
|
|
|
'danger', |
77
|
|
|
"Application admin password from environment setting is not strong enough.<br/><i>{$msg}</i>" |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.