如何在 Laravel 4 中使用 Sentry 2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20994838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:51:20  来源:igfitidea点击:

How to use Sentry 2 in Laravel 4

phplaravellaravel-4cartalyst-sentry

提问by Gilko

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

我的 laravel4 应用程序中有一个 Personcontroller 和一个 Festivalcontroller。这些控制器中的操作只能由管理员访问。

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.

如果我的数据库只有 [email protected] 的用户,则该用户可以访问这 2 个控制器的路由。如果我的数据库没有 [email protected] 的用户,但它有其他用户,则其他用户无法访问这 2 个控制器的路由。当我的数据库有一个用户 [email protected] 并且有其他用户时,每个人都可以访问这两个控制器的路由。

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

我只希望使用电子邮件 [email protected] 的用户访问这些控制器的路由。

I installed Sentry2 by doing this:

我通过这样做安装了 Sentry2:

In composer.json file require:

在 composer.json 文件中需要:

"cartalyst/sentry": "2.0.*"

Run

php composer.phar update

In app > config > app.php:

在 app > config > app.php 中:

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

'Cartalyst\Sentry\SentryServiceProvider',=> 到提供者数组

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

'Sentry' => 'Cartalyst\Sentry\Facades\Laravel\Sentry',=> 到别名数组

After the installation I made the SentrySeeder file:

安装后我制作了 SentrySeeder 文件:

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

在我的 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

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);
        }
    }

回答by Antonio Carlos Ribeiro

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:

看起来您需要使用自己的用户表并使用 Sentry 表。因此,您需要将相关的 Sentry 列添加到您的列中。这很简单:

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

1) 前往vendor\cartalyst\sentry\src\migrations

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

2)为您在那里看到的每个文件创建一个新迁移,例如:

php artisan migrate:make add_sentry_groups_table

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

3) 将up()down()代码(仅!)复制到您的新迁移中。

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

4) 而且,对于用户迁移,您必须进行一些更改:

  • 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();
    
  • 而不是Schema::create('users' ...您这样做Schema::table('users' ...,而是将更多列添加到您的表中。

  • 删除当前用户表中已有的列的所有命令,必须删除的行示例:

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

5) Run a normal ′php artisan migrate′.

5)运行一个普通的'php artisan migrate'。

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

之后,您应该准备好 Sentry 的工作台。

EDIT

编辑

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

由于您没有使用通常的“电子邮件”和“密码”列,请发布 Sentry 的配置:

php artisan config:publish cartalyst/sentry

And alter

并改变

'login_attribute' => 'user_email',