1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Libraries\User;
7
8use App\Libraries\Session\Store as SessionStore;
9use App\Mail\UserForceReactivation;
10use App\Models\UserAccountHistory;
11use App\Models\UserClient;
12use Mail;
13
14class ForceReactivation
15{
16 const INACTIVE = 'inactive';
17 const INACTIVE_DIFFERENT_COUNTRY = 'inactive_different_country';
18
19 private $country;
20 private $reason;
21 private $request;
22 private $user;
23
24 public function __construct($user, $request)
25 {
26 $this->user = $user;
27 $this->request = $request;
28
29 $this->country = request_country($this->request);
30
31 if ($this->user->isInactive()) {
32 if ($this->user->country_acronym !== $this->country) {
33 $this->reason = static::INACTIVE_DIFFERENT_COUNTRY;
34 } elseif ($GLOBALS['cfg']['osu']['user']['inactive_force_password_reset']) {
35 $this->reason = static::INACTIVE;
36 }
37 }
38 }
39
40 public function getReason(): ?string
41 {
42 return $this->reason;
43 }
44
45 public function isRequired()
46 {
47 return $this->reason !== null;
48 }
49
50 public function run()
51 {
52 $userId = $this->user->getKey();
53 $waitingActivation = !present($this->user->user_password);
54
55 $this->addHistoryNote();
56 $this->user->update(['user_password' => '']);
57 SessionStore::batchDelete($userId);
58 UserClient::where('user_id', $userId)->update(['verified' => false]);
59
60 if (!$waitingActivation && is_valid_email_format($this->user->user_email)) {
61 Mail::to($this->user)->send(new UserForceReactivation([
62 'user' => $this->user,
63 'reason' => $this->reason,
64 ]));
65 }
66 }
67
68 private function addHistoryNote()
69 {
70 $message = match ($this->reason) {
71 static::INACTIVE => "First login after {$this->user->user_lastvisit->diffInDays()} days. Forcing password reset.",
72 static::INACTIVE_DIFFERENT_COUNTRY => "First login after {$this->user->user_lastvisit->diffInDays()} days from {$this->country}. Forcing password reset.",
73 default => null,
74 };
75
76 if ($message !== null) {
77 UserAccountHistory::addNote($this->user, $message);
78 }
79 }
80}