3

我有一个问题,我目前正在使用 Laravel 5.3 开发一个小网站,并且我正在使用他们的 Basic Auth 让用户注册和登录。

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

4

5 回答 5

6

我会按照你的建议做 - 使用blockedoractive列来指示用户是否应该能够登录。当我过去做过类似的事情时,为了在登录时检查这个值,我把 -将盒子登录功能放入我的 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.';
}
于 2016-11-16T19:03:11.307 回答
1

您可以使用软删除功能。

除了实际从数据库中删除记录之外,Eloquent 还可以“软删除”模型。当模型被软删除时,它们实际上并没有从您的数据库中删除。相反,在模型上设置了一个 deleted_at 属性并插入到数据库中。如果模型具有非空 deleted_at 值,则模型已被软删除。

于 2016-11-16T16:06:44.963 回答
0

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

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

于 2020-11-01T08:08:23.020 回答
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’);
 }

第三步:

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

于 2017-01-09T19:27:56.720 回答
-1

已解决:此链接(教程)将为您提供帮助: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’);
 }

第三步:

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();
     }

完毕 :)

于 2017-01-09T19:18:13.887 回答