即使组无效,Laravel Sentry也会注册用户

时间:2014-08-22 09:54:50

标签: php laravel cartalyst-sentry

问题是,即使指定的组不存在,Sentry仍在注册用户。

if(Request::isMethod('post'))
{

    $validation = Validator::make([
            'username'  =>  Input::get('username'),
            'password'  =>  Input::get('password'),
            'password_confirmation' =>  Input::get('password_confirmation')
        ],
        [
            'username'  =>  'unique:users,username|required',
            'password'  =>  'same:password_confirmation|required'
        ]);

    if($validation->fails())
        return Redirect::route('addUser')
            ->withErrors($validation->getMessageBag())
            ->withInput(Input::except(['password', 'password_confirmation']));
    else
    {
        /** Do sentry stuff */
        try
        {
            $activated = Input::get('activated') ? true : false;

            // Create the user
            $user = Sentry::createUser(array(
                'username'  =>  Input::get('username'),
                'email'     => Input::get('email'),
                'password'  => Input::get('password'),
                'activated' => $activated,
            ));

            $group = Sentry::findGroupById(Input::get('group'));
            $user->addGroup($group);
        }
        catch (Cartalyst\Sentry\Users\LoginRequiredException $e)
        {
            $message[] = 'Login field is required.';
        }
        catch (Cartalyst\Sentry\Users\PasswordRequiredException $e)
        {
            $message[] = 'Password field is required.';
        }
        catch (Cartalyst\Sentry\Users\UserExistsException $e)
        {
            $message[] = 'User with this login already exists.';
        }
        catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e)
        {
            $message[] = 'Group was not found.';
        }



        if(isset($message))
            return Redirect::route('addUser')
                ->withErrors($message)
                ->withInput(Input::except(['password', 'password_confirmation']));
        else
        {
            $message = 'User has been added';
            return Redirect::route('addUser')
                ->with('success', $message);
        }
    }
}
return $this->render('admin/users/add');

现在,由于异常被抛出,我会假设不会创建用户,但我无法弄清楚它是什么。

是否存在我缺少的特定方式或其他方式,如果没有抛出异常,则仅注册用户?

1 个答案:

答案 0 :(得分:0)

用户必须存在且具有Sentry的ID才能尝试添加该组。我的建议是将其分解为2个try / catch块:一个用于添加用户,一个用于将用户添加到组中,然后...

    catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e)
    {
        $message[] = 'Group was not found.';
        $user->delete();
    }
相关问题