Example #1
0
 public function index()
 {
     //        LeanUser::logIn("wupeixun", "123456");
     //        $user = LeanUser::getCurrentUser();
     //        $token = LeanUser::getCurrentSessionToken();
     $token = 'l8btuv8r1rcqrrjiw2e8oisak';
     // 给定一个 token 可以很容易的拿到用户
     $user = LeanUser::become($token);
     var_dump($user);
     // 我们还支持短信验证码,及第三方授权码登录
     // LeanUser::logInWithSmsCode("phone number", "sms code");
     // LeanUser::logInWith("weibo", array("openid" => "..."));
 }
Example #2
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);
     }
 }
Example #3
0
 /**
  * Initialize application key and settings
  *
  * @param string $appId        Application ID
  * @param string $appKey       Application key
  * @param string $appMasterKey Application master key
  */
 public static function initialize($appId, $appKey, $appMasterKey)
 {
     self::$appId = $appId;
     self::$appKey = $appKey;
     self::$appMasterKey = $appMasterKey;
     self::$defaultHeaders = array('X-LC-Id' => self::$appId, 'Content-Type' => 'application/json;charset=utf-8', 'User-Agent' => self::getVersionString());
     // Use session storage by default
     if (!self::$storage) {
         self::$storage = new SessionStorage();
     }
     // register LeanUser for object storage
     LeanUser::registerClass();
 }
Example #4
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");
 }
Example #5
0
 public function testUnlinkService()
 {
     $user = LeanUser::logInWith("weixin", $this->openToken);
     $token = $user->getSessionToken();
     $authData = $user->get("authData");
     $this->assertEquals($this->openToken, $authData["weixin"]);
     $user->unlinkWith("weixin");
     // re-login with user session token
     $user2 = LeanUser::become($token);
     $authData = $user2->get("authData");
     $this->assertTrue(!isset($authData["weixin"]));
     $user2->destroy();
 }
Example #6
0
 /**
  * Dispatch onVerified hook
  *
  * @param string $type Verify type: email or sms
  * @param array  $body JSON decoded body params
  */
 private function dispatchOnVerified($type, $body)
 {
     $userObj = LeanClient::decode($body["object"], null);
     LeanUser::saveCurrentUser($userObj);
     $meta["remoteAddress"] = $this->env["REMOTE_ADDR"];
     try {
         Cloud::runOnVerified($type, $userObj, $meta);
     } catch (FunctionError $err) {
         $this->renderError($err->getMessage(), $err->getCode());
     }
     $this->renderJSON(array("result" => "ok"));
 }
Example #7
0
 public function testFindUserWithSession()
 {
     $user = LeanUser::logIn("alice", "blabla");
     $query = new LeanQuery("_User");
     // it should not raise: 1 Forbidden to find by class permission.
     $query->first();
 }
Example #8
0
 /**
  * Delete objects in batch
  *
  * @param array $objects Array of LeanObjects to destroy
  */
 public static function destroyAll($objects)
 {
     $batch = array();
     foreach ($objects as $obj) {
         if (!$obj->getObjectId()) {
             throw new \RuntimeException("Cannot destroy object without ID");
         }
         // Remove duplicate objects by ID
         $batch[$obj->getObjectId()] = $obj;
     }
     if (empty($batch)) {
         return;
     }
     $requests = array();
     $objects = array();
     foreach ($batch as $obj) {
         $requests[] = array("path" => "/1.1/classes/{$obj->getClassName()}" . "/{$obj->getObjectId()}", "method" => "DELETE");
         $objects[] = $obj;
     }
     $sessionToken = LeanUser::getCurrentSessionToken();
     $response = LeanClient::batch($requests, $sessionToken);
     $errors = array();
     foreach ($objects as $i => $obj) {
         if (isset($response[$i]["error"])) {
             $errors[] = array("request" => $requests[$i], "error" => $response[$i]["error"]);
         }
     }
     if (count($errors) > 0) {
         throw new CloudException("Batch requests error: " . json_encode($errors));
     }
 }
Example #9
0
 /**
  * Build authentication headers
  *
  * @param string $sessionToken Session token of a LeanUser
  * @param bool   $useMasterKey
  * @return array
  */
 public static function buildHeaders($sessionToken, $useMasterKey)
 {
     if (is_null($useMasterKey)) {
         $useMasterKey = self::$useMasterKey;
     }
     $h = self::$defaultHeaders;
     $h['X-LC-Prod'] = self::$useProduction ? 1 : 0;
     $timestamp = time();
     $key = $useMasterKey ? self::$appMasterKey : self::$appKey;
     $sign = md5($timestamp . $key);
     $h['X-LC-Sign'] = $sign . "," . $timestamp;
     if ($useMasterKey) {
         $h['X-LC-Sign'] .= ",master";
     }
     if (!$sessionToken) {
         $sessionToken = LeanUser::getCurrentSessionToken();
     }
     if ($sessionToken) {
         $h['X-LC-Session'] = $sessionToken;
     }
     return $h;
 }