Skip to main content

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

Read more here

ilestis/miscellany

299 stars
4 code files
View ilestis/miscellany on GitHub

app/Providers/AuthServiceProvider.php

Open in GitHub
use App\Models\Campaign;
use App\Policies\CampaignPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
 
class AuthServiceProvider extends ServiceProvider
{
public function boot()
{
if (!app()->runningInConsole() || $this->shouldLoadPolicies()) {
$this->registerPolicies();
}
//
}
 
protected $policies = [
//
Campaign::class => CampaignPolicy::class,
//
'App\Models\Entity' => 'App\Policies\EntityPolicy',
//
];
}

app/Policies/CampaignPolicy.php

Open in GitHub
use App\User;
use App\Models\Campaign;
 
class CampaignPolicy
{
//
public function access(User $user, Campaign $campaign): bool
{
if ($campaign->isPublic()) {
return true;
}
return $campaign->userIsMember();
}
//
}

app/Policies/EntityPolicy.php

Open in GitHub
use App\Models\Entity;
use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;
 
class EntityPolicy
{
use HandlesAuthorization;
 
public function attributes(?User $user, Entity $entity)
{
if ($entity->exists === false) {
return true;
}
return $entity->is_attributes_private ? ($user && $user->isAdmin()) : true;
}
//
}

app/Http/Controllers/Api/v1/EntityInventoryApiController.php

Open in GitHub
use App\Models\Campaign;
use App\Models\Entity;
 
class EntityInventoryApiController extends ApiController
{
public function index(Campaign $campaign, Entity $entity)
{
$this->authorize('access', $campaign);
$this->authorize('view', $entity->child);
return Resource::collection($entity->inventories);
}
//
}

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.