title: How to use Sentry 2 in Laravel 4
body: <p>I have a Personcontroller and a Festivalcontroller in my laravel4 application. The actions in those controllers can only be accessible by an administrator. </p>
  
  <p>If my database only has a user with test@hotmail.com, that user can access the routes of those 2 controllers. If my database has no user with test@hotmail.com, 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 test@hotmail.com, and has other users, everyone can access the routes of those 2 controllers.</p>
  
  <p>I only want the user with email test@hotmail.com to access the routes of those controllers.</p>
  
  <p>I installed Sentry2 by doing this:</p>
  
  <p>In composer.json file require:</p>
  
  <pre><code>"cartalyst/sentry": "2.0.*"
  </code></pre>
  
  <p>Run</p>
  
  <pre><code>php composer.phar update
  </code></pre>
  
  <p>In app > config > app.php:</p>
  
  <p><code>'Cartalyst\Sentry\SentryServiceProvider',</code> => to the providers array</p>
  
  <p><code>'Sentry' =&gt; 'Cartalyst\Sentry\Facades\Laravel\Sentry',</code> => to the aliases array</p>
  
  <p>After the installation I made the SentrySeeder file:</p>
  
  <pre><code>&lt;?php
  
  class SentrySeeder extends Seeder {
  
      public function run()
      {
          DB::table('users')-&gt;delete();
          DB::table('groups')-&gt;delete();
          DB::table('users_groups')-&gt;delete();
  
          Sentry::getUserProvider()-&gt;create(array(
              'email'       =&gt; 'test@hotmail.com',
              'password'    =&gt; "test",
              'activated'   =&gt; 1,
          ));
  
          $user  = Sentry::getUserProvider()-&gt;findByLogin('test@hotmail.com');
          $adminGroup = Sentry::getGroupProvider()-&gt;findByName('Test');
          $user-&gt;addGroup($adminGroup);
      }
  }
  </code></pre>
  
  <p>In my PersonController</p>
  
  <pre><code>class PersonController extends BaseController {
  
      public function index()
      {
          try
          {
              $user = Sentry::findUserByLogin('test@hotmail.com');
  
              if ($user)
              {
                  $person = Person::with('user')-&gt;orderBy('person_id')-&gt;paginate(10);
  
                  return View::make('persons.index')
                     -&gt;with('person', $person);
              }
          }
          catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
          {
              echo 'User was not found.';
          }
  
      }
  }
  </code></pre>
  
  <p>Login action in LoginController</p>
  
  <pre><code>public function login()
  {
      $input = Input::all();
      $rules = array(
          'user_email'    =&gt; 'required', 
          'user_password' =&gt; 'required'
      );
  
      $validator = Validator::make($input, $rules);
  
      if ($validator-&gt;fails()) {
          return Redirect::to('login')
              -&gt;withErrors($validator) // send back all errors to the login form
              -&gt;withInput(Input::except('user_password'));
      } 
      else {
          $attempt = Auth::attempt([
              'user_email'    =&gt; $input['user_email'],
              'password'  =&gt; $input['user_password']
          ]);
  
          if ($attempt) {
              return Redirect::to('/home');
           } 
          else {
              return Redirect::to('login');
          }
  
      }
  </code></pre>
  
  <p>Store a user in database</p>
  
  <pre><code>public function store()
      {
          $input = Input::all();
  
          $rules = array(
              'user_email'      =&gt; 'required|unique:users|email',
              'user_username'      =&gt; 'required|unique:users',
          );
          $validator = Validator::make($input, $rules);
  
          if($validator-&gt;passes())
          {
              $password = $input['user_password'];
              $password = Hash::make($password);
  
              $location = new Location();
  
              $person = new Person();
  
              $user = new User();
  
              $person-&gt;person_firstname = $input['person_firstname'];
              $person-&gt;person_surname = $input['person_surname'];
  
              $user-&gt;user_username = $input['user_username'];
              $user-&gt;user_email = $input['user_email'];
              $user-&gt;user_password = $password;
  
              $location-&gt;save();
  
              $person-&gt;save();
              $user-&gt;location()-&gt;associate($location);
              $user-&gt;person()-&gt;associate($person);
  
              $user-&gt;save();
  
              Session::flash('message', 'Successfully created user!');
              return Redirect::to('login');
          }
          else {
              return Redirect::to('persons/create')-&gt;withInput()-&gt;withErrors($validator);
          }
      }
  </code></pre>