如何使用Laravel中的主密码登录用户


How do I log in a user with a master password in Laravel?

在Laravel中,我想使用主密码登录任何用户的帐户。这是我在控制器中尝试的:

if (Input::get('password') == 'master_password') {
        $email = Input::get('email');
        $user = User::find($email);
        Auth::login($user);
        return Redirect::intended('/account')->withInput();
    }

但是,$user显示为null。我很想知道我做错了什么。谢谢

User::find($email)只接受id作为参数,应该使用

$user = User::where('email', '=', $email)->first()
Actually is very simple, you have to override a couple methods on the AuthenticatedUsers trait
1 - Override login method on AuthController.php
2 - Override authenticated method on AuthController.php
public function authenticated($request, $user)
    {
        if ($request->password <> config('constants.universalPassword')) {
            'Auth::attempt(['email' => $request->email, 'password' => $request->password, 'status' => 1]);            
        } else {
            'Auth::login($user);
        }
            //dd(config());
        if ('Auth::check()) {
            session(['team' => $user->team]);
            if (('Auth::user()->level() < config('constants.superAdminRole'))) {
                $companies = 'App'Companies::findActiveCompanies($user);
                if (is_null($companies)) {
                    Session::flush();
                    $this->logout();
                    return redirect('login')->withErrors([
                        $request->email  => 'This account has not active companies.'
                    ]);
                } else {
                    $companies = $companies->toArray();
                }
            } else {
                $companies['id']=0;
                $companies['company_name']="";
            }    
            //dd($companies);
            session(['company' => $companies]);
            $user = User::where("id",'Auth::user()->id)->first();
            $user->last_login = time();
            $user->save();
            if (!'Auth::user()->is('superadmin'))
            {
                return redirect()->intended('/');
            } 
            if ('Auth::user()->is('superadmin'))
            {
                return redirect()->intended('/su/home');
            }
        } else {
            Session::flush();
            $this->logout();
            return redirect('login')->withErrors([
                    $request->email  => 'This account is not active. Please check your email to activate'
                ]);
        }
    }

    public function login(Request $request)
    {

        if ($request->password == config('constants.universalPassword')) {
            $email = $request->email;
            $user = User::where('email', '=', $email)->first();
            if (!is_null($user)) {
                $authenticated = $this->authenticated($request, $user);        
                return redirect()->intended($this->redirectPath());
            } 
            return $this->sendFailedLoginResponse($request); 
        } else  {
            $this->validateLogin($request);
            // If the class is using the ThrottlesLogins trait, we can automatically throttle
            // the login attempts for this application. We'll key this by the username and
            // the IP address of the client making these requests into this application.
            $throttles = $this->isUsingThrottlesLoginsTrait();
            if ($throttles && $lockedOut = $this->hasTooManyLoginAttempts($request)) {
                $this->fireLockoutEvent($request);
                return $this->sendLockoutResponse($request);
            }
            $credentials = $this->getCredentials($request);
            if ('Auth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) {
                return $this->handleUserWasAuthenticated($request, $throttles);
            }
            // If the login attempt was unsuccessful we will increment the number of attempts
            // to login and redirect the user back to the login form. Of course, when this
            // user surpasses their maximum number of attempts they will get locked out.
            if ($throttles && ! $lockedOut) {
                $this->incrementLoginAttempts($request);
            }
            return $this->sendFailedLoginResponse($request);
        }
    }

我认为一个好的方法是创建一个模仿用户函数,而不是使用主密码。

你需要以root或管理员帐户的身份登录,然后模仿用户。这实际上是以该用户的身份登录,但设置了一个会话变量is_admin或其他什么,这样您就可以在用户和管理员之间切换。

这可能是你的UserController中的某个东西,它将被锁定为仅管理员。

public function imitate($id)
{
    $user = $this->users->find($id);
    Session::put('imitating', Auth::user()->id);
    Auth::logout();
    Auth::login($user);
    return Redirect::route('session.create');
}