コード例 #1
0
ファイル: DomainManager.php プロジェクト: Tanklong/openvj
 /**
  * 加入域
  *
  * @param int $uid
  * @param \MongoId $domainId
  * @return bool
  * @throws InvalidArgumentException
  * @throws UserException
  */
 public function joinDomainById($uid, \MongoId $domainId)
 {
     if (!Validator::int()->validate($uid)) {
         throw new InvalidArgumentException('uid', 'type_invalid');
     }
     $uid = (int) $uid;
     if (!DomainUtil::isGlobalDomainId($domainId)) {
         // 检查域是否存在
         $d = Application::coll('Domain')->findOne(['_id' => $domainId]);
         if (!DomainUtil::isDomainObjectValid($d)) {
             throw new UserException('DomainManager.joinDomain.invalid_domain');
         }
     }
     // 检查用户是否存在
     $user = UserUtil::getUserObjectByUid($uid);
     if (!UserUtil::isUserObjectValid($user)) {
         throw new UserException('DomainManager.joinDomain.invalid_user');
     }
     // 添加 MEMBER 角色
     Application::coll('UserRole')->update(['uid' => $uid], ['$addToSet' => ['d.' . (string) $domainId => 'DOMAIN_MEMBER']], ['upsert' => true]);
     // 创建空资料
     $document = ['pref' => new \stdClass(), 'rp' => 0.0, 'rp_s' => 0.0, 'rank' => -1, 'level' => 0];
     if (DomainUtil::isGlobalDomainId($domainId)) {
         $document += ['sig' => '', 'sigraw' => '', 'contacts' => []];
     }
     Application::coll('UserInfo')->update(['uid' => $uid, 'domain' => new \MongoId(VJ::DOMAIN_GLOBAL)], ['$setOnInsert' => $document], ['upsert' => true]);
     // 操作非全局域则插入操作记录
     if (!DomainUtil::isGlobalDomainId($domainId)) {
         $doc = ['uid' => $this->session->getCurrentToken(), 'at' => new \MongoDate(), 'type' => 'join', 'ua' => $this->request->getUserAgent(), 'ip' => $this->request->getClientIp(), 'target_uid' => $uid, 'target_domain' => $domainId];
         Application::coll('DomainLog')->insert($doc);
     }
     return true;
 }
コード例 #2
0
ファイル: UserCredential.php プロジェクト: Tanklong/openvj
 /**
  * 检查 记住我 token 是否正确
  *
  * @param string $clientToken
  * @param bool $secretly
  * @return array
  * @throws UserException
  */
 public function checkRememberMeTokenCredential($token, $secretly = false)
 {
     try {
         $tokenRec = Application::get('token_manager')->find('rememberme', $token);
         if ($tokenRec === null) {
             throw new UserException('UserCredential.checkRememberMeTokenCredential.invalid_rememberme_token');
         }
         //是否需要检查 user-agent 和 ip 地址呢
         $user = UserUtil::getUserObjectByUid($tokenRec['data']['uid']);
         if (!UserUtil::isUserObjectValid($user)) {
             throw new UserException('UserCredential.checkRememberMeTokenCredential.user_not_valid');
         }
         if (!$secretly) {
             Application::emit('user.login.succeeded', [VJ::LOGIN_TYPE_TOKEN, $user]);
             Application::info('credential.login.ok', ['uid' => $user['uid']]);
         }
         return $user;
     } catch (InvalidArgumentException $e) {
         throw new UserException('UserCredential.checkRememberMeTokenCredential.invalid_rememberme_token');
     }
 }