Skip to content

Commit

Permalink
refactor: code style
Browse files Browse the repository at this point in the history
  • Loading branch information
uwla committed Sep 6, 2024
1 parent 06e8fb1 commit f0213d0
Show file tree
Hide file tree
Showing 16 changed files with 304 additions and 291 deletions.
4 changes: 2 additions & 2 deletions src/AclServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class AclServiceProvider extends ServiceProvider
{
public function boot()
public function boot(): void
{
// publishes migrations
$src = __DIR__ . '/' . '../database/migrations/create_acl_tables.php';
Expand All @@ -15,4 +15,4 @@ public function boot()
);
$this->publishes([$src => $dest], 'migrations');
}
}
}
2 changes: 1 addition & 1 deletion src/Contracts/HasPermissionContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

interface HasPermissionContract
{
//
// HasPermissionContract
}
2 changes: 1 addition & 1 deletion src/Contracts/HasRoleContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

interface HasRoleContract
{
//
// HasRoleContract
}
137 changes: 72 additions & 65 deletions src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,105 +2,110 @@

namespace Uwla\Lacl\Models;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\DbCollection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use InvalidArgumentException;
use Uwla\Lacl\Traits\Permissionable;

class Permission extends Model
{
use HasFactory, Permissionable;
use HasFactory;
use Permissionable;

/**
* Get the instances of the given model which have this permission
*
* @param string $model_class
* @param string $id_column
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getModels($model_class, $id_column)
* Get the instances of the given model which have this permission
*
* @param string $model_class
* @param string $id_column
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getModels($model_class, $id_column): DbCollection
{
$ids = PermissionModel::where([
'permission_id' => $this->id,
'model' => $model_class,
])->pluck('model_id');
$models = $model_class::whereIn($id_column, $ids)->get();
return $models;
return $model_class::whereIn($id_column, $ids)->get();
}

/**
* Get the roles that have this permission
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getRoles()
* Get the roles that have this permission
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getRoles(): DbCollection
{
return $this->getModels($this::Role(), 'id');
}

/**
* Get the name of the roles that have this permission
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getRoleNames()
* Get the name of the roles.
*
* @return array The names.
*/
public function getRoleNames(): array
{
return $this->getRoles()->pluck('name');
}

/**
* Get permissions by their name
*
* @param array<string>|string $names The names of the permissions
* @param mixed $modelType The class name of the model (optional)
* @param mixed $models The models or their ids (optional)
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function getByName($names, $modelType=null, $models=null)
* Get permissions by their name
*
* @param array<string>|string $names The names of the permissions
* @param mixed $modelType The class name of the model (optional)
* @param mixed $models The models or their ids (optional)
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function getByName($names, $modelType = null, $models = null): DbCollection
{
if (is_string($names))
if (is_string($names)) {
$names = [$names];
if (! is_array($names))
throw new InvalidArgumentException('First arg must be string array');
}
if (! is_array($names)) {
throw new InvalidArgumentException(
'First arg must be string array',
);
}

$n = count($names);
if ($n == 0)
throw new InvalidArgumentException('No permission provided');
if ($n == 0) {
throw new InvalidArgumentException(
'No permission provided',
);
}

if ($modelType != null)
if ($modelType != null) {
$query = static::where('model', $modelType);
else
} else {
$query = static::query();
}

if ($models == null) {
// we are dealing with permissions for a resource group
$query->whereIn('name', $names);
} else {
// we are dealing with permission for specific resources

if (! is_countable($models))
{
if (! is_countable($models)) {
throw new InvalidArgumentException(
'Second arguments must be array or Collection.');
'Second arguments must be array or Collection.'
);
}

if (count($models) != $n)
{
if (count($models) != $n) {
throw new InvalidArgumentException(
'number of permissions and models must match');
'number of permissions and models must match'
);
}

if ($models instanceof Collection)
{
if ($models instanceof DbCollection) {
$models = $models->pluck('id');
}

// each resource is identified by its model_id
$query->where(function($q) use ($names, $models, $n)
{
for ($i = 0; $i < $n; $i+=1)
{
$query->where(function ($q) use ($names, $models, $n) {
for ($i = 0; $i < $n; $i += 1) {
$q->orWhere([
['name', $names[$i]],
['model_id', $models[$i]],
Expand All @@ -113,36 +118,38 @@ public static function getByName($names, $modelType=null, $models=null)
}

/**
* Create one permission by the provided name
*
* @param array<string> $names
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function createOne($name)
* Create one permission by the provided name
*
* @param array<string> $name
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function createOne($name): DbCollection
{
return static::create(['name' => $name]);
}

/**
* Create permissions by the provided names
*
* @param array<string> $names
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function createMany($names)
* Create permissions by the provided names
*
* @param array<string> $names
* @return \Illuminate\Database\Eloquent\Collection
*/
public static function createMany($names): DbCollection
{
if (! is_array($names))
if (! is_array($names)) {
throw new InvalidArgumentException('Expected string array');
if (count($names) == 0)
return new Collection();
}
if (count($names) == 0) {
return new DbCollection();
}

// create permissions
$permissionsToCreate = [];
foreach ($names as $name);
$permissionsToCreate[] = ['name' => $name];
$permissionsToCreate[] = ['name' => $name];
static::insert($permissionsToCreate); // bulk insertion

// return them
return static::whereIn('names', $names)->get();
}
}
}
2 changes: 1 addition & 1 deletion src/Models/PermissionModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ class PermissionModel extends Model
* @var string
*/
protected $table = 'permissions_models';
}
}
6 changes: 3 additions & 3 deletions src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Uwla\Lacl\Models;

use Uwla\Lacl\Contracts\HasPermissionContract;
use Uwla\Lacl\Traits\PermissionableHasRole;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Uwla\Lacl\Contracts\HasPermissionContract;
use Uwla\Lacl\Traits\PermissionableHasRole;

class Role extends Model implements HasPermissionContract
{
use HasFactory, PermissionableHasRole;
}
}
2 changes: 1 addition & 1 deletion src/Models/RoleModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ class RoleModel extends Model
* @var string
*/
protected $table = 'roles_models';
}
}
22 changes: 11 additions & 11 deletions src/Traits/CustomAclModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
Trait CustomAclModels
{
/**
* Get the class of Role
*
* @return string
*/
protected static function Role()
* Get the class of Role
*
* @return string
*/
protected static function Role(): string
{
return Role::class;
}

/**
* Get the class of Permission
*
* @return string
*/
protected static function Permission()
* Get the class of Permission
*
* @return string
*/
protected static function Permission(): string
{
return Permission::class;
}
}
}
Loading

0 comments on commit f0213d0

Please sign in to comment.