|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Facades\Session; |
|
|
|
|
|
|
6
|
|
|
use App\Models\ChocolateyId; |
|
7
|
|
|
use App\Models\User as UserModel; |
|
8
|
|
|
use App\Singleton; |
|
9
|
|
|
use Illuminate\Http\Request; |
|
10
|
|
|
use Illuminate\Support\Facades\Config; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class User. |
|
14
|
|
|
*/ |
|
15
|
|
|
final class User extends Singleton |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Update User Data without overwriting Session. |
|
19
|
|
|
* |
|
20
|
|
|
* @param array $parameters |
|
21
|
|
|
* |
|
22
|
|
|
* @return UserModel |
|
23
|
|
|
*/ |
|
24
|
|
|
public function updateSession(array $parameters) |
|
25
|
|
|
{ |
|
26
|
|
|
return $this->setSession($this->updateUser($this->getUser(), $parameters)); |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Set User Data on Session. |
|
31
|
|
|
* |
|
32
|
|
|
* @param UserModel $user |
|
33
|
|
|
* |
|
34
|
|
|
* @return UserModel |
|
35
|
|
|
*/ |
|
36
|
|
|
public function setSession(UserModel $user) |
|
37
|
|
|
{ |
|
38
|
|
|
return Session::set(Config::get('chocolatey.security.session'), $user); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Update User Data by User Model. |
|
43
|
|
|
* |
|
44
|
|
|
* @param UserModel $user |
|
45
|
|
|
* @param array $parameters |
|
46
|
|
|
* |
|
47
|
|
|
* @return UserModel |
|
48
|
|
|
*/ |
|
49
|
|
|
public function updateUser($user, array $parameters) |
|
50
|
|
|
{ |
|
51
|
|
|
$user->update($parameters); |
|
52
|
|
|
|
|
53
|
|
|
return $user; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get User Data from Session |
|
58
|
|
|
* If User Session doesn't exists, return null. |
|
59
|
|
|
* |
|
60
|
|
|
* @return UserModel|null |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getUser() |
|
63
|
|
|
{ |
|
64
|
|
|
return Session::get(Config::get('chocolatey.security.session')) ?? null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Retrieve Non Banned Users (If all Users are Banned, return the Banned user Also) |
|
69
|
|
|
* |
|
70
|
|
|
* @param Request $request |
|
71
|
|
|
* @param ChocolateyId $chocolateyId |
|
72
|
|
|
* |
|
73
|
|
|
* @return UserModel |
|
74
|
|
|
*/ |
|
75
|
|
|
private function checkForBanAlternative(Request $request, ChocolateyId $chocolateyId) { |
|
|
|
|
|
|
76
|
|
|
$temporaryUsers = UserModel::where('mail', $request->json()->get('email'))->get(); |
|
77
|
|
|
|
|
78
|
|
|
foreach($temporaryUsers as $forUser) { |
|
79
|
|
|
if(!$forUser->isBanned()) { |
|
80
|
|
|
return $forUser; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $temporaryUsers->get(0); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get Users |
|
89
|
|
|
* |
|
90
|
|
|
* @param Request $request |
|
91
|
|
|
* @param ChocolateyId $chocolateyId |
|
92
|
|
|
* |
|
93
|
|
|
* @return UserModel |
|
94
|
|
|
*/ |
|
95
|
|
|
private function retrieveUser(Request $request, ChocolateyId $chocolateyId) { |
|
96
|
|
|
if($chocolateyId->last_logged_id != 0) { |
|
97
|
|
|
$temporaryUser = UserModel::find($chocolateyId->last_logged_id); |
|
98
|
|
|
|
|
99
|
|
|
if($temporaryUser->isBanned()) { |
|
100
|
|
|
return $this->checkForBanAlternative($request, $chocolateyId); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $temporaryUser; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$temporaryUser = UserModel::where('mail', $request->json()->get('email'))->first(); |
|
107
|
|
|
|
|
108
|
|
|
if($temporaryUser->isBanned()) { |
|
109
|
|
|
return $this->checkForBanAlternative($request, $chocolateyId); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $temporaryUser; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Set Session From Login Credentials. |
|
117
|
|
|
* |
|
118
|
|
|
* @param Request $request |
|
119
|
|
|
* |
|
120
|
|
|
* @return UserModel |
|
121
|
|
|
*/ |
|
122
|
|
|
public function loginUser(Request $request) |
|
123
|
|
|
{ |
|
124
|
|
|
$chocolateyId = ChocolateyId::find($request->json()->get('email')); |
|
125
|
|
|
|
|
126
|
|
|
if ($chocolateyId == null) { |
|
127
|
|
|
return null; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$user = $this->retrieveUser($request, $chocolateyId); |
|
131
|
|
|
|
|
132
|
|
|
$chocolateyId->last_logged_id = $user->uniqueId; |
|
133
|
|
|
|
|
134
|
|
|
return $chocolateyId->password == hash(Config::get('chocolatey.security.hash'), $request->json()->get('password')) |
|
135
|
|
|
? $this->setSession($user) : null; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Return if USer Session Exists. |
|
140
|
|
|
* |
|
141
|
|
|
* @return bool |
|
142
|
|
|
*/ |
|
143
|
|
|
public function hasSession() |
|
144
|
|
|
{ |
|
145
|
|
|
return (bool) Session::get(Config::get('chocolatey.security.session')); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Erase User Session. |
|
150
|
|
|
*/ |
|
151
|
|
|
public function eraseSession() |
|
152
|
|
|
{ |
|
153
|
|
|
Session::erase(Config::get('chocolatey.security.session')); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: