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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?