diff --git a/composer.json b/composer.json index 99cff38..0e28b71 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "fleetbase/core-api", - "version": "1.5.7", + "version": "1.5.9", "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, 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 a4202a6..83af50f 100644 --- a/src/Http/Resources/User.php +++ b/src/Http/Resources/User.php @@ -16,32 +16,35 @@ 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), []), + '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 ?? []), + '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, + 'date_of_birth' => $this->date_of_birth, + '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. * 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. *