YII2 REST身份界面返回所有用户?

时间:2017-03-04 21:47:36

标签: php rest yii2

我有一个REST API端点,用于让用户登录并检索有关其帐户的信息。

该实施运行良好......但现在它已经坏了。

我使用basicauth覆盖来使用USERNAME:PASSWORD而不是令牌 控制器和MODEL代码下方

在响应中,我现在找到所有用户...而不是一个

无法理解,因为我们首先使用findOne来选择一个用户,然后检查那个密码。

也许我在这里错过了一些东西:/

用户模特:

<?php
namespace common\models;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;

class User extends ActiveRecord implements IdentityInterface
{

    public static function tableName()
    {
        return '{{%user}}';
    }


    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    public function rules()
    {
        return [
            [['username', 'auth_key', 'password_hash', 'email'], 'required'],
            [['status', 'created_at', 'updated_at', 'background'], 'integer'],
            [['username', 'password_hash', 'password_reset_token', 'email', 'hmac_shopify', 'shop_address', 'room_id', 'wp_address', 'blog_address', 'iosRegisterID', 'androidRegisterID', 'timeZone'], 'string', 'max' => 255],
            [['auth_key'], 'string', 'max' => 32],
            [['account_level'], 'string', 'max' => 45],
            [['username'], 'unique'],
            [['email'], 'unique'],
            [['password_reset_token'], 'unique'],
        ];
    }

    public static function findIdentity($id)
    {
        return static::findOne(['id' => $id]);
    }

    public static function findIdentityByAccessToken($token, $type = null)
    {
        return static::findOne(['auth_key' => $token]);
    }


    public static function findByUsername($username)
    {
        return static::findOne(['username' => $username]);
    }

    public static function findByPasswordResetToken($token)
    {
        if (!static::isPasswordResetTokenValid($token)) {
            return null;
        }

        return static::findOne([
            'password_reset_token' => $token,
            'status' => self::STATUS_ACTIVE,
        ]);
    }

    public static function isPasswordResetTokenValid($token)
    {
        if (empty($token)) {
            return false;
        }

        $timestamp = (int) substr($token, strrpos($token, '_') + 1);
        $expire = Yii::$app->params['user.passwordResetTokenExpire'];
        return $timestamp + $expire >= time();
    }

    public function getId()
    {
        return $this->getPrimaryKey();
    }


    public function getAuthKey()
    {
        return $this->auth_key;
        return $this->hmac_shopify;
    }


    public function validateAuthKey($authKey)
    {
        return $this->getAuthKey() === $authKey;
    }

    public function validatePassword($password)
    {        

        return Yii::$app->security->validatePassword($password, $this->password_hash);
    }

    public function setPassword($password)
    {
        $this->password_hash = Yii::$app->security->generatePasswordHash($password);
    }

    public function generateAuthKey()
    {
        $this->auth_key = Yii::$app->security->generateRandomString();
        $this->room_id = "_r_".Yii::$app->security->generateRandomString();
    }

    public function generatePasswordResetToken()
    {
        $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
    }

    public function removePasswordResetToken()
    {
        $this->password_reset_token = null;
    }

    public static function find() 
    { 
       return new UserQuery(get_called_class()); 
    } 

}

这是控制器:

<?php
namespace api\controllers;
use yii;
use yii\rest\ActiveController;
use \common\models\User;

class RestController extends ActiveController
{

    public $modelClass = '\common\models\User';
    public $password_hash;

    public function behaviors()
    {
    $behaviors = parent::behaviors();
    $behaviors['verbs'] = [
    'class' => \yii\filters\VerbFilter::className(),
    'actions' => [
    'index' => ['get', 'head'],
    ],
    ];

    $behaviors['access'] = [
    'class' => \yii\filters\AccessControl::className(),
    'only' => ['index'],
    'rules' => [
        [
            'actions' => ['index'],
            'allow' => true,
            'roles' => ['@'],
        ],
    ],
];


$behaviors['authenticator'] = [

    'class' => \yii\filters\auth\HttpBasicAuth::className(),

    'auth' => function ($username, $password) {


        $user = \common\models\User::findByUsername($username);

        if ($user ) {

            $password_valid = \common\models\User::validatePassword($password,$user->password_hash);

            if($password_valid)
                return $user;

        }

    }

];

    return $behaviors;
    }




}

来自REST AUTH的数据响应

<pre>
<response>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
<item>...</item>
</response>
</pre>

1 个答案:

答案 0 :(得分:0)

那是def。默认行为,我必须编辑\ yii2 \ rest \ indexAction.php中的prepareDataProvider函数