-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace IanM\TwoFactor\Console; | ||
|
||
use Flarum\Settings\SettingsRepositoryInterface; | ||
use Illuminate\Console\Scheduling\Event; | ||
|
||
class InactiveTokensSchedule | ||
{ | ||
public function __construct( | ||
protected SettingsRepositoryInterface $settings | ||
) { } | ||
|
||
public function __invoke(Event $event) | ||
{ | ||
if (! (bool) $this->settings->get('ianm-twofactor.kill_inactive_tokens')) { | ||
return; | ||
} | ||
|
||
$event->twiceDaily(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace IanM\TwoFactor\Console; | ||
|
||
use Carbon\Carbon; | ||
use Flarum\Http\AccessToken; | ||
use Flarum\Settings\SettingsRepositoryInterface; | ||
use Illuminate\Console\Command; | ||
|
||
class KillInactiveTokensCommand extends Command | ||
{ | ||
protected $signature = 'twofactor:kill-inactive-tokens'; | ||
protected $description = 'Kill all inactive tokens'; | ||
|
||
public function __construct( | ||
protected SettingsRepositoryInterface $settings | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
$age = (int) $this->settings->get('ianm-twofactor.kill_inactive_tokens_age_days'); | ||
$maxAge = Carbon::now()->subdays($age); | ||
Check failure on line 24 in src/Console/KillInactiveTokensCommand.php GitHub Actions / run / PHPStan PHP 8.1
Check failure on line 24 in src/Console/KillInactiveTokensCommand.php GitHub Actions / run / PHPStan PHP 8.2
|
||
|
||
$query = AccessToken::query() | ||
->where('last_activity_at', '<', $maxAge); | ||
|
||
if (! (bool) $this->settings->get('ianm-twofactor.also_kill_developer_tokens')) { | ||
$this->info('Not deleting any developer tokens.'); | ||
$query->where('type', '!=', 'developer'); | ||
} | ||
|
||
$count = $query->count(); | ||
|
||
if ($count === 0) { | ||
$this->info("No tokens found which have not been used in $age+ days."); | ||
return; | ||
} | ||
|
||
$this->info("Found $count tokens which have not been used in $age+ days. Deleting..."); | ||
|
||
$this->output->progressStart($count); | ||
|
||
$query->delete(); | ||
|
||
$this->output->progressFinish(); | ||
} | ||
} |