0

I've installed the Sentinel laravel bundle into my project.

I've published the packages views and added several fields to the registration form. I wanted those fields to write to my users database table.

I found the store() method of the SentryUser class in the SentryUser.php file within the bundle's directories and saw the line:

$user = $this->sentry->register(array('email' => e($data['email']), 'password' => e($data['password'])));

I modified this mapping to include one of my additional fields:

$user = $this->sentry->register(array('email' => e($data['email']), 'password' => e($data['password']), 'first_name' => e($data['first_name']) ));

Which totally works.

So now that I know where the data needs to be passed in, I want to remove those changes, create a new extendedSentryUser.php file in my app\model directory, and extend the SentryUser:

use Sentinel\Repo\User\SentryUser;

<?php namespace Apps\Models;

use Sentinel\Repo\User\SentryUser;


class extendedSentryUser extends SentryUser {

    protected $sentry;

    /**
     * Construct a new SentryUser Object
     */
    public function __construct(SentryUser $sentry)
    {
        $this->sentry = $sentry;
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store($data)
    {

        $result = array();
        try {
            //Attempt to register the user. 
            $user = $this->sentry->register(array('email' => e($data['email']), 'password' => e($data['password']), 'first_name' => e($data['first_name']) ));

            // Add the new user to the specified default group(s).
            $defaultUserGroups = Config::get('Sentinel::config.default_user_groups');

            foreach ($defaultUserGroups as $groupName) {
                $group = $this->sentry->getGroupProvider()->findByName($groupName);
                $user->addGroup($group);
            }

            //success!
            $result['success'] = true;
            $result['message'] = trans('Sentinel::users.created');
            $result['mailData']['activationCode'] = $user->GetActivationCode();
            $result['mailData']['userId'] = $user->getId();
            // $result['mailData']['first_name'] = e($data['first_name']);
            $result['mailData']['email'] = e($data['email']);
        }
        catch (\Cartalyst\Sentry\Users\LoginRequiredException $e)
        {
            $result['success'] = false;
            $result['message'] = trans('Sentinel::users.loginreq');
        }
        catch (\Cartalyst\Sentry\Users\UserExistsException $e)
        {
            $result['success'] = false;
            $result['message'] = trans('Sentinel::users.exists');
        }

        return $result;
    }
}

The thing I'm hung up on is how do I get my application to use this model instead of the existing one? Am I on the completely wrong path???

Chris Schmitz
  • 20,160
  • 30
  • 81
  • 137

1 Answers1

1

Look at the following file: / src / Sentinel / Repo / RepoServiceProvider.php

At the top of the file, replace use Sentinel\Repo\User\SentryUser; with your own class, which looks it should be use Apps\Models\extendedSentryUser;

Then down on line 26, modify the code to use your class instead:

// Bind the User Repository
        $app->bind('Sentinel\Repo\User\UserInterface', function($app)
        {
            //change SentryUser to extendedSentryUser
            return new extendedSentryUser(
                $app['sentry']
            );
        });

Also, you probably want your extendedSentryUser's constructor to just call the parent's construct via parent::__construct.

haven't tested this but it should work. You might need to do a composer dumpautoload as well.

Brian Glaz
  • 15,468
  • 4
  • 37
  • 55
  • I think your constructor on your custom class might be off. The original SentryUser is looking for an instance of Sentry as the first argument, and not a SentryUser. – Brian Glaz Apr 18 '14 at 17:51
  • Actually, just remove the constructor from your custom class altogether, and it will just use the parent one. – Brian Glaz Apr 18 '14 at 17:58
  • Gotcha. I've applied the changes you recommended including the removal of the constructor in my custom class. I'm able to get to the form now, though when I submit I get an error because the app is looking for the `Config::get('Sentinel::config.default_user_groups');` and can't find it. It's looking for it in my `Apps/Models/Config` directory. I think I need to publish the config file for sentry, though that would put it in my `Apps/Config` folder. – Chris Schmitz Apr 18 '14 at 18:04
  • try just adding `use Config;` at the top of your custom class. – Brian Glaz Apr 18 '14 at 18:07
  • Dude, you are a freaking LIFE SAVER!!! That got everything up and running. So just to be sure I'm understanding everything correctly: Editing the `RepoServiceProvider` class and changing the User's bind method to use my custom class means that any time a Sentinel user model is created it will use my model instead. The constructor wasn't needed in my model class because it extends the `SentryUser` class which inherently fires the necessary constructor. Finally, I needed to use the Config class since I'm using it as a closure call in my overriding store method. Sound right? – Chris Schmitz Apr 18 '14 at 18:18
  • 1
    Yup, sounds about right. I think you need to use Config because it looks like Config::get is looking for a file relative to the namespace your class is in. By doing the use Config, it's going up to your top level laravel project. – Brian Glaz Apr 18 '14 at 18:24