-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move enforcer instance into factory (#12)
- Loading branch information
Showing
9 changed files
with
176 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
/** | ||
* EnforcerFactory.php | ||
* | ||
* @license More in LICENSE.md | ||
* @copyright https://www.fastybird.com | ||
* @author Adam Kadlec <[email protected]> | ||
* @package FastyBird:SimpleAuth! | ||
* @subpackage Security | ||
* @since 0.1.0 | ||
* | ||
* @date 21.07.24 | ||
*/ | ||
|
||
namespace FastyBird\SimpleAuth\Security; | ||
|
||
use Casbin; | ||
use FastyBird\SimpleAuth\Exceptions; | ||
|
||
/** | ||
* Class security annotation checker | ||
* | ||
* @package FastyBird:SimpleAuth! | ||
* @subpackage Security | ||
* | ||
* @author Adam Kadlec <[email protected]> | ||
*/ | ||
class EnforcerFactory | ||
{ | ||
|
||
private Casbin\CachedEnforcer|null $enforcer = null; | ||
|
||
public function __construct( | ||
private readonly string $modelFile, | ||
private readonly Casbin\Persist\Adapter $adapter, | ||
) | ||
{ | ||
} | ||
|
||
/** | ||
* @throws Exceptions\InvalidState | ||
*/ | ||
public function getEnforcer(): Casbin\Enforcer | ||
{ | ||
if ($this->enforcer === null) { | ||
try { | ||
$this->enforcer = new Casbin\CachedEnforcer($this->modelFile, $this->adapter); | ||
} catch (Casbin\Exceptions\CasbinException $ex) { | ||
throw new Exceptions\InvalidState('Failed to create an enforcer', $ex->getCode(), $ex); | ||
} | ||
} | ||
|
||
return $this->enforcer; | ||
} | ||
|
||
} |
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
Oops, something went wrong.