1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\Base\app\Http\Controllers\Auth; |
4
|
|
|
|
5
|
|
|
use Alert; |
6
|
|
|
use Backpack\Base\app\Http\Controllers\Controller; |
7
|
|
|
use Backpack\Base\app\Http\Requests\AccountInfoRequest; |
8
|
|
|
use Backpack\Base\app\Http\Requests\ChangePasswordRequest; |
9
|
|
|
use Illuminate\Support\Facades\Hash; |
10
|
|
|
|
11
|
|
|
class MyAccountController extends Controller |
12
|
|
|
{ |
13
|
|
|
protected $data = []; |
14
|
|
|
|
15
|
|
|
public function __construct() |
16
|
|
|
{ |
17
|
|
|
$this->middleware(backpack_middleware()); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Show the user a form to change his personal information. |
22
|
|
|
*/ |
23
|
|
View Code Duplication |
public function getAccountInfoForm() |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
$this->data['title'] = trans('backpack::base.my_account'); |
26
|
|
|
$this->data['user'] = $this->guard()->user(); |
27
|
|
|
|
28
|
|
|
return view('backpack::auth.account.update_info', $this->data); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Save the modified personal information for a user. |
33
|
|
|
*/ |
34
|
|
|
public function postAccountInfoForm(AccountInfoRequest $request) |
35
|
|
|
{ |
36
|
|
|
$result = $this->guard()->user()->update($request->except(['_token'])); |
|
|
|
|
37
|
|
|
|
38
|
|
|
if ($result) { |
39
|
|
|
Alert::success(trans('backpack::base.account_updated'))->flash(); |
40
|
|
|
} else { |
41
|
|
|
Alert::error(trans('backpack::base.error_saving'))->flash(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return redirect()->back(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Show the user a form to change his login password. |
49
|
|
|
*/ |
50
|
|
View Code Duplication |
public function getChangePasswordForm() |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$this->data['title'] = trans('backpack::base.my_account'); |
53
|
|
|
$this->data['user'] = $this->guard()->user(); |
54
|
|
|
|
55
|
|
|
return view('backpack::auth.account.change_password', $this->data); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Save the new password for a user. |
60
|
|
|
*/ |
61
|
|
|
public function postChangePasswordForm(ChangePasswordRequest $request) |
62
|
|
|
{ |
63
|
|
|
$user = $this->guard()->user(); |
64
|
|
|
$user->password = Hash::make($request->new_password); |
|
|
|
|
65
|
|
|
|
66
|
|
|
if ($user->save()) { |
67
|
|
|
Alert::success(trans('backpack::base.account_updated'))->flash(); |
68
|
|
|
} else { |
69
|
|
|
Alert::error(trans('backpack::base.error_saving'))->flash(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return redirect()->back(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the guard to be used for account manipulation. |
77
|
|
|
* |
78
|
|
|
* @return \Illuminate\Contracts\Auth\StatefulGuard |
79
|
|
|
*/ |
80
|
|
|
protected function guard() |
81
|
|
|
{ |
82
|
|
|
return backpack_auth(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
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.