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

[WIP] Random wild refactors #84

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ $moderatorRole = \HttpOz\Roles\Models\Role::create([
]);
```

> Because of `Slugable` trait, if you make a mistake and for example leave a space in slug parameter, it'll be replaced with a dot automatically, because of `str_slug` function.
> Because of `Sluggable` trait, if you make a mistake and for example leave a space in slug parameter, it'll be replaced with a dot automatically, because of `str_slug` function.

### Attaching And Detaching Roles

Expand Down Expand Up @@ -262,9 +262,13 @@ $router->get('/example', [
]);
```

It throws `\HttpOz\Roles\Exceptions\RoleDeniedException` or `\HttpOz\Roles\Exceptions\GroupDeniedException` exceptions if it goes wrong.
There are two kinds of exceptions that are thrown when a user fails to meet the role or group requirements:
- `\HttpOz\Roles\Exceptions\RoleDeniedException`
- `\HttpOz\Roles\Exceptions\GroupDeniedException`

You can catch these exceptions inside `app/Exceptions/Handler.php` file and do whatever you want. You can control the error page that your application users see when they try to open a page their role is not allowed to. This package already has a view bundled with it that should have been published to `resources/views/vendor/roles/error.blade.php` when you published the package. Simply add the below condition inside your `app\Exceptions\Handler.php`'s render function. Feel free to point to another view of your choice.
You can catch these exceptions inside `app/Exceptions/Handler.php` file and do whatever you want.

You can control the error page that your application users see when they try to open a page their role is not allowed to. This package already has a view bundled with it that should have been published to `resources/views/vendor/roles/error.blade.php` when you published the package. Simply add the below condition inside your `app\Exceptions\Handler.php`'s render function. Feel free to point to another view of your choice.

```php
/**
Expand Down
54 changes: 13 additions & 41 deletions src/Contracts/HasRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,91 +3,63 @@
namespace HttpOz\Roles\Contracts;

use HttpOz\Roles\Models\Role;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

interface HasRole
{
/**
* User belongs to many roles.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function roles();
public function roles(): BelongsToMany;

/**
* Get all roles as collection.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getRoles();
public function getRoles(): Collection;

/**
* Check if the user has a role or roles.
*
* @param int|string|array $role
* @param bool $all
* @return bool
*/
public function isRole($role, $all = false);
public function isRole(array|int|string $role, bool $all = false): bool;

/**
* Check if the user has all roles.
*
* @param int|string|array $role
* @return bool
*/
public function isAll($role);
public function isAll(array|int|string $role): bool;

/**
* Check if the user has at least one role.
*
* @param int|string|array $role
* @return bool
*/
public function isOne($role);
public function isOne(array|int|string $role): bool;

/**
* Check if the user has role.
*
* @param int|string $role
* @return bool
*/
public function hasRole($role);
public function hasRole(int|string $role): bool;

/**
* Attach role to a user.
*
* @param int|Role $role
* @return bool
*/
public function attachRole($role): bool;
public function attachRole(int|Role $role): bool;

/**
* Detach role from a user.
*
* @param int|Role $role
* @return int
*/
public function detachRole($role);
public function detachRole(int|Role $role): int;

/**
* Sync roles for a user.
*
* @param array|Role[]|\Illuminate\Database\Eloquent\Collection $roles
* @return array
*/
public function syncRoles($roles);
public function syncRoles(Collection|array $roles): array;

/**
* Detach all roles from a user.
*
* @return int
*/
public function detachAllRoles();
public function detachAllRoles(): int;

/**
* Get role group of a user.
*
* @return string
*/
public function group();
public function group(): string;
}
6 changes: 4 additions & 2 deletions src/Contracts/RoleHasRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
namespace HttpOz\Roles\Contracts;


use Illuminate\Database\Eloquent\Relations\BelongsToMany;

interface RoleHasRelations
{
/**
* Role belongs to many users.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
* @return BelongsToMany
*/

public function users();
public function users(): BelongsToMany;

}
4 changes: 1 addition & 3 deletions src/Exceptions/GroupDeniedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ class GroupDeniedException extends Exception
{
/**
* Create a new group denied exception instance.
*
* @param string $group
*/
public function __construct($group)
public function __construct(string $group)
{
parent::__construct();
$this->message = sprintf("You are not in the required [%s] group.", $group);
Expand Down
4 changes: 1 addition & 3 deletions src/Exceptions/RoleDeniedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ class RoleDeniedException extends Exception
{
/**
* Create a new role denied exception instance.
*
* @param string $role
*/
public function __construct($role)
public function __construct(string $role)
{
parent::__construct();
$this->message = sprintf("You don't have a required ['%s'] role.", $role);
Expand Down
20 changes: 6 additions & 14 deletions src/Middleware/VerifyGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,28 @@
use Closure;
use Illuminate\Contracts\Auth\Guard;
use HttpOz\Roles\Exceptions\GroupDeniedException;
use Illuminate\Http\Request;


class VerifyGroup
{
/**
* @var \Illuminate\Contracts\Auth\Guard
*/
protected $auth;

/**
* Create a new filter instance.
*
* @param \Illuminate\Contracts\Auth\Guard $auth
* @return void
*/
public function __construct(Guard $auth)
public function __construct(protected Guard $auth)
{
$this->auth = $auth;
}

/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param Request $request
* @param Closure $next
* @param int $group
* @return mixed
* @throws \HttpOz\Roles\Exceptions\GroupDeniedException
* @throws GroupDeniedException
*/
public function handle($request, Closure $next, $group)
public function handle(Request $request, Closure $next, int $group): mixed
{
if ($this->auth->check() && $this->auth->user()->group() == $group) {
return $next($request);
Expand Down
20 changes: 7 additions & 13 deletions src/Middleware/VerifyRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,30 @@
use Closure;
use Illuminate\Contracts\Auth\Guard;
use HttpOz\Roles\Exceptions\RoleDeniedException;
use Illuminate\Http\Request;


class VerifyRole
{
/**
* @var \Illuminate\Contracts\Auth\Guard
*/
protected $auth;

/**
* Create a new filter instance.
*
* @param \Illuminate\Contracts\Auth\Guard $auth
* @param Guard $auth
* @return void
*/
public function __construct(Guard $auth)
public function __construct(protected Guard $auth)
{
$this->auth = $auth;
}

/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param Request $request
* @param Closure $next
* @param int|string $role
* @return mixed
* @throws \HttpOz\Roles\Exceptions\RoleDeniedException
* @throws RoleDeniedException
*/
public function handle($request, Closure $next, $role)
public function handle(Request $request, Closure $next, int|string $role): mixed
{
if ($this->auth->check() && $this->auth->user()->isRole($role)) {
return $next($request);
Expand Down
83 changes: 48 additions & 35 deletions src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,62 @@


use HttpOz\Roles\Database\Factories\RoleFactory;
use HttpOz\Roles\Traits\Sluggable;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

use HttpOz\Roles\Traits\RoleHasRelations;
use HttpOz\Roles\Contracts\RoleHasRelations as RoleHasRelationsContract;
use Illuminate\Support\Str;

class Role extends Model implements RoleHasRelationsContract {

use Sluggable, RoleHasRelations, HasFactory;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'slug', 'description', 'group'];

/**
* Create a new model instance.
*
* @param array $attributes
*
* @return void
*/
public function __construct(array $attributes = []) {
parent::__construct($attributes);
if ($connection = config('roles.connection')) {
$this->connection = $connection;
class Role extends Model implements RoleHasRelationsContract
{

use RoleHasRelations, HasFactory;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'slug', 'description', 'group'];

/**
* Create a new model instance.
*/
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
if ($connection = config('roles.connection')) {
$this->connection = $connection;
}
}

/**
* @param $slug
* @return mixed
*/
public static function findBySlug($slug): self
{
return self::where('slug', $slug)->first();
}
}

/**
* @param $slug
* @return mixed
*/
public static function findBySlug($slug) {
return self::where('slug', $slug)->first();
}
protected static function newFactory(): RoleFactory
{
return new RoleFactory();
}

protected static function newFactory()
{
return new RoleFactory();
}
public function Slug(): Attribute
{
return new Attribute(
set: fn ($value) => Str::slug($value, config('roles.separator')),
);
}

public function Group(): Attribute
{
return new Attribute(
set: fn ($value) => Str::slug($value, config('roles.separator')),
);
}
}
17 changes: 17 additions & 0 deletions src/Observers/RoleObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace HttpOz\Roles\Observers;

use HttpOz\Roles\Models\Role;
use Illuminate\Support\Str;

class RoleObserver
{
/**
* Handle the Role "created" event.
*/
public function created(Role $role): void
{
$role->slug = Str::slug($role->name, config('roles.separator'));
}
}
1 change: 1 addition & 0 deletions src/RolesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function boot()
public function register()
{
$this->mergeConfigFrom(__DIR__ . '/../config/roles.php', 'roles');
$this->app->register();
}

public function registerBladeExtensions()
Expand Down
Loading