Laravel 5.1多种认证 [英] Laravel 5.1 multiple authentication

查看:146
本文介绍了Laravel 5.1多种认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么能在Laravel 5.1例如身份验证的多种类型用户求职者,招聘人员,管理等。

How can you authenticate multiple types of users in Laravel 5.1 e.g. Jobseeker, Recruiter, Admin etc.

你们当中有些人建议使用单个用户表只存储密码和电子邮件,创建配置文件表来存储用户特定信息(jobseeker_profile,recruiter_profile)和使用角色的不同类型的用户来区分(即有一个角色和ROLE_USER)表。

Some of you have suggested using a single users table to store only the password and email, creating profile tables to store user specific information (jobseeker_profile, recruiter_profile) and using roles to differentiate between the different types of users (i.e having a roles and role_user) table.

这是所有非常好,但这时如果什么不同类型的用户有不同的注册和登录表单。你如何自定义默认的AUTH控制器开箱以显示正确的看法?

This is all very well but then what if the different types of users have different registration and login forms. How do you customize the default auth controller out of the box to display the correct view?

所以,如果我有以下途径:

So if I have the following routes:

// Jobseeker Authentication routes...
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');

// Jobseeker Registration routes...
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');


// Recruiter Authentication routes...
Route::get('recruiter/auth/login', 'Auth\AuthController@getLogin');
Route::post('recruiter/auth/login', 'Auth\AuthController@postLogin');
Route::get('recruiter/auth/logout', 'Auth\AuthController@getLogout');

// Recruiter Registration routes...
Route::get('recruiter/auth/register', 'Auth\AuthController@getRegister');
Route::post('recruiter/auth/register', 'Auth\AuthController@postRegister');

这是缺省auth控制器开箱:

This is the default auth controller out of the box:

class AuthController extends Controller
{
    use AuthenticatesAndRegistersUsers;

    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }

    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }
}    

使用默认的开箱AUTH控制器的特点:

traits used by the default out of the box auth controller:

trait AuthenticatesUsers
{
   use RedirectsUsers;

   public function getLogin()
   {
        return view('auth.login');
    }

   public function postLogin(Request $request)
   {
        $this->validate($request, [
            'email' => 'required|email', 'password' => 'required',
        ]);

        $credentials = $this->getCredentials($request);

        if (Auth::attempt($credentials, $request->has('remember'))) {
            return redirect()->intended($this->redirectPath());
        }

        return redirect($this->loginPath())
            ->withInput($request->only('email', 'remember'))
            ->withErrors([
               'email' => $this->getFailedLoginMessage(),
            ]);
   }

    public function loginPath()
   {
        return property_exists($this, 'loginPath') ? $this->loginPath : '/auth/login';
    }

}

trait RegistersUsers
{
    use RedirectsUsers;

    public function getRegister()
    {
        return view('auth.register');
    }

    public function postRegister(Request $request)
    {
        $validator = $this->validator($request->all());

        if ($validator->fails()) {
            $this->throwValidationException(
                $request, $validator
            );
         }

         Auth::login($this->create($request->all()));

        return redirect($this->redirectPath());
    }
}

我敢肯定,这是许多Web应用程序的一个非常普遍的要求,但我找不到Laravel具体实现任何有用的教程。所有教程单纯强调开箱实施的一些奇怪的原因。

I'm sure this is a very common requirement for many web applications but I can't find any helpful tutorials for Laravel specific implementations. All the tutorial simply focus on the out of the box implementation for some odd reason.

以上任何帮助将是非常美联社preciated。

Any help on the above would be much appreciated.

推荐答案

这是不是你的问题直接,但要解决你的问题问题的替代方式的解决方案。

This is not a solution to your question directly, but alternative way to solve your question problem with.

在创建针对不同人群不同的用户名和密码代替,使该角色拥有一个中央认证。它呼吁用户和角色

In stead of creating different username and password for different groups, make a central authentication that has roles. It called user and roles.

您可以定义不同的角色组,每个角色都有各自的特定区域的访问。

You can define groups with different roles, and each roles has specific access to respective area.

关于注册过程中,你可以提出两点看法目前存在的,但使用相同的控制器,并为每个视图,您可以创建一个隐藏字段来表明它是为求职者组或招聘组。

Regarding registration process you can make two differnet views but using the same controller, and for each view you can create a hidden field to indicate if it is jobseekers group or recruiter group.

双方将收到两个不同的确认电子邮件,他们应填写的个人资料信息的其余部分,如招聘人员应该把公司名称和求职者应该把自己的名字等,他们可能对档案信息两个不同的表,但仍然使用相同的登录系统。

Both will receive two different confirmation emails where they should fill the rest of the profile information, like recruiter should put company name and jobseeker should put his name etc. they might have two different tables for profile information, but still using the same login system.

通过添加条件中间件和正确的路线,如果求职者试图访问即使求职者在系统登录招聘区,求职者将不能够访问的区域或相反的方向。

By adding condition to middleware and correct route, if jobseeker tries to access recruiter area even if jobseeker is logged in the system, the jobseeker won't be able to access that area or the opposite way.

由于Laravel 5.1有<一个href=\"http://stackoverflow.com/questions/32029689/laravel-5-user-can-not-be-retrieved-after-successful-authentication/32030103#32030103\">build在用户登录系统的,所以你有几个选择,构建自己的角色,或者使用第三方。

Since Laravel 5.1 has build in user login system, so you have few choices, build your own roles or use 3rd party.

我建议你建立你自己的,所以你可以控制你的code和可进一步开发它,你想随着时间的推移。这可能需要你半天得到它运行,并了解它是如何工作的,但它是值得花的时间与方式代替正确的方法,你在你的问题转到使用第三方是罚款也有很多你身边的包可以搜索。我曾亲自使用委托( https://github.com/Zizaco/entrust )很容易和漂亮方式提供角色和权限到项目中。

I suggest you to build your own so you have control over your code and can further develop it as you wish with time. It might take you half day to get it run and understand how it works, but it is worth spending that time with the right approach in stead of the way you go in your Question OR using 3rd party is fine too, there is a lot of packages around you can search for. I have personally used Entrust (https://github.com/Zizaco/entrust) it is easy and nice way to provide roles and permissions to your project.

下面也是由Jeffrey方式在Laracast开发的视频的链接,它建立用户和角色系统从无到有的Laravel 4,但既然你有用户部分,只要按照角色的一部分,并与小的修改,你将有一个角色系统您Laravel 5.1,我已经尝试过了,它的作品。

Here is also a link to video developed by Jeffrey Way at Laracast, it builds user and roles system from scratch for Laravel 4. but since you have user part, just follow roles part and with small modifications you will have a roles system to your Laravel 5.1, I have tried it and it works.

对于在评论你的问题,当你跟随视频你就会明白这个概念。

Regarding your question in the comments, when you follow the video you will understand the concept.

链接到视频: https://laracasts.com/lessons/users-and-roles

您可能需要创建帐户,看视频,大部分视频都是免费的。

You might need to create account to see the video, most of videos are free.

良好做法
它始终是也是一个很好的做法,说明要实现这使得事情变得更容易了什么,我刚才已经做出了表率为您的项目,但是这是唯一的例子学习:

Good practice It is always also a good practice to illustrate what you want to achieve that makes things easier, I have just made an example for your project, but that is only example for learning:

在这里输入的形象描述

我建议你读一些关于角色的主题,在这里你还可以找到一些灵感,以第三方的ACL系统Laravel,可能会有更多的文章,但这里是一些:

I encourage you to read some of the topics regarding roles, here you will also find some inspiration to 3rd party acl systems to Laravel, there might be more articles but here is some:

阅读:

<一href=\"https://laracasts.com/discuss/channels/laravel/which-package-is-best-for-roles-permissions/?page=2\" rel=\"nofollow\">https://laracasts.com/discuss/channels/laravel/which-package-is-best-for-roles-permissions/?page=2

https://laracasts.com/discuss/channels/general-discussion/laravel-5-user-groups-management

https://laracasts.com/discuss/channels/general-discussion/roles-and-permissions-in-laravel-5

修改

重要注意

Laravel 5.1推出了授权,我还没有发现太多在线文档尚未但它是值得花一些时间来学习它:

Important Note
Laravel 5.1 has introduced Authorization, I have not found much documentation online yet but it is worth to spend some time learning it:

http://laravel.com/docs/5.1/authorization#policies

新的更新

有你问的一些伟大的视频解决方案,请访问控制列表的部分在这里
https://laracasts.com/series/whats-new-in-laravel -5-1

这可能是非常有趣太:
https://laracasts.com/lessons/email-verification-in-laravel

This might be very interesting too: https://laracasts.com/lessons/email-verification-in-laravel

这会给你一个完整的自己开发的解决方案。

This will give you a complete own developed solution.

这篇关于Laravel 5.1多种认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆