Skip to content

Commit

Permalink
Merge pull request #112 from fleetbase/dev-v1.5.9
Browse files Browse the repository at this point in the history
Dev v1.5.9
  • Loading branch information
roncodes authored Oct 2, 2024
2 parents e3d17b4 + a9ddbe2 commit 4f7b679
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 34 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion config/database.connections.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 15 additions & 5 deletions src/Console/Commands/CreatePermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 15 additions & 1 deletion src/Http/Controllers/Internal/v1/LookupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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());
}

/**
Expand Down
55 changes: 29 additions & 26 deletions src/Http/Resources/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}

Expand Down
5 changes: 5 additions & 0 deletions src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Traits/HasMetaAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 4f7b679

Please sign in to comment.