简体   繁体   English

根据用户角色和当前用户显示图标

[英]Showing Icons Based on User Role and Current User

I have a page that lists all the users from my user database table. 我有一个页面,列出了我的用户数据库表中的所有用户。 I am currently looping through my users array and outputting each user's object property in the corresponding table field. 我目前正在遍历users数组,并在对应的表字段中输出每个用户的object属性。 In the last field it displays the actions for the logged in user to have access to. 在最后一个字段中,它显示登录用户有权访问的操作。 I need some help modifying this a bit for the additional needs I have. 我需要一些帮助,可以针对我的其他需求进行一些修改。

I currently have it to where if the current logged in user and then user in the array being displayed are the same then it won't display the icons that way they can't accidentally delete themselves. 我当前拥有的位置是,如果当前登录的用户与正在显示的数组中的用户相同,那么它将不会显示图标,以使他们不会意外删除自己。 The edit icon I don't want displayed because they can just go to their profile and edit themselves that way anyway. 我不希望显示编辑图标,因为他们只能进入自己的个人资料并以这种方式进行自我编辑。 This I would like to keep. 我想保持这一点。

What I'm wanting to do is take this field and if the current logged in user and the user in the array being displayed have the same role id then it CAN NOT display the action icons. 我要执行的操作是使用此字段,并且如果当前登录的用户和正在显示的数组中的用户具有相同的角色ID,则它将无法显示操作图标。 To explain this if the current logged in user is an admin and the user in the array to be displayed is also an admin they will not be able to delete that user. 为了解释这一点,如果当前登录的用户是管理员,并且要显示的阵列中的用户也是管理员,则他们将无法删除该用户。

ALSO if the current logged in user has a role id lower than than the user in the array being displayed then it ALSO does not display the actions. 另外,如果当前登录的用户的角色ID低于正在显示的阵列中的用户,则它也不会显示操作。 To explain this I will use the following example. 为了解释这一点,我将使用以下示例。 If the current logged in user only has a role id of 4 (admin) they will not be able to delete user's that have the role id of 5 (owner). 如果当前登录用户的角色ID为4(管理员),则他们将无法删除角色ID为5(所有者)的用户。

<tbody>
    @foreach($users as $user)
        <tr>
            <td class="center">{{ $user->id }}</td>
            <td>{{ $user->getFullName() }}</td>
            <td>{{ $user->email_address }}</td>
            <td>{{ $user->username }}</td>
            <td>{{ $user->role->role_name }}</td>
            <td>{{ $user->status->status_name }}</td>
            <td class="center">
                @if ( $user->id != Auth::id())
                    <a data-original-title="Edit" href="{{ route('backstage.users.edit', $user->id) }}" data-toggle="tooltip" title="" class="tooltips"><i class="fa fa-pencil"></i></a>
                    <a data-original-title="Delete" href="{{ route('backstage.users.destroy', $user->id) }}" data-toggle="tooltip" title="" class="tooltips js-ajax-delete"><i class="fa fa-trash-o"></i></a>
                @endif
            </td>
        </tr>
    @endforeach
</tbody>

Here is the preformatted array of user objects. 这是用户对象的预格式化数组。 I have only included to dummy users. 我只包括虚拟用户。

array(51) {
    [0]=>
        array(13) {
            ["id"]=> string(1) "1"
            ["first_name"]=> string(7) "Will"
            ["last_name"]=> string(8) "Stevens"
            ["username"]=> string(10) "wstevens"
            ["email_address"]=> string(20) "wstevens@gmail.com"
            ["avatar"]=> string(10) "wstevens" 
            ["role_id"]=> string(1) "4" 
            ["status_id"]=> string(1) "1"
            ["created_at"]=> string(19) "2014-11-26 22:27:38"
            ["updated_at"]=> string(19) "2014-11-26 22:27:38"
            ["deleted_at"]=> NULL
            ["role"]=>
                array(5) {
                    ["id"]=> string(1) "4"
                    ["role_name"]=> string(5) "Owner"
                    ["created_at"]=> string(19) "2014-11-26 22:27:38"
                    ["updated_at"]=> string(19) "2014-11-26 22:27:38"
                    ["deleted_at"]=> NULL
                }
            ["status"]=>
                array(5) {
                    ["id"]=> string(1) "1" 
                    ["status_name"]=> string(6) "Active"
                    ["created_at"]=> string(19) "2014-11-26 22:27:38"
                    ["updated_at"]=> string(19) "2014-11-26 22:27:38"
                    ["deleted_at"]=> NULL
                }
        }
    [1]=>
        array(13) {
            ["id"]=> string(1) "2"
            ["first_name"]=> string(6) "Furman"
            ["last_name"]=> string(8) "O'Reilly"
            ["username"]=> string(12) "wyman.haylie"
            ["email_address"]=> string(19) "pearlie17@yahoo.com"
            ["avatar"]=> string(8) "ikyyyhzn"
            ["role_id"]=> string(1) "2"
            ["status_id"]=> string(1) "2"
            ["created_at"]=> string(19) "2014-11-26 22:27:38"
            ["updated_at"]=> string(19) "2014-11-26 22:27:38"
            ["deleted_at"]=> NULL
            ["role"]=>
                array(5) {
                    ["id"]=> string(1) "2"
                    ["role_name"]=> string(6) "Editor"
                    ["created_at"]=> string(19) "2014-11-26 22:27:38"
                    ["updated_at"]=> string(19) "2014-11-26 22:27:38"
                    ["deleted_at"]=> NULL
                }
            ["status"]=>
                array(5) {
                    ["id"]=> string(1) "2"
                    ["status_name"]=> string(8) "Inactive"
                    ["created_at"]=> string(19) "2014-11-26 22:27:38"
                    ["updated_at"]=> string(19) "2014-11-26 22:27:38"
                    ["deleted_at"]=> NULL
                }
        }
}

You need to change the condition of your @if statement to: 您需要将@if语句的条件更改为:

@if($user->role['id'] < Auth::user()->role['id']) {
     <a data-original-title="Edit" href="{{ route('backstage.users.edit', $user->id) }}" data-toggle="tooltip" title="" class="tooltips"><i class="fa fa-pencil"></i></a>
     <a data-original-title="Delete" href="{{ route('backstage.users.destroy', $user->id) }}" data-toggle="tooltip" title="" class="tooltips js-ajax-delete"><i class="fa fa-trash-o"></i></a>
@endif

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

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