From 31b2cd10ebfad26fa57fa6f39f5e24161440ee4c Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Tue, 15 Aug 2023 20:12:14 +0330 Subject: [PATCH 01/12] Update .styleci.yml --- .styleci.yml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.styleci.yml b/.styleci.yml index 63f8503..33b4d91 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -6,14 +6,7 @@ use-tabs: false finder: exclude: - "docs" - - "modules" - - "node_modules" - - "nova" - - "nova-components" - - "storage" - - "spark" - "vendor" name: "*.php" not-name: - - "*.blade.php" - "_ide_helper.php" From 258b19056e69a1fe876c72ede64f9b39d0328395 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Tue, 15 Aug 2023 20:46:31 +0330 Subject: [PATCH 02/12] role contract created for models; --- src/Models/Contracts/Role.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/Models/Contracts/Role.php diff --git a/src/Models/Contracts/Role.php b/src/Models/Contracts/Role.php new file mode 100644 index 0000000..30bf0d5 --- /dev/null +++ b/src/Models/Contracts/Role.php @@ -0,0 +1,30 @@ + Date: Tue, 15 Aug 2023 20:46:56 +0330 Subject: [PATCH 03/12] role contract implemented in RoleDelegate; --- tests/skeleton/laravel-10.x/app/Models/RoleDelegate.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/skeleton/laravel-10.x/app/Models/RoleDelegate.php b/tests/skeleton/laravel-10.x/app/Models/RoleDelegate.php index 5ae2b18..932c6b3 100644 --- a/tests/skeleton/laravel-10.x/app/Models/RoleDelegate.php +++ b/tests/skeleton/laravel-10.x/app/Models/RoleDelegate.php @@ -3,12 +3,12 @@ namespace App\Models; use Hans\Horus\Helpers\Enums\CacheEnum; +use Hans\Sphinx\Models\Contracts\Role as RoleContract; use Illuminate\Support\Facades\Cache; use Spatie\Permission\Models\Role; use Throwable; -// TODO: not documented in horus -class RoleDelegate extends Role +class RoleDelegate extends Role implements RoleContract { /** * @param int $id @@ -49,6 +49,8 @@ public function increaseVersion(): bool } /** + * Make the unique key for caching the instance + * * @param int $id * * @return string From 50ebc2b3ece2ebb9efc15dabb8ae1a831ba41aa8 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Tue, 15 Aug 2023 20:47:32 +0330 Subject: [PATCH 04/12] RoleIdValidator checks role_model to be an instance of role contract; --- src/Drivers/Constraints/RoleIdValidator.php | 11 ++++++++++- src/Exceptions/SphinxErrorCode.php | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Drivers/Constraints/RoleIdValidator.php b/src/Drivers/Constraints/RoleIdValidator.php index 5df12f0..4442d5e 100644 --- a/src/Drivers/Constraints/RoleIdValidator.php +++ b/src/Drivers/Constraints/RoleIdValidator.php @@ -4,6 +4,7 @@ use Hans\Sphinx\Exceptions\SphinxErrorCode; use Hans\Sphinx\Exceptions\SphinxException; +use Hans\Sphinx\Models\Contracts\Role as RoleContract; use Lcobucci\JWT\Token; use Lcobucci\JWT\Validation\Constraint; use Symfony\Component\HttpFoundation\Response as ResponseAlias; @@ -35,7 +36,15 @@ public function assert(Token $token): void ); } - $role = app(sphinx_config('role_model'))->findAndCache($role_id); + $model = app( sphinx_config( 'role_model' ) ); + if ( ! $model instanceof RoleContract ){ + $modelClass = get_class( $model ); + throw new SphinxException( + "Role model [$modelClass] must implement the ".RoleContract::class." contract.", + SphinxErrorCode::MUST_IMPLEMENT_ROLE_CONTRACT, + ); + } + $role = $model->findAndCache($role_id); if ($role->getVersion() != $role_version) { throw new SphinxException( diff --git a/src/Exceptions/SphinxErrorCode.php b/src/Exceptions/SphinxErrorCode.php index 1427a9f..95d08af 100644 --- a/src/Exceptions/SphinxErrorCode.php +++ b/src/Exceptions/SphinxErrorCode.php @@ -17,4 +17,5 @@ class SphinxErrorCode public const FAILED_TO_SET_HEADER = 1013; public const FAILED_TO_CREATE_TOKEN = 1014; public const FAILED_TO_CREATE_REFRESH_TOKEN = 1015; + public const MUST_IMPLEMENT_ROLE_CONTRACT = 1016; } From 587ab85187059cc0dca8db01650da130d10f6841 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 15 Aug 2023 17:29:10 +0000 Subject: [PATCH 05/12] Apply fixes from StyleCI --- src/Drivers/Constraints/RoleIdValidator.php | 17 ++++--- src/Models/Contracts/Role.php | 49 +++++++++---------- .../laravel-10.x/app/Models/RoleDelegate.php | 2 +- 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/Drivers/Constraints/RoleIdValidator.php b/src/Drivers/Constraints/RoleIdValidator.php index 4442d5e..cdaf874 100644 --- a/src/Drivers/Constraints/RoleIdValidator.php +++ b/src/Drivers/Constraints/RoleIdValidator.php @@ -36,14 +36,15 @@ public function assert(Token $token): void ); } - $model = app( sphinx_config( 'role_model' ) ); - if ( ! $model instanceof RoleContract ){ - $modelClass = get_class( $model ); - throw new SphinxException( - "Role model [$modelClass] must implement the ".RoleContract::class." contract.", - SphinxErrorCode::MUST_IMPLEMENT_ROLE_CONTRACT, - ); - } + $model = app(sphinx_config('role_model')); + if (!$model instanceof RoleContract) { + $modelClass = get_class($model); + + throw new SphinxException( + "Role model [$modelClass] must implement the ".RoleContract::class.' contract.', + SphinxErrorCode::MUST_IMPLEMENT_ROLE_CONTRACT, + ); + } $role = $model->findAndCache($role_id); if ($role->getVersion() != $role_version) { diff --git a/src/Models/Contracts/Role.php b/src/Models/Contracts/Role.php index 30bf0d5..e27180e 100644 --- a/src/Models/Contracts/Role.php +++ b/src/Models/Contracts/Role.php @@ -1,30 +1,29 @@ Date: Thu, 17 Aug 2023 14:08:56 +0330 Subject: [PATCH 06/12] Role contract renamed to RoleMethods; --- src/Drivers/Constraints/RoleIdValidator.php | 2 +- src/Models/Contracts/{Role.php => RoleMethods.php} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/Models/Contracts/{Role.php => RoleMethods.php} (95%) diff --git a/src/Drivers/Constraints/RoleIdValidator.php b/src/Drivers/Constraints/RoleIdValidator.php index cdaf874..10bcf11 100644 --- a/src/Drivers/Constraints/RoleIdValidator.php +++ b/src/Drivers/Constraints/RoleIdValidator.php @@ -4,7 +4,7 @@ use Hans\Sphinx\Exceptions\SphinxErrorCode; use Hans\Sphinx\Exceptions\SphinxException; -use Hans\Sphinx\Models\Contracts\Role as RoleContract; +use Hans\Sphinx\Models\Contracts\RoleMethods as RoleContract; use Lcobucci\JWT\Token; use Lcobucci\JWT\Validation\Constraint; use Symfony\Component\HttpFoundation\Response as ResponseAlias; diff --git a/src/Models/Contracts/Role.php b/src/Models/Contracts/RoleMethods.php similarity index 95% rename from src/Models/Contracts/Role.php rename to src/Models/Contracts/RoleMethods.php index e27180e..a510565 100644 --- a/src/Models/Contracts/Role.php +++ b/src/Models/Contracts/RoleMethods.php @@ -2,7 +2,7 @@ namespace Hans\Sphinx\Models\Contracts; - interface Role + interface RoleMethods { /** * Find the given id and cache the result. From 5fd03655eac0d8c7f953b7fd2557af51e86f763e Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Thu, 17 Aug 2023 14:09:09 +0330 Subject: [PATCH 07/12] RoleMethod trait created; --- src/Models/Traits/RoleMethods.php | 58 +++++++++++++++++++ .../laravel-10.x/app/Models/RoleDelegate.php | 56 +----------------- 2 files changed, 61 insertions(+), 53 deletions(-) create mode 100644 src/Models/Traits/RoleMethods.php diff --git a/src/Models/Traits/RoleMethods.php b/src/Models/Traits/RoleMethods.php new file mode 100644 index 0000000..7e7be55 --- /dev/null +++ b/src/Models/Traits/RoleMethods.php @@ -0,0 +1,58 @@ + self::query()->findOrFail($id) + ); + } + + /** + * @return int + */ + public function getVersion(): int + { + return $this->version ?: self::query()->findOrFail($this->id, ['id', 'version'])->version; + } + + /** + * @return bool + */ + public function increaseVersion(): bool + { + try { + $this->increment('version'); + $this->fill(['version' => $this->getVersion() + 1])->saveQuietly(); + Cache::forget(self::cacheKey($this->id)); + Cache::forever(self::cacheKey($this->id), $this); + } catch (Throwable $e) { + return false; + } + + return true; + } + + /** + * Make the unique key for caching the instance. + * + * @param int $id + * + * @return string + */ + protected static function cacheKey(int $id): string + { + return "role_cache_$id"; + } + } \ No newline at end of file diff --git a/tests/skeleton/laravel-10.x/app/Models/RoleDelegate.php b/tests/skeleton/laravel-10.x/app/Models/RoleDelegate.php index 6d7d464..0df2578 100644 --- a/tests/skeleton/laravel-10.x/app/Models/RoleDelegate.php +++ b/tests/skeleton/laravel-10.x/app/Models/RoleDelegate.php @@ -2,61 +2,11 @@ namespace App\Models; -use Hans\Horus\Helpers\Enums\CacheEnum; -use Hans\Sphinx\Models\Contracts\Role as RoleContract; -use Illuminate\Support\Facades\Cache; +use Hans\Sphinx\Models\Contracts\RoleMethods as RoleContract; +use Hans\Sphinx\Models\Traits\RoleMethods; use Spatie\Permission\Models\Role; -use Throwable; class RoleDelegate extends Role implements RoleContract { - /** - * @param int $id - * - * @return static - */ - public static function findAndCache(int $id): self - { - return Cache::rememberForever( - self::cacheKey($id), - fn () => self::query()->findOrFail($id) - ); - } - - /** - * @return int - */ - public function getVersion(): int - { - return $this->version ?: self::query()->findOrFail($this->id, ['id', 'version'])->version; - } - - /** - * @return bool - */ - public function increaseVersion(): bool - { - try { - $this->increment('version'); - $this->fill(['version' => $this->getVersion() + 1])->saveQuietly(); - Cache::forget(self::cacheKey($this->id)); - Cache::forever(self::cacheKey($this->id), $this); - } catch (Throwable $e) { - return false; - } - - return true; - } - - /** - * Make the unique key for caching the instance. - * - * @param int $id - * - * @return string - */ - private static function cacheKey(int $id): string - { - return CacheEnum::ROLE->value."_$id"; - } +use RoleMethods; } From c4766ace88c7e52c8a5cbd72239a8118fa48964a Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 17 Aug 2023 10:39:24 +0000 Subject: [PATCH 08/12] Apply fixes from StyleCI --- src/Models/Contracts/RoleMethods.php | 46 ++++---- src/Models/Traits/RoleMethods.php | 101 +++++++++--------- .../laravel-10.x/app/Models/RoleDelegate.php | 2 +- 3 files changed, 75 insertions(+), 74 deletions(-) diff --git a/src/Models/Contracts/RoleMethods.php b/src/Models/Contracts/RoleMethods.php index a510565..1551309 100644 --- a/src/Models/Contracts/RoleMethods.php +++ b/src/Models/Contracts/RoleMethods.php @@ -2,28 +2,28 @@ namespace Hans\Sphinx\Models\Contracts; - interface RoleMethods - { - /** - * Find the given id and cache the result. - * - * @param int $id - * - * @return static - */ - public static function findAndCache(int $id): self; +interface RoleMethods +{ + /** + * Find the given id and cache the result. + * + * @param int $id + * + * @return static + */ + public static function findAndCache(int $id): self; - /** - * Return version of the current instance. - * - * @return int - */ - public function getVersion(): int; + /** + * Return version of the current instance. + * + * @return int + */ + public function getVersion(): int; - /** - * Increase the version by one unit. - * - * @return bool - */ - public function increaseVersion(): bool; - } + /** + * Increase the version by one unit. + * + * @return bool + */ + public function increaseVersion(): bool; +} diff --git a/src/Models/Traits/RoleMethods.php b/src/Models/Traits/RoleMethods.php index 7e7be55..ab94a11 100644 --- a/src/Models/Traits/RoleMethods.php +++ b/src/Models/Traits/RoleMethods.php @@ -1,58 +1,59 @@ self::query()->findOrFail($id) - ); - } + trait RoleMethods + { + /** + * @param int $id + * + * @return static + */ + public static function findAndCache(int $id): self + { + return Cache::rememberForever( + static::cacheKey($id), + fn () => self::query()->findOrFail($id) + ); + } - /** - * @return int - */ - public function getVersion(): int - { - return $this->version ?: self::query()->findOrFail($this->id, ['id', 'version'])->version; - } + /** + * @return int + */ + public function getVersion(): int + { + return $this->version ?: self::query()->findOrFail($this->id, ['id', 'version'])->version; + } - /** - * @return bool - */ - public function increaseVersion(): bool - { - try { - $this->increment('version'); - $this->fill(['version' => $this->getVersion() + 1])->saveQuietly(); - Cache::forget(self::cacheKey($this->id)); - Cache::forever(self::cacheKey($this->id), $this); - } catch (Throwable $e) { - return false; - } + /** + * @return bool + */ + public function increaseVersion(): bool + { + try { + $this->increment('version'); + $this->fill(['version' => $this->getVersion() + 1])->saveQuietly(); + Cache::forget(self::cacheKey($this->id)); + Cache::forever(self::cacheKey($this->id), $this); + } catch (Throwable $e) { + return false; + } - return true; - } + return true; + } - /** - * Make the unique key for caching the instance. - * - * @param int $id - * - * @return string - */ - protected static function cacheKey(int $id): string - { - return "role_cache_$id"; - } - } \ No newline at end of file + /** + * Make the unique key for caching the instance. + * + * @param int $id + * + * @return string + */ + protected static function cacheKey(int $id): string + { + return "role_cache_$id"; + } + } diff --git a/tests/skeleton/laravel-10.x/app/Models/RoleDelegate.php b/tests/skeleton/laravel-10.x/app/Models/RoleDelegate.php index 0df2578..4154eef 100644 --- a/tests/skeleton/laravel-10.x/app/Models/RoleDelegate.php +++ b/tests/skeleton/laravel-10.x/app/Models/RoleDelegate.php @@ -8,5 +8,5 @@ class RoleDelegate extends Role implements RoleContract { -use RoleMethods; + use RoleMethods; } From 0b83473a2f0feb65745b88b6c8f22fd868efe854 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Thu, 17 Aug 2023 14:17:57 +0330 Subject: [PATCH 09/12] installation link replaced with docs; --- docs/layouts/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/layouts/index.html b/docs/layouts/index.html index c10aaeb..719fbff 100644 --- a/docs/layouts/index.html +++ b/docs/layouts/index.html @@ -8,7 +8,7 @@

{{.Title}}

{{.Description}}

From 675377047822a41cb39d598b1ad7d79cfca159b2 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Thu, 17 Aug 2023 14:18:13 +0330 Subject: [PATCH 10/12] bref extra dot at the end fixed; --- docs/content/docs/facade.md | 2 +- docs/content/docs/installation.md | 2 +- docs/content/docs/trait.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/content/docs/facade.md b/docs/content/docs/facade.md index 7d66066..8d35347 100644 --- a/docs/content/docs/facade.md +++ b/docs/content/docs/facade.md @@ -4,7 +4,7 @@ draft = false weight = 10 description = "To make it easy to use Sphinx." title = "Facade" -bref= "There is a facade class to make working easier. this facade contains several methods that we will introduce in continue." +bref= "There is a facade class to make working easier. this facade contains several methods that we will introduce in continue" toc = false +++ diff --git a/docs/content/docs/installation.md b/docs/content/docs/installation.md index 8282d6f..4976b9a 100644 --- a/docs/content/docs/installation.md +++ b/docs/content/docs/installation.md @@ -4,7 +4,7 @@ draft = false weight = 9 description = "Installation guidance to install and setup Sphinx." title = "Installation" -bref= "To install Sphinx, follow the below steps." +bref= "To install Sphinx, follow the below steps" toc = false +++ diff --git a/docs/content/docs/trait.md b/docs/content/docs/trait.md index cfdfde3..cb87638 100644 --- a/docs/content/docs/trait.md +++ b/docs/content/docs/trait.md @@ -4,7 +4,7 @@ draft = false weight = 20 description = "A bunch of useful and handy methods." title = "Trait" -bref= "Sphinx adds some useful and necessary methods through the trait. there is the list of available methods." +bref= "Sphinx adds some useful and necessary methods through the trait. there is the list of available methods" toc = false +++ From 95a1756fe977b3a3fb7c37cb58087acd7f039231 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Thu, 17 Aug 2023 14:26:54 +0330 Subject: [PATCH 11/12] RoleMethod contract documented; --- docs/content/docs/installation.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/docs/content/docs/installation.md b/docs/content/docs/installation.md index 4976b9a..e950cd0 100644 --- a/docs/content/docs/installation.md +++ b/docs/content/docs/installation.md @@ -85,4 +85,23 @@ And finally, you can set the `jwt` guard as default. ], ``` -All sets. enjoy! \ No newline at end of file +All sets. + +## Role model + +Sphinx expects the role model of your project, contains requested method +on `Hans\Sphinx\Models\Contracts\RoleMethods` interface class. So, you should implement this contract and for your +convenient, there is the `Hans\Sphinx\Models\Traits\RoleMethods` trait class that contains all the methods that you must +implement. + +``` +use Hans\Sphinx\Models\Contracts\RoleMethods as RoleContract; +use Hans\Sphinx\Models\Traits\RoleMethods; +use Spatie\Permission\Models\Role; + +class RoleDelegate extends Role implements RoleContract { +use RoleMethods; + +// ... +} +``` \ No newline at end of file From 2af317d8655c2f448109313626f8c72ecb6bdb5c Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Thu, 17 Aug 2023 14:34:11 +0330 Subject: [PATCH 12/12] Apply StyleCI diff file; --- src/Models/Traits/RoleMethods.php | 96 +++++++++++++++---------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/src/Models/Traits/RoleMethods.php b/src/Models/Traits/RoleMethods.php index ab94a11..551eecb 100644 --- a/src/Models/Traits/RoleMethods.php +++ b/src/Models/Traits/RoleMethods.php @@ -2,58 +2,58 @@ namespace Hans\Sphinx\Models\Traits; - use Illuminate\Support\Facades\Cache; - use Throwable; +use Illuminate\Support\Facades\Cache; +use Throwable; - trait RoleMethods +trait RoleMethods +{ + /** + * @param int $id + * + * @return static + */ + public static function findAndCache(int $id): self { - /** - * @param int $id - * - * @return static - */ - public static function findAndCache(int $id): self - { - return Cache::rememberForever( - static::cacheKey($id), - fn () => self::query()->findOrFail($id) - ); - } - - /** - * @return int - */ - public function getVersion(): int - { - return $this->version ?: self::query()->findOrFail($this->id, ['id', 'version'])->version; - } + return Cache::rememberForever( + static::cacheKey($id), + fn () => self::query()->findOrFail($id) + ); + } - /** - * @return bool - */ - public function increaseVersion(): bool - { - try { - $this->increment('version'); - $this->fill(['version' => $this->getVersion() + 1])->saveQuietly(); - Cache::forget(self::cacheKey($this->id)); - Cache::forever(self::cacheKey($this->id), $this); - } catch (Throwable $e) { - return false; - } + /** + * @return int + */ + public function getVersion(): int + { + return $this->version ?: self::query()->findOrFail($this->id, ['id', 'version'])->version; + } - return true; + /** + * @return bool + */ + public function increaseVersion(): bool + { + try { + $this->increment('version'); + $this->fill(['version' => $this->getVersion() + 1])->saveQuietly(); + Cache::forget(self::cacheKey($this->id)); + Cache::forever(self::cacheKey($this->id), $this); + } catch (Throwable $e) { + return false; } - /** - * Make the unique key for caching the instance. - * - * @param int $id - * - * @return string - */ - protected static function cacheKey(int $id): string - { - return "role_cache_$id"; - } + return true; + } + + /** + * Make the unique key for caching the instance. + * + * @param int $id + * + * @return string + */ + protected static function cacheKey(int $id): string + { + return "role_cache_$id"; } +}