Example #1
0
 public function infoAction()
 {
     if ($resp = $this->checkUserInput(['user_id'], self::GET)) {
         $resp->flush();
         return false;
     }
     $model = UserManager::getInstance()->getUserById(ApiRequest::getParam('user_id'));
     if (!$model) {
         (new ApiResponse(Code::FAIL_USER_NOT_EXISTS, null))->flush();
     } else {
         (new ApiResponse(Code::SUCCESS, $model->toArray()))->flush();
     }
     return false;
 }
Example #2
0
 public function addLog(LoginLog $model)
 {
     $model->createTime = time();
     $model->ip = ApiRequest::getRemoteIp();
     if ($model->status != LoginLog::STATUS_SUCCESS) {
         $model->status = LoginLog::STATUS_FAILED;
     }
     $dbManager = Db::getInstance();
     $statement = $dbManager->prepare(self::ADD_LOGIN_LOG);
     if (!$statement->execute([$model->uid, $model->createTime, $model->ip, $model->status])) {
         return new ApiResponse(Code::FAIL_DATABASE_ERROR, 'add login log failed' . var_export($statement->errorInfo(), true));
     }
     return null;
 }
Example #3
0
 public function addUserEx(User $model)
 {
     $ip = ApiRequest::getRemoteIp();
     $regex = '/^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}(\\.\\d{1,3}\\.\\d{1,3})?$/';
     if (!$ip || !preg_match($regex, $ip)) {
         return new ApiResponse(Code::FAIL_INVALID_IP, null);
     }
     $dbManager = Db::getInstance();
     $statement = $dbManager->prepare(self::USER_COUNT_BY_IP);
     if (!$statement->execute([$ip, time() - 86400]) || false === ($number = $statement->fetchColumn())) {
         return new ApiResponse(Code::FAIL_DATABASE_ERROR, 'fetch create user count failed' . var_export($statement->errorInfo(), true));
     }
     if ($number > 100) {
         return new ApiResponse(Code::FAIL_USER_CREATE_TOO_FREQUENTLY, null);
     }
     return $this->addUser($model);
 }
Example #4
0
 public function listAction()
 {
     if ($resp = $this->checkUserInput(null, self::GET)) {
         $resp->flush();
         return false;
     }
     $roster = new Roster();
     $roster->uid = $this->user->id;
     $roster->group_id = ApiRequest::getParam('group_id');
     $roster->status = ApiRequest::getParam('status');
     $rosters = RosterManager::getInstance()->getRosterAll($roster);
     $list = [];
     foreach ($rosters as $r) {
         $list[] = $r->toArray();
     }
     (new ApiResponse(Code::SUCCESS, $list))->flush();
     return false;
 }
Example #5
0
 protected function checkAuth(Request_Abstract $request, Response_Abstract $response)
 {
     $config = ['allow' => ['api-user-create' => 1, 'api-user-login' => 1, 'api-user-recommend' => 1, 'api-user-info' => 1, 'api-group-recommend' => 1]];
     $id = strtolower($request->getModuleName() . '-' . $request->getControllerName() . '-' . $request->getActionName());
     if (!isset($config['allow'][$id])) {
         $user = new User();
         $user->id = ApiRequest::getParam('uid');
         $token = ApiRequest::getParam('token');
         if (!$user->id || TextUtil::isEmptyString($token)) {
             return new ApiResponse(Code::FAIL_PARAMETER_MISSING, 'uid or token is missing');
         }
         if ($resp = UserManager::getInstance()->getUser($user)) {
             return $resp;
         }
         if ($user->token != $token) {
             return new ApiResponse(Code::FAIL_USER_TOKEN_EXPIRE, null);
         }
         UserManager::getInstance()->setAuthorizedUser($user);
     }
     return null;
 }
Example #6
0
 protected function checkUserInput($param, $method)
 {
     /* @var \Yaf\Request\Http $request */
     $request = Dispatcher::getInstance()->getRequest();
     if ($method != null && $method != $request->getMethod()) {
         return new ApiResponse(Code::FAIL_REQUEST_METHOD, $method . ' support only');
     }
     if ($param) {
         foreach ($param as $k => $v) {
             if (is_int($k) && $k <= self::TYPE_MAX) {
                 switch ($k) {
                     case self::LONG:
                         if (!is_long($v)) {
                             return new ApiResponse(Code::FAIL_PARAMETER_TYPE, $v . ' should be long');
                         }
                         break;
                     case self::INT:
                         if (!is_int($v)) {
                             return new ApiResponse(Code::FAIL_PARAMETER_TYPE, $v . ' should be integer');
                         }
                         break;
                     case self::UNSIGNED_INT:
                         if (!is_int($v) || $v < 0) {
                             return new ApiResponse(Code::FAIL_PARAMETER_TYPE, $v . ' should be unsigned integer');
                         }
                         break;
                     case self::STRING:
                         if (!is_string($v)) {
                             return new ApiResponse(Code::FAIL_PARAMETER_TYPE, $v . ' should be string');
                         }
                         break;
                     case self::FLOAT:
                         if (!is_float($v)) {
                             return new ApiResponse(Code::FAIL_PARAMETER_TYPE, $v . ' should be float');
                         }
                         break;
                     case self::DOUBLE:
                         if (!is_double($v)) {
                             return new ApiResponse(Code::FAIL_PARAMETER_TYPE, $v . ' should be float');
                         }
                         break;
                     case self::FILE:
                         if ($request->getFiles($v) === null) {
                             return new ApiResponse(Code::FAIL_PARAMETER_TYPE, $v . ' should be file');
                         }
                         break;
                     default:
                         return new ApiResponse(Code::FAIL_PARAMETER_TYPE, 'type of ' . $v . ' not support');
                 }
             } else {
                 if (ApiRequest::getParam($v) === null) {
                     return new ApiResponse(Code::FAIL_PARAMETER_MISSING, 'parameter ' . $v . ' missing');
                 }
             }
         }
     }
     return null;
 }
Example #7
0
 public function list_userAction()
 {
     if ($resp = $this->checkUserInput(['group_id'], self::GET)) {
         $resp->flush();
         return false;
     }
     if (!($group = GroupManager::getInstance()->getGroupById(ApiRequest::getParam('group_id')))) {
         (new ApiResponse(Code::FAIL_GROUP_NOT_EXISTS, null))->flush();
         return false;
     }
     $groupUserManager = GroupUserManager::getInstance();
     if (!($groupUser = $groupUserManager->getGroupUserByGidUid($group->id, $this->user->id))) {
         (new ApiResponse(Code::FAIL_GROUP_USER_NOT_EXISTS, null))->flush();
         return false;
     }
     $status = ApiRequest::getParam('status');
     $groupUserModel = new GroupUser();
     if ($group->type == Group::TYPE_CHATROOM) {
         $groupUserModel->status = GroupUser::STATUS_AGREE;
     } else {
         $groupUserModel->status = $status;
         if (!$groupUserModel->checkStatus()) {
             (new ApiResponse(Code::FAIL_GROUP_USER_STATUS, 'chatroom must set status'))->flush();
             return false;
         }
         if ($status != GroupUser::STATUS_AGREE && $groupUser->permission < GroupUser::PERMISSION_ADMIN) {
             (new ApiResponse(Code::FAIL_GROUP_USER_PERMISSION, null))->flush();
             return false;
         }
     }
     $objects = $groupUserManager->getAllGroupUser($group, $groupUserModel, $this->getOffset(), $this->getLimit());
     if (is_array($objects)) {
         $list = [];
         foreach ($objects as $o) {
             $list[] = $o->toArray();
         }
         (new ApiResponse(Code::SUCCESS, $list))->flush();
     } else {
         (new ApiResponse($groupUserManager->getLastErrorCode(), $groupUserManager->getLastErrorString()))->flush();
     }
     return false;
 }