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


Laravel Sentry registering user even though group is invalid

问题是,即使指定的组不存在,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');

现在,当抛出异常时,我假定用户不会被创建,但是我不知道为什么会这样。

是否有一种特定的方式或另一种方式,我错过了其中只注册用户,如果没有抛出异常?

用户必须存在并且具有id,以便Sentry尝试添加组。我的建议是将它分成2个try/catch块:一个用于添加用户,一个用于将用户添加到组,然后…

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