-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Move EntryRepository from Contracts to Stache directory
- Remove custom permissions - Introduce customizable restriction check - Adjust EntryQueryBuilder and EntryPolicy to new restriction logic
- Loading branch information
Showing
7 changed files
with
101 additions
and
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Doefom\Restrict\Facades; | ||
|
||
use Illuminate\Support\Facades\Facade; | ||
|
||
/** | ||
* @mixin \Doefom\Restrict\Restrict | ||
*/ | ||
class Restrict extends Facade | ||
{ | ||
|
||
protected static function getFacadeAccessor(): string | ||
{ | ||
return 'restrict'; | ||
} | ||
|
||
} |
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,66 @@ | ||
<?php | ||
|
||
namespace Doefom\Restrict; | ||
|
||
use Illuminate\Support\Facades\Route; | ||
use Statamic\Contracts\Auth\User; | ||
use Statamic\Contracts\Entries\Entry; | ||
|
||
class Restrict | ||
{ | ||
protected $callback; | ||
|
||
public function __construct() | ||
{ | ||
// Default callback returns true which will ultimately not apply any | ||
// restrictions and therefore preserve the default behavior. | ||
$this->callback = fn (User $user, Entry $entry) => true; | ||
} | ||
|
||
/** | ||
* Set the restriction callback used to determine if the current user is authorized to view the entry. | ||
* The callback expects a User and Entry instance and should return a boolean. | ||
* | ||
* @param callable $callback | ||
* @return void | ||
*/ | ||
public function setRestriction(callable $callback): void | ||
{ | ||
$this->callback = $callback; | ||
} | ||
|
||
/** | ||
* Check if the current user is authorized to view the entry. | ||
* | ||
* @param User $user | ||
* @param Entry $entry | ||
* @return bool | ||
*/ | ||
public function isAuthorized(User $user, Entry $entry): bool | ||
{ | ||
return call_user_func($this->callback, $user, $entry); | ||
} | ||
|
||
/** | ||
* Determine if the current user and route require restriction checks. Restriction | ||
* checks are only required for authenticated CP routes and non-super users. | ||
* | ||
* @param User|null $user | ||
* @return bool | ||
*/ | ||
public function isRestricted(?User $user): bool | ||
{ | ||
return $this->isAuthenticatedCpRoute() && $user && ! $user->isSuper(); | ||
} | ||
|
||
/** | ||
* Check if the current route is a CP route that requires authentication by checking | ||
* if the 'statamic.cp.authenticated' middleware is applied to the route. | ||
* | ||
* @return bool | ||
*/ | ||
protected function isAuthenticatedCpRoute(): bool | ||
{ | ||
return in_array('statamic.cp.authenticated', Route::current()->gatherMiddleware()); | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
src/Contracts/Entries/EntryRepository.php → src/Stache/Repositories/EntryRepository.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