Update expiring/expired roles email sending and account demotion

This commit is contained in:
DariusIII
2019-07-06 23:37:16 +02:00
parent 3d2dffeab6
commit df572aab27
4 changed files with 23 additions and 52 deletions
+1
View File
@@ -1,4 +1,5 @@
2019-07-06 DariusIII
* Chg: Update expiring/expired roles email sending and account demotion
* Chg: Update used libraries to their latest versions
2019-07-05 DariusIII
* Chg: Update filp/whoops (2.4.0 => 2.4.1)
@@ -12,7 +12,7 @@ class NntmuxUpdateExpiredRoles extends Command
*
* @var string
*/
protected $signature = 'nntmux:update-expired-roles {--p|period=* : Update expired roles, add the switch to check user that will have their accounts expire in future}';
protected $signature = 'nntmux:update-expired-roles';
/**
* The console command description.
@@ -39,14 +39,8 @@ class NntmuxUpdateExpiredRoles extends Command
public function handle()
{
$this->info('Updating expired roles.');
if (empty($this->option('period'))) {
$this->info('Updating users who have their role expired');
User::updateExpiredRoles();
} else {
$days = $this->option('period')[0];
$this->info('Updating users that will have their role expire in '.$days.' days.');
User::updateExpiredRoles($days);
}
$this->info('Updating users that will have their roles expire or are already expired');
User::updateExpiredRoles();
$this->info('Expired roles updated.');
}
}
+1 -1
View File
@@ -28,7 +28,7 @@ class Kernel extends ConsoleKernel
$schedule->command('disposable:update')->weekly();
$schedule->command('clean:directories')->hourly();
$schedule->command('nntmux:delete-unverified-users')->twiceDaily(1, 13);
$schedule->command('nntmux:update-expired-roles')->twiceDaily(1, 13);
$schedule->command('nntmux:update-expired-roles')->daily();
$schedule->command('telescope:prune')->daily();
$schedule->command('horizon:snapshot')->everyFiveMinutes();
if (config('nntmux.purge_inactive_users') === true) {
+18 -42
View File
@@ -2,6 +2,7 @@
namespace App\Models;
use Carbon\CarbonImmutable;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
@@ -386,52 +387,27 @@ class User extends Authenticatable
}
}
/**
* @param int|null $period
*/
public static function updateExpiredRoles(int $period = null): void
public static function updateExpiredRoles(): void
{
$now = now();
$endDate = $now->addDays($period);
$query = self::query()->whereDate('rolechangedate', '>', now())->whereDate('rolechangedate', '<=', $endDate);
if ($period === null) {
$endDate = $now;
$query = self::query()->whereDate('rolechangedate', '<', $endDate);
}
$now = CarbonImmutable::now();
$period = [
'day' => $now->addDay(),
'week' => $now->addWeek(),
'month' => $now->addMonth(),
];
$usersCheck = $query->get();
foreach ($usersCheck as $userCheck) {
$emailCheckQuery = RoleExpirationEmail::query()->where('users_id', $userCheck->id)->get();
if ($period === null) {
$userCheck->update(['roles_id' => self::ROLE_USER, 'rolechangedate' => null]);
$userCheck->syncRoles('User');
SendAccountExpiredEmail::dispatch($userCheck);
} else {
if ($emailCheckQuery->isNotEmpty()) {
$booleans = [
'day' => ($period === 1 && $emailCheckQuery->day === false) ? true : $emailCheckQuery->day,
'week' => ($period === 7 && $emailCheckQuery->week === false) ? true : $emailCheckQuery->week,
'month' => ($period === 30 && $emailCheckQuery->month === false) ? true : $emailCheckQuery->month,
];
RoleExpirationEmail::query()->where('users_id', '=', $userCheck->id)->update($booleans);
} else {
$booleans = [
'day' => $period === 1,
'week' => $period === 7,
'month' => $period === 30,
];
$userId = [
'users_id' => $userCheck->id,
];
$booleans += $userId;
RoleExpirationEmail::create($booleans);
}
SendAccountWillExpireEmail::dispatch($userCheck, $period);
foreach ($period as $value) {
$users = self::query()->whereDate('rolechangedate', '=', $value)->get();
$days = $now->diffInDays($value);
foreach ($users as $user) {
SendAccountWillExpireEmail::dispatch($user, $days);
}
}
foreach (self::query()->whereDate('rolechangedate', '<', $now)->get() as $expired) {
$expired->update(['roles_id' => self::ROLE_USER, 'rolechangedate' => null]);
$expired->syncRoles('User');
SendAccountExpiredEmail::dispatch($expired);
}
}
/**