Code Monkey home page Code Monkey logo

Comments (4)

antonkomarev avatar antonkomarev commented on June 7, 2024

PRs are welcomed! Could look into implementation not early than next week.

from laravel-ownership.

antonkomarev avatar antonkomarev commented on June 7, 2024

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.

antonkomarev avatar antonkomarev commented on June 7, 2024

@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.

antonkomarev avatar antonkomarev commented on June 7, 2024

Closing this issue. Feel free to continue conversation if there is need.

from laravel-ownership.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.