Skip to content

Commit

Permalink
patches for dev logging, and additional useful user functions
Browse files Browse the repository at this point in the history
  • Loading branch information
roncodes committed May 30, 2024
1 parent a637e7e commit d99b707
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Http/Requests/OnboardRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class OnboardRequest extends FleetbaseRequest
*
* @return array
*/
protected $excludedWords = ['test', 'testing', 'example', 'trial', 'trialing', 'asdf', '1234', 'asdas', 'dsdsds'];
protected $excludedWords = ['test', 'test123', 'abctest', 'testing', 'example', 'trial', 'trialing', 'asdf', '1234', 'asdas', 'dsdsds', 'dummy', 'xxxx', 'aaa', 'demo', 'zzz', 'zzzz', 'none'];

/**
* Determine if the user is authorized to make this request.
Expand Down
3 changes: 3 additions & 0 deletions src/Jobs/LogApiRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Spatie\ResponseCache\Facades\ResponseCache;

class LogApiRequest implements ShouldQueue
{
Expand Down Expand Up @@ -58,6 +59,8 @@ public function handle()
{
// Log::info('Logging API Request ' . print_r($this->payload, true));
ApiRequestLog::on($this->dbConnection)->create($this->payload);
// Clear response cache
ResponseCache::clear();
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Models/ApiCredential.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,14 @@ public static function generateKeys($encode, $testKey = false)
'secret' => $hash,
];
}

/**
* Update the datetime of the last usage.
*
* @return bool
*/
public function trackLastUsed()
{
return $this->update(['last_used_at' => now()]);
}
}
2 changes: 2 additions & 0 deletions src/Models/ApiEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
use Fleetbase\Casts\Json;
use Fleetbase\Traits\Filterable;
use Fleetbase\Traits\HasApiModelBehavior;
use Fleetbase\Traits\HasPublicId;
use Fleetbase\Traits\HasUuid;
use Fleetbase\Traits\Searchable;

class ApiEvent extends Model
{
use HasUuid;
use HasPublicId;
use HasApiModelBehavior;
use Searchable;
use Filterable;
Expand Down
2 changes: 2 additions & 0 deletions src/Models/ApiRequestLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
use Fleetbase\Casts\Json;
use Fleetbase\Traits\Filterable;
use Fleetbase\Traits\HasApiModelBehavior;
use Fleetbase\Traits\HasPublicId;
use Fleetbase\Traits\HasUuid;
use Fleetbase\Traits\Searchable;

class ApiRequestLog extends Model
{
use HasUuid;
use HasPublicId;
use HasApiModelBehavior;
use Searchable;
use Filterable;
Expand Down
37 changes: 37 additions & 0 deletions src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,25 @@ public static function applyUserInfoFromRequest($request, array $attributes = []
return $attributes;
}

/**
* Create a new User instance with enriched attributes from the request.
*
* This static method constructs a new User object using information obtained from
* a request object. It enhances the initial user attributes with additional details
* such as country, timezone, and IP-related metadata by leveraging the
* applyUserInfoFromRequest() method. This method is ideal for initializing a user with
* comprehensive details at the point of creation, particularly during registration processes.
*
* @param \Illuminate\Http\Request $request the request object containing user's IP address and possibly other details
* @param array $attributes an optional array of initial attributes that may be provided for the user
*
* @return User returns the newly created User instance with enriched attributes
*/
public static function newUserWithRequestInfo($request, $attributes = []): User
{
return new User(static::applyUserInfoFromRequest($request, $attributes));
}

/**
* Sets user information from the request on the current User model instance.
*
Expand Down Expand Up @@ -804,11 +823,29 @@ public function setUserInfoFromRequest($request, bool $save = false): User
return $this;
}

/**
* Retrieve the last seen timestamp of the user.
*
* This method acts as an accessor for the 'lastSeenAt' attribute of the User model.
* It returns the datetime when the user was last active in the system. This can be
* used to display the last seen status or to calculate if the user is offline.
*
* @return \Carbon\Carbon|null returns the Carbon instance for the last seen timestamp or null if not set
*/
public function getLastSeenAtAttribute()
{
return $this->lastSeenAt();
}

/**
* Check if the user is currently online.
*
* This accessor method for the 'isOnline' attribute determines if the user is considered
* online based on certain criteria like their last activity timestamp. It leverages the
* isOnline() method, which should contain the logic to ascertain the user's online status.
*
* @return bool returns true if the user is online, otherwise false
*/
public function getIsOnlineAttribute()
{
return $this->isOnline();
Expand Down
3 changes: 3 additions & 0 deletions src/Support/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public static function setSession($user = null, $login = false): bool
session(['is_admin' => $user->isAdmin()]);
}

// track last usage of api credential
$apiCredential->trackLastUsed();

return true;
}

Expand Down
15 changes: 13 additions & 2 deletions src/Types/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,18 @@ public function only($keys = []): array
$key = Arr::first(array_keys($key));
}

$result[$as] = strpos($key, '.') > 0 ? Utils::get($this, $key) : $this->{$key};
if (!is_string($key)) {
continue;
}

if (strpos($key, '.') > 0) {
$result[$as] = Utils::get($this, $key);
continue;
}

if (isset($this->{$key})) {
$result[$as] = $this->{$key};
}
}

return $result;
Expand Down Expand Up @@ -232,7 +243,7 @@ function ($country) use ($query) {
strtolower($country->getCode()) === $query,
strtolower($country->getCca2()) === $query,
Str::contains(strtolower($country->getAbbrev()), $query),
Str::contains(strtolower($country->getName()), $query),
// Str::contains(strtolower($country->getName()), $query),
];

return count(array_filter($matches));
Expand Down

0 comments on commit d99b707

Please sign in to comment.