A
A
alex5e2014-04-12 07:28:11
Yii
alex5e, 2014-04-12 07:28:11

Time limit for users to perform certain Yii actions?

Who can tell how to restrict users to perform some actions (for example, once every 30 minutes), but this is provided that the model has been successfully saved?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan, 2014-04-13
@alex5e

1. In the table with users, add the lastActionTime field with the timestamp data type
2. In the model that you save in the action:

protected function afterSave() {
  if (parent::afterSave()) {
    $user = User::model()->findByPk(Yii::app()->user->id);
    $user->lastActionTime = date('Y-m-d H:i:s');
    $user->save();
    return true;
  } else return false;
}

3. In the controller:
public function filters() {
  return array(
    'LastActionTime + index'
  );
}

public function filterLastActionTime($filterChain) {
  $user = User::model()->findByPk(Yii::app()->user->id);
  if ($user->lastActionTime > date('Y-m-d H:i:s', time() - 60 * 30)) { // 30 минут
    Yii::app()->user->setFlash('error', 'Вы уже выполняли это действие меньше чем полчаса назад :(');
    $this->redirect(Yii::app()->homeUrl);
  }
  $filterChain->run();
}

Don't forget to replace index with the name of your action.

S
Sergey, 2014-04-12
Protko @Fesor

on successful save, add an entry to which log (in a separate table) for the user. Next, you can write a filter that will allow the user to access the action only if this very specific time has passed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question