🎉 سال نو، مهارت نو، مشاوره رایگان نقشه راه برنامه نویسی (آفر ویژه ثبت نام قبل از افزایش قیمت 🔥)
۰ ثانیه
۰ دقیقه
۰ ساعت
۱۱ هادی امانی
ساخت Policy
جامعه لاراول ایجاد شده در ۲۲ تیر ۱۴۰۱

سلام . موقع استفاده مستقیم از دستورات authorize داخل متد مشکلی وجود نداره و برنامه به درستی اجرا میشه ولی موقع استفاده در construct این ابزار روی کل پروژه اجرا میشه و برنامه کلامتوقف میشه

سلام دوست عزیز

لطفا هر زمانی که به خطایی میخورید و قصد ایجاد تاپیک دارید هم متن خطا و هم کد خودتون رو قرار بدین تا در وقت خودتون و مجموعه صرفه جویی قابل توجهی کرده باشید.

محمد گازری ۲۳ تیر ۱۴۰۱، ۰۴:۱۷

سلام مجدد.وقتی به این شکل استفاده میکنم مشکلی نیست



  public function edit(Request $request, Video $video)
    {
        
         $this->authorize('update',$video);
        $categories = Category::all();
        return view('frontend.edit', compact('video', 'categories'));
    }

اما وقتی دستور رو میبرم داخل construct  مثل حالت زیر برنامه کلا متوقف میشه


    public function __construct(){
       $this->authorizeResource(Video::class,'video');
    }


اینم کد داخل VideoPolicy که فقط قسمت update رو تکمیل کردم


namespace App\\Policies;
use App\\Models\\User;
use App\\Models\\Video;
use Illuminate\\Auth\\Access\\HandlesAuthorization;
use Illuminate\\Auth\\Access\\Response;
class VideoPolicy
{
    use HandlesAuthorization;
    /**
     * Determine whether the user can view any models.
     *
     * @param  \\App\\Models\\User  $user
     * @return \\Illuminate\\Auth\\Access\\Response|bool
     */
    public function viewAny(User $user)
    {
        //
    }
    /**
     * Determine whether the user can view the model.
     *
     * @param  \\App\\Models\\User  $user
     * @param  \\App\\Models\\Video  $video
     * @return \\Illuminate\\Auth\\Access\\Response|bool
     */
    public function view(User $user, Video $video)
    {
        //
    }
    /**
     * Determine whether the user can create models.
     *
     * @param  \\App\\Models\\User  $user
     * @return \\Illuminate\\Auth\\Access\\Response|bool
     */
    public function create(User $user)
    {
        //
    }
    /**
     * Determine whether the user can update the model.
     *
     * @param  \\App\\Models\\User  $user
     * @param  \\App\\Models\\Video  $video
     * @return \\Illuminate\\Auth\\Access\\Response|bool
     */
    public function update(User $user, Video $video)
    {
        return $video->user_id == $user->id
            ? Response::allow()
            : Response::deny(__('maseeges.You must be an administrator'));
    }
    /**
     * Determine whether the user can delete the model.
     *
     * @param  \\App\\Models\\User  $user
     * @param  \\App\\Models\\Video  $video
     * @return \\Illuminate\\Auth\\Access\\Response|bool
     */
    public function delete(User $user, Video $video)
    {
        //
    }
    /**
     * Determine whether the user can restore the model.
     *
     * @param  \\App\\Models\\User  $user
     * @param  \\App\\Models\\Video  $video
     * @return \\Illuminate\\Auth\\Access\\Response|bool
     */
    public function restore(User $user, Video $video)
    {
        //
    }
    /**
     * Determine whether the user can permanently delete the model.
     *
     * @param  \\App\\Models\\User  $user
     * @param  \\App\\Models\\Video  $video
     * @return \\Illuminate\\Auth\\Access\\Response|bool
     */
    public function forceDelete(User $user, Video $video)
    {
        //
    }
}
هادی امانی ۲۳ تیر ۱۴۰۱، ۰۷:۰۶

چرا مجددا اومدید دسترسی رو توی متد update بررسی کردید؟

    /**
     * Determine whether the user can update the model.
     *
     * @param  \\App\\Models\\User  $user
     * @param  \\App\\Models\\Video  $video
     * @return \\Illuminate\\Auth\\Access\\Response|bool
     */
    public function update(User $user, Video $video)
    {
        return $video->user_id == $user->id
            ? Response::allow()
            : Response::deny(__('maseeges.You must be an administrator'));
    }
محمد گازری ۲۳ تیر ۱۴۰۱، ۰۷:۱۷

این روش دقیقا مثل خود فیلم بوده

هادی امانی ۲۳ تیر ۱۴۰۱، ۰۷:۳۴

متن خطاتون رو قرار میدین

محمد گازری ۲۳ تیر ۱۴۰۱، ۰۷:۴۴

خطا نمیده دسترسی کل برنامه 7011-Screenshot (55).pngرو با 403 میبنده

هادی امانی ۲۳ تیر ۱۴۰۱، ۰۷:۴۸

خب الان policy اجرا شده

شرط هاتون رو بررسی کنید

محمد گازری ۲۳ تیر ۱۴۰۱، ۱۰:۰۸

میدونم policy اجرا شده .

سوال من کلا اینه چرا بدون نوشتن هیچ فیلتری در VideoPolicy دسترسی به صفحه ایندکس هم قطع میشه وقتی از construct  استفاده میکنم؟

هادی امانی ۲۳ تیر ۱۴۰۱، ۱۴:۱۵

توی policy چک کنید false برگشت داده شده یا نه

محمد گازری ۲۳ تیر ۱۴۰۱، ۱۴:۲۴

323c-12.png

هادی امانی ۲۳ تیر ۱۴۰۱، ۱۴:۲۵

کد استاد در پایین ویدیوهای بعدی پیوست شده

با کد خودتون مقایسه کنید اگر به نتیجه نرسیدید پروژتون رو روی گیت‌هاب پوش کنید لینکش رو قرار بدین

محمد گازری ۲۳ تیر ۱۴۰۱، ۱۴:۲۹