Laravel 编码技巧
GitHub
  • 简介
  • 数据库模型与 Eloquent
  • 模型关联
  • 数据库迁移
  • 视图
  • 路由
  • 验证
  • 集合
  • 授权
  • 邮件
  • Artisan
  • 工厂
  • 日志与调试
  • API
  • 其他
由 GitBook 提供支持
在本页
  • 一次检查多个权限
  • 更多关于用户注册的事件
  • 你知道 Authonce 吗
  • 更改用户密码更新的 API 令牌
  • 覆盖超级管理员的权限

这有帮助吗?

在GitHub上编辑

授权

一次检查多个权限

除了 @can Blade 指令外,你知道可以用 @canany 指令一次检查多个权限吗?

@canany(['update', 'view', 'delete'], $post)
    // The current user can update, view, or delete the post
@elsecanany(['create'], \App\Post::class)
    // The current user can create a post
@endcanany

更多关于用户注册的事件

希望在新用户注册后执行一些操作? 转到 app/Providers/EventServiceProvider.php 和 添加更多的监听类,然后在 $event->user 对象中实现 handle() 方法。

class EventServiceProvider extends ServiceProvider
{
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,

            // You can add any Listener class here
            // With handle() method inside of that class
        ],
    ];

你知道 Authonce 吗

你可以用用户登录一个请求,使用方法 Auth::once()。 不会使用任何会话或 cookie,这意味着该方法在构建无状态 API 时可能很有帮助。

if (Auth::once($credentials)) {
    //
}

更改用户密码更新的 API 令牌

当用户的密码更改时,可以方便地更改用户的 API 令牌。 模型:

public function setPasswordAttribute($value)
{
    $this->attributes['password'] = $value;
    $this->attributes['api_token'] = Str::random(100);
}

覆盖超级管理员的权限

如果你已经定义了网关(Gates)但是又想要覆盖超级管理员的所有权限。 给超级管理员所有权限,你可以在 AuthServiceProvider.php 文件中用 Gate::before() 语句拦截网关(Gates)。

// Intercept any Gate and check if it's super admin
Gate::before(function($user, $ability) {
    if ($user->is_super_admin == 1) {
        return true;
    }
});

// Or if you use some permissions package...
Gate::before(function($user, $ability) {
    if ($user->hasPermission('root')) {
        return true;
    }
});
上一页集合下一页邮件

最后更新于3年前

这有帮助吗?