Skip to content

Instantly share code, notes, and snippets.

@drizzentic
Forked from jamband/LoginAttempt.php
Created December 18, 2012 14:13
Show Gist options
  • Save drizzentic/4328298 to your computer and use it in GitHub Desktop.
Save drizzentic/4328298 to your computer and use it in GitHub Desktop.

Revisions

  1. @jamband jamband renamed this gist Dec 13, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @jamband jamband renamed this gist Dec 13, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @jamband jamband revised this gist Apr 12, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion LoginAttempt.php
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ class LoginAttempt extends ActiveRecord
    protected function beforeSave()
    {
    $this->create_time = time();
    $this->expiration_time = strtotime(self::BANNED_IP_EXPIRATION_TIME, time());
    $this->expiration_time = strtotime(self::BANNED_IP_EXPIRATION_TIME, $this->create_time);

    return true;
    }
  4. @jamband jamband revised this gist Apr 8, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions LoginAttempt.php
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ protected function beforeSave()
    public function isBanned($ip)
    {
    $c = $this->getCriteriaByIp($ip);
    if ($this->count($c) > self::LOGIN_ATTEMPT_LIMIT)
    if ($this->count($c) >= self::LOGIN_ATTEMPT_LIMIT)
    return true;

    return false;
    @@ -37,7 +37,7 @@ public function isBanned($ip)
    public function purgeBannedIp($ip)
    {
    $c = $this->getCriteriaByIp($ip);
    if ($this->count($c) > self::LOGIN_ATTEMPT_LIMIT)
    if ($this->count($c) >= self::LOGIN_ATTEMPT_LIMIT)
    {
    $c->order = 't.create_time DESC';
    $model = $this->find($c);
  5. @jamband jamband revised this gist Apr 8, 2012. No changes.
  6. @jamband jamband revised this gist Apr 8, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion LoginController.php
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,6 @@ class LoginController extends Controller
    public function init()
    {
    $this->_ip = Yii::app()->request->userHostAddress;
    return parent::init();
    }

    /**
  7. @jamband jamband revised this gist Apr 8, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion LoginAttempt.php
    Original file line number Diff line number Diff line change
    @@ -41,7 +41,7 @@ public function purgeBannedIp($ip)
    {
    $c->order = 't.create_time DESC';
    $model = $this->find($c);
    if (time() >= $model->expiration_time)
    if (time() > $model->expiration_time)
    $this->deleteAll('ip = :ip', array(':ip' => $ip));
    }
    }
  8. @jamband jamband revised this gist Apr 8, 2012. 1 changed file with 27 additions and 0 deletions.
    27 changes: 27 additions & 0 deletions login.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    <?php if (!$isBanned): ?>

    <div class="form">
    <?php echo CHtml::form(); ?>
    <?php echo CHtml::errorSummary($model, false); ?>

    <div class="row">
    <?php echo CHtml::activeLabel($model, 'login'); ?>
    <?php echo CHtml::activeTextField($model, 'login', array('maxlength' => 64)); ?>
    </div>
    <div class="row">
    <?php echo CHtml::activeLabel($model, 'password'); ?>
    <?php echo CHtml::activePasswordField($model, 'password', array('maxlength' => 64)); ?>
    </div>
    <div class="row">
    <?php echo CHtml::submitButton('ログイン'); ?>
    <?php echo CHtml::activeCheckBox($model, 'rememberMe'); ?>
    <?php echo CHtml::activeLabel($model, 'rememberMe'); ?>
    </div>
    </form>
    </div><!-- /.form -->
    <?php else: ?>

    <div class="flash-error">
    ログイン試行回数が制限を越えてしまいました。時間をおいて、再度ログインを試してください
    </div>
    <?php endif; ?>
  9. @jamband jamband revised this gist Apr 8, 2012. 1 changed file with 51 additions and 0 deletions.
    51 changes: 51 additions & 0 deletions LoginController.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    <?php
    class LoginController extends Controller
    {
    private $_ip;

    /**
    * @see CController::defaultAction
    */
    public $defaultAction = 'login';

    /**
    * @see CController::init()
    */
    public function init()
    {
    $this->_ip = Yii::app()->request->userHostAddress;
    return parent::init();
    }

    /**
    * ユーザのログイン
    */
    public function actionLogin()
    {
    if (Yii::app()->user->id)
    $this->redirect('/');

    $loginAttempt = new LoginAttempt;
    $loginAttempt->purgeBannedIp($this->_ip);

    if (!$isBanned = $loginAttempt->isBanned($this->_ip))
    {
    $model = new Login;
    if (isset($_POST['Login']))
    {
    $model->attributes = $_POST['Login'];
    if ($model->validate())
    $this->redirect(Yii::app()->user->returnUrl);

    if (!empty($model->login))
    {
    $loginAttempt->login = $model->login;
    $loginAttempt->ip = $this->_ip;
    $loginAttempt->save(false);
    }
    }
    }

    $this->render('/user/login', compact('model', 'isBanned'));
    }
    }
  10. @jamband jamband revised this gist Apr 8, 2012. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions LoginAttempt.php
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    <?php
    class LoginAttempt extends ActiveRecord
    {
    const LOGIN_ATTEMPT_LIMIT = 5;
    const BANNED_IP_EXPIRATION_TIME = '+1 minute';
    const LOGIN_ATTEMPT_LIMIT = 5; // ログイン試行回数のリミット
    const BANNED_IP_EXPIRATION_TIME = '+1 hour'; // この場合は禁止されてから一時間経たないとログインできない

    ...
    /**
    @@ -24,7 +24,7 @@ protected function beforeSave()
    public function isBanned($ip)
    {
    $c = $this->getCriteriaByIp($ip);
    if ($this->count($c) >= self::LOGIN_ATTEMPT_LIMIT)
    if ($this->count($c) > self::LOGIN_ATTEMPT_LIMIT)
    return true;

    return false;
    @@ -37,7 +37,7 @@ public function isBanned($ip)
    public function purgeBannedIp($ip)
    {
    $c = $this->getCriteriaByIp($ip);
    if ($this->count($c) >= self::LOGIN_ATTEMPT_LIMIT)
    if ($this->count($c) > self::LOGIN_ATTEMPT_LIMIT)
    {
    $c->order = 't.create_time DESC';
    $model = $this->find($c);
  11. @jamband jamband created this gist Apr 8, 2012.
    62 changes: 62 additions & 0 deletions LoginAttempt.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    <?php
    class LoginAttempt extends ActiveRecord
    {
    const LOGIN_ATTEMPT_LIMIT = 5;
    const BANNED_IP_EXPIRATION_TIME = '+1 minute';

    ...
    /**
    * @see CActiveRecord::beforeSave()
    */
    protected function beforeSave()
    {
    $this->create_time = time();
    $this->expiration_time = strtotime(self::BANNED_IP_EXPIRATION_TIME, time());

    return true;
    }

    /**
    * ログインが禁止されたIPアドレスか、そうでないか
    * @param string $ip ip address
    * @return boolean
    */
    public function isBanned($ip)
    {
    $c = $this->getCriteriaByIp($ip);
    if ($this->count($c) >= self::LOGIN_ATTEMPT_LIMIT)
    return true;

    return false;
    }

    /**
    * ログインが禁止されたIPアドレスを期限切れの場合は削除する
    * @param string $ip ip address
    */
    public function purgeBannedIp($ip)
    {
    $c = $this->getCriteriaByIp($ip);
    if ($this->count($c) >= self::LOGIN_ATTEMPT_LIMIT)
    {
    $c->order = 't.create_time DESC';
    $model = $this->find($c);
    if (time() >= $model->expiration_time)
    $this->deleteAll('ip = :ip', array(':ip' => $ip));
    }
    }

    /**
    * IPを条件にしたcriteriaを取得する
    * @param string $ip ip address
    * @return object criteria
    */
    protected function getCriteriaByIp($ip)
    {
    $c = new CDbCriteria;
    $c->condition = 't.ip = :ip';
    $c->params[':ip'] = $ip;

    return $c;
    }
    ...