Comments (4)
PRs are welcomed! Could look into implementation not early than next week.
from laravel-ownership.
It's not trivial thing because you need to resolve what exact model will be checked in middleware.
You could create it in your own application:
Imagine that you have AccountController
and all methods need to check if current user is owner of an Account
model.
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class AccountController extends Controller
{
public function __construct()
{
$model = App\Account::class;
$id = 1; // Here you need to determine ID from your route
// Call middleware and pass a model's class name and an id
$this->middleware("OnlyOwnerAccess:{$model},{$id}");
}
// Here goes actual methods: show, edit, update, ...
}
And create your middleware:
<?php
namespace App\Http\Middleware;
class OnlyOwnerAccess
{
public function handle($request, Closure $next, $ownableClass, $id)
{
$user = auth()->user();
if (!$user) {
return redirect('/'); // Redirecting guest on main page
}
// Instantiate the model from App Container and find a specific one by id
$ownable = app($ownableClass)->whereKey($id)->whereOwnedBy($user)->firstOrFail();
// Or write your custom logic of resolving and checks.
// Maybe you need to send flash message to user or log this action to admins.
return $next($request);
}
}
Don't forget to register middleware in App\Http\Kernel
.
from laravel-ownership.
@blendsoft I will be glad to receive your feedback about this solution. At this moment I wouldn't add this in package because it's tightly coupled with application's logic.
It would be good to start a PR if you'll find more flexible and easy way to handle it.
from laravel-ownership.
Closing this issue. Feel free to continue conversation if there is need.
from laravel-ownership.
Related Issues (10)
- Laravel ban triggers errors on composer install. HOT 4
- OwnedByDefaultUser HOT 3
- OwnedOrFail HOT 3
- The Model Owner Wont Link Up With The Owned Model HOT 4
- Behavior with softDeletes HOT 4
- Total refactoring HOT 12
- Remove laravel/legacy-factory dev dependency
- Ownership change events
- Overwrite owner on create
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-ownership.