Skip to content

Commit

Permalink
Merge pull request #14 from gregorip02/2.x
Browse files Browse the repository at this point in the history
[2.x] new permissive authorizer
  • Loading branch information
gregorip02 authored Jul 19, 2020
2 parents 6ef10d5 + 84281bb commit 110b999
Show file tree
Hide file tree
Showing 23 changed files with 173 additions and 63 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ jobs:
- name: Install dependencies
run: |
composer update --prefer-stable --prefer-dist --no-interaction --no-suggest
- name: Execute tests
run: vendor/bin/phpunit
run: composer test
8 changes: 5 additions & 3 deletions NEXT.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
- [x] Add commands for make **authorizers** and **resolvers.**
- [ ] Add mutation **clausule** for update resources.
- [ ] Add mutation **clausule** for delete resources.
- [ ] Allow to add multiple where clausules and document it.
- [ ] Create **orWhere** clausule.
- [ ] Create **whereBetween** clausule.
- [x] Create **whereIn** clausule.
- [x] Create **whereNotIn** clausule.
- [ ] Add **pagination** support.
- [ ] Add **pagination** documentation.
- [ ] Create **orWhere** clausule.
- [ ] Allow to select attributes from resolvers.
- [ ] Add **pagination** support and documentation.
- [x] Move all testing for SQLite.
- [x] Update the wiki and write documentation about the **RestQL.**
- [x] Implement `orchestra/testbench` testing for "real world" situations.
- [x] Implement `orchestra/testbench` testing for "real world" situations.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "gregorip02/restql",
"description": "A data resolution package for Laravel",
"type": "library",
"keywords": ["restql", "eloquent", "laravel", "graphql", "drp"],
"keywords": ["restql", "eloquent", "laravel", "graphql"],
"url": "https://github.com/gregorip02/restql",
"license": "LGPL-3.0",
"authors": [
Expand Down
2 changes: 1 addition & 1 deletion config/restql.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
// Uncoment this and get the currently authenticated user.
// 'whoami' => [
// 'class' => 'Restql\Resolvers\WhoamiResolver',
// 'authorizer' => 'Restql\Authorizers\WhoamiAuthorizer',
// 'authorizer' => 'Restql\Authorizers\PermissiveAuthorizer',
// 'middlewares' => ['auth']
// ]
],
Expand Down
11 changes: 6 additions & 5 deletions src/Arguments/WhereArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Restql\Arguments;

use Restql\Contracts\ArgumentContract;
use Restql\ModelArgument;
use Restql\Contracts\ArgumentContract;

class WhereArgument extends ModelArgument implements ArgumentContract
{
Expand All @@ -22,11 +22,11 @@ class WhereArgument extends ModelArgument implements ArgumentContract
public function values(): array
{
if ($this->isImplicitValue()) {
$value = $this->first();

/// When an implicit type value is received, it will be assumed that
/// it corresponds to the value of the primary column of the model.
return compact('value');
return [
'value' => $this->first()
];
}

return parent::values();
Expand All @@ -42,7 +42,8 @@ public function getDefaultArgumentValues(): array
return [
'column' => $this->getKeyName(),
'operator' => '=',
'value' => null
'value' => null,
'boolean' => 'and'
];
}
}
30 changes: 28 additions & 2 deletions src/Arguments/WhereInOrNotInArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,36 @@

namespace Restql\Arguments;

use Restql\Arguments\WhereArgument;
use Restql\ModelArgument;
use Restql\Contracts\ArgumentContract;

class WhereInOrNotInArgument extends WhereArgument
class WhereInOrNotInArgument extends ModelArgument implements ArgumentContract
{
/**
* Determines if the argument accepts implicit values.
*
* @var boolean
*/
protected $hasImplicitValues = true;

/**
* Get the argument values as array.
*
* @return array
*/
public function values(): array
{
if ($this->isImplicitValue()) {
/// When an implicit type value is received, it will be assumed that
/// it corresponds to the value of the primary column of the model.
return [
'value' => (array) $this->first()
];
}

return parent::values();
}

/**
* Get default argument values.
*
Expand Down
10 changes: 5 additions & 5 deletions src/Arguments/WithArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Restql\Arguments;

use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Collection;
use Restql\ClausuleExecutor;
use Restql\Contracts\ArgumentContract;
use Restql\ModelArgument;
use Restql\ClausuleExecutor;
use Restql\Support\ReflectionSupport;
use Restql\Contracts\ArgumentContract;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\Relation;

class WithArgument extends ModelArgument implements ArgumentContract
{
Expand Down
19 changes: 19 additions & 0 deletions src/Authorizers/PermissiveAuthorizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Restql\Authorizers;

use Restql\Authorizer;

final class PermissiveAuthorizer extends Authorizer
{
/**
* Can get one or more resources.
*
* @param array $clausules
* @return bool
*/
public static function get($clausules = []): bool
{
return true;
}
}
20 changes: 0 additions & 20 deletions src/Authorizers/WhoamiAuthorizer.php

This file was deleted.

9 changes: 4 additions & 5 deletions src/Clausules/SelectClausule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

namespace Restql\Clausules;

use Illuminate\Database\Eloquent\Builder as QueryBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Collection;
use Restql\Argument;
use Restql\Arguments\SelectArgument;
use Restql\Clausule;
use Restql\Arguments\SelectArgument;
use Restql\Support\ReflectionSupport;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Builder as QueryBuilder;

class SelectClausule extends Clausule
{
Expand Down
4 changes: 2 additions & 2 deletions src/Clausules/WhereClausule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Restql\Clausules;

use Illuminate\Database\Eloquent\Builder as QueryBuilder;
use Restql\Argument;
use Restql\Arguments\WhereArgument;
use Restql\Clausule;
use Restql\Arguments\WhereArgument;
use Restql\Exceptions\MissingArgumentException;
use Illuminate\Database\Eloquent\Builder as QueryBuilder;

class WhereClausule extends Clausule
{
Expand Down
2 changes: 1 addition & 1 deletion src/Clausules/WhereInClausule.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function build(QueryBuilder $builder): void
{
$args = array_values($this->arguments->data());

$builder->whereIn($args[0], (array) $args[1]);
$builder->whereIn(...$args);
}

/**
Expand Down
13 changes: 12 additions & 1 deletion src/Clausules/WhereNotInClausule.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ public function build(QueryBuilder $builder): void
{
$args = array_values($this->arguments->data());

$builder->whereNotIn($args[0], (array) $args[1]);
$builder->whereNotIn(...$args);
}

/**
* Throw a exception if can't build this clausule.
*
* @return void
*/
protected function canBuild(): void
{
parent::throwIfMethodIsNotAllowed('whereNotIn');
$this->throwIfArgumentIsMissing('whereNotIn');
}
}
9 changes: 2 additions & 7 deletions src/Clausules/WithClausule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@

namespace Restql\Clausules;

use Illuminate\Database\Eloquent\Builder as QueryBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Collection;
use Restql\Clausule;
use Restql\Argument;
use Restql\Arguments\WithArgument;
use Restql\Clausule;
use Restql\Support\ReflectionSupport;
use Illuminate\Database\Eloquent\Builder as QueryBuilder;

class WithClausule extends Clausule
{
Expand Down
1 change: 0 additions & 1 deletion src/MutationClausule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder as QueryBuilder;

abstract class MutationClausule extends Clausule
{
Expand Down
2 changes: 0 additions & 2 deletions src/RequestParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Restql\Exceptions\InvalidEncodingValue;
use Restql\Services\ConfigService;
use Restql\Traits\HasConfigService;

final class RequestParser
Expand Down
1 change: 0 additions & 1 deletion src/Resolvers/QueryBuilderResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Restql\Resolver;
use Restql\SchemaDefinition;
use Restql\Traits\ModelResolver;
use Illuminate\Database\Eloquent\Builder as QueryBuilder;

final class QueryBuilderResolver extends Resolver implements SchemaHandlerContract
{
Expand Down
3 changes: 0 additions & 3 deletions src/Resolvers/WhoamiResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ public function handle(SchemaDefinition $schema): Collection
{
$user = Auth::user();

/// Uncoment this for testing...
/// $user = factory('App\Author', 1)->make();

return Collection::make($user);
}
}
1 change: 0 additions & 1 deletion src/Restql.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Illuminate\Http\Response;
use Illuminate\Support\Collection;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Database\Eloquent\Builder as QueryBuilder;

final class Restql implements Responsable
{
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion tests/App/config/restql.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
'resolvers' => [
'whoami' => [
'class' => 'Restql\Resolvers\WhoamiResolver',
'authorizer' => 'Restql\Authorizers\WhoamiAuthorizer',
'authorizer' => 'Restql\Authorizers\PermissiveAuthorizer',
'middlewares' => ['auth']
]
],
Expand Down
42 changes: 42 additions & 0 deletions tests/Feature/Clausules/WhereInClausuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Testing\Feature\Clausules;

use Testing\TestCase;

final class WhereInClausuleTest extends TestCase
{
/**
* Fake in articles id.
*
* @var array
*/
protected $articles = [1, 3, 10, 14];

/**
* @test Get articles where id in (1, 14, 3, 10)
*/
public function getArticlesWhereIdInUsingImplicitMethod(): void
{
$response = $this->json('get', 'restql', [
'articles' => [
'whereIn' => [$this->articles],
'select' => 'id'
]
]);

$response->assertJsonCount(count($this->articles), 'data.articles');

$response->assertExactJson([
'data' => [
'articles' => array_map(function (int $id) {
return ['id' => $id];
}, $this->articles)
]
]);

$articles = $response->decodeResponseJson('data.articles.*.id');

$this->assertEquals($this->articles, $articles);
}
}
42 changes: 42 additions & 0 deletions tests/Feature/Clausules/WhereNotInClausuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Testing\Feature\Clausules;

use Testing\TestCase;

final class WhereNotInClausuleTest extends TestCase
{
/**
* Fake not in articles.
*
* @var array
*/
protected $articles = [4, 5, 9, 10];

/**
* @test Exclude articles where id not in (4, 5, 9, 10)
*/
public function excludeArticlesWhereIdNotInUsingImplicitMethod(): void
{
$response = $this->json('get', 'restql', [
'articles' => [
'select' => 'id',
'whereNotIn' => [$this->articles]
]
]);

/// We get 15 by default because we are excluding [$this->articles]
/// and this is rejected with others articles.
$response->assertJsonCount(15, 'data.articles');

$articles = $response->decodeResponseJson('data.articles.*.id');

/// Don't see any article id defined in $this->articles in
/// $articles responsed by RestQL.
foreach ($this->articles as $article) {
$this->assertFalse(
in_array($article, $articles)
);
}
}
}

0 comments on commit 110b999

Please sign in to comment.