我使用policies进行用户授权。访客用户如何使用策略? 下面是我的代码: 在控制器中: class PostController extends Controller
{
public function index(Post $post)
{
$this->authorize($post);
return $post->all();
}
} 在策略中: class PostPolicy
{
// This function executes only for authenticated users.
//
这里有一个酒店项目,每家酒店都有一个管理员。
我所做的:
在用户类型表中,厨师、经理、酒店等不同类型的用户
如果用户类型是酒店,则意味着它是该酒店的管理员。现在,对于每一个CRUD和其他功能,我得到了这样的旅馆id
$hotel_id = \Auth::user()->hotel_id;
并在每个查询(like get user, save user, get invoice,set invoice,check food etc)中检查此like get user, save user, get invoice,set invoice,check food etc。
这样做对吗?
我有以下政策:
<?php
namespace App\Policies;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class EventPolicy
{
use HandlesAuthorization;
/**
* Determine whether the user can view the whole model.
*
* @param User $user
* @return boolean
*/
我正在尝试创建一个项目来添加编辑联系人。
为了限制用户可以添加/编辑自己的联系人,因此添加了如下策略ContactPolicy
<?php
namespace App\Policies;
use Illuminate\Auth\Access\HandlesAuthorization;
use App\User;
use App\Contact;
class ContactPolicy
{
use HandlesAuthorization;
/**
* Create a new policy instance.
*
* @return
我有模特Post。我怎么能拒绝编辑文章,如果用户不是作者这篇文章?
我知道,我只能通过以下方式向用户展示他的帖子:
Auth::user()->posts
因为模型用户有:
public function posts() {
return $this->hasMany('App\Models\Post');
}
但是每个用户都可以使用链接:
编辑陌生人的帖子。我怎么才能阻止这一切?
我的编辑帖子控制器:
/**
* Show the form for editing the specified resource.
*
* @param int $i
其思想是能够从路由中定义哪些类型的用户将能够访问视图。我有代码,但我不知道这样做是否有安全问题。
在该路由中,我们将调用middelware,并将允许的用户类型传递给它。只有那些被允许的。
Route::get('/MyURL', [MyClassController::class, 'MyMethod'])->name('MyName')->middleware('isAuthenticate:Role1,Role2');
中间件应该是这样的
//The function receives the allowed r
假设我们有一个具有多到多关系的用户和会话模型。
class User extends Model ... {
public function conversations()
{
return $this->belongsToMany('App\Conversation');
}
}
class Conversation extends Model {
public function users()
{
return $this->belongsToMany('App\User&
我正在使用laravel基本策略系统来保护未经授权的用户不被更新。例如,用户有Id 1,在posts表中,User_id也是1。
现在,以$this->authorize('update',$post);的方式,我只能传递一个变量$post进行身份验证。在can方法中,我还可以使用$user变量$user->can('update',$post)进行授权。
这是代码:
在PostPolicy.php中:
public function update(User $user, Post $post)
{
return $user->id =
我在Laravel中实现了如下的用户策略。
namespace App\Policies;
use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class UserPolicy
{
use HandlesAuthorization;
/**
* Determine whether the user can list the model.
*
* @param \App\User $user
* @return mixed
*/
publi
有时需要检查没有任何参数的操作的用户授权,比如create/store方法:
class PostPolicy
{
use HandlesAuthorization;
/**
* Determine if the user can create a new post.
*
* @param \App\User $user
* @return bool
*/
public function create(User $user)
{
if($user->is_admin)
{
我正在构建我的Laravel界面,并在每个资源中添加必要的字段。但是,我注意到,编辑/详细/垃圾按钮没有出现在我的索引视图中。
是否需要将某些内容添加到我的资源类中,还是与如何构建控制器有关?
这就是我的字段方法的样子:
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
ID::ma
我正在试着设置后管理区。该部分的任务是显示属于已登录用户的所有文章。我处理标签和类别的方式是正确的(标签和类别不需要为任何用户过滤)。帖子页面可以正确地显示登录用户拥有的帖子,但问题是用户无法编辑或显示任何帖子,并且尝试存储新帖子会重定向到403页面。我被这个错误搞糊涂了,也没有任何解决方案。我很感谢你的帮助。 PostModel <?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
cla
我们需要为我们的设置提供架构建议。
我们正在开发一个移动应用程序,它将:
使用身份验证
支持匿名用户(允许用户跳过注册过程)
仍然对API请求执行Authentication形式的 (如果可能)
我们试图执行的是:
当用户选择跳过注册过程时,匿名客户端-:
1. Trigger a **silent sign-up request to B2C via** [**MS Graph**](https://learn.microsoft.com/en-us/graph/overview)
2. Populate a custom field with a **bac
我想要管理员授权。所以我在中间件中有了新的类。
class Admin {
public function handle($request, Closure $next)
{
if ( Auth::check() && Auth::user()->isAdmin() )
{
return $next($request);
}
Session::flash('message', 'You need to be an administrator to visit this page.');