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
Showing
142 changed files
with
4,185 additions
and
2,632 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Spira framework. | ||
* | ||
* @link https://github.com/spira/spira | ||
* | ||
* For the full copyright and license information, please view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Extensions\Controller; | ||
|
||
use Spira\Auth\Access\Gate; | ||
use Spira\Contract\Exception\ForbiddenException; | ||
|
||
trait AuthorizesRequestsTrait | ||
{ | ||
/** | ||
* Authorize a given action against a set of arguments. | ||
* | ||
* @param mixed $ability | ||
* @param mixed|array $arguments | ||
* @return void | ||
* | ||
* @throws ForbiddenException | ||
*/ | ||
public function authorize($ability, $arguments = []) | ||
{ | ||
list($ability, $arguments) = $this->parseAbilityAndArguments($ability, $arguments); | ||
|
||
if (! $this->getGate()->check($ability, $arguments)) { | ||
throw $this->createGateUnauthorizedException($ability, $arguments); | ||
} | ||
} | ||
|
||
/** | ||
* Authorize a given action for a user. | ||
* | ||
* @param \Illuminate\Contracts\Auth\Authenticatable|mixed $user | ||
* @param mixed $ability | ||
* @param mixed|array $arguments | ||
* @return void | ||
* | ||
* @throws ForbiddenException | ||
*/ | ||
public function authorizeForUser($user, $ability, $arguments = []) | ||
{ | ||
list($ability, $arguments) = $this->parseAbilityAndArguments($ability, $arguments); | ||
|
||
$result = $this->getGate()->forUser($user)->check($ability, $arguments); | ||
|
||
if (! $result) { | ||
throw $this->createGateUnauthorizedException($ability, $arguments); | ||
} | ||
} | ||
|
||
/** | ||
* Guesses the ability's name if it wasn't provided. | ||
* | ||
* @param mixed $ability | ||
* @param mixed|array $arguments | ||
* @return array | ||
*/ | ||
protected function parseAbilityAndArguments($ability, $arguments) | ||
{ | ||
if (is_string($ability)) { | ||
return [$ability, $arguments]; | ||
} | ||
|
||
return [debug_backtrace(false, 3)[2]['function'], $ability]; | ||
} | ||
|
||
/** | ||
* @return Gate | ||
*/ | ||
public function getGate() | ||
{ | ||
return app(Gate::GATE_NAME); | ||
} | ||
|
||
/** | ||
* Throw an unauthorized exception based on gate results. | ||
* | ||
* @param string $ability | ||
* @param array $arguments | ||
* @return ForbiddenException | ||
*/ | ||
protected function createGateUnauthorizedException($ability, $arguments) | ||
{ | ||
return new ForbiddenException(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.