feat: api admin user and game policies
This commit is contained in:
@@ -12,9 +12,6 @@ class GamePolicy
|
||||
*/
|
||||
public function viewAny(User $user): bool
|
||||
{
|
||||
if ($user->blocked === 1) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -23,12 +20,9 @@ class GamePolicy
|
||||
*/
|
||||
public function view(User $user, Game $game): bool
|
||||
{
|
||||
if ($user->type === "A") {
|
||||
if ($user->type === 'A') {
|
||||
return true;
|
||||
}
|
||||
if ($user->blocked === 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (
|
||||
$game->player1_user_id === $user->id ||
|
||||
@@ -45,7 +39,7 @@ class GamePolicy
|
||||
*/
|
||||
public function create(User $user): bool
|
||||
{
|
||||
if ($user->type === "A" || $user->blocked === 1) {
|
||||
if ($user->type === 'A') {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -54,12 +48,12 @@ class GamePolicy
|
||||
|
||||
public function join(User $user, Game $game): bool
|
||||
{
|
||||
if ($user->type === "A" || $user->blocked === 1) {
|
||||
if ($user->type === 'A') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (
|
||||
$game->status !== "waiting" ||
|
||||
$game->status !== 'waiting' ||
|
||||
$game->player1_user_id === $user->id
|
||||
) {
|
||||
return false;
|
||||
@@ -70,12 +64,12 @@ class GamePolicy
|
||||
|
||||
public function resign(User $user, Game $game): bool
|
||||
{
|
||||
if ($user->type === "A" || $user->blocked === 1) {
|
||||
if ($user->type === 'A') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (
|
||||
$game->status !== "ongoing" ||
|
||||
$game->status !== 'ongoing' ||
|
||||
($game->player1_user_id !== $user->id &&
|
||||
$game->player2_user_id !== $user->id)
|
||||
) {
|
||||
@@ -90,12 +84,12 @@ class GamePolicy
|
||||
*/
|
||||
public function update(User $user, Game $game): bool
|
||||
{
|
||||
if ($user->type === "A" || $user->blocked === 1) {
|
||||
if ($user->type === 'A') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (
|
||||
$game->status !== "ongoing" ||
|
||||
$game->status !== 'ongoing' ||
|
||||
($game->player1_user_id !== $user->id &&
|
||||
$game->player2_user_id !== $user->id)
|
||||
) {
|
||||
@@ -110,7 +104,8 @@ class GamePolicy
|
||||
*/
|
||||
public function delete(User $user, Game $game): bool
|
||||
{
|
||||
return false;
|
||||
// Only admins can delete games
|
||||
return $user->type === 'A';
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Access\Response;
|
||||
|
||||
class UserPolicy
|
||||
{
|
||||
@@ -12,7 +11,7 @@ class UserPolicy
|
||||
*/
|
||||
public function viewAny(User $user): bool
|
||||
{
|
||||
return false;
|
||||
return $user->type === 'A';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -20,7 +19,7 @@ class UserPolicy
|
||||
*/
|
||||
public function view(User $user, User $model): bool
|
||||
{
|
||||
return false;
|
||||
return $user->id === $model->id || $user->type === 'A';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -28,7 +27,7 @@ class UserPolicy
|
||||
*/
|
||||
public function create(User $user): bool
|
||||
{
|
||||
return false;
|
||||
return $user->type === 'A';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,7 +35,7 @@ class UserPolicy
|
||||
*/
|
||||
public function update(User $user, User $model): bool
|
||||
{
|
||||
return false;
|
||||
return $user->id === $model->id;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -44,7 +43,17 @@ class UserPolicy
|
||||
*/
|
||||
public function delete(User $user, User $model): bool
|
||||
{
|
||||
return false;
|
||||
return ($user->type === 'A' && $user->id !== $model->id) || ($user->type === 'P' && $user->id === $model->id);
|
||||
}
|
||||
|
||||
public function block(User $user, User $model): bool
|
||||
{
|
||||
return $user->type === 'A' && $user->id !== $model->id && $model->type === 'P';
|
||||
}
|
||||
|
||||
public function unblock(User $user, User $model): bool
|
||||
{
|
||||
return $user->type === 'A' && $user->id !== $model->id && $model->type === 'P';
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user