Create common function for userIsFormOwner & rewrite protected form (#244)

* Create common function for userIsFormOwner & rewrite protected form

* fix testcase
This commit is contained in:
formsdev
2023-11-28 15:53:04 +05:30
committed by GitHub
parent 64e79f34f2
commit d65c1be9b5
6 changed files with 39 additions and 34 deletions

View File

@@ -2,7 +2,7 @@
namespace App\Http\Resources;
use App\Http\Middleware\Form\PasswordProtectedForm;
use App\Http\Middleware\Form\ProtectedForm;
use App\Http\Resources\UserResource;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Auth;
@@ -20,8 +20,8 @@ class FormResource extends JsonResource
*/
public function toArray($request)
{
if(!$this->userIsFormOwner() && $this->doesMissPassword($request)){
return $this->getPasswordProtectedForm();
if(!$this->userIsFormOwner() && ProtectedForm::isProtected($request, $this->resource)){
return $this->getProtectedForm();
}
$ownerData = $this->userIsFormOwner() ? [
@@ -96,14 +96,7 @@ class FormResource extends JsonResource
return $this;
}
private function doesMissPassword(Request $request)
{
if (!$this->has_password) return false;
return !PasswordProtectedForm::hasCorrectPassword($request, $this->resource);
}
private function getPasswordProtectedForm()
private function getProtectedForm()
{
return [
'id' => $this->id,
@@ -131,8 +124,7 @@ class FormResource extends JsonResource
private function userIsFormOwner() {
return $this->extra?->userIsOwner ??
(
Auth::check()
&& Auth::user()->workspaces()->find($this->workspace_id) !== null
Auth::check() && Auth::user()->ownsForm($this->resource)
);
}