How to use Sentry 2 in Laravel 4

10,238

Looks like you need to use your own users table and also use Sentry's. So you'll need to add related Sentry's columns to yours. It's easy:

1) Go to vendor\cartalyst\sentry\src\migrations.

2) Create one new migration for every file you see there, example:

php artisan migrate:make add_sentry_groups_table

3) Copy the up() and down() code (ONLY!) to your new migrations.

4) And, for the users migration, you'll have to do some changes:

  • Instead of Schema::create('users' ... you do Schema::table('users' ..., to add more columns to your table.

  • Delete all commands for columns that you alread have in your current users table, examples of lines you must delete:

    $table->increments('id'); 
    $table->timestamps();
    

5) Run a normal ´php artisan migrate´.

After that you should have the Sentry's tables ready to work.

EDIT

As you're not using the usual 'email' and 'password' columns, publish Sentry's configuration:

php artisan config:publish cartalyst/sentry

And alter

'login_attribute' => 'user_email',
Share:
10,238
Gilko
Author by

Gilko

Updated on June 04, 2022

Comments

  • Gilko
    Gilko almost 2 years

    I have a Personcontroller and a Festivalcontroller in my laravel4 application. The actions in those controllers can only be accessible by an administrator.

    If my database only has a user with [email protected], that user can access the routes of those 2 controllers. If my database has no user with [email protected], but it has other users, those other users can't access the routes of those 2 controllers. And when my database has a user with [email protected], and has other users, everyone can access the routes of those 2 controllers.

    I only want the user with email [email protected] to access the routes of those controllers.

    I installed Sentry2 by doing this:

    In composer.json file require:

    "cartalyst/sentry": "2.0.*"
    

    Run

    php composer.phar update
    

    In app > config > app.php:

    'Cartalyst\Sentry\SentryServiceProvider', => to the providers array

    'Sentry' => 'Cartalyst\Sentry\Facades\Laravel\Sentry', => to the aliases array

    After the installation I made the SentrySeeder file:

    <?php
    
    class SentrySeeder extends Seeder {
    
        public function run()
        {
            DB::table('users')->delete();
            DB::table('groups')->delete();
            DB::table('users_groups')->delete();
    
            Sentry::getUserProvider()->create(array(
                'email'       => '[email protected]',
                'password'    => "test",
                'activated'   => 1,
            ));
    
            $user  = Sentry::getUserProvider()->findByLogin('[email protected]');
            $adminGroup = Sentry::getGroupProvider()->findByName('Test');
            $user->addGroup($adminGroup);
        }
    }
    

    In my PersonController

    class PersonController extends BaseController {
    
        public function index()
        {
            try
            {
                $user = Sentry::findUserByLogin('[email protected]');
    
                if ($user)
                {
                    $person = Person::with('user')->orderBy('person_id')->paginate(10);
    
                    return View::make('persons.index')
                       ->with('person', $person);
                }
            }
            catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
            {
                echo 'User was not found.';
            }
    
        }
    }
    

    Login action in LoginController

    public function login()
    {
        $input = Input::all();
        $rules = array(
            'user_email'    => 'required', 
            'user_password' => 'required'
        );
    
        $validator = Validator::make($input, $rules);
    
        if ($validator->fails()) {
            return Redirect::to('login')
                ->withErrors($validator) // send back all errors to the login form
                ->withInput(Input::except('user_password'));
        } 
        else {
            $attempt = Auth::attempt([
                'user_email'    => $input['user_email'],
                'password'  => $input['user_password']
            ]);
    
            if ($attempt) {
                return Redirect::to('/home');
             } 
            else {
                return Redirect::to('login');
            }
    
        }
    

    Store a user in database

    public function store()
        {
            $input = Input::all();
    
            $rules = array(
                'user_email'      => 'required|unique:users|email',
                'user_username'      => 'required|unique:users',
            );
            $validator = Validator::make($input, $rules);
    
            if($validator->passes())
            {
                $password = $input['user_password'];
                $password = Hash::make($password);
    
                $location = new Location();
    
                $person = new Person();
    
                $user = new User();
    
                $person->person_firstname = $input['person_firstname'];
                $person->person_surname = $input['person_surname'];
    
                $user->user_username = $input['user_username'];
                $user->user_email = $input['user_email'];
                $user->user_password = $password;
    
                $location->save();
    
                $person->save();
                $user->location()->associate($location);
                $user->person()->associate($person);
    
                $user->save();
    
                Session::flash('message', 'Successfully created user!');
                return Redirect::to('login');
            }
            else {
                return Redirect::to('persons/create')->withInput()->withErrors($validator);
            }
        }