Skip to content

Commit

Permalink
Refactor FormPolicy and TemplatePolicy to centralize write operation …
Browse files Browse the repository at this point in the history
…logic

- Introduced a private method `canPerformWriteOperation` in both FormPolicy and TemplatePolicy to encapsulate the logic for determining if a user can perform write operations on the respective models.
- Updated the `update`, `delete`, `restore`, and `forceDelete` methods in FormPolicy to use the new method for improved readability and maintainability.
- Simplified the `update` and `delete` methods in TemplatePolicy to leverage the centralized write operation logic.

This refactoring enhances code clarity and reduces duplication across policy classes.
  • Loading branch information
JhumanJ committed Dec 18, 2024
1 parent bd10e85 commit 036c10b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
16 changes: 12 additions & 4 deletions api/app/Policies/FormPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,22 @@ public function create(User $user)
return !$user->is_readonly;
}

/**
* Determine whether the user can perform write operations on the model.
*/
private function canPerformWriteOperation(User $user, Form $form): bool
{
return $user->ownsForm($form) && !$user->is_readonly;
}

/**
* Determine whether the user can update the model.
*
* @return mixed
*/
public function update(User $user, Form $form)
{
return $user->ownsForm($form) && !$user->is_readonly;
return $this->canPerformWriteOperation($user, $form);
}

/**
Expand All @@ -57,7 +65,7 @@ public function update(User $user, Form $form)
*/
public function delete(User $user, Form $form)
{
return $user->ownsForm($form) && !$user->is_readonly;
return $this->canPerformWriteOperation($user, $form);
}

/**
Expand All @@ -67,7 +75,7 @@ public function delete(User $user, Form $form)
*/
public function restore(User $user, Form $form)
{
return $user->ownsForm($form) && !$user->is_readonly;
return $this->canPerformWriteOperation($user, $form);
}

/**
Expand All @@ -77,6 +85,6 @@ public function restore(User $user, Form $form)
*/
public function forceDelete(User $user, Form $form)
{
return $user->ownsForm($form) && !$user->is_readonly;
return $this->canPerformWriteOperation($user, $form);
}
}
20 changes: 8 additions & 12 deletions api/app/Policies/TemplatePolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,27 @@ class TemplatePolicy

/**
* Determine whether the user can create models.
*
* @return \Illuminate\Auth\Access\Response|bool
*/
public function create(User $user)
{
return $user !== null && !$user->is_readonly;
}

/**
* Determine whether the user can update the model.
*
* @return mixed
* Determine whether the user can perform write operations on the model.
*/
public function update(User $user, Template $template)
private function canPerformWriteOperation(User $user, Template $template): bool
{
return ($user->admin || $user->template_editor || $template->creator_id === $user->id) && !$user->is_readonly;
}

/**
* Determine whether the user can delete the model.
*
* @return mixed
*/
public function update(User $user, Template $template)
{
return $this->canPerformWriteOperation($user, $template);
}

public function delete(User $user, Template $template)
{
return ($user->admin || $user->template_editor || $template->creator_id === $user->id) && !$user->is_readonly;
return $this->canPerformWriteOperation($user, $template);
}
}

0 comments on commit 036c10b

Please sign in to comment.