/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = AccessApp::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['created' => $this->created, 'modified' => $this->modified]); $query->andFilterWhere(['like', 'appkey', $this->appkey])->andFilterWhere(['like', 'appname', $this->appname])->andFilterWhere(['like', 'client_id', $this->client_id])->andFilterWhere(['like', 'client_secret', $this->client_secret]); return $dataProvider; }
public static function validateAPIAuth($appkey, $clientid, $clientsecurity) { /** * @var AccessToken $accessToken * @var AccessApp $accessApp */ //app $accessApp = AccessApp::find()->andWhere('appkey=:appkey', array(':appkey' => $appkey))->one(); if (empty($accessApp)) { throw new Exception('传入appkey 错误'); } if ($accessApp->client_id != $clientid) { throw new Exception('client_id 错误'); } if ($accessApp->client_secret != $clientsecurity) { throw new Exception('client_secret 错误'); } $accessToken = AccessToken::findOne(array('clientid' => $clientid, 'appkey' => $appkey)); $usable = true; if (!empty($accessToken)) { $date1 = date_create(BaseDataHelper::getCurrentTime()); $date2 = date_create($accessToken->createtime); $diff = date_diff($date1, $date2); if ($diff->format('%y') > 0 || $diff->format('%m') > 0 || $diff->format('%d') > 0 || $diff->format('%h') > 0 || $diff->format('%i') > $accessToken->validity / 60) { $accessToken->delete(); $usable = false; } } else { $usable = false; } //token if (!$usable) { $accessToken = new AccessToken(); $accessToken->tokenid = DataHelper::random(10); $accessToken->appkey = $appkey; $accessToken->clientid = $clientid; $accessToken->validity = 600; //60秒 $accessToken->uid = $accessApp->uid; $accessToken->orgid = $accessApp->user->orgid; if (!$accessToken->save()) { var_dump($accessToken->errors); die; } //当前登录人信息 $session = Yii::$app->session; $model = AccessToken::findOne(array('tokenid' => $accessToken->tokenid)); $session->set('user', $model->user); } return $accessToken; }