1. Go to this page and download the library: Download masq/guardian library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
masq / guardian example snippets
use Guardian\Concerns\Guardable;
class User extends Authenticatable
{
use Guardable;
// Optional hooks Guardian calls when a state is entered:
public function guardianRestrict(\Guardian\Enums\TrustState $state, array $ctx = []): void
{
// soft restriction — e.g. freeze rewards
}
public function guardianBan(array $ctx = []): void
{
$this->tokens()->delete(); // your definition of "banned"
}
}
use Guardian\ValueObjects\Signal;
// 1. Record an observation (points fade with the default decay)
$user->raiseSuspicion(Signal::soft('login_velocity', 15, ['ip' => $ip]));
// 2. Read where the subject stands (served from cache, not the DB)
$user->trustState(); // TrustState enum: Trusted / Watch / Restricted / Review / Banned
$user->suspicionScore(); // int — current, decayed score
$user->isBanned();
$user->isFlagged(); // worse than Trusted
$user->needsReview(); // has an open moderator case
// 3. Protect a route
Route::post('/play', ...)->middleware('guardian:banned');
// routes/console.php
use Illuminate\Support\Facades\Schedule;
use Guardian\Jobs\ReevaluateTrust;
Schedule::job(new ReevaluateTrust)->daily();
'thresholds' => [
0 => TrustState::Trusted,
20 => TrustState::Watch,
50 => TrustState::Restricted,
80 => TrustState::Review, // opens a moderator case
120 => TrustState::Banned,
],
use Guardian\Contracts\TrustStateContract;
enum TrustState: string implements TrustStateContract
{
case Trusted = 'trusted';
case Watch = 'watch';
case Probation = 'probation'; // your extra rung
case Review = 'review';
case Banned = 'banned';
public function key(): string { return $this->value; }
public function level(): int { /* order: lower = more trusted */ }
public static function base(): self { return self::Trusted; } // baseline
public static function terminal(): self { return self::Banned; } // ban target
public static function fromKey(string $k): self { return self::from($k); }
public static function tryFromKey(?string $k): ?self { return $k === null ? null : self::tryFrom($k); }
public static function all(): array { return self::cases(); }
}
Guardian::inspect($user, ['hits' => $hits]); // every enabled detector
Guardian::run('rate_limit', $user, ['hits' => $hits]); // one by key
Guardian::register($adHocDetector); // runtime only
// list form — enum case, no ->value
'actions' => [
['state' => TrustState::Restricted, 'actions' => [FreezeAction::class]],
['state' => TrustState::Review, 'actions' => [QueueForReviewAction::class]],
['state' => TrustState::Banned, 'actions' => [QueueForReviewAction::class, BanAction::class]],
],
// keyed map — string keys also work ('restricted' value or 'Restricted' name)
'actions' => [
'restricted' => [FreezeAction::class],
'review' => [QueueForReviewAction::class],
],
Guardian::track('behavior')->run('chat_filter', $user, ['message' => $text]);
Guardian::track('behavior')->ban($user, 'harassment');
$user->isBanned('behavior'); // independent of the default (anti-cheat) track
$user->isBanned(); // default track
Guardian::recordThrottleHit($user, 'login');
'throttle_hits' => [
'class' => Guardian\Detectors\ThrottleHitDetector::class,
'enabled' => true,
'allowed_hits' => 5, // free hits inside the window
'window_seconds' => 900, // counter window (used to size the cache TTL)
'base_points' => 12, // points once the allowance is exceeded
'points_per_extra_hit' => 6, // + per hit beyond the allowance
'max_points' => 100, // cap for one signal
'decay' => 'half_life',
],