Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply fixes from StyleCI #10

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use DtKt\Restrictions\Enum\RestrictionType;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
* Class CreateRestrictionsTable.
Expand All @@ -21,15 +21,15 @@ public function up()
{
Schema::create('restrictions', function (Blueprint $table) {
$table->increments('id');
$table->string("entity_type")->charset('latin1');
$table->unsignedBigInteger("entity_id");
$table->string('entity_type')->charset('latin1');
$table->unsignedBigInteger('entity_id');
$table->string('restriction')->charset('latin1');
$table->unsignedTinyInteger('type')->default(RestrictionType::DENY);
$table->boolean('enabled');
$table->timestamps();

$table->index(['restriction', 'enabled', 'type']);
$table->index(["entity_type", "entity_id"]);
$table->index(['entity_type', 'entity_id']);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
* Class CreateRestrictionsRulesTable.
Expand Down
4 changes: 2 additions & 2 deletions src/Entities/Restriction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace DKulyk\Restrictions\Entities;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
* Class Restriction.
Expand Down
2 changes: 1 addition & 1 deletion src/Restrictions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/**
* Class Restrictions.
*
*
* @method static array pushRestrictions(array $restrictions, bool $merge = true) Push restrictions to scope and returns new restrictions.
* @method static array popRestrictions() Pop restrictions from scope and returns them.
* @method static void where(array $restrictions, $merge = true, callable $callback = null)
Expand Down
2 changes: 1 addition & 1 deletion src/RestrictionsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ public function getRestrictions(): array
{
return end($this->restrictions) ?: [];
}
}
}
19 changes: 10 additions & 9 deletions src/RestrictionsScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

namespace DKulyk\Restrictions;

use ReflectionMethod;
use Illuminate\Contracts\Support\Arrayable;
use DKulyk\Restrictions\Entities\Restriction;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\{Model, Scope, Builder, Collection};
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Query\Builder as Query;
use ReflectionMethod;

/**
* Class RestrictionsScope.
Expand Down Expand Up @@ -77,7 +80,6 @@ private function applyRestrictions(Builder $builder, Model $model, array $restri

$builder->where(function (Builder $builder) use ($model, $restrictions, $morphMap, $addHasWhere) {
foreach ($restrictions as $restriction => $keys) {

$restrictionModel = Relation::getMorphedModel($restriction) ?? $restriction;
if (! in_array($restrictionModel, $this->allowedRestrictions)) {
continue;
Expand All @@ -104,7 +106,6 @@ private function applyRestrictions(Builder $builder, Model $model, array $restri
Relation::getMorphedModel($restriction),
]);


//Deny query
$hasQuery = $relation->getRelationExistenceQuery($relation->getRelated()->newQuery(), $builder);

Expand All @@ -115,10 +116,10 @@ private function applyRestrictions(Builder $builder, Model $model, array $restri
->where('restriction', $restrictionModel)
->where('type', Restriction::DENY)
->where('enabled', true)
->whereExists(function(Query $query) use ($rules,$keys){
->whereExists(function (Query $query) use ($rules,$keys) {
$query->from($rules->getTable())
->whereColumn($rules->getQualifiedParentKeyName(), '=', $rules->getQualifiedForeignPivotKeyName())
->wherein($rules->getQualifiedRelatedPivotKeyName(), (array)$keys);
->wherein($rules->getQualifiedRelatedPivotKeyName(), (array) $keys);
});

$addHasWhere->invoke($builder, $hasQuery, $relation, '<', 1, 'and');
Expand All @@ -132,10 +133,10 @@ private function applyRestrictions(Builder $builder, Model $model, array $restri
->where('restriction', $restrictionModel)
->where('type', Restriction::ALLOW)
->where('enabled', true)
->whereNotExists(function(Query $query) use ($rules,$keys){
->whereNotExists(function (Query $query) use ($rules,$keys) {
$query->from($rules->getTable())
->whereColumn($rules->getQualifiedParentKeyName(), '=', $rules->getQualifiedForeignPivotKeyName())
->wherein($rules->getQualifiedRelatedPivotKeyName(), (array)$keys);
->wherein($rules->getQualifiedRelatedPivotKeyName(), (array) $keys);
});

$addHasWhere->invoke($builder, $hasQuery, $relation, '<', 1, 'and');
Expand Down
5 changes: 2 additions & 3 deletions src/RestrictionsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

namespace DKulyk\Restrictions;

use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\ServiceProvider;

/**
* Class RestrictionsServiceProvider.
*/
final class RestrictionsServiceProvider extends ServiceProvider
{

public function register()
{
$this->app->singleton('eloquent-restrictions', function () {
Expand Down Expand Up @@ -45,7 +44,7 @@ public function boot()
private function registerMigrations()
{
if ($this->app->runningInConsole()) {
$this->loadMigrationsFrom(dirname(__DIR__) . '/database/migrations');
$this->loadMigrationsFrom(dirname(__DIR__).'/database/migrations');
}
}
}