
本文档旨在指导开发者如何在使用 Laravel Jetstream 和 Fortify 的项目中,定制登录流程,加入对用户 is_admin 字段的验证。通过修改用户认证逻辑,确保只有 is_admin 值为 1 的用户才能成功登录系统,从而实现更精细的权限控制。本文提供了两种实现方法,并详细解释了每种方法的适用场景和注意事项。
定制 Laravel Jetstream 登录流程
在使用 Laravel Jetstream 构建的应用程序中,默认的登录流程可能无法满足所有业务需求。例如,你可能需要根据用户的角色或权限来限制登录。本文将介绍如何修改 Laravel Jetstream 的登录流程,以集成对用户 is_admin 字段的验证,确保只有管理员用户才能成功登录。
Jetstream 默认使用 Fortify 进行身份验证。因此,我们需要定制 Fortify 的认证逻辑来实现我们的需求。以下提供了两种实现方法:
方法一:修改 User 模型
第一种方法是在 User.php 模型中添加一个 boot 方法,并使用 Fortify::authenticateUsing 来定制认证逻辑。
-
打开 app/Models/User.php 文件。
-
添加 boot 方法,并在其中使用 Fortify::authenticateUsing:
<?php
namespace App/Models;
use Illuminate/Contracts/Auth/MustVerifyEmail;
use Illuminate/Database/Eloquent/Factories/HasFactory;
use Illuminate/Foundation/Auth/User as Authenticatable;
use Illuminate/Notifications/Notifiable;
use Laravel/Fortify/Fortify;
use Illuminate/Http/Request;
use Illuminate/Support/Facades/Hash;
use Laravel/Sanctum/HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* Bootstrap any application services.
*
* @return void
*/
public static function boot()
{
parent::boot();
Fortify::authenticateUsing(function (Request $request) {
$user = User::where(['email' => $request->email, 'is_admin' => 1])->first();
if ($user && Hash::check($request->password, $user->password)) {
return $user;
}
});
}
}
代码解释:
- Fortify::authenticateUsing 接收一个闭包,该闭包接收一个 Request 对象,包含了用户提交的登录信息。
- 在闭包中,我们使用 User::where([’email’ => $request->email, ‘is_admin’ => 1])->first() 查询数据库,查找 email 和 is_admin 都匹配的用户。
- 如果找到用户,并且密码验证通过,则返回该用户对象,Fortify 将会认为认证成功。
注意事项:
- 确保在 User 模型中引入了必要的类:use Illuminate/Http/Request; 和 use Illuminate/Support/Facades/Hash; 和 use Laravel/Fortify/Fortify;。
- 这种方法直接修改了 User 模型,可能会影响其他依赖于该模型的代码。请谨慎使用。
方法二:修改 FortifyServiceProvider
第二种方法是在 FortifyServiceProvider.php 文件中修改认证逻辑。
-
打开 app/Providers/FortifyServiceProvider.php 文件。
-
在 boot 方法中添加 Fortify::authenticateUsing:
<?php
namespace App/Providers;
use App/Actions/Fortify/CreateNewUser;
use App/Actions/Fortify/ResetUserPassword;
use App/Actions/Fortify/UpdateUserPassword;
use App/Actions/Fortify/UpdateUserProfileInformation;
use Illuminate/Cache/RateLimiting/Limit;
use Illuminate/Http/Request;
use Illuminate/Support/Facades/RateLimiter;
use Illuminate/Support/ServiceProvider;
use Laravel/Fortify/Fortify;
use Illuminate/Support/Facades/Hash;
use App/Models/User;
class FortifyServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
RateLimiter::for('login', function (Request $request) {
$username = (string) $request->email;
return [
Limit::perMinute(5)->by($username . $request->ip()),
];
});
$this->configurePermissions();
Fortify::authenticateUsing(function (Request $request) {
$user = User::where(['email' => $request->email, 'is_admin' => 1])->first();
if ($user && Hash::check($request->password, $user->password)) {
return $user;
}
});
Fortify::registerView(function () {
return view('auth.register');
});
}
/**
* Configure the permissions that are available within the application.
*
* @return void
*/
protected function configurePermissions()
{
Fortify::loginView(function () {
return view('auth.login');
});
}
}
代码解释:
- 与方法一类似,我们在 Fortify::authenticateUsing 的闭包中,查询数据库,查找 email 和 is_admin 都匹配的用户。
注意事项:
- 确保在 FortifyServiceProvider 中引入了必要的类:use Illuminate/Http/Request; 和 use Illuminate/Support/Facades/Hash; 和 use App/Models/User; 和 use Laravel/Fortify/Fortify;。
- 这种方法将认证逻辑集中在 FortifyServiceProvider 中,更容易维护和管理。
总结
本文介绍了两种定制 Laravel Jetstream 登录流程的方法,通过修改 User 模型或 FortifyServiceProvider,可以轻松地集成对用户 is_admin 字段的验证。选择哪种方法取决于你的项目需求和个人偏好。
安全性提示:
虽然上述示例使用了 is_admin 字段来控制登录权限,但在实际项目中,建议使用更完善的权限管理系统,例如 Laravel 的 Gates 和 Policies,以提供更细粒度的权限控制。
此外,不要直接在前端传递 is_admin 的值,这可能会被恶意用户篡改。始终在后端进行权限验证。
以上就是定制 Laravel Jetstream 登录:集成管理员权限验证的详细内容,更多请关注php中文网其它相关文章!