Commit 07553a50 authored by xiabin's avatar xiabin

添加逻辑

parent 4988f39c
...@@ -15,6 +15,12 @@ return [ ...@@ -15,6 +15,12 @@ return [
'password' => 'mysql123', 'password' => 'mysql123',
'charset' => 'utf8', 'charset' => 'utf8',
], ],
'redis' => [
'class' => 'yii\redis\Connection',
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
],
'mailer' => [ 'mailer' => [
'class' => 'yii\swiftmailer\Mailer', 'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail', 'viewPath' => '@common/mail',
......
...@@ -3,4 +3,6 @@ return [ ...@@ -3,4 +3,6 @@ return [
'adminEmail' => 'admin@example.com', 'adminEmail' => 'admin@example.com',
'supportEmail' => 'support@example.com', 'supportEmail' => 'support@example.com',
'user.passwordResetTokenExpire' => 3600, 'user.passwordResetTokenExpire' => 3600,
'captchaId' => '4d6ad870bf63a30be3d22c92925b04ca',
'privateKey' => 'a2ca5e5139d1f43ac4e1cd248f3e52e4'
]; ];
This diff is collapsed.
...@@ -3,23 +3,48 @@ ...@@ -3,23 +3,48 @@
namespace frontend\assets; namespace frontend\assets;
use yii\web\AssetBundle; use yii\web\AssetBundle;
use yii\web\View;
use Yii;
/** /**
* Main frontend application asset bundle. * Main frontend application asset bundle.
*/ */
class AppAsset extends AssetBundle class AppAsset extends AssetBundle
{ {
public $basePath = '@webroot'; public $basePath = '@webroot/assets';
public $baseUrl = '@web'; public $baseUrl = '@web/assets';
public $css = [ public $css = [
'css/site.css', 'css/site.css',
]; ];
public $js = [ public $js = [
['common/common.js', 'position' => View::POS_BEGIN]
]; ];
public $depends = [ public $depends = [
'yii\web\YiiAsset', 'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset', 'yii\bootstrap\BootstrapAsset',
]; ];
//定义按需加载JS方法,注意加载顺序在最后
public static function addScript($view, $jsfile)
{
$jsfile = self::appendTimestamp($jsfile);
$view->registerJsFile($jsfile, [AppAsset::className(), 'depends' => 'frontend\assets\AppAsset']);
}
//定义按需加载css方法,注意加载顺序在最后
public static function addCss($view, $cssfile)
{
$cssfile = self::appendTimestamp($cssfile);
$view->registerCssFile($cssfile, [AppAsset::className(), 'depends' => 'frontend\assets\AppAsset']);
}
public static function appendTimestamp($url)
{
if (Yii::$app->assetManager->appendTimestamp && strncmp($url, '@web/', 5) === 0) {
$fileToCacheBust = Yii::getAlias(str_replace('@web', '@webroot', $url));
if (($timestamp = @filemtime("$fileToCacheBust")) > 0)
$url .= "?v={$timestamp}";
}
return $url;
}
} }
...@@ -15,7 +15,11 @@ return [ ...@@ -15,7 +15,11 @@ return [
'request' => [ 'request' => [
'csrfParam' => '_csrf-frontend', 'csrfParam' => '_csrf-frontend',
], ],
'assetManager' => [
'appendTimestamp' => TRUE
],
'session' => [ 'session' => [
'timeout' => 3600,
'name' => 'zuiektang-frontend', 'name' => 'zuiektang-frontend',
'class' => 'yii\redis\Session', 'class' => 'yii\redis\Session',
'redis' => [ 'redis' => [
...@@ -28,6 +32,11 @@ return [ ...@@ -28,6 +32,11 @@ return [
'identityClass' => 'common\models\User', 'identityClass' => 'common\models\User',
'enableAutoLogin' => TRUE, 'enableAutoLogin' => TRUE,
'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => TRUE], 'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => TRUE],
'on afterLogin' => function ($event) {
$user = $event->identity;
$user->logged_at = time();
$user->save();
}
], ],
'log' => [ 'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0, 'traceLevel' => YII_DEBUG ? 3 : 0,
......
<?php <?php
namespace frontend\controllers; namespace frontend\controllers;
use common\libs\Geetest;
use common\models\User; use common\models\User;
use frontend\models\TokenInputForm; use frontend\models\TokenInputForm;
use Yii; use Yii;
use yii\base\InvalidParamException; use yii\base\InvalidParamException;
use yii\helpers\HtmlPurifier;
use yii\web\BadRequestHttpException; use yii\web\BadRequestHttpException;
use yii\web\Controller; use yii\web\Controller;
use yii\filters\VerbFilter; use yii\filters\VerbFilter;
...@@ -26,7 +28,8 @@ class SiteController extends Controller ...@@ -26,7 +28,8 @@ class SiteController extends Controller
const RESET_PASSWORD_FIRST = 'rp_first'; const RESET_PASSWORD_FIRST = 'rp_first';
const RESET_PASSWORD_SECOND = 'rp_second'; const RESET_PASSWORD_SECOND = 'rp_second';
const RESET_PASSWORD_THREE = 'rp_three'; const RESET_PASSWORD_THREE = 'rp_three';
const GT_SERVER = 'gt_server';
const GT_USER_ID = 'gt_user_id';
/** /**
* @inheritdoc * @inheritdoc
...@@ -159,8 +162,25 @@ class SiteController extends Controller ...@@ -159,8 +162,25 @@ class SiteController extends Controller
public function actionSignup() public function actionSignup()
{ {
$model = new SignupForm(); $model = new SignupForm();
if ($model->load(Yii::$app->request->post())) { $request = Yii::$app->request;
if ($user = $model->signup()) {
$from = $request->get('from', 0);
$from = HtmlPurifier::process($from);
$model->from = (int)$from;
if ($model->load($request->post())) {
$geetestChallenge = $request->post('geetest_challenge');
$geetestSeccode = $request->post('geetest_seccode');
$geetestValidate = $request->post('geetest_validate');
$GtSdk = new Geetest(Yii::$app->params['captchaId'], Yii::$app->params['privateKey']);
$user_id = Yii::$app->session->get(self::GT_USER_ID);
if (Yii::$app->session->get(self::GT_SERVER) == 1) { //服务器正常
$gtResult = $GtSdk->success_validate($geetestChallenge, $geetestValidate, $geetestSeccode, $user_id);
} else { //服务器宕机,走failback模式
$gtResult = $GtSdk->fail_validate($geetestChallenge, $geetestValidate, $geetestSeccode);
}
if ($gtResult && $user = $model->signup()) {
if (Yii::$app->getUser()->login($user)) { if (Yii::$app->getUser()->login($user)) {
return $this->goHome(); return $this->goHome();
} }
...@@ -177,7 +197,8 @@ class SiteController extends Controller ...@@ -177,7 +197,8 @@ class SiteController extends Controller
* *
* @return mixed * @return mixed
*/ */
public function actionRequestPasswordReset() public
function actionRequestPasswordReset()
{ {
$model = new PasswordResetRequestForm(); $model = new PasswordResetRequestForm();
if ($model->load(Yii::$app->request->post()) && $model->validate()) { if ($model->load(Yii::$app->request->post()) && $model->validate()) {
...@@ -198,7 +219,8 @@ class SiteController extends Controller ...@@ -198,7 +219,8 @@ class SiteController extends Controller
/** /**
* @return mixed * @return mixed
*/ */
public function actionTokenInput() public
function actionTokenInput()
{ {
if (!Yii::$app->session->has(self::RESET_PASSWORD_FIRST)) { if (!Yii::$app->session->has(self::RESET_PASSWORD_FIRST)) {
$this->redirect('request-password-reset'); $this->redirect('request-password-reset');
...@@ -222,7 +244,8 @@ class SiteController extends Controller ...@@ -222,7 +244,8 @@ class SiteController extends Controller
* Resets password. * Resets password.
* @return mixed * @return mixed
*/ */
public function actionResetPassword() public
function actionResetPassword()
{ {
if (!Yii::$app->session->has(self::RESET_PASSWORD_SECOND)) { if (!Yii::$app->session->has(self::RESET_PASSWORD_SECOND)) {
$this->redirect('request-password-reset'); $this->redirect('request-password-reset');
...@@ -244,7 +267,8 @@ class SiteController extends Controller ...@@ -244,7 +267,8 @@ class SiteController extends Controller
* 密码设置成功 * 密码设置成功
* @return string * @return string
*/ */
public function actionResetPasswordSuccess() public
function actionResetPasswordSuccess()
{ {
if (!Yii::$app->session->has(self::RESET_PASSWORD_THREE)) { if (!Yii::$app->session->has(self::RESET_PASSWORD_THREE)) {
$this->redirect('request-password-reset'); $this->redirect('request-password-reset');
...@@ -252,4 +276,28 @@ class SiteController extends Controller ...@@ -252,4 +276,28 @@ class SiteController extends Controller
Yii::$app->session->remove(self::RESET_PASSWORD_THREE); Yii::$app->session->remove(self::RESET_PASSWORD_THREE);
return $this->render('resetPasswordSuccess'); return $this->render('resetPasswordSuccess');
} }
/**
* 极验证开始
* ajax
*/
public
function actionStartCaptcha()
{
if (Yii::$app->request->isAjax) {
$type = Yii::$app->request->get('type');
if ($type == 'pc') {
$GtSdk = new Geetest(Yii::$app->params['captchaId'], Yii::$app->params['privateKey']);
} else {
throw new \yii\web\ForbiddenHttpException('参数错误');
}
$user_id = Yii::$app->security->generateRandomString(16);
$status = $GtSdk->pre_process($user_id);
Yii::$app->session->set(self::GT_SERVER, $status);
Yii::$app->session->set(self::GT_USER_ID, $user_id);
echo $GtSdk->get_response_str();
} else {
throw new \yii\web\ForbiddenHttpException('非法请求');
}
}
} }
...@@ -9,8 +9,11 @@ ...@@ -9,8 +9,11 @@
namespace frontend\controllers; namespace frontend\controllers;
use common\libs\Test;
use yii\filters\AccessControl; use yii\filters\AccessControl;
use yii\web\Controller; use yii\web\Controller;
use Yii;
use yii\helpers\HtmlPurifier;
class UserCenterController extends Controller class UserCenterController extends Controller
{ {
...@@ -26,17 +29,23 @@ class UserCenterController extends Controller ...@@ -26,17 +29,23 @@ class UserCenterController extends Controller
'rules' => [ 'rules' => [
[ [
'allow' => TRUE, 'allow' => TRUE,
'roles' => ['teacher'], 'roles' => ['?'],
], ],
], ],
], ],
]; ];
} }
public function actionTest()
{
$test = new Test();
$test->index();
}
public function actionIndex() public function actionIndex()
{ {
echo \Yii::$app->user->identity->role; echo 'index';
// echo Yii::$app->user->identity->role;
} }
} }
\ No newline at end of file
...@@ -10,6 +10,9 @@ use common\models\User; ...@@ -10,6 +10,9 @@ use common\models\User;
*/ */
class LoginForm extends Model class LoginForm extends Model
{ {
const MAX_LOGIN_NUM = 5;
const LOGIN_CYCLE = 15 * 60;//second
const REDIS_KEY_PREFIX = 'login_ip';
public $telephone; public $telephone;
public $password; public $password;
public $rememberMe = TRUE; public $rememberMe = TRUE;
...@@ -42,9 +45,23 @@ class LoginForm extends Model ...@@ -42,9 +45,23 @@ class LoginForm extends Model
*/ */
public function validatePassword($attribute, $params) public function validatePassword($attribute, $params)
{ {
$redis = Yii::$app->redis;
$key = self::REDIS_KEY_PREFIX . '::' . Yii::$app->request->getUserIP();
$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()) { if (!$this->hasErrors()) {
$user = $this->getUser(); $user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) { if (!$user || !$user->validatePassword($this->password)) {
if ($value !== NULL) {
$redis->decr($key);
}
$this->addError($attribute, 'Incorrect telephone or password.'); $this->addError($attribute, 'Incorrect telephone or password.');
} }
} }
......
...@@ -13,10 +13,11 @@ class SignupForm extends Model ...@@ -13,10 +13,11 @@ class SignupForm extends Model
{ {
const USERNAME_PREFIX = 'ZK_'; const USERNAME_PREFIX = 'ZK_';
public $telephone; public $telephone;
public $password; public $password;
public $rePassword; public $rePassword;
public $from;
/** /**
* @inheritdoc * @inheritdoc
...@@ -25,14 +26,14 @@ class SignupForm extends Model ...@@ -25,14 +26,14 @@ class SignupForm extends Model
{ {
return [ return [
['telephone', 'trim'], ['telephone', 'trim'],
['telephone', 'required'], [['telephone', 'from'], 'required'],
['telephone', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This telephone has already been taken.'], ['telephone', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This telephone has already been taken.'],
['telephone', 'string', 'length' => 11], ['telephone', 'string', 'length' => 11],
[['password', 'rePassword'], 'required'], [['password', 'rePassword'], 'required'],
['password', 'string', 'min' => 6], ['password', 'string', 'min' => 6],
['rePassword', 'string', 'min' => 6], ['rePassword', 'string', 'min' => 6],
['rePassword', 'compare', 'compareAttribute' => 'rePassword'], ['rePassword', 'compare', 'compareAttribute' => 'rePassword'],
['from', 'integer']
]; ];
} }
...@@ -43,6 +44,7 @@ class SignupForm extends Model ...@@ -43,6 +44,7 @@ class SignupForm extends Model
*/ */
public function signup() public function signup()
{ {
if (!$this->validate()) { if (!$this->validate()) {
return NULL; return NULL;
} }
...@@ -62,4 +64,12 @@ class SignupForm extends Model ...@@ -62,4 +64,12 @@ class SignupForm extends Model
} }
return $user; return $user;
} }
public function formName()
{
return '';
}
} }
...@@ -6,9 +6,12 @@ ...@@ -6,9 +6,12 @@
use yii\helpers\Html; use yii\helpers\Html;
use yii\bootstrap\ActiveForm; use yii\bootstrap\ActiveForm;
use frontend\assets\AppAsset;
$this->title = 'Signup'; $this->title = 'Signup';
$this->params['breadcrumbs'][] = $this->title; $this->params['breadcrumbs'][] = $this->title;
AppAsset::addScript($this, '@web/assets/signup.js');
$this->registerJsFile('http://static.geetest.com/static/tools/gt.js');
?> ?>
<div class="site-signup"> <div class="site-signup">
<h1><?= Html::encode($this->title) ?></h1> <h1><?= Html::encode($this->title) ?></h1>
...@@ -26,8 +29,12 @@ $this->params['breadcrumbs'][] = $this->title; ...@@ -26,8 +29,12 @@ $this->params['breadcrumbs'][] = $this->title;
<?= $form->field($model, 'rePassword')->passwordInput() ?> <?= $form->field($model, 'rePassword')->passwordInput() ?>
<div id="embed-captcha"></div>
<p id="wait" class="show">正在加载验证码......</p>
<p id="notice" class="hide">请先拖动验证码到相应位置</p>
<div class="form-group"> <div class="form-group">
<?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?> <?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'id' => 'signup-button']) ?>
</div> </div>
<?php ActiveForm::end(); ?> <?php ActiveForm::end(); ?>
......
/**
* Created by xiabin on 16/9/24.
*/
alert(123);
\ No newline at end of file
/**
* Created by xiabin on 2016/9/26.
*/
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