Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
只有创建模型的经过身份验证的用户才能访问模型(Laravel)_Laravel_Authentication_Laravel 5_Repository_Repository Pattern - Fatal编程技术网

只有创建模型的经过身份验证的用户才能访问模型(Laravel)

只有创建模型的经过身份验证的用户才能访问模型(Laravel),laravel,authentication,laravel-5,repository,repository-pattern,Laravel,Authentication,Laravel 5,Repository,Repository Pattern,我正在编写一个软件应用程序,让人们拥有自己的烹饪食谱私人档案 RecipeController构造函数包含: $this->middleware('auth') 因为只有注册用户才能使用配方,但我还需要保护对模型的访问权 关键是用户只能查看和修改自己的食谱 示例:用户Tortelli工程师可以使用模型配方创建配方“Tortelli秘方”;他可以查看、更新和删除他的配方,但没有人能看到他珍贵的“玉米饼秘方” 那么,哪种方式最干净 我在模型配方中添加了一个user\u id属性 每次我向

我正在编写一个软件应用程序,让人们拥有自己的烹饪食谱私人档案

RecipeController构造函数包含:

$this->middleware('auth') 
因为只有注册用户才能使用配方,但我还需要保护对模型的访问权

关键是用户只能查看和修改自己的食谱

示例:用户Tortelli工程师可以使用模型配方创建配方“Tortelli秘方”;他可以查看、更新和删除他的配方,但没有人能看到他珍贵的“玉米饼秘方”

那么,哪种方式最干净

  • 我在模型配方中添加了一个user\u id属性
  • 每次我向数据库请求配方时,都必须使用此参数(再见ID的“findOrFail”)
  • 这意味着每次发出请求时,我都必须访问包含User\u id的User的request对象
  • 每次需要一个(或n个)配方时都使用Auth::id()
像这样:

class RecipeRepository{


public function all(){
    return Recipe::where('user_id', Auth::id())
                ->orderBy('created_at', 'asc')
                ->get();
}

public function find($recipe_id){
    return Recipe::where('user_id', Auth::id())
                ->where('id', $recipe_id)
                ->firstOrFail();
}

对吗?你恨我吗?您知道更好或更正确的方法吗?

大多数时候,我会在模型内部创建一个方法,检查某人是否获得授权、所有者等。。关于某件事

例如:

// User model

public function owns_recipe($recipe)
{

    return ($recipe->user_id == $this->id);

}
您可以在控制器的方法的一开始调用此函数:

// Controller

public function index (Request $request)
{

    $recipe = Recipe::find($request->id); // Get recipe

    $user = ... // Get user somehow

    if (!$recipe) App::abort(404); // Show 404 not found, or something

    if (!$user->owns_recipe($recipe)) App::abort(403); // Show 403 permission denied, or something


    ... // Do whatever you want :)

}

大多数情况下,我会在模型内部创建一个方法来检查某人是否被授权、所有者等。。关于某件事

例如:

// User model

public function owns_recipe($recipe)
{

    return ($recipe->user_id == $this->id);

}
您可以在控制器的方法的一开始调用此函数:

// Controller

public function index (Request $request)
{

    $recipe = Recipe::find($request->id); // Get recipe

    $user = ... // Get user somehow

    if (!$recipe) App::abort(404); // Show 404 not found, or something

    if (!$user->owns_recipe($recipe)) App::abort(403); // Show 403 permission denied, or something


    ... // Do whatever you want :)

}

虽然有很多方法可以实现这一点,但Laravel确实提供了一些内置方法来处理操作的一般身份验证。首先,我会按照您的意图做一些事情(在RecipeRepository中有一个getRecipesByOwner方法),您可以将用户从注入的请求对象传递给它:

// RecipeController

public function index(Request $request)
{
    $recipes = $this->recipeRepo->findRecipesByOwner($request->user());
}
此外,我建议创建策略来管理用户是否能够更新/删除/查看单个食谱。然后,您可以通过以下内置方法在控制器/刀片模板等中授权其操作:

// Controller

public function update(Request $request, Recipe $recipe)
{
    $this->authorize('update', $recipe);
}

// Blade template

@can('update', $recipe)

@endcan

文档可从以下网址获得:

虽然有许多方法可以实现这一点,但Laravel确实提供了一些内置方法来处理操作的一般身份验证。首先,我会按照您的意图做一些事情(在RecipeRepository中有一个getRecipesByOwner方法),您可以将用户从注入的请求对象传递给它:

// RecipeController

public function index(Request $request)
{
    $recipes = $this->recipeRepo->findRecipesByOwner($request->user());
}
此外,我建议创建策略来管理用户是否能够更新/删除/查看单个食谱。然后,您可以通过以下内置方法在控制器/刀片模板等中授权其操作:

// Controller

public function update(Request $request, Recipe $recipe)
{
    $this->authorize('update', $recipe);
}

// Blade template

@can('update', $recipe)

@endcan

该文档位于:

Nice且易于理解!很好,很容易理解!在函数index()上,我更喜欢在repository->findRecipes()函数代码中管理授权,因此我不必费心在index()中注入请求,以使用$Request->user作为repository函数的参数。无论如何,您的示例与Laravel文档的中间教程非常相似,因此,您必须正确使用函数索引()上的公共函数findRecipes(){return Auth::user()->recipes()->get();}我更喜欢在存储库->findRecipes()函数代码中管理授权,所以我不必在索引()中插入请求使用$request->user作为存储库函数的参数。无论如何,您的示例与Laravel文档的中间教程非常相似,因此您必须正确使用公共函数findRecipes(){return Auth::user()->recipes()->get();}