Laravel 5.3 Auth 阻止用户

标签 laravel authentication laravel-5.3 laravel-authorization

我有一个问题,我目前正在使用 Laravel 5.3 开发一个小网站,我正在使用他们的基本身份验证供用户注册和登录。

现在我想要以下内容:每个人都可以注册和登录,但是如果我单击一个按钮(作为管理员),我可以“阻止”一个特定用户(例如,如果他做了不允许的事情),我不'不能完全删除数据库中的行,但以某种方式确保如果用户尝试登录,他会收到一条消息,内容为“您无法再登录,您的帐户已被阻止,请联系管理员以获取更多信息”或类似的内容。问题是:最好的方法是什么?我没有找到内置的东西,如果我错了,请纠正我...... 当然,我可以更改用户表并添加一个名为“blocked”的列,通常设置为 false,然后使用按钮将其设置为 true,然后在登录时以某种方式检查该值并(如果为 true)显示此值消息并且不允许登录。这是执行此操作的最佳方法吗?如果是,我必须在哪里检查这个值以及如何显示该消息?如果没有,更好的方法是什么?

最佳答案

我会按照您的建议进行操作 - 使用 blockedactive 列来指示用户是否应该能够登录。当我完成某些操作后与过去类似,为了在登录时检查此值,我将开箱即用的登录功能移至我的 LoginController 中并添加了一些内容。我的登录方法现在如下所示:

/**
 * Handle a login request to the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function login(Request $request)
{
    $this->validateLogin($request);

    $user = User::where('email', $request->email)->firstOrFail();
    if ( $user && !$user->active ) {
        return $this->sendLockedAccountResponse($request);
    }

    if ($this->hasTooManyLoginAttempts($request)) {
        $this->fireLockoutEvent($request);

        return $this->sendLockoutResponse($request);
    }

    if ($this->attemptLogin($request)) {
        return $this->sendLoginResponse($request);
    }

    $this->incrementLoginAttempts($request);

    return $this->sendFailedLoginResponse($request);
}

我还添加了这些函数来处理不活跃的用户:

/**
 * Get the locked account response instance.
 *
 * @param \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
protected function sendLockedAccountResponse(Request $request)
{
    return redirect()->back()
        ->withInput($request->only($this->loginUsername(), 'remember'))
        ->withErrors([
            $this->loginUsername() => $this->getLockedAccountMessage(),
        ]);
}

/**
 * Get the locked account message.
 *
 * @return string
 */
protected function getLockedAccountMessage()
{
    return Lang::has('auth.locked')
            ? Lang::get('auth.locked')
            : 'Your account is inactive. Please contact the Support Desk for help.';
}

关于Laravel 5.3 Auth 阻止用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40636768/

相关文章:

php - 如何多次使用一个表中的一个外键在 Laravel 中获取数据?

asp.net-mvc - 根据用户角色类型更改布局

php - Laravel 5.3 更改密码

php 获取变量值并将其以动态方式附加到查询中

git - 最佳实践 Laravel gitignore

php - 如何解决错误 "Cookies are blocked due to unexpected output."?

java - 使用 amazon 登录并访问 S3、EC2 等 aws 资源

php - Laravel 5.3 PHP Artisan 不工作 PHP 警告 : require(bootstrap/. ./vendor/autoload.php)

php - Laravel 观察者不工作

javascript - Laravel 5 : show comments using Vue.js