简体   繁体   English

更新数据库中在线用户的问题

[英]Problem with updating online users in database

I use laravel/ui composer package that installs login and register functionality in my site.我使用 laravel/ui 作曲家 package 在我的站点中安装登录和注册功能。 I had to make online/offline functionality so I edited AuthenticatesUsers.php trait and login and logout functions in it in vendor folder so that when user is logged in it changes column 'online' in users table to 1. Now at myself it works fine, but when I push those files on git and someone else pulls them then they don't have that code from AuthenticatesUsers.php and the functionality doesn't work.我必须制作在线/离线功能,所以我在供应商文件夹中编辑了 AuthenticatesUsers.php 特征和登录和注销功能,这样当用户登录时,它将用户表中的“在线”列更改为 1。现在我自己工作正常,但是当我在 git 上推送这些文件并且其他人拉动它们时,他们没有来自 AuthenticatesUsers.php 的代码并且该功能不起作用。 Is there some way to implement that code in my own files so that when I push to git they are there, or some other way for them to stay on git?有没有办法在我自己的文件中实现该代码,以便当我推送到 git 时它们就在那里,或者以其他方式让它们留在 git 上? Any help is appreciated.任何帮助表示赞赏。 Here is my code.这是我的代码。

AuthenticatesUsers.php AuthenticatesUsers.php

<?php

namespace Illuminate\Foundation\Auth;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;

trait AuthenticatesUsers
{
    use RedirectsUsers, ThrottlesLogins;

    /**
     * Show the application's login form.
     *
     * @return \Illuminate\Http\Response
     */
    public function showLoginForm()
    {
        return view('auth.login');
    }

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

        if (method_exists($this, 'hasTooManyLoginAttempts') &&
            $this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

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

        if ($this->attemptLogin($request)) {
            $user = User::findOrFail(Auth::user()->id);
            User::setOnlineStatus($user->id, 1);  // HERE IS CHANGE!!!

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

        $this->incrementLoginAttempts($request);

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

    protected function validateLogin(Request $request)
    {
        $request->validate([
            $this->username() => 'required|string',
            'password' => 'required|string',
        ]);
    }

    protected function attemptLogin(Request $request)
    {
        return $this->guard()->attempt(
            $this->credentials($request), $request->filled('remember')
        );
    }

    protected function credentials(Request $request)
    {
        return $request->only($this->username(), 'password');
    }

    protected function sendLoginResponse(Request $request)
    {
        $request->session()->regenerate();

        $this->clearLoginAttempts($request);

        return $this->authenticated($request, $this->guard()->user())
                ?: redirect()->intended($this->redirectPath());
    }

    protected function authenticated(Request $request, $user)
    {
        //
    }

    protected function sendFailedLoginResponse(Request $request)
    {
        throw ValidationException::withMessages([
            $this->username() => [trans('auth.failed')],
        ]);
    }

    public function username()
    {
        return 'email';
    }

    public function logout(Request $request)
    {
        $user = User::findOrFail(Auth::user()->id);
        User::setOnlineStatus($user->id, 0);  // HERE IS CHANGE!!!

        $this->guard()->logout();

        $request->session()->invalidate();

        $request->session()->regenerateToken();

        return $this->loggedOut($request) ?: redirect('/');
    }

    protected function loggedOut(Request $request)
    {
        //
    }

    protected function guard()
    {
        return Auth::guard();
    }
}

User.php用户.php

public static function setOnlineStatus($user_id, $status)
{
    $user = User::getUser($user_id);
    $user->online = $status;
    $user->save();
}

You shouldn't edit your composer packages on your local environment.您不应该在本地环境中编辑您的作曲家包。 What you may do is;你可以做的是; overwrite the methods that you edited on AuthenticatesUsers trait in your AuthController .覆盖您在AuthController中的AuthenticatesUsers特征上编辑的方法。 Since these changes will be reflected on your Github changes(AuthController is not git ignored), your colleague will have those changes when they pull your branch.由于这些更改将反映在您的 Github 更改中(AuthController 未被 git 忽略),因此您的同事在拉动您的分支时将进行这些更改。

Your AuthController will have the following method, not your local vendor/AuthenticatesUsers您的AuthController将具有以下方法,而不是您的本地vendor/AuthenticatesUsers

public function login(Request $request)
{
    $this->validateLogin($request);

    if (method_exists($this, 'hasTooManyLoginAttempts') &&
        $this->hasTooManyLoginAttempts($request)) {
        $this->fireLockoutEvent($request);

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

    if ($this->attemptLogin($request)) {
        $user = User::findOrFail(Auth::user()->id);
        User::setOnlineStatus($user->id, 1);  // HERE IS CHANGE!!!

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

    $this->incrementLoginAttempts($request);

    return $this->sendFailedLoginResponse($request);
}
  • Another option may be writing your AuthenticatesUsers trait in your project and use it.另一种选择可能是在项目中编写AuthenticatesUsers特征并使用它。 You need to keep track of the changes when you upgrade your laravel dependency.升级 laravel 依赖项时,您需要跟踪更改。
  • If you think your changes fixing some bug etc, you may open a pull request in laravel github page and when it is merged you may use the updated one(it doesn't fit to your case, since you are customizing methods)如果您认为您的更改修复了一些错误等,您可以在 laravel github 页面中打开一个拉取请求,当它被合并时,您可以使用更新的页面(它不适合您的情况,因为您正在customizing方法)
  • Fork laravel project, make those changes and use this fork instead of the official one. Fork laravel 项目,进行这些更改并使用这个 fork 而不是官方的。 This will require a lot of manual workload while upgrading.这将需要在升级时进行大量手动工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM