不同的用户类型有不同的个人资料页面,但我需要一个能够看到其他用户

时间:2018-05-01 10:36:27

标签: laravel laravel-5 eloquent blade

我的应用程序中有两种用户类型。房东和房客。目前,当房东是一个租户页面时,他们可以看到开始租户按钮,就是这样。如果发送了请求,并且我尝试显示该请求,则房东无法在租户页面上看到该消息,但租户可以在他们的页面中看到它/

如何让房东看到租客可以看到的消息。

这是数据库行(假数据):

This is the database row (fake data)

呈现配置文件的控制器代码是

public function index($id){
    //Authenticated different user types
    //Sends Landlord and Tenant to appropiate pages
    $user = User::where('id', $id)->first();
    //Allows landlords to see their relationship with tenants, and vice versa.
    $landlordTenancies = Tenancy::all()->where('landlord_id', Auth::id());
    $tenantTenancies = Tenancy::all()->where('tenant_id', Auth::id());

    //Allows the attirbutes from the table to be access by correct landlord and tenant
    $tenancy = Tenancy::where('tenant_id', Auth::id())->first();
    $Tenancy = Tenancy::where('landlord_id', Auth::id())->first();

    //Sends different use types to relevant view

    if($user->userType == "Landlord"){
        return view('/pages/account/landlord', compact('properties', 'user', 'Watchlists', 'property', 'landlordTenancies', 'Tenancy'));
    }

    else{
        return view('/pages/account/tenant', compact('properties', 'user', 'Watchlists', 'property', 'tenantTenancies', 'tenancy'));
    }
 }

租户视图页

<div class="container">
    <div class="row">
        <div class="col-md-12">
            @foreach($tenantTenancies as $tenancy)
            <span class="lead"><strong>Landlord Name: </strong>{{$tenancy->landlord_name}}</span><br>
            <span class="lead"><strong>Property Address: </strong>{{$tenancy->property_address}}</span><br>
            @endforeach

                {{-- This blocks a tenant adding themselves --}}
                {{-- Shows add button, if tenancy model is empty. Also when no relationship established. --}}

            @if(Auth::user()->id != $user->id)
                @if($tenancy == null || $tenancy->accepted == 0 && $tenancy->request_sent != 1)
                    <a href="/account/tenancy/{{$user->id}}/create" class="btn btn-primary">Start Tenancy</a>
                @elseif($tenancy->request_sent === 1)
                    <span class="text-muted">You have a tenancy request</span>
                @endif
            @endif

                {{-- Only shows following buttons, if the current signed in user, is the relevant user. --}}
                {{-- Shows accept/reject if the request has been sent, but not accepted yet. --}}

            @if(Auth::user()->id == $user->id)
                @if($tenancy != null && $tenancy->accepted == 0 && $tenancy->request_sent == 1)
                    <form method="POST" action="/account/tenancy/{{$user->id}}/accept">
                        {{ csrf_field() }}
                        <input type="submit" class="btn btn-primary" value="Accept Request">
                    </form>

                    <form method="POST" action="/account/tenancy/{{$user->id}}/reject">
                        {{ csrf_field() }}
                        <input type="submit" class="btn btn-warning" value="Reject Request">
                    </form>

                    {{-- Allows the tenancy to be ended. --}}

                @elseif($tenancy != null && $tenancy->accepted == 1 && $tenancy->request_sent == 0)
                    <form method="POST" action="/account/tenancy/{{$user->id}}/end">
                        {{ csrf_field() }}
                        <input type="submit" class="btn btn-primary" value="End Tenancy">
                    </form>
                @endif
            @endif
        </div>
    </div>

业主查看页面

<div class="container">


    <div class="row text-center d-flex flex-wrap">
        <div class="col-lg-12">
        @foreach($landlordTenancies as $tenancy)
            <span class="lead"><strong>Tenant Name: </strong>{{$tenancy->tenant_name}}</span><br>
            <span class="lead"><strong>Property Address: </strong>{{$tenancy->property_address}}</span><br>
        @endforeach
            <h3>Your Active Adverts</h3>
            <div class="row py-2">

            @foreach ($properties as $property)
                <div class="col-md-4 mb-4">
                    <a href="/property/{{$property->id}}">
                        <img class="list-image img-fluid" src="{{$property->photo}}">
                    </a>
                    <p class="mt-2">{{$property->address .', '. $property->town .', '. $property->county}}</p>
                </div>
            @endforeach
        </div> <!-- ./col -->
    </div> <!-- ./row -->
</div>  <!-- ./container -->

目前,当房东在租户页面上时,他们可以看到这一点。即使发送了请求,他们也看不到消息,请求已发送。

LandlordOnTenant:

LandlordOnTenant

当租户在租户上时,他们会看到发送的消息请求以及接受/拒绝按钮。

tenantOnTenantPage:

tenantOnTenantPage

我需要房东能够在租户页面上看到请求发送的消息,并且租户不能看到它。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

查看Laravel的身份验证文档,尤其是政策https://laravel.com/docs/5.6/authorization#creating-policies

我建议的第一步是为属性模型创建一些策略,也许像(可以)管理房东类型用户和(可以)租给租户的东西,只要语言是真的就没关系有点自然,因为你的代码看起来像

TelemetryClient

在你的控制器中有类似的对应物。您也可以在路由级别使用此授权

@can('manage', $property)
some html for landlords only
@endcan

对于非房东,这将返回禁止的错误。这使得保护某些端点变得非常容易,例如,我的所有管理操作都是对某些路由的POST,因此,只允许管理员访问该路由,然后控制器可以假设只有管理员才能访问它。 然后剩下的就是让你在这些检查中将你只需要显示的东西包含在某些用户中。 @can是一种速记,可以用@if()形式编写。

有很多东西要描述,只需使用文档并弄清楚。在你考虑做某事之前,谷歌如何在laravel中做到这一点,它可能会有你需要的方法或可下载的软件包,没有必要重新发明轮子。