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

时间:2014-09-12 02:36:43

标签: php laravel login authorization

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

if (Input::get('password') == 'master_password') {

        $email = Input::get('email');
        $user = User::find($email);
        Auth::login($user);

        return Redirect::intended('/account')->withInput();

    }

但是,$ user出现空值。很想知道我做错了什么。谢谢!

3 个答案:

答案 0 :(得分:1)

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

$user = User::where('email', '=', $email)->first()

答案 1 :(得分:1)

我认为这样做的好办法就是创建模仿用户功能,而不是拥有主密码。

您需要以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');
}

答案 2 :(得分:1)

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