Skip to content

Commit

Permalink
'删除多余扩展,新增后台登录权限'
Browse files Browse the repository at this point in the history
  • Loading branch information
yidashi committed Nov 8, 2016
1 parent 35038bc commit 43b6dfa
Show file tree
Hide file tree
Showing 13 changed files with 262 additions and 271 deletions.
12 changes: 1 addition & 11 deletions backend/config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,6 @@
'migration' => [
'class' => 'migration\Module',
],
'user' => [
'defaultRoute' => 'admin',
'controllerMap' => [
'security' => [
'class' => 'common\modules\user\controllers\SecurityController',
'layout' => '@backend/views/layouts/main-login',
'viewPath' => '@backend/views/site'
]
],
],
'config' => [
'class' => 'config\Module'
]
Expand All @@ -123,7 +113,7 @@
'as access' => [
'class' => 'rbac\components\AccessControl',
'allowActions' => [
'user/security/logout'
'user/admin/logout'
],
],
'as adminLog' => 'backend\\behaviors\\AdminLogBehavior',
Expand Down
4 changes: 2 additions & 2 deletions backend/views/layouts/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ class="dropdown-toggle" data-toggle="dropdown"> <img
<?= Html::a('修改密码', ['/user/admin/reset-password', 'id' => Yii::$app->user->id], ['class' => 'btn btn-default btn-flat'])?>
</div>
<div class="pull-right">
<?= Html::a('登出', ['/user/security/logout' ], ['data-method' => 'post', 'class' => 'btn btn-default btn-flat'])?>
<?= Html::a('登出', ['/user/admin/logout' ], ['data-method' => 'post', 'class' => 'btn btn-default btn-flat'])?>
</div>
</li>
</ul>
</li>
<li><?= Html::a('<i class="fa fa-sign-out"></i>', ['/user/security/logout'], ['data-method' => 'post']) ?></li>
<li><?= Html::a('<i class="fa fa-sign-out"></i>', ['/user/admin/logout'], ['data-method' => 'post']) ?></li>
</ul>
</div>
<div class="navbar-header">
Expand Down
53 changes: 29 additions & 24 deletions common/modules/config/models/DatabaseConfigForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,8 @@ class DatabaseConfigForm extends Model
public function rules()
{
return [
[
[
'hostname',
'username',
'database'
],
'required'
],
[
[
'hostname',
'username',
'database',
"password"
],
'checkDb'
]
[['hostname', 'username', 'database'], 'required'],
[['hostname', 'username', 'database', 'password'], 'checkDb']
];
}

Expand Down Expand Up @@ -79,10 +64,10 @@ public function checkDb($attribute, $params)
public function attributeLabels()
{
return [
'hostname' => 'Hostname',
'username' => 'Username',
'password' => 'Password',
'database' => 'Name of Database'
'hostname' => '数据库地址',
'username' => '数据库用户名',
'password' => '数据库密码',
'database' => '数据库名字'
];
}

Expand All @@ -103,10 +88,30 @@ public function save($runValidation = true, $attributeNames = null)
}

$config = $this->getConfig();
$config->setEnv('DB_DSN', "mysql:host=" . $this->hostname . ";dbname=" . $this->database.";port=3306");
$config->setEnv('DB_USERNAME', $this->username);
$config->setEnv('DB_PASSWORD', $this->password);
$config->set('DB_DSN', "mysql:host=" . $this->hostname . ";dbname=" . $this->database.";port=3306");
$config->set('DB_USERNAME', $this->username);
$config->set('DB_PASSWORD', $this->password);

return true;
}

// 不用env的用这个方法
public function save2($runValidation = true, $attributeNames = null)
{
if ($runValidation && ! $this->validate($attributeNames)) {
return false;
}

$config = $this->getConfig();
$db = [];
$db['class'] = 'yii\db\Connection';
$db['dsn'] = "mysql:host=" . $this->hostname . ";dbname=" . $this->database.";port=3306";
$db['username'] = $this->username;
$db['password'] = $this->password;
$localConfig = $config->getConfigFromLocal();
$localConfig['components']['db'] = $db;
$config->setConfigToLocal($localConfig);
Yii::$app->set('db', $db);
return true;
}
}
50 changes: 9 additions & 41 deletions common/modules/config/models/MailConfigForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,54 +21,22 @@ public function rules()
{
return [
// Host
[
'mailHost',
'required'
],
[
'mailHost',
'string',
'max' => 255
],

['mailHost', 'required'],
['mailHost', 'string', 'max' => 255],
// Username
[
'mailUsername',
'required'
],
[
'mailUsername',
'string',
'max' => 255
],
['mailUsername', 'required'],
['mailUsername', 'string', 'max' => 255],

// Password
[
'mailPassword',
'required'
],
[
'mailPassword',
'string',
'max' => 255
],
['mailPassword', 'required'],
['mailPassword', 'string', 'max' => 255],

// Port
[
'mailPort',
'required'
],
[
'mailPort',
'integer'
],
['mailPort', 'required'],
['mailPort', 'integer'],

// Encryption
[
'mailEncryption',
'string',
'max' => 10
],
['mailEncryption', 'string', 'max' => 10],
];
}

Expand Down
45 changes: 27 additions & 18 deletions common/modules/user/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@ class Module extends \yii\base\Module implements BootstrapInterface

public $urlPrefix = 'user';

public $urlRules = [
'<id:\d+>' => 'default/index',
'<action:(login|logout)>' => 'security/<action>',
'<action:(signup)>' => 'registration/<action>',
'<action:(up|article-list|create-article|update-article|notice|favourite)>' => 'default/<action>',
];
public $urlRules = [];

public function init()
{
Expand All @@ -62,27 +57,41 @@ public function init()

public function bootstrap($app)
{
Yii::$app->set('user', [
'class' => 'yii\web\User',
'identityClass' => 'common\modules\user\models\User',
'loginUrl' => ['/user/security/login'],
'enableAutoLogin' => true,
'on afterLogin' => function($event) {
$event->identity->touch('login_at');
}
]);

if ($app->id == 'app-frontend') {
$this->attachBehavior('frontend', 'common\modules\user\filters\FrontendFilter');
} elseif ($app->id == 'app-backend') {
}
if ($app->id == 'app-backend') {
$this->attachBehavior('backend', 'common\modules\user\filters\BackendFilter');
Yii::$container->set('yii\web\User', [
Yii::$app->set('user', [
'class' => 'yii\web\User',
'identityClass' => 'common\modules\user\models\User',
'loginUrl' => ['/user/admin/login'],
'enableAutoLogin' => true,
'on afterLogin' => function($event) {
$event->identity->touch('login_at');
},
'idParam' => '__idBackend',
'identityCookie' => ['name' => '_identityBackend', 'httpOnly' => true]
]);
$app->urlManager->addRules([
'user/<action:\S+>' => 'user/admin/<action>',
], false);
} else {
Yii::$app->set('user', [
'class' => 'yii\web\User',
'identityClass' => 'common\modules\user\models\User',
'loginUrl' => ['/user/security/login'],
'enableAutoLogin' => true,
'on afterLogin' => function($event) {
$event->identity->touch('login_at');
}
]);
$this->urlRules = [
'<id:\d+>' => 'default/index',
'<action:(login|logout)>' => 'security/<action>',
'<action:(signup)>' => 'registration/<action>',
'<action:(up|article-list|create-article|update-article|notice|favourite)>' => 'default/<action>',
];
}

$configUrlRule = [
Expand Down
30 changes: 30 additions & 0 deletions common/modules/user/controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace common\modules\user\controllers;


use common\modules\user\models\LoginForm;
use common\modules\user\models\Profile;
use common\modules\user\models\User;
use common\modules\user\traits\AjaxValidationTrait;
Expand Down Expand Up @@ -38,6 +39,35 @@ public function behaviors()
],
];
}
public function actionLogin()
{
$this->layout = '@common/modules/user/views/admin/main-login.php';
if (!\Yii::$app->user->isGuest) {
return $this->goHome();
}

$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->loginAdmin()) {
return $this->goBack();
} else {
if (Yii::$app->request->isAjax) {
return $this->renderAjax('login', [
'model' => $model,
]);
}

return $this->render('login', [
'model' => $model,
]);
}
}

public function actionLogout()
{
Yii::$app->user->logout();

return $this->goHome();
}
/**
* Lists all User models.
* @return mixed
Expand Down
14 changes: 14 additions & 0 deletions common/modules/user/models/LoginForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ public function login()
}
}

public function loginAdmin()
{
if ($this->validate()) {
if ($this->getUser()->getIsAdmin()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
} else {
$this->addError('username', '无权登录');
return false;
}
} else {
return false;
}
}

/**
* Finds user by [[username]].
*
Expand Down
4 changes: 3 additions & 1 deletion common/modules/user/models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public function rules()
{
return [
['username', 'required', 'on' => 'create'],
['username', 'unique', 'on' => 'create'],
['email', 'required', 'on' => 'create'],
['email', 'unique', 'on' => 'create'],
['password', 'required', 'on' => ['register']],
];
}
Expand Down Expand Up @@ -362,7 +364,7 @@ public function getIsAdmin()
{
return
(\Yii::$app->getAuthManager() && $this->module->adminPermission ?
\Yii::$app->user->can($this->module->adminPermission) : false)
Yii::$app->getAuthManager()->checkAccess($this->getId(), $this->module->adminPermission) : false)
|| in_array($this->username, $this->module->admins);
}

Expand Down
59 changes: 59 additions & 0 deletions common/modules/user/views/admin/login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;

/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \common\models\LoginForm */

$this->title = '登录';

$fieldOptions1 = [
'options' => ['class' => 'form-group has-feedback'],
'inputTemplate' => "{input}<span class='glyphicon glyphicon-envelope form-control-feedback'></span>"
];

$fieldOptions2 = [
'options' => ['class' => 'form-group has-feedback'],
'inputTemplate' => "{input}<span class='glyphicon glyphicon-lock form-control-feedback'></span>"
];
?>

<div class="login-box">
<div class="login-logo">
<a href="#"><b><?= Yii::$app->config->get('SITE_NAME') ?></b></a>
</div>
<!-- /.login-logo -->
<div class="login-box-body">
<p class="login-box-msg">登录</p>

<?php $form = ActiveForm::begin(['id' => 'login-form', 'enableClientValidation' => false]); ?>

<?= $form
->field($model, 'username', $fieldOptions1)
->label(false)
->textInput(['placeholder' => $model->getAttributeLabel('username')]) ?>

<?= $form
->field($model, 'password', $fieldOptions2)
->label(false)
->passwordInput(['placeholder' => $model->getAttributeLabel('password')]) ?>

<div class="row">
<div class="col-xs-8">
<?= $form->field($model, 'rememberMe')->checkbox() ?>
</div>
<!-- /.col -->
<div class="col-xs-4">
<?= Html::submitButton('登录', ['class' => 'btn btn-primary btn-flat btn-block btn-flat', 'name' => 'login-button']) ?>
</div>
<!-- /.col -->
</div>


<?php ActiveForm::end(); ?>


</div>
<!-- /.login-box-body -->
</div><!-- /.login-box -->
Loading

0 comments on commit 43b6dfa

Please sign in to comment.