使用Sentry 2处理登录

Ste*_*ess 4 php laravel laravel-4 cartalyst-sentry

我无法理解Sentry 2的登录实现.我的意思是在哨兵这是相当紧张的前进.提供从输入到Sentry::login()方法的用户名/电子邮件和密码,但他们现在改变它,这真的很混乱.

首先,他们删除了Username列,这没有任何意义.
其次,登录方法现在需要使用用户的id检索一个User对象,这再次没有意义,因为你不知道用户id,除非你做了另一个查询,所以它们真的很复杂.

我的代码:

public function login()
{
    // Deny access to already logged-in user
    if(!Sentry::check())
    {
        $rules = array(
            'username' => 'required|unique:users',
            'password' => 'required' );

        $validator = Validator::make(Input::all(), $rules);

        if($validator->fails())
        {
            Session::flash('error', $validator->errors());
            return Redirect::to('/');
        }

        $fetch = User::where('username', '=', trim(Input::get('username')));
        $user = Sentry::getUserProvider()->findById($fetch->id);

        if(!Sentry::login($user, false))
        {
            Session::flash('error', 'Wrong Username or Password !');
        }

        return Redirect::to('/');

    }

    return Redirect::to('/');
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用这种方法,但它抛出一个异常:尽管id是表的一部分,但id是未知的,而User模型只是一个带有$ table ='users'的类声明; 属性.

我在这里做错了什么或不理解.

Ant*_*iro 17

下面的代码是我使用Sentry 2的登录方法.我基本上让Sentry为我做一切验证,找到用户,当然还有登录用户.消息是葡萄牙语,但如果你需要我翻译只是告诉.

public function login()
{
    try
    {
        $credentials = array(
            'email'    => Input::has('email') ? Input::get('email') : null,
            'password' => Input::has('password') ? Input::get('password') : null,
        );

        // Log the user in
        $user = Sentry::authenticate($credentials, Input::has('remember_me') and Input::get('remember_me') == 'checked');

        return View::make('site.common.message')
            ->with('title','Seja bem-vindo!')
            ->with('message','Você efetuou login com sucesso em nossa loja.');

    }
    catch (Cartalyst\Sentry\Users\LoginRequiredException $e)
    {
        return View::make('site.common.message')
            ->with('title','Erro')
            ->with('message','O campo do e-mail é necessário.');
    }
    catch (Cartalyst\Sentry\Users\PasswordRequiredException $e)
    {
        return View::make('site.common.message')
            ->with('title','Erro')
            ->with('message','O campo do senha é necessário.');
    }
    catch (Cartalyst\Sentry\Users\UserNotActivatedException $e)
    {
        $user = Sentry::getUserProvider()->findByLogin(Input::get('email'));

        Email::queue($user, 'site.users.emailActivation', 'Ativação da sua conta na Vevey');

        return View::make('site.common.message')
            ->with('title','Usuário não ativado')
            ->with('message',"O seu usuário ainda não foi ativado na nossa loja. Um novo e-mail de ativação foi enviado para $user->email, por favor verifique a sua caixa postal e clique no link que enviamos na mensagem. Verifique também se os nossos e-mails não estão indo direto para a sua caixa de SPAM.");
    }
    catch (Cartalyst\Sentry\Users\WrongPasswordException $e)
    {
        return View::make('site.common.message')
            ->with('title','Erro')
            ->with('message','A senha fornecida para este e-mail é inválida.');
    }       
    catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
    {
        return View::make('site.common.message')
            ->with('title','Erro')
            ->with('message','Não existe usuário cadastrado com este e-mail em nossa loja.');
    }

    // Following is only needed if throttle is enabled
    catch (Cartalyst\Sentry\Throttling\UserSuspendedException $e)
    {
        $time = $throttle->getSuspensionTime();

        return View::make('site.common.message')
            ->with('title','Erro')
            ->with('message',"Este usário está suspenso por [$time] minutes. Aguarde e tente novamente mais tarde.");
    }
    catch (Cartalyst\Sentry\Throttling\UserBannedException $e)
    {
        return View::make('site.common.message')
            ->with('title','Erro')
            ->with('message',"Este usário está banido do nossa loja.");
    }

}
Run Code Online (Sandbox Code Playgroud)

  • authenticate()和login()之间有什么区别? (2认同)