Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

iluminar/goodwork

2227 stars
4 code files
View iluminar/goodwork on GitHub

app/Base/Policies/UserPolicy.php

Open in GitHub
use App\Base\Models\User;
use App\Authorization\Authorization;
use Illuminate\Auth\Access\HandlesAuthorization;
 
class UserPolicy
{
use HandlesAuthorization;
 
public function add(User $user)
{
return (new Authorization($user))->userHasPermissionTo('add', 'member');
}
 
public function remove(User $user)
{
return (new Authorization($user))->userHasPermissionTo('remove', 'member');
}
}

app/Base/Http/Controllers/MemberController.php

Open in GitHub
use App\Base\Models\User;
 
class MemberController extends Controller
{
public function store()
{
$this->authorize('add', User::class);
//
}
 
public function destroy()
{
$this->authorize('remove', User::class);
//
}
//
}

app/Base/Providers/AuthServiceProvider.php

Open in GitHub
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
 
class AuthServiceProvider extends ServiceProvider
{
protected $policies = [
//
\App\Base\Models\User::class => \App\Base\Policies\UserPolicy::class,
//
];
//
}

app/Authorization/Authorization.php

Open in GitHub
use App\Base\Models\User;
 
class Authorization
{
public $user;
 
public function __construct(User $user)
{
$this->user = $user;
}
 
public function userHasPermissionTo($action, $resource, $resourceId = null, $groupRelated = false, $groupType = null, $groupId = null)
{
if ($groupRelated) {
return (
(
$resourceId ? $this->user->isOwner($resource, $resourceId) : false
) || (
$this->user->isMember($groupType, $groupId) &&
$this->user->isAllowedTo($action, $resource, true, $groupType, $groupId)
)
) && (
$this->user->isNotForbiddenTo($action, $resource, $groupType, $groupId)
);
}
 
if ($groupType) {
return (
$this->user->isOwner($resource, $resourceId) || (
$this->user->isMember($groupType, $groupId) &&
$this->user->isAllowedTo($action, $resource, false, $groupType, $groupId)
)
) && (
$this->user->isNotForbiddenTo($action, $resource, $groupType, $groupId)
);
}
 
return $this->user->isAllowedTo($action, $resource) &&
$this->user->isNotForbiddenTo($action, $resource);
}
//
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.