/** * Finds the AccessToken model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return AccessToken the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = AccessToken::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
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; }