doutun1362 2014-01-08 11:50
浏览 57
已采纳

如何在Laravel 4中使用Sentry 2

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 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.

I only want the user with email test@hotmail.com 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'       => 'test@hotmail.com',
            'password'    => "test",
            'activated'   => 1,
        ));

        $user  = Sentry::getUserProvider()->findByLogin('test@hotmail.com');
        $adminGroup = Sentry::getGroupProvider()->findByName('Test');
        $user->addGroup($adminGroup);
    }
}

In my PersonController

class PersonController extends BaseController {

    public function index()
    {
        try
        {
            $user = Sentry::findUserByLogin('test@hotmail.com');

            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);
        }
    }
  • 写回答

1条回答 默认 最新

  • dongpian4954 2014-01-08 12:32
    关注

    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',
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。