示例#1
0
 /**
  * Initialize file
  *
  * @param string $name     File base name
  * @param mixed  $data     (optional) File content
  * @param string $mimeType (optional) Mime type
  */
 public function __construct($name, $data = null, $mimeType = null)
 {
     $this->_data["name"] = $name;
     $this->_source = $data;
     if (!$mimeType) {
         $ext = pathinfo($name, PATHINFO_EXTENSION);
         $mimeType = MIMEType::getType($ext);
     }
     $this->_data["mime_type"] = $mimeType;
     $user = LeanUser::getCurrentUser();
     $this->_metaData["owner"] = $user ? $user->getObjectId() : "unknown";
     if ($this->_source) {
         $this->_metaData["size"] = strlen($this->_source);
     }
 }
示例#2
0
 public function testCircularGetCurrentUser()
 {
     // ensure getCurrentUser neither run indefinetely, nor throw maximum
     // function all error
     $avatar = LeanFile::createWithUrl("alice.png", "https://leancloud.cn/favicon.png");
     $user = LeanUser::logIn("alice", "blabla");
     $user->set("avatar", $avatar);
     $user->save();
     $token = LeanUser::getCurrentSessionToken();
     $user->logOut();
     LeanUser::setCurrentSessionToken($token);
     $user2 = LeanUser::getCurrentUser();
     $this->assertEquals($user2->getUsername(), "alice");
 }
示例#3
0
 public function testSignUpWithLinkedService()
 {
     $user = LeanUser::logInWith("weixin", $this->openToken);
     $this->assertNotEmpty($user->getSessionToken());
     $this->assertNotEmpty($user->getObjectId());
     $this->assertEquals($user, LeanUser::getCurrentUser());
     $user->destroy();
 }
示例#4
0
 /**
  * Dispatch class hook and render result
  *
  * @param string $className
  * @param string $hookName
  * @param array  $body     JSON decoded body params
  */
 private function dispatchHook($className, $hookName, $body)
 {
     $json = $body["object"];
     $json["__type"] = "Object";
     $json["className"] = $className;
     $obj = LeanClient::decode($json, null);
     // set hook marks to prevent infinite loop. For example if user
     // invokes `$obj->save` in an afterSave hook, API will not again
     // invoke afterSave if we set hook marks.
     foreach (array("__before", "__after", "__after_update") as $key) {
         if (isset($json[$key])) {
             $obj->set($key, $json[$key]);
         }
     }
     // in beforeUpdate hook, attach updatedKeys to object so user
     // can detect changed keys in hook.
     if (isset($json["_updatedKeys"])) {
         $obj->updatedKeys = $json["_updatedKeys"];
     }
     $meta["remoteAddress"] = $this->env["REMOTE_ADDR"];
     try {
         $result = Cloud::runHook($className, $hookName, $obj, LeanUser::getCurrentUser(), $meta);
     } catch (FunctionError $err) {
         $this->renderError($err->getMessage(), $err->getCode());
     }
     if ($hookName == "beforeDelete") {
         $this->renderJSON(array());
     } else {
         if (strpos($hookName, "after") === 0) {
             $this->renderJSON(array("result" => "ok"));
         } else {
             $outObj = $result;
             // Encode result object to type-less literal JSON
             $this->renderJSON($outObj->toJSON());
         }
     }
 }