Esempio n. 1
0
 public function beforeSave($insert)
 {
     $user = AccessTokenService::getCurrentUser();
     if ($this->isNewRecord) {
         $this->contr_no = ContractService::generateNumber(empty($this->oldcontr), $this->type, $this->oldcontr);
         $this->orgid = $user->orgid;
         $this->createtime = BaseDataHelper::getCurrentTime();
         $this->userid = $user->id;
     }
     $this->modified = BaseDataHelper::getCurrentTime();
     return parent::beforeSave($insert);
     // TODO: Change the autogenerated stub
 }
 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;
 }