Laravel 4 教程 - 身份验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17199698/
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:00:06  来源:igfitidea点击:

Tutorial for Laravel 4 - Authentication

laravellaravel-4cartalyst-sentry

提问by tinyhook

I looked around and found some tutorials about Laravel 4authenticationusing Sentryor Confideand Entrust. Which are good but a little vague for me, I am Laravelbeginner and this is my first framework.

我环顾四周,发现了一些有关使用or和 进行Laravel 4身份验证的教程。这对我来说很好但有点模糊,我是初学者,这是我的第一个框架。SentryConfideEntrustLaravel

Does anyone know of any tutorial or suggestions implementing user authentication with user roles.

有谁知道使用用户角色实现用户身份验证的任何教程或建议。

Here is what I am trying to make. - Its an internal website for work. Where writers can sign in and submit articles. - Admins Can go over those articles. - These articles are not public so no one can view them. - Writers cannot see each others articles, but admins have access to everything.

这是我正在尝试制作的内容。- 它是一个内部工作网站。作者可以在这里登录并提交文章。- 管理员可以查看这些文章。- 这些文章不是公开的,所以没有人可以查看它们。- 作者不能看到彼此的文章,但管理员可以访问所有内容。

I am just looking for tutorial that goes over user roles and how to implement them.

我只是在寻找介绍用户角色以及如何实现它们的教程。

Edit

编辑

This is what I ended up doing.这就是我最终要做的。

After Installing Sentry in the way specified by @Antonio Carlos Ribeiro.

按照@Antonio Carlos Ribeiro指定的方式安装 Sentry 后。

I had Users,Groupsand few other tables (I just had to use user and groups).

我有UsersGroups和其他几个表(我只需要使用用户和组)。

Here is my seeder that I initially used for creating users and groups. It can be made more efficient, but for anyone who wants to just get started this would work.

这是我最初用于创建用户和组的播种机。它可以提高效率,但对于任何想要开始的人来说,这都行得通。

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'    => "admin",
        'first_name'  => 'John',
        'last_name'   => 'McClane',
        'activated'   => 1,
    ));

    Sentry::getUserProvider()->create(array(
        'email'       => '[email protected]',
        'password'    => "user",
        'first_name'  => 'Saad',
        'last_name'   => 'Kabir',
        'activated'   => 1,
    ));

    Sentry::getUserProvider()->create(array(
        'email'       => '[email protected]',
        'password'    => "user",
        'first_name'  => 'Hyman',
        'last_name'   => 'Doe',
        'activated'   => 1,
    ));

    Sentry::getUserProvider()->create(array(
        'email'       => '[email protected]',
        'password'    => "user",
        'first_name'  => 'Jon',
        'last_name'   => 'Doe',
        'activated'   => 1,
    ));

    Sentry::getGroupProvider()->create(array(
        'name'        => 'Admin',
        'permissions' => array('admin' => 1),
    ));

    Sentry::getGroupProvider()->create(array(
        'name'        => 'Writer',
        'permissions' => array('writer' => 1),
    ));



    // Assign user permissions
    $adminUser  = Sentry::getUserProvider()->findByLogin('[email protected]');
    $adminGroup = Sentry::getGroupProvider()->findByName('Admin');
    $adminUser->addGroup($adminGroup);


    $userUser  = Sentry::getUserProvider()->findByLogin('[email protected]');
    $userGroup = Sentry::getGroupProvider()->findByName('Writer');
    $userUser->addGroup($userGroup);

    $userUser  = Sentry::getUserProvider()->findByLogin('[email protected]');
    $userGroup = Sentry::getGroupProvider()->findByName('Writer');
    $userUser->addGroup($userGroup);

    $userUser  = Sentry::getUserProvider()->findByLogin('[email protected]');
    $userGroup = Sentry::getGroupProvider()->findByName('Writer');
    $userUser->addGroup($userGroup);
}

}

}

After adding the initial users I was using a form to add new users, So in my controller I had something like this. Again this is just for learning/testing the framework, original implementation is very different. But for testing purposes this should work.

添加初始用户后,我使用表单添加新用户,所以在我的控制器中我有这样的东西。同样,这仅用于学习/测试框架,原始实现非常不同。但出于测试目的,这应该有效。

Assuming you have a form that submits to a controller@function, you can have something like this,

假设你有一个提交给 controller@function 的表单,你可以有这样的东西,

$user = Sentry::getUserProvider()->create(array(
            'email'       => Input::get('email'),
            'password'    => Input::get('password'),
            'first_name'  => Input::get('first_name'),
            'last_name'   => Input::get('last_name'),
            'activated'   => 1,
        ));

        $writerGroup = Sentry::getGroupProvider()->findByName('writer');

        $user->addGroup($writerGroup);

Rest you can find in Sentry documentation: Sentry Docs

您可以在 Sentry 文档中找到休息: Sentry Docs

Feel free to edit this question to make it more informative or add new examples.

随意编辑此问题以使其提供更多信息或添加新示例。

回答by Antonio Carlos Ribeiro

Well, this is not exactly an article about it, but it covers most of what we use on auth and roles in Sentry2. So, basically you have to

好吧,这并不是一篇关于它的文章,但它涵盖了我们在 Sentry2 中用于身份验证和角色的大部分内容。所以,基本上你必须

Install composer by executing

通过执行安装composer

curl -sS https://getcomposer.org/installer | php

Put it on a executable folder, renaming it

把它放在一个可执行文件夹中,重命名它

sudo mv composer.phar /bin/composer

Set the executable bit

设置可执行位

sudo chmod +x /bin/composer

Install laravel by executing

通过执行安装laravel

composer create-project laravel/laravel

Install Sentry 2

安装哨兵 2

composer require cartalyst/sentry:2.0.*

Then you just have to use Sentry:

然后你只需要使用哨兵:

Create your user groups and permissions for each group:

为每个组创建您的用户组和权限:

Sentry::getGroupProvider()->create(array(
    'name'        => 'Super Administrators',
    'permissions' => array(
            'system' => 1,
    ),
));

Sentry::getGroupProvider()->create(array(
    'name'        => 'Managers',
    'permissions' => array(
        'system.articles' => 1,
    ),
));

Sentry::getGroupProvider()->create(array(
    'name'        => 'Publishers',
    'permissions' => array(
        'system.articles.add' => 1,
        'system.articles.edit' => 1,
        'system.articles.delete' => 1,
        'system.articles.publish' => 1,
    ),
));

Sentry::getGroupProvider()->create(array(
    'name'        => 'Authors',
    'permissions' => array(
        'system.articles.add' => 1,
        'system.articles.edit' => 1,
        'system.articles.delete' => 1,
    ),
));

Set a group to a particular user, in this case it is setting Managers to the current logged user

将组设置为特定用户,在这种情况下,它将管理器设置为当前登录的用户

Sentry::getUser()->addGroup( Sentry::getGroupProvider()->findByName('Author') );

Check if a user can publish and an added article

检查用户是否可以发布和添加的文章

if ( Sentry::getUser()->hasAnyAccess(['system','system.articles','system.articles.publish']) )
{
    // will be able to publish something
}

Check if a user is Super Administrator (only this group has the 'system' access)

检查用户是否是超级管理员(只有该组具有“系统”访问权限)

if ( Sentry::getUser()->hasAnyAccess(['system']) )
{
    // will be able to do a thing
}

Get all groups from a particular user

从特定用户获取所有组

try
{
    // Find the user using the user id
    $user = Sentry::getUserProvider()->findById(1);

    // Get the user groups
    $groups = $user->getGroups();
}
catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
{
    echo 'User was not found.';
}