简体   繁体   English

检查页面中的用户角色

[英]Checking user role in pages

I have parent User class whose instance is returned when the user is authenticated. 我有父User类,当用户通过身份验证时返回其实例。 That User instance has role_id which indicates the user's role. User实例具有role_id ,表示用户的角色。 There are three roles in the system; 系统中有三个角色; standarduser, trainer and gym (each has different table in database) and each has different functions and privileges. 标准用户,培训师和健身房(每个都有不同的数据库表),每个都有不同的功能和特权。

There are some possibilities I can do; 我可以做一些事情;

1) I can write role checks in User 's model class; 1)我可以在User的模型类中编写角色检查;

public function getName()
{
    if($user->role == 0)
    {
        $user->standarduser()->name;
    }
    else if($user->role == 1)
    {
        $user->gym()->name;
    }
}

public function getGymUsers()
{
    if($user->role == 2)
    {
        $user->gym()->users;
    }
    else
    {
        return null;
    }
}

2) I can write functions to each role class but in view (or controller), I have to do that control; 2)我可以为每个角色类编写函数,但在视图(或控制器)中,我必须执行该控制;

if($user->isStandarduser())
{
    echo $user->standarduser()->name;
}
. . .

if($user->isGym())
{
    echo $user->gym()->users;
}
else
{
    echo "";
}

3) Making specific views for each model so there will be no need to check roles. 3)为每个模型制作特定视图,因此不需要检查角色。 For example, if I'm in gym's page, I don't check if the object is a gym object because I know it's the gym's page. 例如,如果我在健身房的页面,我不会检查对象是否是健身房对象,因为我知道这是健身房的页面。

I can't decide which way to go. 我无法决定走哪条路。 If there are some better designs I can also try them. 如果有更好的设计,我也可以试试。

If the gym and the user's view are extremely similar and you are using the blade engine provided by laravel then you can use blade's template system 如果健身房和用户的视图非常相似,并且您正在使用laravel提供的刀片引擎,那么您可以使用刀片的模板系统

http://laravel.com/docs/5.1/blade http://laravel.com/docs/5.1/blade

/resources/layouts/default.blade.php /resources/layouts/default.blade.php

<body>
    <h1>@yield('name')</h1>
    <div>@yield('content')</div>
    <p>stuff that is the same for all users</p>
</body>

/resources/gym.blade.php /resources/gym.blade.php

@extends('layouts.default')

@section('name',$user->gym()->name)

@section('content')
<p>This is content unique to the gym</p>
@endsection

/resources/standarduser.blade.php /resources/standarduser.blade.php

@extends('layouts.default')

@section('name',$user->standarduser()->name)

@section('content')
<p>This is content unique to a standard user</p>
@endsection

Controller 调节器

public function show($id){
    $user = User::where($id)->first();
    if($user->isGym())
        return view('gym', ['user' => $user]);
    else if($user->isStandardUser())
        return view('standarduser', ['user' => $user]);
    else
        abort(500,'Not implemented yet');
}

Because each view is extending the default layout you will be able to have shared components while providing unique components for each type of user 因为每个视图都扩展了默认布局,所以您可以拥有共享组件,同时为每种类型的用户提供独特的组件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM