From 338109ab8ae943bc87db318a8a675b0658051ef1 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Fri, 13 Sep 2024 17:55:58 +0800 Subject: [PATCH 1/3] sefault sandbox db conneciton port to mysql connection env port --- composer.json | 2 +- config/database.connections.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 99cff38..9bdee22 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "fleetbase/core-api", - "version": "1.5.7", + "version": "1.5.8", "description": "Core Framework and Resources for Fleetbase API", "keywords": [ "fleetbase", diff --git a/config/database.connections.php b/config/database.connections.php index 9a0460a..3d989f8 100644 --- a/config/database.connections.php +++ b/config/database.connections.php @@ -86,7 +86,7 @@ 'sandbox' => [ 'driver' => 'mysql', 'host' => $host, - 'port' => env('SANDBOX_DB_PORT', '3306'), + 'port' => env('SANDBOX_DB_PORT', env('DB_PORT', '3306')), 'database' => $database . '_sandbox', 'username' => $username, 'password' => $password, From 563092d4b20df819b893cf4b5cb81c9355342158 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Wed, 25 Sep 2024 14:08:46 +0800 Subject: [PATCH 2/3] added locale to user model and json resource --- composer.json | 2 +- src/Http/Resources/User.php | 53 +++++++++++++++++++------------------ src/Models/User.php | 5 ++++ 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/composer.json b/composer.json index 9bdee22..0e28b71 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "fleetbase/core-api", - "version": "1.5.8", + "version": "1.5.9", "description": "Core Framework and Resources for Fleetbase API", "keywords": [ "fleetbase", diff --git a/src/Http/Resources/User.php b/src/Http/Resources/User.php index a4202a6..e01bc7c 100644 --- a/src/Http/Resources/User.php +++ b/src/Http/Resources/User.php @@ -16,32 +16,33 @@ class User extends FleetbaseResource public function toArray($request) { return [ - 'id' => $this->when(Http::isInternalRequest(), $this->id, $this->public_id), - 'uuid' => $this->when(Http::isInternalRequest(), $this->uuid), - 'company_uuid' => $this->when(Http::isInternalRequest(), $this->company_uuid), - 'public_id' => $this->when(Http::isInternalRequest(), $this->public_id), - 'company' => $this->when(Http::isPublicRequest(), $this->company ? $this->company->public_id : null), - 'name' => $this->name, - 'username' => $this->username, - 'email' => $this->email, - 'phone' => $this->phone, - 'country' => $this->country, - 'timezone' => $this->timezone, - 'avatar_url' => $this->avatar_url, - 'meta' => $this->meta, - 'role' => $this->when(Http::isInternalRequest(), new Role($this->role), null), - 'policies' => $this->when(Http::isInternalRequest(), Policy::collection($this->policies), []), - 'permissions' => $this->when(Http::isInternalRequest(), $this->serializePermissions($this->permissions), []), - 'type' => $this->type, - 'types' => $this->when(Http::isInternalRequest(), $this->types ?? []), - 'company_name' => $this->when(Http::isInternalRequest(), $this->company_name), - 'session_status' => $this->when(Http::isInternalRequest(), $this->session_status), - 'is_admin' => $this->when(Http::isInternalRequest(), $this->is_admin), - 'is_online' => $this->is_online, - 'last_seen_at' => $this->last_seen_at, - 'last_login' => $this->last_login, - 'updated_at' => $this->updated_at, - 'created_at' => $this->created_at, + 'id' => $this->when(Http::isInternalRequest(), $this->id, $this->public_id), + 'uuid' => $this->when(Http::isInternalRequest(), $this->uuid), + 'company_uuid' => $this->when(Http::isInternalRequest(), $this->company_uuid), + 'public_id' => $this->when(Http::isInternalRequest(), $this->public_id), + 'company' => $this->when(Http::isPublicRequest(), $this->company ? $this->company->public_id : null), + 'name' => $this->name, + 'username' => $this->username, + 'email' => $this->email, + 'phone' => $this->phone, + 'country' => $this->country, + 'timezone' => $this->timezone, + 'avatar_url' => $this->avatar_url, + 'meta' => $this->meta, + 'role' => $this->when(Http::isInternalRequest(), new Role($this->role), null), + 'policies' => $this->when(Http::isInternalRequest(), Policy::collection($this->policies), []), + 'permissions' => $this->when(Http::isInternalRequest(), $this->serializePermissions($this->permissions), []), + 'type' => $this->type, + 'locale' => $this->getLocale(), + 'types' => $this->when(Http::isInternalRequest(), $this->types ?? []), + 'company_name' => $this->when(Http::isInternalRequest(), $this->company_name), + 'session_status' => $this->when(Http::isInternalRequest(), $this->session_status), + 'is_admin' => $this->when(Http::isInternalRequest(), $this->is_admin), + 'is_online' => $this->is_online, + 'last_seen_at' => $this->last_seen_at, + 'last_login' => $this->last_login, + 'updated_at' => $this->updated_at, + 'created_at' => $this->created_at, ]; } diff --git a/src/Models/User.php b/src/Models/User.php index bc27418..eb086aa 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -328,6 +328,11 @@ public function groups(): HasManyThrough return $this->hasManyThrough(Group::class, GroupUser::class, 'user_uuid', 'uuid', 'uuid', 'group_uuid'); } + public function getLocale(): string + { + return Setting::lookup('user.' . $this->uuid . '.locale', 'en-us'); + } + /** * Generates a unique username based on the provided name. * From a9ddbe26a96f558a433acde2216afa2171c17cb1 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Wed, 2 Oct 2024 13:29:49 +0800 Subject: [PATCH 3/3] fixes and patches --- src/Console/Commands/CreatePermissions.php | 20 ++++++++++++++----- .../Internal/v1/LookupController.php | 16 ++++++++++++++- src/Http/Resources/User.php | 2 ++ src/Traits/HasMetaAttributes.php | 10 ++++++++++ 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/Console/Commands/CreatePermissions.php b/src/Console/Commands/CreatePermissions.php index 584a45a..34b5910 100644 --- a/src/Console/Commands/CreatePermissions.php +++ b/src/Console/Commands/CreatePermissions.php @@ -346,9 +346,13 @@ public function createDirectives(Model $subject, string $service, string $guard, } foreach ($directives as $permission => $rules) { - // dd($permission, $rules); - // role permission names can be shorthanded by excluding the service since the schema provides the service name - $permissionName = Str::startsWith($permission, $service) ? $permission : $service . ' ' . $permission; + $permissionSegmentCount = count(explode(' ', $permission)); + if ($permissionSegmentCount === 3) { + $permissionName = $permission; + } else { + // role permission names can be shorthanded by excluding the service since the schema provides the service name + $permissionName = Str::startsWith($permission, $service) ? $permission : $service . ' ' . $permission; + } // next we validate the permission name $permissionNameSegmentsCount = count(explode(' ', $permissionName)); @@ -404,8 +408,14 @@ public function createDirectives(Model $subject, string $service, string $guard, public function assignPermissions(Model $subject, string $service, string $guard, array $permissions = []): Model { foreach ($permissions as $permissionName) { - // role permission names can be shorthanded by excluding the service since the schema provides the service name - $permissionName = Str::startsWith($permissionName, $service) ? $permissionName : $service . ' ' . $permissionName; + $permissionSegmentCount = count(explode(' ', $permissionName)); + if ($permissionSegmentCount === 3) { + $permissionName = $permissionName; + } else { + // role permission names can be shorthanded by excluding the service since the schema provides the service name + $permissionName = Str::startsWith($permissionName, $service) ? $permissionName : $service . ' ' . $permissionName; + } + // next we validate the permission name $permissionNameSegmentsCount = count(explode(' ', $permissionName)); if ($permissionNameSegmentsCount !== 3) { diff --git a/src/Http/Controllers/Internal/v1/LookupController.php b/src/Http/Controllers/Internal/v1/LookupController.php index 8553c25..490fba3 100644 --- a/src/Http/Controllers/Internal/v1/LookupController.php +++ b/src/Http/Controllers/Internal/v1/LookupController.php @@ -122,6 +122,8 @@ public function countries(Request $request) $query = strtolower($request->input('query', null)); $simple = $request->boolean('simple'); $columns = $request->array('columns'); + $only = array_map(fn ($s) => strtolower($s), $request->array('only')); + $except = array_map(fn ($s) => strtolower($s), $request->array('except')); $countries = Country::search($query); @@ -141,7 +143,19 @@ function ($country) { ); } - return response()->json($countries); + if ($only) { + $countries = $countries->filter(function ($country) use ($only) { + return in_array(strtolower(data_get($country, 'cca2')), $only); + }); + } + + if ($except) { + $countries = $countries->filter(function ($country) use ($except) { + return !in_array(strtolower(data_get($country, 'cca2')), $except); + }); + } + + return response()->json($countries->values()); } /** diff --git a/src/Http/Resources/User.php b/src/Http/Resources/User.php index e01bc7c..83af50f 100644 --- a/src/Http/Resources/User.php +++ b/src/Http/Resources/User.php @@ -32,6 +32,7 @@ public function toArray($request) 'role' => $this->when(Http::isInternalRequest(), new Role($this->role), null), 'policies' => $this->when(Http::isInternalRequest(), Policy::collection($this->policies), []), 'permissions' => $this->when(Http::isInternalRequest(), $this->serializePermissions($this->permissions), []), + 'role_name' => $this->when(Http::isInternalRequest(), $this->role ? $this->role->name : null), 'type' => $this->type, 'locale' => $this->getLocale(), 'types' => $this->when(Http::isInternalRequest(), $this->types ?? []), @@ -39,6 +40,7 @@ public function toArray($request) 'session_status' => $this->when(Http::isInternalRequest(), $this->session_status), 'is_admin' => $this->when(Http::isInternalRequest(), $this->is_admin), 'is_online' => $this->is_online, + 'date_of_birth' => $this->date_of_birth, 'last_seen_at' => $this->last_seen_at, 'last_login' => $this->last_login, 'updated_at' => $this->updated_at, diff --git a/src/Traits/HasMetaAttributes.php b/src/Traits/HasMetaAttributes.php index 5fe1182..56ab977 100644 --- a/src/Traits/HasMetaAttributes.php +++ b/src/Traits/HasMetaAttributes.php @@ -142,6 +142,16 @@ public function missingMeta($key): bool return !$this->hasMeta($key); } + /** + * Checks if a meta-data key is missing. + * + * @param string $key key of the meta-data to check + */ + public function doesntHaveMeta($key): bool + { + return !$this->hasMeta($key); + } + /** * Checks if a meta-data property's value is true. *