首页 文章

使用Sentry与Codesleeve Stapler?

提问于
浏览
4

更新#1

所以我想我发现了问题所在,但由于我缺乏知识,我无法解决问题 .

当使用Sentry时,'ve tried extending both '模型' which is default with the Sentry package and Laravel' s 'Eloquent'在 Illuminate\Database\Eloquent\Model 下 . 都没有奏效 . 但是当他自己使用Laravel的Eloquent(没有Sentry包插件)时,Stapler工作得很好 .

所以我猜Stapler和Sentry的User模型之间存在一些兼容性问题,即使它使用了Eloquent(它的额外功能可能与订书机的特征或构造函数相冲突,我不知道) . 太糟糕了,因为Sentry是一个管理身份验证和限制的绝佳软件包,据我所知广泛使用 .

是否有任何简单的方法来使用与哨兵的订书机不可知版本或我将不得不重写我的所有代码以使用laravel的auth?


主要问题

我正在构建一个使用Cartalyst哨兵管理用户的应用程序 . 但是,我希望用户和其他模型能够拥有关联的头像,所以我也使用了Codesleeve的Stapler .

我已经阅读了laravel-stapler的自述文件和大部分stapler 's standalone version documentation but couldn'来弄清楚如何使它们与Sentry一起工作 . 每当我尝试通过此功能创建用户时:

$user = Sentry::createUser(array(
    'username'      => Input::get('username'),
    'email'         => Input::get('email'),
    'password'      => Input::get('password'),
    'activated'     => false,
    'avatar'        => Input::file('avatar')
));

return 'success';

我明白了:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'avatar' in 'field list'

所以似乎Sentry正在查询,好像化身是一个字符串而Stapler不工作 .

这是我的Sentry用户模型(我在Laravel中扩展):

use Illuminate\Database\Eloquent\SoftDeletingTrait;
use Illuminate\Database\Eloquent\Model;
use Codesleeve\Stapler\ORM\StaplerableInterface;
use Codesleeve\Stapler\ORM\EloquentTrait;
use Cartalyst\Sentry\Groups\GroupInterface;
use Cartalyst\Sentry\Hashing\HasherInterface;
use Cartalyst\Sentry\Users\LoginRequiredException;
use Cartalyst\Sentry\Users\PasswordRequiredException;
use Cartalyst\Sentry\Users\UserAlreadyActivatedException;
use Cartalyst\Sentry\Users\UserExistsException;
use Cartalyst\Sentry\Users\UserInterface;

class User extends Model implements UserInterface, StaplerableInterface {

        use SoftDeletingTrait, EloquentTrait;

        protected $fillabe = array('username','email','password','password_temp','code','active','avatar');

        public function __construct(array $attributes = array()) {
                $this->hasAttachedFile('avatar', [
                    'styles' => [
                        'medium' => '300x300',
                        'thumb' => '100x100'
                    ]
                ]);

                parent::__construct($attributes);
        }
        protected $dates = ['deleted_at'];

在Sentry的配置中:

'model' => 'Cartalyst\Sentry\Users\Eloquent\User',

这可能很容易解决,因为我是Laravel,Sentry和Stapler的新手,但我找不到任何能让我解决问题的信息 .

提前致谢!

1 回答

  • 1

    是的,可以做到 . 基于's in your OP, the root cause appears to be that your user model doesn'知道 avatar 是Stapler提供的列 .

    理论:Sentry用户提供商制造用户模型,right here . 根据Stapler docs,该用户模型必须是Stapler-aware . 幸运的是,Sentry是可配置的,您可以告诉它使用自定义模型setModel() .

    解决方案的粗略概述:首先,您需要一个能够识别Stapler的模型 . 你可能已经有了这个 . 请注意,它扩展了Sentry提供的模型类,还导入了Stapler特征 . 两者都很重要 .

    <?php // app/models/UserModel.php
    namespace App\Model;
    use Codesleeve\Stapler\ORM\StaplerableInterface;
    use Codesleeve\Stapler\ORM\EloquentTrait;
    use Cartalyst\Sentry\Users\Eloquent\User as SentryUserModel;
    
    class UserModel extends SentryUserModel implements StaplerableInterface {
        use EloquentTrait;
    }
    

    其次,你需要指示Sentry使用它 . 下面我使用运行时配置更改,这是我的偏好 . 但你也可以change the Sentry configuration file

    <?php // app/bootstrap.php
    
    \Sentry::getUserProvider()->setModel('App\Model\UserModel');
    

    我是从Laravel 4的内存中做到这一点的,因此可能需要进行一些调整 .

相关问题