Example #1
0
 protected function loginByCookie()
 {
     $value = isset($_COOKIE['_identity']) ? $_COOKIE['_identity'] : null;
     if ($value === null) {
         return;
     }
     $data = json_decode($value, true);
     if (count($data) !== 3 || !isset($data[0], $data[1], $data[2])) {
         return;
     }
     list($id, $authKey, $duration) = $data;
     $identity = User::findIdentity($id);
     if ($identity === null) {
         return;
     } elseif (!$identity instanceof IdentityInterface) {
         throw new \ErrorException("User::findIdentity() must return an object implementing IdentityInterface.");
     }
     if ($identity->validateAuthKey($authKey)) {
         $this->switchIdentity($identity, $duration);
         Application::info("User '{$id}' logged in via cookie.", __METHOD__);
     } else {
         Application::warning("Invalid auth key attempted for user '{$id}': {$authKey}", __METHOD__);
     }
 }