Skip to content

Commit

Permalink
fix(core): initial
Browse files Browse the repository at this point in the history
  • Loading branch information
hsbmaulana committed Oct 11, 2024
1 parent c12f381 commit f20705f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 19 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ $repository = app(IACLRepository::class);
// $repository->grant("posts.update.5"); //
// $repository->revoke("posts.update.5"); //
// $repository->owns(); //
// $repository->owns(\App\Models\Post::class, 5); //
// $repository->can(iacl(\App\Models\Post::class, "update", 5)); //
// $repository->can("posts.update.5"); //
// auth()->user()->can("posts.update.5"); //
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tripteki/laravelphp-acl",
"version": "2.0.0",
"version": "2.0.1",
"description": "Trip Teknologi's Laravel.php ACLs",

"readme": "README.md",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class CreateRolesPermissionsTable extends Migration
*/
protected $permission;

/**
* @var \Spatie\Permission\PermissionRegistrar
*/
protected $registrar;

/**
* @return void
*/
Expand All @@ -34,6 +39,7 @@ public function __construct()
$this->keytype = app(AuthModelContract::class)->getKeyType();
$this->role = app(IACLRoleRepository::class);
$this->permission = app(IACLPermissionRepository::class);
$this->registrar = app(PermissionRegistrar::class);
}

/**
Expand Down Expand Up @@ -101,17 +107,17 @@ public function up()
else if ($keytype == "string") $table->uuid($columnNames["model_morph_key"]);

$table->index([ $columnNames["model_morph_key"], "model_type", ], "model_has_permissions_model_id_model_type_index");
$table->foreignUuid(PermissionRegistrar::$pivotPermission)->references("id")->on($tableNames["permissions"])->onUpdate("cascade")->onDelete("cascade");
$table->foreignUuid($this->registrar->pivotPermission)->references("id")->on($tableNames["permissions"])->onUpdate("cascade")->onDelete("cascade");

if ($teams) {

$table->unsignedBigInteger($columnNames["team_foreign_key"]);
$table->index($columnNames["team_foreign_key"], "model_has_permissions_team_foreign_key_index");
$table->primary([ $columnNames["team_foreign_key"], PermissionRegistrar::$pivotPermission, $columnNames["model_morph_key"], "model_type", ], "model_has_permissions_permission_model_type_primary");
$table->primary([ $columnNames["team_foreign_key"], $this->registrar->pivotPermission, $columnNames["model_morph_key"], "model_type", ], "model_has_permissions_permission_model_type_primary");

} else {

$table->primary([ PermissionRegistrar::$pivotPermission, $columnNames["model_morph_key"], "model_type", ], "model_has_permissions_permission_model_type_primary");
$table->primary([ $this->registrar->pivotPermission, $columnNames["model_morph_key"], "model_type", ], "model_has_permissions_permission_model_type_primary");
}
});

Expand All @@ -123,26 +129,26 @@ public function up()
else if ($keytype == "string") $table->uuid($columnNames["model_morph_key"]);

$table->index([ $columnNames["model_morph_key"], "model_type", ], "model_has_roles_model_id_model_type_index");
$table->foreignUuid(PermissionRegistrar::$pivotRole)->references("id")->on($tableNames["roles"])->onUpdate("cascade")->onDelete("cascade");
$table->foreignUuid($this->registrar->pivotRole)->references("id")->on($tableNames["roles"])->onUpdate("cascade")->onDelete("cascade");

if ($teams) {

$table->unsignedBigInteger($columnNames["team_foreign_key"]);
$table->index($columnNames["team_foreign_key"], "model_has_roles_team_foreign_key_index");
$table->primary([ $columnNames["team_foreign_key"], PermissionRegistrar::$pivotRole, $columnNames["model_morph_key"], "model_type", ], "model_has_roles_role_model_type_primary");
$table->primary([ $columnNames["team_foreign_key"], $this->registrar->pivotRole, $columnNames["model_morph_key"], "model_type", ], "model_has_roles_role_model_type_primary");

} else {

$table->primary([ PermissionRegistrar::$pivotRole, $columnNames["model_morph_key"], "model_type", ], "model_has_roles_role_model_type_primary");
$table->primary([ $this->registrar->pivotRole, $columnNames["model_morph_key"], "model_type", ], "model_has_roles_role_model_type_primary");
}
});

Schema::create($tableNames["role_has_permissions"], function (Blueprint $table) use ($keytype, $tableNames) {

$table->foreignUuid(PermissionRegistrar::$pivotPermission, "acl_role_has_permissions_permission_id_foreign")->references("id")->on($tableNames["permissions"])->onUpdate("cascade")->onDelete("cascade");
$table->foreignUuid(PermissionRegistrar::$pivotRole, "acl_role_has_permissions_role_id_foreign")->references("id")->on($tableNames["roles"])->onUpdate("cascade")->onDelete("cascade");
$table->foreignUuid($this->registrar->pivotPermission, "acl_role_has_permissions_permission_id_foreign")->references("id")->on($tableNames["permissions"])->onUpdate("cascade")->onDelete("cascade");
$table->foreignUuid($this->registrar->pivotRole, "acl_role_has_permissions_role_id_foreign")->references("id")->on($tableNames["roles"])->onUpdate("cascade")->onDelete("cascade");

$table->primary([ PermissionRegistrar::$pivotPermission, PermissionRegistrar::$pivotRole, ], "role_has_permissions_permission_id_role_id_primary");
$table->primary([ $this->registrar->pivotPermission, $this->registrar->pivotRole, ], "role_has_permissions_permission_id_role_id_primary");
});

app("cache")->store(config("permission.cache.store") != "default" ? config("permission.cache.store") : null)->forget(config("permission.cache.key"));
Expand Down
25 changes: 19 additions & 6 deletions database/migrations/2023_01_20_000001_add_teams_fields.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ use Spatie\Permission\PermissionRegistrar;

class AddTeamsFields extends Migration
{
/**
* @var \Spatie\Permission\PermissionRegistrar
*/
protected $registrar;

/**
* @return void
*/
public function __construct()
{
$this->registrar = app(PermissionRegistrar::class);
}

/**
* @return void
*/
Expand Down Expand Up @@ -52,15 +65,15 @@ class AddTeamsFields extends Migration

if (DB::getDriverName() !== "sqlite") {

$table->dropForeign([ PermissionRegistrar::$pivotPermission, ]);
$table->dropForeign([ $this->registrar->pivotPermission, ]);
}

$table->dropPrimary();
$table->primary([ $columnNames["team_foreign_key"], PermissionRegistrar::$pivotPermission, $columnNames["model_morph_key"], "model_type", ], "model_has_permissions_permission_model_type_primary");
$table->primary([ $columnNames["team_foreign_key"], $this->registrar->pivotPermission, $columnNames["model_morph_key"], "model_type", ], "model_has_permissions_permission_model_type_primary");

if (DB::getDriverName() !== "sqlite") {

$table->foreignUuid(PermissionRegistrar::$pivotPermission)->references("id")->on($tableNames["permissions"])->onUpdate("cascade")->onDelete("cascade");
$table->foreignUuid($this->registrar->pivotPermission)->references("id")->on($tableNames["permissions"])->onUpdate("cascade")->onDelete("cascade");
}
});
}
Expand All @@ -74,15 +87,15 @@ class AddTeamsFields extends Migration

if (DB::getDriverName() !== "sqlite") {

$table->dropForeign([ PermissionRegistrar::$pivotRole, ]);
$table->dropForeign([ $this->registrar->pivotRole, ]);
}

$table->dropPrimary();
$table->primary([ $columnNames["team_foreign_key"], PermissionRegistrar::$pivotRole, $columnNames["model_morph_key"], "model_type", ], "model_has_roles_role_model_type_primary");
$table->primary([ $columnNames["team_foreign_key"], $this->registrar->pivotRole, $columnNames["model_morph_key"], "model_type", ], "model_has_roles_role_model_type_primary");

if (DB::getDriverName() !== "sqlite") {

$table->foreignUuid(PermissionRegistrar::$pivotRole)->references("id")->on($tableNames["roles"])->onDelete("cascade");
$table->foreignUuid($this->registrar->pivotRole)->references("id")->on($tableNames["roles"])->onDelete("cascade");
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/Helpers/IACL.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @param int|string $target
* @return string
*/
function iacl($resource, $action, $target)
function iacl($resource, $action = "*", $target = "*")
{
return OwnScope::space(app($resource)).".".Str::lower($action).".".$target;
};
Expand Down
6 changes: 4 additions & 2 deletions src/Repositories/Eloquent/ACLRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,12 @@ public function permissions()
}

/**
* @param string $resource
* @param int|string $target
* @return \Illuminate\Support\Collection
*/
public function owns()
public function owns($resource = "%", $target = "%")
{
return $this->user->getDirectPermissions();
return $resource === "%" ? $this->user->getDirectPermissions() : $this->user->getDirectPermissions()->whereLike("name", iacl($resource, "%", $target));
}
};

0 comments on commit f20705f

Please sign in to comment.