This repository has been archived by the owner on Apr 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
982eaec
commit 6fd3ea8
Showing
3 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace App\Policies; | ||
|
||
use App\User; | ||
use App\Animal; | ||
use Illuminate\Auth\Access\HandlesAuthorization; | ||
|
||
class AnimalPolicy | ||
{ | ||
use HandlesAuthorization; | ||
|
||
public function before($user, $ability) | ||
{ | ||
if ($user->isSuperAdmin()) { | ||
return true; | ||
} | ||
} | ||
/** | ||
* Determine whether the user can view any animals. | ||
* | ||
* @param \App\User $user | ||
* @return mixed | ||
*/ | ||
public function viewAny(User $user) | ||
{ | ||
//未登入也可以看所有動物資料,不需要用到這個方法 | ||
} | ||
|
||
/** | ||
* Determine whether the user can view the animal. | ||
* | ||
* @param \App\User $user | ||
* @param \App\Animal $animal | ||
* @return mixed | ||
*/ | ||
public function view(User $user, Animal $animal) | ||
{ | ||
//未登入也可以看動物資料,不需要用到這個方法 | ||
} | ||
|
||
/** | ||
* Determine whether the user can create animals. | ||
* | ||
* @param \App\User $user | ||
* @return mixed | ||
*/ | ||
public function create(User $user) | ||
{ | ||
// 登入後認證授權確認皆可以創建送養動物資料,所以不需要製作這方法 | ||
// 是否有權限操作,請參考前幾天 Victor 的鐵人賽文章。 | ||
} | ||
|
||
/** | ||
* Determine whether the user can update the animal. | ||
* | ||
* @param \App\User $user | ||
* @param \App\Animal $animal | ||
* @return mixed | ||
*/ | ||
public function update(User $user, Animal $animal) | ||
{ | ||
// 修改動物資料必須檢查,動物是否是由該會員新建的,利用animal 的user_id 判斷 | ||
if ($user->id === $animal->user_id) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Determine whether the user can delete the animal. | ||
* | ||
* @param \App\User $user | ||
* @param \App\Animal $animal | ||
* @return mixed | ||
*/ | ||
public function delete(User $user, Animal $animal) | ||
{ | ||
// 刪除動物資料必須檢查,動物是否是由該會員新建的,利用animal 的user_id 判斷 | ||
if ($user->id === $animal->user_id) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Determine whether the user can restore the animal. | ||
* | ||
* @param \App\User $user | ||
* @param \App\Animal $animal | ||
* @return mixed | ||
*/ | ||
public function restore(User $user, Animal $animal) | ||
{ | ||
// 軟體刪除後「復原用」類似丟到資料丟到垃圾桶後,要再把資料救回來時判斷。 | ||
// 因為沒有實作軟刪除的部分,這部分直接空著 | ||
} | ||
|
||
/** | ||
* Determine whether the user can permanently delete the animal. | ||
* | ||
* @param \App\User $user | ||
* @param \App\Animal $animal | ||
* @return mixed | ||
*/ | ||
public function forceDelete(User $user, Animal $animal) | ||
{ | ||
// 軟體刪除後,強制刪除資料表的動物資料。類似資料丟到垃圾桶後,要永久刪除資料的時判斷的邏輯。 | ||
// 因為沒有實作軟刪除的部分,這部分直接空著 | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
database/migrations/2019_09_04_234303_add_user_id_to_animals.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class AddUserIdToAnimals extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::table('animals', function (Blueprint $table) { | ||
// 增加 user_id 欄位 (這裡要注意 先把這個欄位設定為 users 資料表中的任一個已存在的會員id ) | ||
$table->bigInteger('user_id')->unsigned()->default(1)->comment('使用者ID'); | ||
|
||
// 新增外鍵約束 如果user id:1 刪除 animal user_id = 1 也會全部刪除 | ||
$table->foreign('user_id') | ||
->references('id')->on('users') | ||
->onDelete('cascade'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
|
||
Schema::table('animals', function (Blueprint $table) { | ||
// 刪除外鍵約束 (這個表名_外鍵名_foreign) | ||
$table->dropForeign('animals_user_id_foreign'); | ||
|
||
//刪除user_id 欄位 | ||
$table->dropColumn('user_id'); | ||
}); | ||
|
||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
database/migrations/2019_09_05_071448_add_permission_to_users.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class AddPermissionToUsers extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::table('users', function (Blueprint $table) { | ||
//使用 permission 來記錄是不是管理員或其他類別 | ||
$table->string('permission')->comment('帳號權限'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::table('users', function (Blueprint $table) { | ||
//刪除 permission 欄位 | ||
$table->dropColumn('permission'); | ||
}); | ||
} | ||
} |