1
0

AccessToken.php 829 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. use yii\behaviors\TimestampBehavior;
  5. use yii\db\ActiveRecord;
  6. /**
  7. * Class AccessToken
  8. * @package common\models
  9. */
  10. class AccessToken extends ActiveRecord
  11. {
  12. /**
  13. * @inheritdoc
  14. */
  15. public function behaviors()
  16. {
  17. return [
  18. [
  19. 'class' => TimestampBehavior::className(),
  20. 'createdAtAttribute' => 'created_at',
  21. 'updatedAtAttribute' => 'updated_at',
  22. //'value' => new Expression('NOW()'),
  23. 'value' => function() { return time();},
  24. ],
  25. ];
  26. }
  27. /**
  28. * getUidByApiToken
  29. * @param $token
  30. * @return bool
  31. */
  32. public static function getUidByApiToken($token)
  33. {
  34. $uid = static::find()->select('user_id')
  35. ->where(['access_token' => $token])
  36. ->andWhere(['>', 'expires', time()])
  37. ->scalar();
  38. return $uid != null && is_numeric($uid) ? $uid : null;
  39. }
  40. }