1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
use App\Http\Models\Api; |
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Support\Facades\Auth; |
7
|
|
|
use Illuminate\Support\Facades\Input; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class BuildersController. |
12
|
|
|
*/ |
13
|
|
|
class BuildersController extends Controller |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Create account |
18
|
|
|
*/ |
19
|
|
View Code Duplication |
public function createAccount(Request $request) |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$data = [ |
22
|
|
|
"name" => $request->name, |
23
|
|
|
"cp" => $request->cp, |
24
|
|
|
"phone" => $request->phone, |
25
|
|
|
"sexe" => $request->sexe, |
26
|
|
|
"news" => $request->news, |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
$manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017'); |
30
|
|
|
$collection = new \MongoDB\Collection($manager, 'builders', 'account'); |
31
|
|
|
$stat = [ |
32
|
|
|
'email' => $request->email, |
33
|
|
|
'data' => $data, |
34
|
|
|
'created' => new \DateTime("now"), |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
try{ |
38
|
|
|
$collection->insertOne($stat); |
39
|
|
|
}catch (\Exception $e){ |
40
|
|
|
return response()->json(['state' => false]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$data["email"] = $request->email; |
|
|
|
|
44
|
|
|
return response()->json(['data' => $data, 'state' => true]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Auth |
49
|
|
|
*/ |
50
|
|
|
public function connect(Request $request) |
51
|
|
|
{ |
52
|
|
|
$email = $request->email; |
|
|
|
|
53
|
|
|
$password = $request->password; |
|
|
|
|
54
|
|
|
|
55
|
|
|
if (!empty($email) && !empty($password)) { |
56
|
|
|
$manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017'); |
57
|
|
|
$collection = new \MongoDB\Collection($manager, 'builders', 'account'); |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
if ($collection->count(["email" => $email]) == 0) { |
61
|
|
|
return response()->json(['data' => "User doesn't exist", 'state' => false]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$user = $collection->findOne(["email" => $email])->bsonSerialize(); |
65
|
|
|
|
66
|
|
|
if(password_verify($password, $user->password) == false){ |
67
|
|
|
return response()->json(['data' => "Bad email or password", 'state' => false]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$api = new Api($user); |
71
|
|
|
Auth::login($api); |
72
|
|
|
|
73
|
|
|
dump(Auth::user()); |
74
|
|
|
|
75
|
|
|
return response()->json(['data' => $user, 'state' => true]); |
76
|
|
|
|
77
|
|
|
} else { |
78
|
|
|
return response()->json(['data' => "Invalid parameters", 'state' => false]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return response()->json(['data' => "Bad credentials", 'state' => false]); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
/** |
84
|
|
|
* Auth |
85
|
|
|
*/ |
86
|
|
|
public function connectAlreadyExist(Request $request) |
87
|
|
|
{ |
88
|
|
|
$email = $request->email; |
|
|
|
|
89
|
|
|
$password = $request->password; |
|
|
|
|
90
|
|
|
|
91
|
|
|
if (!empty($email) && !empty($password)) { |
92
|
|
|
$auth = auth()->guard('user'); |
93
|
|
|
|
94
|
|
|
$credentials = [ |
95
|
|
|
'email' => $email, |
96
|
|
|
'password' => $password, |
97
|
|
|
'enabled' => true |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
if ($auth->attempt($credentials) && auth()->guard('user')->check()) { |
101
|
|
|
|
102
|
|
|
return response()->json(['data' => auth()->guard('user')->user()->toArray(), 'state' => true]); |
103
|
|
|
} else { |
104
|
|
|
return response()->json(['data' => "Bad email or password", 'state' => false]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
} else { |
108
|
|
|
return response()->json(['data' => "Invalid parameters", 'state' => false]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return response()->json(['data' => "Bad credentials", 'state' => false]); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* List account |
116
|
|
|
*/ |
117
|
|
|
public function listAccount() |
118
|
|
|
{ |
119
|
|
|
$manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017'); |
120
|
|
|
$collection = new \MongoDB\Collection($manager, 'builders', 'account'); |
121
|
|
|
|
122
|
|
|
$result = $collection->find()->toArray(); |
123
|
|
|
|
124
|
|
|
$tab = []; |
125
|
|
|
foreach($result as $one){ |
126
|
|
|
$tab[] = $one->bsonSerialize(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return response()->json($tab); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Update account |
134
|
|
|
*/ |
135
|
|
View Code Duplication |
public function updateAccount(Request $request) |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
$data = [ |
138
|
|
|
"name" => $request->name, |
139
|
|
|
"cp" => $request->cp, |
140
|
|
|
"phone" => $request->phone, |
141
|
|
|
"sexe" => $request->sexe, |
142
|
|
|
"news" => $request->news, |
143
|
|
|
]; |
144
|
|
|
|
145
|
|
|
$manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017'); |
146
|
|
|
$collection = new \MongoDB\Collection($manager, 'builders', 'account'); |
147
|
|
|
$stat = [ |
148
|
|
|
'email' => $request->email, |
149
|
|
|
'data' => $data, |
150
|
|
|
'created' => new \DateTime("now"), |
151
|
|
|
]; |
152
|
|
|
|
153
|
|
|
try{ |
154
|
|
|
$collection->updateOne(["email" => $request->email], $stat); |
155
|
|
|
}catch (\Exception $e){ |
156
|
|
|
return response()->json(['state' => false]); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$data["email"] = $request->email; |
|
|
|
|
160
|
|
|
return response()->json(['data' => $data, 'state' => true]); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.