-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use policy authorization for client-side server endpoints
- Loading branch information
1 parent
948c659
commit 375e338
Showing
30 changed files
with
317 additions
and
180 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
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
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
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
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
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
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
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
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
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
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
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
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
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,109 @@ | ||
<?php | ||
|
||
namespace Convoy\Http\Requests; | ||
|
||
use Illuminate\Auth\Access\AuthorizationException; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
use Webmozart\Assert\Assert; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
abstract class BaseApiRequest extends FormRequest | ||
{ | ||
/** | ||
* Tracks if the request has been validated internally or not to avoid | ||
* making duplicate validation calls. | ||
*/ | ||
private bool $hasValidated = false; | ||
|
||
public function authorize(): bool | ||
{ | ||
return $this->user()->root_admin; | ||
} | ||
|
||
/** | ||
* Validate that the resource exists and can be accessed prior to booting | ||
* the validator and attempting to use the data. | ||
* | ||
* @throws AuthorizationException | ||
*/ | ||
protected function prepareForValidation(): void | ||
{ | ||
if (!$this->passesAuthorization()) { | ||
$this->failedAuthorization(); | ||
} | ||
|
||
$this->hasValidated = true; | ||
} | ||
|
||
/* | ||
* Determine if the request passes the authorization check as well | ||
* as the exists check. | ||
* | ||
* @return bool | ||
* | ||
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException | ||
*/ | ||
protected function passesAuthorization(): bool | ||
{ | ||
// If we have already validated we do not need to call this function | ||
// again. This is needed to work around Laravel's normal auth validation | ||
// that occurs after validating the request params since we are doing auth | ||
// validation in the prepareForValidation() function. | ||
if ($this->hasValidated) { | ||
return true; | ||
} | ||
|
||
if (!parent::passesAuthorization()) { | ||
return false; | ||
} | ||
|
||
// Only let the user know that a resource does not exist if they are | ||
// authenticated to access the endpoint. This avoids exposing that | ||
// an item exists (or does not exist) to the user until they can prove | ||
// that they have permission to know about it. | ||
if ($this->attributes->get('is_missing_model', false)) { | ||
throw new NotFoundHttpException(trans('exceptions.api.resource_not_found')); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function requiredToOptional(array $rules): array | ||
{ | ||
foreach ($rules as &$rule) { | ||
if (is_string($rule)) { | ||
$rule = str_replace('required', 'sometimes', $rule); | ||
} | ||
|
||
if (is_array($rule)) { | ||
$rule = $this->requiredToOptional($rule); | ||
} | ||
} | ||
|
||
return $rules; | ||
} | ||
|
||
/** | ||
* Returns the named route parameter and asserts that it is a real model that | ||
* exists in the database. | ||
* | ||
* @template T of Model | ||
* | ||
* @param class-string<T> $expect | ||
* @return T | ||
* | ||
* @noinspection PhpDocSignatureInspection | ||
*/ | ||
public function parameter(string $key, string $expect) | ||
{ | ||
$value = $this->route()->parameter($key); | ||
|
||
Assert::isInstanceOf($value, $expect); | ||
Assert::isInstanceOf($value, Model::class); | ||
Assert::true($value->exists); | ||
|
||
/* @var T $value */ | ||
return $value; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
app/Http/Requests/Client/Servers/Backups/DeleteBackupRequest.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,18 @@ | ||
<?php | ||
|
||
namespace Convoy\Http\Requests\Client\Servers\Backups; | ||
|
||
use Convoy\Http\Requests\BaseApiRequest; | ||
use Convoy\Models\Backup; | ||
use Convoy\Models\Server; | ||
|
||
class DeleteBackupRequest extends BaseApiRequest | ||
{ | ||
public function authorize(): bool | ||
{ | ||
$server = $this->parameter('server', Server::class); | ||
$backup = $this->parameter('backup', Backup::class); | ||
|
||
return $this->user()->can('delete', [$backup, $server]); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
app/Http/Requests/Client/Servers/Backups/RestoreBackupRequest.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,18 @@ | ||
<?php | ||
|
||
namespace Convoy\Http\Requests\Client\Servers\Backups; | ||
|
||
use Convoy\Http\Requests\BaseApiRequest; | ||
use Convoy\Models\Backup; | ||
use Convoy\Models\Server; | ||
|
||
class RestoreBackupRequest extends BaseApiRequest | ||
{ | ||
public function authorize(): bool | ||
{ | ||
$server = $this->parameter('server', Server::class); | ||
$backup = $this->parameter('backup', Backup::class); | ||
|
||
return $this->user()->can('restore', [$backup, $server]); | ||
} | ||
} |
Oops, something went wrong.