我有这两个表与多到多的关系连接使用连接表。我的想法是,我可以获得用户数据,使用户在期刊数据中成为作者,到目前为止,它是有效的。
用户表:
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' => ['JournalController@destroy', $journal->id]]) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger btn-sm']) !!}
{!! Form::close() !!}
</div>
</td>
@endif
</tr>
<?php endforeach ?>
</tbody>对不起,我的英语不好,如果我的问题是愚蠢的。谢谢!
发布于 2016-11-11 15:37:47
如下所示:
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;
}如下所示:
@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' => ['JournalController@destroy', $journal->id]]) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger btn-sm']) !!}
{!! Form::close() !!}
</div>
@endcanedit和delete路线将不得不使用delete。你的路线应该是:
//路线::获取(“日志/”)。{$journal_id}‘/编辑’,‘as’=‘=>’杂志,‘中间件’,‘编辑’,‘使用’期刊控制器@编辑‘//你需要改变你的删除形式,所以动作指向路线::删除(’日志/‘)。{$journal_id},‘as’=‘删除日志’,‘中间件’=>‘日志:删除’,‘使用’delete=‘>’期刊控制器@破坏‘在您的中间件中,您应该有如下内容:
//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")
}
}发布于 2016-11-11 15:30:54
在类似的情况下,我所做的是在模型中添加一个函数,您想要检查所有的逻辑。因此,例如,在您的例子中,应该是这样的:
/Model/Journal.php
public function canBeModifiedByUser($user_id){ //Check all the things that you want }
然后在视图中可以执行如下操作:if($journal->canBeModifiedByUser($journal->user->id))
此外,我建议您检查一些ACL包,这可能是一个过分的您的atm,但它可能正是您所需要的。
发布于 2016-11-11 15:44:33
我建议你用门
在您的服务提供商中,您可以这样做。
$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这也可以在您的控制器中使用
$this->authorize('can-modify');或
Gate::allows('can-modify');这在https://laravel.com/docs/5.3/authorization#writing-gates的文档中
https://stackoverflow.com/questions/40550982
复制相似问题