thinkstudeo / laravel-rakshak
Laravel 的身份验证库。
Requires
- php: >=7.1.3
- doctrine/dbal: ^2.9
- guzzlehttp/guzzle: ^6.3
- illuminate/support: 5.7.*|5.8.*|5.9.*
- moontoast/math: ^1.1
- thinkstudeo/textlocal-notification-channel: ^1.0
Requires (Dev)
- beyondcode/laravel-dump-server: ^1.0
- filp/whoops: ^2.0
- fzaninotto/faker: ^1.4
- mockery/mockery: ^1.0
- nunomaduro/collision: ^2.0
- orchestra/database: 3.7.*|3.8.*
- orchestra/testbench: 3.7.*|3.8.*
- phpunit/phpunit: ^7.5
This package is auto-updated.
Last update: 2024-09-22 18:10:02 UTC
README
此包扩展了 Lavarel 应用程序的身份验证和授权。
- 角色和能力用于细粒度授权。
- 双因素认证功能。
- 身份验证视图、Blade 指令和路由中间件。
内容
安装
$ composer require thinkstudeo/laravel-rakshak
然后使用 artisan 命令安装/集成包到您的 Laravel 应用程序中。
$ php artisan rakshak:install
artisan 命令将
- 注册身份验证路由
- 提供身份验证视图(与
auth:make
命令非常相似) - 发布用于管理 角色、能力和 Rakshak 设置 的视图
- 注册两个中间件
rakshak.2fa
用于验证 otp,为路由提供 2fa 保护role
用于为路由提供授权保护
- 将包配置
rakshak.php
发布到config
文件夹
最后迁移数据库 bash $ php artisan migrate
迁移将更改 users
表以添加
- 用户名
- 手机
- mobile_verified_at
- enable_2fa
- otp_token
- otp_expiry
- otp_channel
- 状态(供管理员激活/停用用户)
迁移将添加在配置文件 roles
键中定义的两个角色。只有具有角色 config('rakshak.roles.super_user')
的用户才能访问 /rakshak/settings
路由以查看和编辑双因素设置。具有 config('rakshak.roles.super_user')
或 config('rakshak.roles.authorizer')
的用户
建议在新的 Laravel 应用程序中安装此包
配置
包配置文件发布在 config/rakshak.php
。
允许启用 双因素认证 - 默认情况下未启用。它还允许替换/修改用于发送 otp 和注册欢迎消息的通知。
默认通知发布在 app/Notifications/Rakshak
目录中。您可以根据需要修改或替换通知,然后使用您的新通知的 FQCN 替换相应的配置键。
<?php return [ /* |-------------------------------------------------------------------------- | Route Prefix |-------------------------------------------------------------------------- | | This value will be used as route prefix for all rakshak routes. | */ 'route_prefix' => 'rakshak', /* |-------------------------------------------------------------------------- | Application's User Model |-------------------------------------------------------------------------- | | Mainly to indicate the primary key type for the User model in your | application - whether its the default bigIncrements or uuid. | */ 'users' => [ 'pk_type' => 'unsignedBigInteger' ], /* |-------------------------------------------------------------------------- | Two Factor Authentication |-------------------------------------------------------------------------- | | Use the below keys to configure the two factor authentication for app. | Switch to enable or disable two factor authentication - enable_2fa. | Notification classes and sms templates for otp and welcome message. | Remember to use approved templates for sms messages in countries | where there are DND restrictions for transactional messaging. | */ 'enable_2fa' => false, 'login' => [ 'email' => ['App\Notifications\Rakshak\LoginOtpMail'], 'sms' => ['App\Notifications\Rakshak\LoginOtpSms'], 'verify_mobile' => ['App\Notifications\Rakshak\VerifyMobileOtpSms'], 'otp_template' => 'Your OTP for ' . config('app.name') . ' is 234567. It is valid for the next 10 minutes only.', 'verify_mobile_sms_template' => '%s: Confirmation code to verify your mobile number is %s.' ], 'register' => [ 'welcome_email' => ['App\Notifications\Rakshak\RegistrationWelcomeEmail'], 'welcome_sms' => ['App\Notifications\Rakshak\RegistrationWelcomeSms'], 'welcome_template' => 'Welcome %s! We are happy to have you onboard. Team %s', ], /* |-------------------------------------------------------------------------- | Authorisation Roles |-------------------------------------------------------------------------- | | Define the name for the super user role. | Define the name for the authorizer user role. | User with authorizer roler can perform crud ops for Role & Ability | */ 'roles' => [ 'super_user' => 'super', 'authorizer' => 'rakshak' ], /* |-------------------------------------------------------------------------- | Sender |-------------------------------------------------------------------------- | | Define the sender for email and sms messages. | */ 'sender' => [ 'email' => [ 'from' => env('RAKSHAK_EMAIL_FROM', config('mail.from.address')), 'name' => env('RAKSHAK_EMAIL_FROM_NAME', config('mail.from.address')) ], 'sms' => [ 'from' => env('TEXTLOCAL_TRANSACTIONAL_SENDER'), 'number' => env('TEXTLOCAL_TRANSACTIONAL_FROM') ] ] ];
您可以使用 Laravel 通知渠道 中的任何一种进行通知。默认情况下,包包括 Textlocal 通知渠道 用于短信消息。
使用
该包提供启用 双因素认证 的选项,以及控制级别,即一旦管理员启用 2fa
模块,它可以由管理员应用于所有用户,或者可以给每个用户选择启用 2fa
的选项。
双因素认证
路由:/rakshak/settings
也提供了方便其他/客户端管理员控制双因素认证的方法。
如果您想提供对启用/禁用双因素认证的用户级控制,则需要实现
- 用户访问设置区域的路由和导航
- 设置视图以
- 启用/禁用
2fa
,选择通道sms
或默认email
- 如果选择
sms
通道,检查手机(号码)是否已验证 - 按钮/链接以发送用于手机(号码)验证的 otp
- 启用/禁用
HasRakshak 特性
根据 config('auth.providers.users.model')
更新 User.php
文件以使用 HasRakshak
特性。该特性提供了一系列函数和 roles
关系到 User
。
$user = User::first(); $role = Role::whereName('hr_manager')->first(); $ability = Ability::whereName('manage_users')->first();
向角色添加和撤回能力
// You can add an existing ability to a role //By passing the Ability model instance $role->addAbility($ability); //Or by passing an Ability name string $role->addAbility('manage_users'); //To retract an ability form a role //By passing the Ability name string $role->retractAbility('manage_users'); //Or - by passing the model instance $role->retractAbility($ability);
分配和撤回角色给用户
//By passing the Role model instance $user->assignRole($role); //Or - by passing the Role name string $user->assignRole('hr_manager'); //Retract a role from the user //By passing the Role model instance $user->retractRole($role); //Or by passing the Role name string $user->retractRole('hr_manager');
检查用户是否有角色
//Passing the Role model instance $user->hasRole($role); //Passing the Role name string $user->hasRole('hr_manager');
检查用户是否有给定的多个角色之一
//Passing an array of multiple Role model instances $user->hasAnyRole([$role, $role2]); //Passing array of multiple Role name strings $user->hasAnyRole(['hr_manager', 'content_manager']);
检查用户是否有能力
//Passing the Ability model instance $user->hasAbility($ability); //Passing the Ability name string $user->hasAbility('manage_users');
检查用户是否有给定的多个能力之一
//Passing an array of multiple Ability model instances $user->hasAnyAbility([$ability, $ability2]); //Passing array of multiple Ability name strings $user->hasAnyAbility(['manage_users', 'manage_content']);
角色和能力
路由:/rakshak/roles
将提供所有现有角色的列表
路由:/rakshak/roles/{role}/edit
将提供编辑角色表单
路由:/rakshak/roles/create
将提供创建新角色表单
同样,还提供了对应 Abilities 的路由,以方便进行 CRUD 操作。
所有包视图都已发布到 resources/views/vendor/rakshak
目录。
路由中间件
该包注册了一个名为 role
的路由中间件。
//Protect the route and make it accessible only to users having hr_manager role. Route::get('/some-route', 'SomeController@action')->middleware('role:hr_manager'); //Protect the route and make it accessible only to users having either of hr_manager, content_manager or super role. Route::get('/some-route', 'SomeController@action')->middleware('role:hr_manager|content_manager|super');
为了使用有效的 opt 保护路由,请使用 rakshak.2fa
中间件。
一旦启用双因素认证并将 rakshak.2fa
中间件应用于任何路由,它将检查与用户关联的有效 otp。
一旦生成的 otp 发送给用户且用户使用 otp 验证登录,它将保持验证状态,直到会话生命周期结束。
//Protect the route to check for valid otp token. Route::post('/another-route', 'AnotherController@action')->middleware('rakshak.2fa');
Blade 指令
@role('hr_manager') User has the role of hr_manager @elserole('super') User has the role of super @else User does not have the role of hr_manager or super @endrole
如果您想检查多个角色,还有一个可以检查多个由 |
分隔的角色的指令。
@anyrole('hr_manager|super') Visible to user having role of hr_manager or super @else User does not have the role of hr_manager or super @endanyrole
变更日志
请参阅 变更日志 了解最近发生了什么变化。
测试
$ composer test
安全
如果您发现任何安全问题,请通过电子邮件 neerav@thinkstudeo.com 报告,而不是使用问题跟踪器。
贡献
请参阅 贡献指南 获取详细信息。
鸣谢
许可
MIT 许可证 (MIT)。请参阅 许可文件 获取更多信息。