Completed
Push — master ( f2e185...41b738 )
by Jonathan
18:00 queued 09:08
created

AccountController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Uccello\Core\Http\Controllers\User;
4
5
use Uccello\Core\Http\Controllers\Core\Controller;
6
use Uccello\Core\Models\Domain;
7
use Uccello\Core\Models\Module;
8
use Uccello\Core\Models\Field;
9
use Illuminate\Support\Facades\Validator;
10
11
class AccountController extends Controller
12
{
13
    /**
14
     * @inheritDoc
15
     */
16
    public function __construct()
17
    {
18
        parent::__construct();
19
20
        $this->module = ucmodule('user');
21
    }
22
23
    /**
24
     * Display user account page
25
     *
26
     * @return \Illuminate\Http\Response
27
     */
28
    public function index(?Domain $domain, Module $module)
29
    {
30
        $this->preProcess($domain, $module, request());
31
32
        $this->viewName = 'account.main';
33
34
        $user = auth()->user();
35
36
        return $this->autoView(compact('user'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->autoView(compact('user')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
37
    }
38
39
    /**
40
     * Update user profile
41
     *
42
     * @return \Illuminate\Http\Response
43
     */
44
    public function updateProfile(?Domain $domain, Module $module)
45
    {
46
        $this->preProcess($domain, $module, request());
47
48
        $user = auth()->user();
49
50
        $rules = Field::where('module_id', ucmodule('user')->id)->pluck('data', 'name')->map(function($item, $key) use($user) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

50
        $rules = Field::where('module_id', ucmodule('user')->id)->pluck('data', 'name')->map(function($item, /** @scrutinizer ignore-unused */ $key) use($user) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
            return isset($item->rules) ? explode('|', str_replace('%id%', $user->id, $item->rules)) : '';
52
        });
53
54
        $validator = Validator::make(request()->all(), [
55
            'username' => $rules['username'],
56
            'name' => $rules['name'],
57
            'email' => $rules['email'],
58
        ]);
59
60
        if ($validator->fails()) {
61
            ucnotify(uctrans('notification.form.not_valid', $module), 'error');
62
63
            return redirect(ucroute('uccello.user.account', $domain))
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(ucroute(...'form_name', 'profile') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
64
                        ->withErrors($validator)
65
                        ->withInput()
66
                        ->with('form_name', 'profile');
67
        }
68
69
        $user->username = request('username');
70
        $user->name = request('name');
71
        $user->email = request('email');
72
        $user->save();
73
74
        ucnotify(uctrans('success.profile_updated', ucmodule('user')), 'success');
75
76
        return redirect(ucroute('uccello.user.account', $domain));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(ucroute(...ser.account', $domain)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
77
    }
78
79
    /**
80
     * Update user password
81
     *
82
     * @return \Illuminate\Http\Response
83
     */
84
    public function updatePassword(?Domain $domain, Module $module)
85
    {
86
        $this->preProcess($domain, $module, request());
87
88
        $user = auth()->user();
89
90
        $field = Field::where('module_id', ucmodule('user')->id)->where('name', 'password')->first();
91
        $password_rules = isset($field->data->rules) ? explode('|', $field->data->rules) : '';
0 ignored issues
show
Bug introduced by
The property data does not seem to exist on Uccello\Core\Models\Field. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
92
        $password_rules[] = 'confirmed';
93
94
        $validator = Validator::make(request()->all(), [
95
            'current_password' => function($attribute, $value, $fail) use ($user) {
96
                if (!\Hash::check($value, $user->password)) {
97
                    $fail(uctrans('error.current_password', ucmodule('user')));
98
                }
99
            },
100
            'password' => $password_rules,
101
        ]);
102
103
        if ($validator->fails()) {
104
            ucnotify(uctrans('notification.form.not_valid', $module), 'error');
105
106
            return redirect(ucroute('uccello.user.account', $domain))
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(ucroute(...form_name', 'password') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
107
                        ->withErrors($validator)
108
                        ->withInput()
109
                        ->with('form_name', 'password');
110
        }
111
112
        $user->password = \Hash::make(request('password'));
0 ignored issues
show
Bug introduced by
It seems like request('password') can also be of type array; however, parameter $value of Illuminate\Support\Facades\Hash::make() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
        $user->password = \Hash::make(/** @scrutinizer ignore-type */ request('password'));
Loading history...
113
        $user->save();
114
115
        ucnotify(uctrans('success.password_updated', ucmodule('user')), 'success');
116
117
        return redirect(ucroute('uccello.user.account', $domain));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(ucroute(...ser.account', $domain)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
118
    }
119
}