Laravel 5.3 Auth阻止用户

时间:2016-11-16 16:05:17

标签: laravel authentication laravel-5.3 laravel-authorization

我有一个问题,我目前正在使用Laravel 5.3开发一个小站点,我正在使用它们的Basic Auth供用户注册和登录。

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

5 个答案:

答案 0 :(得分:7)

我会按照您的建议行事 - 使用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.';
}

答案 1 :(得分:1)

您可以使用soft deleting功能。

  

除了实际删除数据库中的记录外,Eloquent还可以"软删除"楷模。模型被软删除后,实际上不会从数据库中删除它们。而是在模型上设置deleted_at属性并将其插入到数据库中。如果模型具有非null的deleted_at值,则模型已被软删除。

答案 2 :(得分:0)

步骤1:

add new field to the User table called ‘status’ (1:enabled, 0:disabed)

步骤2:

to block the web login , in app/Http/Controllers/Auth/LoginController.php add the follwoing function:

/**
 * Get the needed authorization credentials from the request.
 *
 * @param \Illuminate\Http\Request $request
 * @return array
 */
 protected function credentials(\Illuminate\Http\Request $request)
 {
 $credentials = $request->only($this->username(), ‘password’);

return array_add($credentials, ‘status’, ‘1’);
 }

步骤3:

to block the user when using passport authentication ( token ) , in the User.php model add the following function :

public function findForPassport($identifier) {
     return User::orWhere(‘email’, $identifier)->where(‘status’, 1)->first();
     }

参考此链接(教程)将对您有所帮助:https://medium.com/@mshanak/solved-tutorial-laravel-5-3-disable-enable-block-user-login-web-passport-oauth-4bfb74b0c810

答案 3 :(得分:0)

有一个软件包,它不仅可以阻止用户,还可以让您在决定是否阻止用户之前对其进行监视。

Laravel监视: https://github.com/neelkanthk/laravel-surveillance

答案 4 :(得分:-2)

已解决:此链接(教程)将为您提供帮助:https://medium.com/@mshanak/solved-tutorial-laravel-5-3-disable-enable-block-user-login-web-passport-oauth-4bfb74b0c810

步骤1:

add new field to the User table called ‘status’ (1:enabled, 0:disabed)

步骤2:

to block the web login , in app/Http/Controllers/Auth/LoginController.php add the follwoing function:

/**
 * Get the needed authorization credentials from the request.
 *
 * @param \Illuminate\Http\Request $request
 * @return array
 */
 protected function credentials(\Illuminate\Http\Request $request)
 {
 $credentials = $request->only($this->username(), ‘password’);

return array_add($credentials, ‘status’, ‘1’);
 }

步骤3:

to block the user when using passport authentication ( token ) , in the User.php model add the following function :

public function findForPassport($identifier) {
     return User::orWhere(‘email’, $identifier)->where(‘status’, 1)->first();
     }

完成:)