Exemple #1
0
 public function create($name, $class, $config)
 {
     // check whether action exists
     $condition = new Condition();
     $condition->equals('name', $name);
     $action = $this->actionTable->getOneBy($condition);
     if (!empty($action)) {
         throw new StatusCode\BadRequestException('Action already exists');
     }
     // create action
     $this->actionTable->create(array('status' => Table\Action::STATUS_ACTIVE, 'name' => $name, 'class' => $class, 'config' => $config, 'date' => new \DateTime()));
 }
Exemple #2
0
 public function getRoutes($startIndex = 0, $search = null)
 {
     $condition = new Condition();
     $condition->equals('status', self::STATUS_ACTIVE);
     $condition->notLike('path', '/backend%');
     $condition->notLike('path', '/consumer%');
     $condition->notLike('path', '/doc%');
     $condition->notLike('path', '/authorization%');
     $condition->notLike('path', '/export%');
     if (!empty($search)) {
         $condition->like('path', '%' . $search . '%');
     }
     $definition = ['totalResults' => $this->getCount($condition), 'startIndex' => $startIndex, 'itemsPerPage' => 16, 'entry' => $this->doCollection([$this, 'getAll'], [$startIndex, 16, null, Sql::SORT_DESC, $condition], ['id' => 'id', 'status' => 'status', 'path' => 'path', 'controller' => 'controller'])];
     return $this->build($definition);
 }
Exemple #3
0
 public function create($name, $description, array $routes = null)
 {
     // check whether scope exists
     $condition = new Condition();
     $condition->equals('name', $name);
     $scope = $this->scopeTable->getOneBy($condition);
     if (!empty($scope)) {
         throw new StatusCode\BadRequestException('Scope already exists');
     }
     try {
         $this->scopeTable->beginTransaction();
         // create scope
         $this->scopeTable->create(array('name' => $name, 'description' => $description));
         // insert routes
         $scopeId = $this->scopeTable->getLastInsertId();
         $this->insertRoutes($scopeId, $routes);
         $this->scopeTable->commit();
     } catch (\Exception $e) {
         $this->scopeTable->rollBack();
         throw $e;
     }
 }
Exemple #4
0
 public function getCondition($alias = null)
 {
     $alias = $alias !== null ? $alias . '.' : '';
     $condition = new Condition();
     $condition->greaterThen($alias . 'date', $this->from->format('Y-m-d 00:00:00'));
     $condition->lowerThen($alias . 'date', $this->to->format('Y-m-d 23:59:59'));
     if (!empty($this->appId)) {
         $condition->equals($alias . 'appId', $this->appId);
     }
     if (!empty($this->routeId)) {
         $condition->equals($alias . 'routeId', $this->routeId);
     }
     if (!empty($this->ip)) {
         $condition->like($alias . 'ip', $this->ip);
     }
     if (!empty($this->userAgent)) {
         $condition->like($alias . 'userAgent', '%' . $this->userAgent . '%');
     }
     if (!empty($this->method)) {
         $condition->equals($alias . 'method', $this->method);
     }
     if (!empty($this->path)) {
         $condition->like($alias . 'path', $this->path . '%');
     }
     if (!empty($this->header)) {
         $condition->like($alias . 'header', '%' . $this->header . '%');
     }
     if (!empty($this->body)) {
         $condition->like($alias . 'body', '%' . $this->body . '%');
     }
     return $condition;
 }
Exemple #5
0
 public function create($userId, $name, $url, array $scopes = null)
 {
     // validate data
     $this->assertName($name);
     $this->assertUrl($url);
     // check limit of apps which an user can create
     $condition = new Condition();
     $condition->equals('userId', $userId);
     $condition->in('status', [Table\App::STATUS_ACTIVE, Table\App::STATUS_PENDING, Table\App::STATUS_DEACTIVATED]);
     if ($this->appTable->getCount($condition) > $this->appCount) {
         throw new StatusCode\BadRequestException('Maximal amount of apps reached. Please delete another app in order to register a new one');
     }
     $scopes = $this->getValidUserScopes($userId, $scopes);
     if (empty($scopes)) {
         throw new StatusCode\BadRequestException('Provide at least one valid scope for the app');
     }
     $this->appService->create($userId, $this->appApproval === false ? Table\App::STATUS_ACTIVE : Table\App::STATUS_PENDING, $name, $url, null, $scopes);
 }
Exemple #6
0
 public function create($path, $config)
 {
     // check whether route exists
     $condition = new Condition();
     $condition->equals('status', Table\Routes::STATUS_ACTIVE);
     $condition->equals('path', $path);
     $route = $this->routesTable->getOneBy($condition);
     if (!empty($route)) {
         throw new StatusCode\BadRequestException('Route already exists');
     }
     try {
         $this->routesTable->beginTransaction();
         // create route
         $this->routesTable->create(['status' => Table\Routes::STATUS_ACTIVE, 'methods' => 'GET|POST|PUT|DELETE', 'path' => $path, 'controller' => 'Fusio\\Impl\\Controller\\SchemaApiController']);
         // get last insert id
         $routeId = $this->routesTable->getLastInsertId();
         $this->handleConfig($routeId, $path, $config);
         $this->routesTable->commit();
     } catch (\Exception $e) {
         $this->routesTable->rollBack();
         throw $e;
     }
 }
Exemple #7
0
 public function createRemote($provider, $id, $name, $email, array $scopes = null)
 {
     // check whether user exists
     $condition = new Condition();
     $condition->equals('provider', $provider);
     $condition->equals('remoteId', $id);
     $user = $this->userTable->getOneBy($condition);
     if (!empty($user)) {
         return $user->id;
     }
     // replace spaces with a dot
     $name = str_replace(' ', '.', $name);
     // check values
     $this->assertName($name);
     if (!empty($email)) {
         $this->assertEmail($email);
     } else {
         $email = null;
     }
     try {
         $this->userTable->beginTransaction();
         // create user
         $this->userTable->create(array('provider' => $provider, 'status' => Table\User::STATUS_CONSUMER, 'remoteId' => $id, 'name' => $name, 'email' => $email, 'password' => null, 'date' => new DateTime()));
         $userId = $this->userTable->getLastInsertId();
         // add scopes
         $this->insertScopes($userId, $scopes);
         $this->userTable->commit();
     } catch (\Exception $e) {
         $this->userTable->rollBack();
         throw $e;
     }
     return $userId;
 }
Exemple #8
0
 public function create($name, $class, $config)
 {
     // check whether connection exists
     $condition = new Condition();
     $condition->equals('name', $name);
     $connection = $this->connectionTable->getOneBy($condition);
     if (!empty($connection)) {
         throw new StatusCode\BadRequestException('Connection already exists');
     }
     $this->testConnection($class, $config);
     // create connection
     $this->connectionTable->create(array('name' => $name, 'class' => $class, 'config' => self::encryptConfig($config, $this->secretKey)));
 }
Exemple #9
0
 /**
  * @param string $ip
  * @param string $timespan
  * @param \Fusio\Engine\Model\App $app
  * @return integer
  */
 protected function getRequestCount($ip, $timespan, Model\App $app)
 {
     if (empty($timespan)) {
         return 0;
     }
     $now = new \DateTime();
     $past = new \DateTime();
     $past->sub(new \DateInterval($timespan));
     $condition = new Condition();
     if ($app->isAnonymous()) {
         $condition->equals('ip', $ip);
     } else {
         $condition->equals('appId', $app->getId());
     }
     $condition->between('date', $past->format('Y-m-d H:i:s'), $now->format('Y-m-d H:i:s'));
     return $this->logTable->getCount($condition);
 }
Exemple #10
0
 public function getMethod($routeId, $version, $method)
 {
     if ($version == '*' || empty($version)) {
         $version = $this->methodTable->getLatestVersion($routeId);
     }
     $condition = new Condition();
     $condition->equals('routeId', $routeId);
     $condition->equals('method', $method);
     $condition->equals('version', $version);
     $condition->equals('active', Resource::STATUS_ACTIVE);
     return $this->methodTable->getOneBy($condition);
 }
Exemple #11
0
 public function create($name, $source)
 {
     if (!preg_match('/^[A-z0-9\\-\\_]{3,64}$/', $name)) {
         throw new StatusCode\BadRequestException('Invalid schema name');
     }
     // check whether schema exists
     $condition = new Condition();
     $condition->equals('name', $name);
     $connection = $this->schemaTable->getOneBy($condition);
     if (!empty($connection)) {
         throw new StatusCode\BadRequestException('Connection already exists');
     }
     // create schema
     $this->schemaTable->create(array('status' => Table\Schema::STATUS_ACTIVE, 'name' => $name, 'source' => $source, 'cache' => $this->schemaParser->parse(json_encode($source))));
 }
Exemple #12
0
 public function create($userId, $status, $name, $url, $parameters = null, array $scopes = null)
 {
     // check whether app exists
     $condition = new Condition();
     $condition->equals('userId', $userId);
     $condition->notEquals('status', Table\App::STATUS_DELETED);
     $condition->equals('name', $name);
     $app = $this->appTable->getOneBy($condition);
     if (!empty($app)) {
         throw new StatusCode\BadRequestException('App already exists');
     }
     // parse parameters
     if ($parameters !== null) {
         $parameters = $this->parseParameters($parameters);
     }
     // create app
     $appKey = TokenGenerator::generateAppKey();
     $appSecret = TokenGenerator::generateAppSecret();
     try {
         $this->appTable->beginTransaction();
         $this->appTable->create(array('userId' => $userId, 'status' => $status, 'name' => $name, 'url' => $url, 'parameters' => $parameters, 'appKey' => $appKey, 'appSecret' => $appSecret, 'date' => new DateTime()));
         $appId = $this->appTable->getLastInsertId();
         if ($scopes !== null) {
             // insert scopes
             $this->insertScopes($appId, $scopes);
         }
         $this->appTable->commit();
     } catch (\Exception $e) {
         $this->appTable->rollBack();
         throw $e;
     }
 }
Exemple #13
0
 protected function saveUserDecision($appId, $allow)
 {
     $condition = new Condition();
     $condition->equals('userId', $this->userId);
     $condition->equals('appId', $appId);
     $table = $this->tableManager->getTable('Fusio\\Impl\\Table\\User\\Grant');
     $userApp = $table->getOneBy($condition);
     if (empty($userApp)) {
         $table->create(['userId' => $this->userId, 'appId' => $appId, 'allow' => $allow ? 1 : 0, 'date' => new \DateTime()]);
     } else {
         $table->update(['id' => $userApp['id'], 'userId' => $this->userId, 'appId' => $appId, 'allow' => $allow ? 1 : 0, 'date' => new \DateTime()]);
     }
 }