|
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
|
|
|
* Set Session From Login Credentials. |
|
69
|
|
|
* |
|
70
|
|
|
* @param Request $request |
|
71
|
|
|
* |
|
72
|
|
|
* @return UserModel |
|
73
|
|
|
*/ |
|
74
|
|
|
public function loginUser(Request $request) |
|
75
|
|
|
{ |
|
76
|
|
|
$chocolateyId = ChocolateyId::find($request->json()->get('email')); |
|
77
|
|
|
|
|
78
|
|
|
$user = $chocolateyId->last_logged_id == 0 ? UserModel::where('mail', $request->json()->get('email'))->first() : |
|
79
|
|
|
UserModel::find($chocolateyId->last_logged_id); |
|
80
|
|
|
|
|
81
|
|
|
$chocolateyId->last_logged_id = $user->uniqueId; |
|
82
|
|
|
|
|
83
|
|
|
return $chocolateyId->password == hash(Config::get('chocolatey.security.hash'), $request->json()->get('password')) |
|
84
|
|
|
? $this->setSession($user) : null; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Return if USer Session Exists. |
|
89
|
|
|
* |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
public function hasSession() |
|
93
|
|
|
{ |
|
94
|
|
|
return (bool)Session::get(Config::get('chocolatey.security.session')); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Erase User Session. |
|
99
|
|
|
*/ |
|
100
|
|
|
public function eraseSession() |
|
101
|
|
|
{ |
|
102
|
|
|
Session::erase(Config::get('chocolatey.security.session')); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|