Commit 8f6a9d34 authored by 杨培's avatar 杨培

管理员登录

parent 8a9b6ee2
...@@ -17,7 +17,7 @@ return [ ...@@ -17,7 +17,7 @@ return [
'csrfParam' => '_csrf-backend', 'csrfParam' => '_csrf-backend',
], ],
'user' => [ 'user' => [
'identityClass' => 'common\models\User', 'identityClass' => 'backend\models\User',
'enableAutoLogin' => true, 'enableAutoLogin' => true,
'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true], 'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true],
], ],
......
...@@ -13,6 +13,7 @@ use common\libs\Utils; ...@@ -13,6 +13,7 @@ use common\libs\Utils;
use common\models\Classes; use common\models\Classes;
use common\models\User; use common\models\User;
use Yii; use Yii;
use yii\filters\AccessControl;
use yii\web\Controller; use yii\web\Controller;
class ClassesController extends Controller class ClassesController extends Controller
...@@ -20,6 +21,24 @@ class ClassesController extends Controller ...@@ -20,6 +21,24 @@ class ClassesController extends Controller
public $layout = 'back'; public $layout = 'back';
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['@'],
]
]
]
];
}
public function actionIndex() public function actionIndex()
{ {
$status = Yii::$app->request->get('status', Classes::STATUS_OK); $status = Yii::$app->request->get('status', Classes::STATUS_OK);
......
...@@ -17,6 +17,7 @@ use common\models\TranscodeQueue; ...@@ -17,6 +17,7 @@ use common\models\TranscodeQueue;
use common\models\User; use common\models\User;
use Yii; use Yii;
use yii\base\ExitException; use yii\base\ExitException;
use yii\filters\AccessControl;
use yii\web\Controller; use yii\web\Controller;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
...@@ -25,6 +26,24 @@ class CourseController extends Controller ...@@ -25,6 +26,24 @@ class CourseController extends Controller
public $layout = 'back'; public $layout = 'back';
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['@'],
]
]
]
];
}
public function actionIndex() public function actionIndex()
{ {
$status = Yii::$app->request->get('status', Course::STATUS_OK); $status = Yii::$app->request->get('status', Course::STATUS_OK);
......
<?php <?php
namespace backend\controllers; namespace backend\controllers;
use backend\models\LoginForm;
use backend\models\User;
use common\libs\Utils;
use Yii; use Yii;
use yii\web\Controller; use yii\web\Controller;
use yii\filters\AccessControl; use yii\filters\AccessControl;
use common\models\LoginForm;
/** /**
* Site controller * Site controller
*/ */
class SiteController extends Controller class SiteController extends Controller
{ {
public $layout = 'lte';
/** /**
* @inheritdoc * @inheritdoc
*/ */
...@@ -53,7 +58,7 @@ class SiteController extends Controller ...@@ -53,7 +58,7 @@ class SiteController extends Controller
*/ */
public function actionIndex() public function actionIndex()
{ {
return $this->render('index'); return $this->redirect(['course/index']);
} }
/** /**
...@@ -63,6 +68,15 @@ class SiteController extends Controller ...@@ -63,6 +68,15 @@ class SiteController extends Controller
*/ */
public function actionLogin() public function actionLogin()
{ {
//$user = new User();
//$user->username = 'admin';
//$user->password_hash = \Yii::$app->security->generatePasswordHash('111111');
//$user->ip = Utils::getRealIP();
//$user->logged_at = time();
//$user->save();
//
//print_r($user->errors);
if (!Yii::$app->user->isGuest) { if (!Yii::$app->user->isGuest) {
return $this->goHome(); return $this->goHome();
} }
...@@ -85,7 +99,6 @@ class SiteController extends Controller ...@@ -85,7 +99,6 @@ class SiteController extends Controller
public function actionLogout() public function actionLogout()
{ {
Yii::$app->user->logout(); Yii::$app->user->logout();
return $this->goHome(); return $this->goHome();
} }
} }
<?php
namespace backend\models;
use common\libs\Utils;
use Yii;
use yii\base\Model;
/**
* Login form
*/
class LoginForm extends Model
{
const MAX_LOGIN_NUM = 5;
const LOGIN_CYCLE = 900; //second
const REDIS_KEY_PREFIX = 'yy_adminlogin_ip';
public $username;
public $password;
public $rememberMe = true;
private $_user;
public function formName()
{
return '';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
// username and password are both required
[['username', 'password'], 'required'],
// rememberMe must be a boolean value
// password is validated by validatePassword()
['password', 'validatePassword'],
['username', 'string']
];
}
/**
* @return array
*/
public function attributeLabels()
{
return [
'username' => '用户名',
'password' => '密码',
];
}
/**
* Validates the password.
* This method serves as the inline validation for password.
*
* @param string $attribute the attribute currently being validated
* @param array $params the additional name-value pairs given in the rule
*/
public function validatePassword($attribute, $params)
{
$redis = Yii::$app->redis;
$key = self::REDIS_KEY_PREFIX . '::' . Utils::getRealIP();
$value = $redis->get($key);
if ($value === NULL) {
$redis->setex($key, self::LOGIN_CYCLE, self::MAX_LOGIN_NUM);
} else {
if ($value <= 0) {
$this->addError('password', '登陆已经被限制了.');
}
}
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) {
if ($value !== NULL) {
$redis->decr($key);
}
$this->addError($attribute, '用户名或者密码错误');
}
}
}
/**
* Logs in a user using the provided username and password.
*
* @return boolean whether the user is logged in successfully
*/
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), 86400*7);
} else {
return false;
}
}
/**
* Finds user by [[username]]
*
* @return User
*/
protected function getUser()
{
if ($this->_user === NULL) {
$this->_user = User::findOne(['username'=>$this->username]);
}
return $this->_user;
}
}
<?php
namespace backend\models;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
/**
* 后台管理用户类
*
* @property integer $id
* @property string $username
* @property string $auth_key
* @property string $password_hash
* @property integer $status
* @property string $ip
* @property string $created_at
* @property string $updated_at
* @property string $logged_at
*/
class User extends ActiveRecord implements \yii\web\IdentityInterface
{
public static function tableName()
{
return '{{%admin_users}}';
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
]
]
];
}
public function rules()
{
return [
[['username', 'password_hash', 'ip', 'logged_at'], 'required'],
[['id', 'status'], 'integer'],
[['created_at', 'updated_at', 'logged_at'], 'safe'],
[['username', 'auth_key', 'ip'], 'string', 'max' => 32],
[['password_hash'], 'string', 'max' => 255],
['status', 'default', 'value' => 1]
];
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
return $this->auth_key;
}
public function validateAuthKey($authKey)
{
return $this->getAuthKey() == $authKey;
}
/**
* Validates password
*
* @param string $password password to validate
* @return boolean if password provided is valid for current user
*/
public function validatePassword($password)
{
return \Yii::$app->security->validatePassword($password, $this->password_hash);
}
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
if ($this->isNewRecord) {
$this->auth_key = \Yii::$app->security->generateRandomString();
}
return true;
}
return false;
}
public static function findIdentity($id)
{
return self::findOne(['id'=>$id]);
}
public static function findIdentityByAccessToken($token, $type = null)
{
throw new \yii\base\NotSupportedException('"findIdentityByAccessToken" is not implemented.');
}
}
\ No newline at end of file
This diff is collapsed.
...@@ -24,12 +24,8 @@ AppAsset::register($this); ...@@ -24,12 +24,8 @@ AppAsset::register($this);
<script src="http://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script> <script src="http://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
<![endif]--> <![endif]-->
</head> </head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
<?php $this->beginBody() ?> <?php $this->beginBody() ?>
<?=$content?> <?=$content?>
<?php $this->endBody() ?> <?php $this->endBody() ?>
</div>
</body>
</html> </html>
<?php $this->endPage() ?> <?php $this->endPage() ?>
\ No newline at end of file
<?php <?php
/**
/* @var $this yii\web\View */ * @var $this yii\web\View
/* @var $form yii\bootstrap\ActiveForm */ * @var $form yii\bootstrap\ActiveForm
/* @var $model \common\models\LoginForm */ * @var $model \backend\models\LoginForm
*/
use yii\helpers\Html; use yii\helpers\Html;
use yii\bootstrap\ActiveForm; use yii\bootstrap\ActiveForm;
$this->title = 'Login'; $this->title = 'Login';
$this->params['breadcrumbs'][] = $this->title; //$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-login">
<h1><?= Html::encode($this->title) ?></h1>
<p>Please fill out the following fields to login:</p>
<div class="row">
<div class="col-lg-5">
<?php $form = ActiveForm::begin(['id' => 'login-form']); ?>
<?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?> $usernameError = $model->getFirstError('username');
$passwordError = $model->getFirstError('password');
<?= $form->field($model, 'password')->passwordInput() ?> ?>
<?= $form->field($model, 'rememberMe')->checkbox() ?>
<div class="form-group"> <body class="hold-transition login-page">
<?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?> <div class="login-box">
<div class="login-logo"><b>Admin</b>YunYa</div>
<!-- /.login-logo -->
<div class="login-box-body">
<p class="login-box-msg">Sign in to start your session</p>
<?php $form = ActiveForm::begin(['id' => 'login-form']); ?>
<div class="form-group<?php if ($usernameError) { ?> has-error<?php } ?>">
<?php if ($usernameError) { ?>
<label class="control-label" for="inputError">
<i class="fa fa-times-circle-o"></i> <?=$usernameError?>
</label>
<?php } ?>
<input type="text" name="username" class="form-control" placeholder="Username" value="<?=$model->username?>">
<span class="glyphicon glyphicon-user form-control-feedback"></span>
</div>
<div class="form-group<?php if ($passwordError) { ?> has-error<?php } ?>">
<?php if ($passwordError) { ?>
<label class="control-label" for="inputError">
<i class="fa fa-times-circle-o"></i> <?=$passwordError?>
</label>
<?php } ?>
<input type="password" name="password" class="form-control" placeholder="Password" value="<?=$model->password?>">
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="row">
<div class="col-xs-8"></div>
<div class="col-xs-4">
<?= Html::submitButton('Sign In', ['class' => 'btn btn-primary btn-block btn-flat', 'name' => 'login-button']) ?>
</div> </div>
</div>
<?php ActiveForm::end(); ?> <?php ActiveForm::end(); ?>
</div>
</div> </div>
</div> </div>
\ No newline at end of file
...@@ -53,20 +53,14 @@ class QiniuTranscode ...@@ -53,20 +53,14 @@ class QiniuTranscode
$fops = self::transcodeFops($avInfo); $fops = self::transcodeFops($avInfo);
$bucket = $qiniu->getStorageConfig('video', 'bucket'); $bucket = $qiniu->getStorageConfig('video', 'bucket');
$target = $bucket.':'.$targetKey; //存储位置
//存储位置
$target = $bucket.':'.$targetKey;
$notifyUrl = YII_ENV == 'dev'
? 'http://t.pp.cc/tmp/qiniu_callback.php'
: Url::to(['qiniu-callback/transcode'], true);;
/* @var $err null|\Qiniu\Http\Error */ /* @var $err null|\Qiniu\Http\Error */
if ($fops) { if ($fops) {
$fops .= '|saveas/'.\Qiniu\base64_urlSafeEncode($target); $fops .= '|saveas/'.\Qiniu\base64_urlSafeEncode($target);
//发送处理请求 //发送处理请求
$persistent = new PersistentFop($auth, $bucket, null, $notifyUrl); $persistent = new PersistentFop($auth, $bucket, null, self::getNotifyUrl());
list($presistendId, $err) = $persistent->execute($sourceKey, $fops); list($presistendId, $err) = $persistent->execute($sourceKey, $fops);
if ($err) { if ($err) {
...@@ -124,17 +118,11 @@ class QiniuTranscode ...@@ -124,17 +118,11 @@ class QiniuTranscode
//存储位置 //存储位置
$target = $bucket.':'.$targetKey; $target = $bucket.':'.$targetKey;
$fops = $fops.'/ss/0/t/'.($freeTime*60).'|saveas/'.\Qiniu\base64_urlSafeEncode($target); $fops = $fops.'/ss/0/t/'.($freeTime*60).'|saveas/'.\Qiniu\base64_urlSafeEncode($target);
$notifyUrl = YII_ENV == 'dev'
? 'http://t.pp.cc/tmp/qiniu_callback.php'
: Url::to(['qiniu-callback/transcode'], true);;
//发送处理请求 //发送处理请求
/* @var $err null|\Qiniu\Http\Error */ /* @var $err null|\Qiniu\Http\Error */
$persistent = new PersistentFop($auth, $bucket, null, $notifyUrl); $persistent = new PersistentFop($auth, $bucket, null, self::getNotifyUrl());
list($presistendId, $err) = $persistent->execute($standKey, $fops); list($presistendId, $err) = $persistent->execute($standKey, $fops);
if ($err) { if ($err) {
...@@ -211,5 +199,12 @@ class QiniuTranscode ...@@ -211,5 +199,12 @@ class QiniuTranscode
return $fops; return $fops;
} }
protected static function getNotifyUrl()
{
return YII_ENV == 'dev'
? 'http://t.pp.cc/tmp/qiniu_callback.php'
: Url::to(['qiniu-callback/transcode'], true);
}
} }
\ No newline at end of file
...@@ -36,5 +36,7 @@ return [ ...@@ -36,5 +36,7 @@ return [
'mchid' => '1399794002', 'mchid' => '1399794002',
'key' => '8934e7d15453e97507ef794cf7b0519d', 'key' => '8934e7d15453e97507ef794cf7b0519d',
'appsecret' => '7c18222c6ec31efa65d21606a629585d', 'appsecret' => '7c18222c6ec31efa65d21606a629585d',
] ],
'fronend.baseUrl' => 'http://dev.zuiketang.com/',
'backend.baseUrl' => 'http://backend.zuiketang.com/'
]; ];
...@@ -36,5 +36,7 @@ return [ ...@@ -36,5 +36,7 @@ return [
'mchid' => '1399794002', 'mchid' => '1399794002',
'key' => '8934e7d15453e97507ef794cf7b0519d', 'key' => '8934e7d15453e97507ef794cf7b0519d',
'appsecret' => '7c18222c6ec31efa65d21606a629585d', 'appsecret' => '7c18222c6ec31efa65d21606a629585d',
] ],
'fronend.baseUrl' => 'http://c.pp.cc/',
'backend.baseUrl' => 'http://admin.c.pp.cc/'
]; ];
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment