/
ProfileController.php
310 lines (267 loc) · 8.04 KB
/
ProfileController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
namespace REBELinBLUE\Deployer\Http\Controllers;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Routing\UrlGenerator;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Intervention\Image\ImageManager;
use MicheleAngioni\MultiLanguage\LanguageManager;
use PragmaRX\Google2FA\Contracts\Google2FA as Google2FA;
use REBELinBLUE\Deployer\Events\EmailChangeRequested;
use REBELinBLUE\Deployer\Http\Requests\StoreProfileRequest;
use REBELinBLUE\Deployer\Http\Requests\StoreSettingsRequest;
use REBELinBLUE\Deployer\Repositories\Contracts\UserRepositoryInterface;
use REBELinBLUE\Deployer\Settings;
/**
* The use profile controller.
*/
class ProfileController extends Controller
{
/**
* @var UserRepositoryInterface
*/
private $repository;
/**
* @var Google2FA
*/
private $google2fa;
/**
* @var ViewFactory
*/
private $view;
/**
* @var Translator
*/
private $translator;
/**
* @var Redirector
*/
private $redirect;
/**
* @var Guard
*/
private $auth;
/**
* ProfileController constructor.
*
* @param UserRepositoryInterface $repository
* @param Google2FA $google2fa
* @param ViewFactory $view
* @param Translator $translator
* @param Redirector $redirector
* @param Guard $auth
*/
public function __construct(
UserRepositoryInterface $repository,
Google2FA $google2fa,
ViewFactory $view,
Translator $translator,
Redirector $redirector,
Guard $auth
) {
$this->repository = $repository;
$this->google2fa = $google2fa;
$this->view = $view;
$this->translator = $translator;
$this->redirect = $redirector;
$this->auth = $auth;
}
/**
* View user profile.
*
* @param Request $request
*
* @param LanguageManager $languageManager
* @param Settings $settings
* @return \Illuminate\View\View
*/
public function index(Request $request, LanguageManager $languageManager, Settings $settings)
{
$user = $this->auth->user();
$code = $this->google2fa->generateSecretKey();
if ($user->has_two_factor_authentication || $request->old('google_code')) {
$code = $request->old('google_code', $user->google2fa_secret);
}
$img = $this->google2fa->getQRCodeGoogleUrl('Deployer', $user->email, $code);
return $this->view->make('user.profile', [
'google_2fa_url' => $img,
'google_2fa_code' => $code,
'title' => $this->translator->trans('users.update_profile'),
'locales' => $languageManager->getAvailableLanguages(),
'settings' => $settings,
]);
}
/**
* Update user's basic profile.
*
* @param StoreProfileRequest $request
*
* @return \Illuminate\Http\RedirectResponse
*/
public function update(StoreProfileRequest $request)
{
$this->repository->updateById($request->only(
'name',
'password'
), $this->auth->id());
return $this->redirect->to('/');
}
/**
* Update user's settings.
*
* @param StoreSettingsRequest $request
*
* @return \Illuminate\Http\RedirectResponse
*/
public function settings(StoreSettingsRequest $request)
{
$this->repository->updateById($request->only(
'skin',
'scheme',
'language'
), $this->auth->id());
return $this->redirect->to('/');
}
/**
* Send email to change a new email.
*
* @param Dispatcher $dispatcher
* @return string
*/
public function requestEmail(Dispatcher $dispatcher)
{
$dispatcher->dispatch(new EmailChangeRequested($this->auth->user()));
return 'success';
}
/**
* Show the page to input the new email.
*
* @param string $token
*
* @return \Illuminate\View\View
*/
public function email($token)
{
return $this->view->make('user.change-email', [
'token' => $token,
]);
}
/**
* Change the user's email.
*
* @param Request $request
*
* @return \Illuminate\Http\RedirectResponse
*/
public function changeEmail(Request $request)
{
$user = $this->repository->findByEmailToken($request->get('token'));
if ($request->get('email')) {
$user->email = $request->get('email');
$user->email_token = '';
$user->save();
}
return $this->redirect->to('/');
}
/**
* Upload file.
*
* @param Request $request
*
* @param UrlGenerator $url
* @return array|string
*/
public function upload(Request $request, UrlGenerator $url)
{
$this->validate($request, [
'file' => 'required|image',
]);
if ($request->hasFile('file') && $request->file('file')->isValid()) {
$file = $request->file('file');
$path = '/storage/' . date('Y-m-d');
$destinationPath = public_path() . $path;
$filename = uniqid() . '.' . $file->getClientOriginalExtension();
$file->move($destinationPath, $filename);
return [
'image' => $url->to($path . '/' . $filename),
'path' => $path . '/' . $filename,
'message' => 'success',
];
}
return 'failed';
}
/**
* Reset the user's avatar to gravatar.
*
* @return array
*/
public function gravatar()
{
$user = $this->auth->user();
$user->avatar = null;
$user->save();
return [
'image' => $user->avatar_url,
'success' => true,
];
}
/**
* Set and crop the avatar.
*
* @param Request $request
* @param UrlGenerator $url
* @param ImageManager $image
*
* @return array
*/
public function avatar(Request $request, UrlGenerator $url, ImageManager $image)
{
$path = $request->get('path', '/placeholder.jpg');
$image = $image->make(public_path() . $path);
$rotate = $request->get('dataRotate');
if ($rotate) {
$image->rotate($rotate);
}
$width = $request->get('dataWidth');
$height = $request->get('dataHeight');
$left = $request->get('dataX');
$top = $request->get('dataY');
$image->crop($width, $height, $left, $top);
$path = '/storage/' . date('Y-m-d') . '/avatar' . uniqid() . '.jpg';
$image->save(public_path() . $path);
$user = $this->auth->user();
$user->avatar = $path;
$user->save();
return [
'image' => $url->to($path),
'success' => true,
];
}
/**
* Activates two factor authentication.
*
* @param Request $request
*
* @return \Illuminate\Http\RedirectResponse
*/
public function twoFactor(Request $request)
{
$secret = null;
if ($request->has('two_factor')) {
$secret = $request->get('google_code');
if (!$this->google2fa->verifyKey($secret, $request->get('2fa_code'))) {
$secret = null;
return $this->redirect->back()
->withInput($request->only('google_code', 'two_factor'))
->withError($this->translator->trans('auth.invalid_code'));
}
}
$user = $this->auth->user();
$user->google2fa_secret = $secret;
$user->save();
return $this->redirect->to('/');
}
}