0

I look for to make two interface

  • Administrator (Administrator role)
  • Subscriber (subscriber role)

I used for now auth and entrust (Github Link) of Laravel 4

A recording User

in my SubscriptionContoller.php I make this :

public function doRegister(){

    $password=Input::get('password');

    $data = array(
    'email'           => Input::get('email'),
    'password'        => Hash::make($password)
    ); 

    $rules = array(
        'email'           => 'required|email',
        'password'        => 'required'
    );

    $messages = array(
        'required' => 'The :attribute field is required.',
    );

    $validator = Validator::make($data, $rules, $messages);        

    if ($validator->fails()) {

        Session::flash('message', 'This email is already registered, please choose another one.');

        return Redirect::to('subscription/register')
            ->withErrors($validator);

    } else {

        $user = new User;

        $role = Role::where('name','=','agency')->first();

        $user->email = Input::get('email');
        $user->password = Hash::make($password); 
        $user->save();

        $user->roles()->attach($role); 

        Session::flash('message', 'Successfully created account! Please login to submit your ad.');
        return Redirect::to('subscription/dashbord');
    }
}

How should i write the routes.php and filters.php ??

Zahan Safallwa
  • 3,880
  • 2
  • 25
  • 32
Sourceforest
  • 130
  • 1
  • 9

1 Answers1

0

Just put this line on top of your routes.php

Entrust::routeNeedsRole( 'subscription*', 'agency' );

More Details:

This will only allow users with agency role to be able to access subscription/* urls. If user didn't have that role, then by default an App::abort(403); will be executed, which will look for 403.blade.php in your views/public/ folder.

If you want to do something specific if user didn't match the role, you can pass the third parameter as follow:

Entrust::routeNeedsRole( 'subscription*', 'agency', Redirect::to('/not-authorized'));
Mustafa Ehsan Alokozay
  • 5,583
  • 2
  • 24
  • 30