|
2 | 2 |
|
3 | 3 | namespace SecTheater\Jarvis\Activation;
|
4 | 4 |
|
| 5 | +use SecTheater\Jarvis\Interfaces\TokenInterface; |
5 | 6 | use SecTheater\Jarvis\Interfaces\RestrictionInterface;
|
6 | 7 | use SecTheater\Jarvis\Repositories\Repository;
|
| 8 | +use SecTheater\Jarvis\Traits\IssueTokens; |
7 | 9 |
|
8 |
| -class ActivationRepository extends Repository implements ActivationInterface |
| 10 | +class ActivationRepository extends Repository implements TokenInterface |
9 | 11 | {
|
| 12 | + use IssueTokens; |
10 | 13 | protected $model;
|
11 |
| - |
| 14 | + protected $process = 'activation'; |
12 | 15 | public function __construct(EloquentActivation $model)
|
13 | 16 | {
|
14 | 17 | $this->model = $model;
|
15 | 18 | }
|
16 | 19 |
|
17 |
| - public function tokenExists(RestrictionInterface $user, bool $create = false) |
18 |
| - { |
19 |
| - if ($create) { |
20 |
| - if (!$this->model->where('user_id', $user->id)->exists()) { |
21 |
| - return $this->generateToken($user); |
22 |
| - } |
23 |
| - } |
24 |
| - |
25 |
| - return ($this->model->where('user_id', $user->id)->exists()) ? $this->model->where('user_id', $user->id)->first() : false; |
26 |
| - } |
27 |
| - |
28 |
| - public function completed(RestrictionInterface $user) |
29 |
| - { |
30 |
| - if ($activation = $this->tokenExists($user)) { |
31 |
| - return $activation->completed; |
32 |
| - } |
33 |
| - |
34 |
| - return false; |
35 |
| - } |
36 |
| - |
37 |
| - public function complete(RestrictionInterface $user) |
38 |
| - { |
39 |
| - $activation = $this->tokenExists($user); |
40 |
| - if ($activation && $activation->token !== null) { |
41 |
| - return (bool) $activation->update([ |
42 |
| - 'token' => null, |
43 |
| - 'user_id' => $user->id, |
44 |
| - 'completed' => true, |
45 |
| - 'completed_at' => date('Y-m-d H:i:s'), |
46 |
| - ]); |
47 |
| - } elseif ($activation && $activation->completed === true) { |
48 |
| - return true; |
49 |
| - } |
50 |
| - |
51 |
| - return false; |
52 |
| - } |
53 |
| - |
54 |
| - public function clear(bool $completed = false):bool |
55 |
| - { |
56 |
| - return (bool) $this->model->where('completed', $completed)->delete(); |
57 |
| - } |
58 |
| - |
59 |
| - public function clearFor(RestrictionInterface $user, bool $completed = false, bool $any = false):bool |
60 |
| - { |
61 |
| - if ($any) { |
62 |
| - return (bool) $this->model->where(['user_id' => $user->id])->delete(); |
63 |
| - } |
64 |
| - |
65 |
| - return (bool) $this->model->where(['user_id' => $user->id, 'completed' => $completed])->delete(); |
66 |
| - } |
67 |
| - |
68 |
| - public function generateToken(RestrictionInterface $user) |
69 |
| - { |
70 |
| - if ($activation = $this->tokenExists($user)) { |
71 |
| - return $activation; |
72 |
| - } |
73 |
| - |
74 |
| - return $this->create([ |
75 |
| - 'token' => str_random(32), |
76 |
| - 'user_id' => $user->id, |
77 |
| - 'completed' => false, |
78 |
| - 'created_at' => date('Y-m-d H:i:s'), |
79 |
| - 'updated_at' => null, |
80 |
| - ]); |
81 |
| - } |
82 | 20 |
|
83 |
| - public function regenerateToken(RestrictionInterface $user, bool $create = false) |
84 |
| - { |
85 |
| - if ($activation = $this->tokenExists($user)) { |
86 |
| - $activation->update([ |
87 |
| - 'token' => str_random(32), |
88 |
| - 'completed' => false, |
89 |
| - 'completed_at' => null, |
90 |
| - 'updated_at' => date('Y-m-d H:i:s'), |
91 |
| - ]); |
92 |
| - |
93 |
| - return $activation; |
94 |
| - } |
95 |
| - if ($create) { |
96 |
| - return $this->create([ |
97 |
| - 'token' => str_random(32), |
98 |
| - 'user_id' => $user->id, |
99 |
| - 'completed' => false, |
100 |
| - 'created_at' => date('Y-m-d H:i:s'), |
101 |
| - 'updated_at' => null, |
102 |
| - ]); |
103 |
| - } |
104 |
| - |
105 |
| - return false; |
106 |
| - } |
107 |
| - |
108 |
| - public function removeExpired() |
109 |
| - { |
110 |
| - return (bool) $this->model->where(['completed' => false, ['created_at', '>=', \Carbon\Carbon::now()->subDays(config('jarvis.activations.expiration'))->format('Y-m-d H:i:s')]])->delete(); |
111 |
| - } |
112 | 21 | }
|
0 commit comments