-
Notifications
You must be signed in to change notification settings - Fork 48
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
9 changed files
with
199 additions
and
50 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,29 @@ | ||
<?php | ||
|
||
namespace tauthz\cache; | ||
|
||
use tauthz\model\Rule; | ||
use tauthz\traits\Configurable; | ||
use think\db\Query; | ||
|
||
class CacheHandler implements CacheHandlerContract | ||
{ | ||
use Configurable; | ||
|
||
/** | ||
* Cache policies for the given model. | ||
* | ||
* @param Rule $model The model to cache policies for. | ||
* @return Query The cached query or a new query if caching is disabled. | ||
*/ | ||
public function cachePolicies(Rule $model): Query | ||
{ | ||
if ($this->config('cache.enabled', false)) { | ||
$key = $this->config('cache.key', 'tauthz'); | ||
$expire = $this->config('cache.expire', 0); | ||
return $model->cache($key, $expire); | ||
} else { | ||
return $model->newQuery(); | ||
} | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
namespace tauthz\cache; | ||
|
||
use tauthz\model\Rule; | ||
use think\db\Query; | ||
|
||
interface CacheHandlerContract | ||
{ | ||
public function cachePolicies(Rule $model): Query; | ||
} |
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,20 @@ | ||
<?php | ||
|
||
namespace tauthz\traits; | ||
|
||
trait Configurable | ||
{ | ||
/** | ||
* Gets config value by key. | ||
* | ||
* @param string $key | ||
* @param string $default | ||
* | ||
* @return mixed | ||
*/ | ||
protected function config(string $key = null, $default = null) | ||
{ | ||
$driver = config('tauthz.default'); | ||
return config('tauthz.enforcers.' . $driver . '.' . $key, $default); | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
namespace tauthz\tests; | ||
|
||
class CommandTest extends TestCase | ||
{ | ||
public function testPublish() | ||
{ | ||
$this->refreshApplication(); | ||
// delete published files | ||
$this->deletePublishedFiles(); | ||
// run command | ||
$this->app->console->call('tauthz:publish'); | ||
|
||
$this->assertFileExists($this->app->getRootPath() . '/database/migrations/20181113071924_create_rules_table.php'); | ||
$this->assertFileExists(config_path() . 'tauthz-rbac-model.conf'); | ||
$this->assertFileExists(config_path() . 'tauthz.php'); | ||
} | ||
|
||
protected function deletePublishedFiles() | ||
{ | ||
$destination = $this->app->getRootPath() . '/database/migrations'; | ||
if (file_exists($destination . '/20181113071924_create_rules_table.php')) { | ||
unlink($destination . '/20181113071924_create_rules_table.php'); | ||
rmdir($destination); | ||
} | ||
if (file_exists(config_path() . 'tauthz-rbac-model.conf')) { | ||
unlink(config_path() . 'tauthz-rbac-model.conf'); | ||
} | ||
if (file_exists(config_path() . 'tauthz.php')) { | ||
unlink(config_path() . 'tauthz.php'); | ||
} | ||
} | ||
} |
Oops, something went wrong.