2016-11-11 147 views
1

我有這些2表與多對多關係連接使用聯結表。我的想法是,我可以獲取用戶數據,使用戶成爲日記數據中的作者,並且迄今爲止工作正常。創建身份驗證laravel 5

用戶表:

public function up() 
    { 
     Schema::create('users', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('name'); 
      $table->string('email')->unique(); 
      $table->integer('phone')->nullable(); 
      $table->string('address')->nullable(); 
      $table->string('password'); 
      $table->rememberToken(); 
      $table->enum('level', ['admin', 'author']); 
      $table->timestamps(); 
     }); 
    } 

期刊表:

public function up() 
    { 
     Schema::create('journal', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('title', 255); 
      $table->text('abstract'); 
      $table->text('file'); 
      $table->integer('id_edition')->unsigned(); 
      $table->timestamps(); 
     }); 
    } 

結表:

public function up() 
    { 
     Schema::create('penulis', function (Blueprint $table) { 
      // Create tabel penulis 
      $table->integer('id_user')->unsigned()->index(); 
      $table->integer('id_journal')->unsigned()->index(); 
      $table->timestamps(); 
      // Set PK 
      $table->primary(['id_user', 'id_journal']); 
      // Set FK penulis --- user 
      $table->foreign('id_user') 
        ->references('id') 
        ->on('users') 
        ->onDelete('cascade') 
        ->onUpdate('cascade'); 
      // Set FK penulis --- journal 
      $table->foreign('id_journal') 
        ->references('id') 
        ->on('journal') 
        ->onDelete('cascade') 
        ->onUpdate('cascade'); 
     }); 
    } 

現在我有這樣的觀點,即顯示日誌數據與按鈕一起編輯或刪除它。我想要做的是隻有那些有權作爲日記的作者纔有權訪問這些按鈕的用戶。我如何製作它?下面是查看代碼:

<tbody> 
             <?php foreach ($journal_list as $journal): ?> 
              <tr> 
               <td style=""><a href="{{ url('journal/' . $journal->id) }}">{{ $journal->title }}</a></td> 

               @if (Auth::check()) 
               <td style="width: 130px; overflow: hidden;"> 
                <div class="box-button"> 
                 {{ link_to('journal/' . $journal->id . '/edit?edition=' . $edition->id, 'Edit', ['class' => 'btn btn-warning btn-sm']) }} 
                </div> 
                <div class="box-button"> 
                 {!! Form::open(['method' => 'DELETE', 'action' => ['[email protected]', $journal->id]]) !!} 
                 {!! Form::submit('Delete', ['class' => 'btn btn-danger btn-sm']) !!} 
                 {!! Form::close() !!} 
                </div> 
               </td> 
               @endif 

              </tr> 
             <?php endforeach ?> 
            </tbody> 

對不起,我的英語不好,如果我的問題是愚蠢的。謝謝!

回答

1

您需要使用middlewareGate立面的組合。

  1. Generate a policy
  2. Write a policy

像這樣:

public function edit-journal(User $user, Journal $journal) 
{ 
    return $user->id === $journal->user_id; 
} 

public function delete-journal(User $user, Journal $journal) 
{ 
    return $user->id === $journal->user_id; 
} 

3.您現在可以使用Gate外觀與刀片

像這樣:

@can('edit-journal', $journal) 
    <div class="box-button"> 
     {{ link_to('journal/' . $journal->id . '/edit?edition=' . $edition->id, 'Edit', ['class' => 'btn btn-warning btn-sm']) }} 
    </div> 
@endcan 

@can('delete-journal', $journal) 
    <div class="box-button"> 
     {!! Form::open(['method' => 'DELETE', 'action' => ['[email protected]', $journal->id]]) !!} 
     {!! Form::submit('Delete', ['class' => 'btn btn-danger btn-sm']) !!} 
     {!! Form::close() !!} 
    </div> 
@endcan 
  • 你將不得不register a middlewareeditdelete路線。你的路線應該是這樣的:

    //Routes 
    Route::get('journal/' . {$journal_id} . '/edit', ['as'=>'editJournal','middleware' => 'journal:edit', 'uses'=>'[email protected]'] 
    //You need to change your delete form so the action points to that route 
    Route::delete('journal/' . {$journal_id}, ['as'=>'deleteJournal','middleware' => 'journal:delete', 'uses'=>'[email protected]'] 
    
  • 在你的中間件,你應該有這樣的:

    //Journal Middleware 
        public function handle($request, Closure $next, $role) 
        { 
         $parameters = $request->route()->parameters(); 
         $journal = Journal::findOrFail($parameters['journal_id']); 
         if (Gate::allows($role.'-journal', $journal)) { 
          return $next($request); 
         }else{ 
          abort(403, "You do not have the permission to ".$role." this journal") 
         } 
        } 
    
    +1

    打我也是大聲笑 – rchatburn

    +0

    通過使用這個,我必須創建一個新的中間件?如果是的話,我試過這個:public function handle($ request,Closure $ next,Journal $ journal){Gate :: allow('editjournal',$ journal)){ return $ next($ request); } else { abort(403,「您沒有更新此日誌的權限」); } }給出了這個錯誤:參數3傳遞給App \ Http \ Middleware \ CheckAuthor :: handle()必須是App \ Journal的一個實例,沒有給出, –

    +0

    @AriandoMiller中間件是防止惡意用戶訪問資源(例如:刪除日誌),而沒有適當的訪問級別。您需要將'$ journal_id'從您的路由傳遞到中間件,而不是檢索相應的'Journal'。我編輯了答案 – Wistar

    0

    我在類似的情況下所做的是在模型中添加一個函數,您想要用所有的邏輯來檢查。因此,例如,你的情況是這樣的:

    /Model/Journal.php

    public function canBeModifiedByUser($user_id){ //Check all the things that you want }

    然後你可以做類似的觀點: if($journal->canBeModifiedByUser($journal->user->id))

    此外,我建議你檢查一些ACL包,它可能是一個矯枉過正的atm,但它可能只是你需要的東西。

    0

    我會建議使用門

    在您的身份驗證服務提供商,你可以做

    $gate->define('can-modifiy', function ($user) { 
         // whatever code you want to determine if the user can eg 
         return $user->hasRole('admin'); 
        }); 
    

    那麼你的觀點,你可以使用@can

    @can ('can-modify') 
        <button>delete</button> 
    @endcan 
    

    這也可以在你的控制器在https://laravel.com/docs/5.3/authorization#writing-gates

    使用

    $this->authorize('can-modify'); 
    

    Gate::allows('can-modify'); 
    

    這是在文檔