1 | <?php namespace App\Traits\Users; |
||
7 | trait Activates |
||
8 | { |
||
9 | /** |
||
10 | * @param \App\Models\User $user |
||
11 | * |
||
12 | * @return \App\Models\UserActivation |
||
13 | */ |
||
14 | 1 | protected function create(User $user) |
|
24 | |||
25 | /** |
||
26 | * @param \App\Models\User $user |
||
27 | * @param string|null $code |
||
28 | * |
||
29 | * @return bool |
||
30 | */ |
||
31 | 1 | protected function exists(User $user, $code = null) |
|
32 | { |
||
33 | 1 | $expires = $this->expires(); |
|
34 | 1 | $activation = UserActivation::where('user_id', $user->id)->where('completed', false)->where('created_at', '>', $expires); |
|
35 | 1 | if ($code) { |
|
|
|||
36 | $activation->where('code', $code); |
||
37 | } |
||
38 | |||
39 | 1 | return $activation->first() ? true : false; |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * Returns the expiration date. |
||
44 | * |
||
45 | * @return \Carbon\Carbon |
||
46 | */ |
||
47 | 1 | protected function expires() |
|
53 | |||
54 | /** |
||
55 | * @param \App\Models\User $user |
||
56 | * |
||
57 | * @return bool |
||
58 | */ |
||
59 | protected function completed(User $user) |
||
65 | |||
66 | /** |
||
67 | * @param \App\Models\User $user |
||
68 | * @param string $code |
||
69 | * |
||
70 | * @return bool |
||
71 | */ |
||
72 | public function complete(User $user, $code) |
||
73 | { |
||
74 | $expires = $this->expires(); |
||
75 | $activation = UserActivation::where('user_id', $user->id)->where('code', $code)->where('completed', false)->where('created_at', '>', $expires)->first(); |
||
76 | if ($activation === null) { |
||
77 | return false; |
||
78 | } |
||
79 | $activation->fill([ |
||
80 | 'completed' => true, |
||
81 | 'completed_at' => Carbon::now(), |
||
82 | ]); |
||
83 | $activation->save(); |
||
84 | |||
85 | return true; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Return a random string for an activation code. |
||
90 | * |
||
91 | * @return string |
||
92 | */ |
||
93 | 1 | protected function generateActivationCode() |
|
97 | |||
98 | /** |
||
99 | * Get the e-mail subject line to be used for the reset link email. |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | 1 | protected function getActivationEmailSubject() |
|
107 | } |
||
108 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: