Completed
Push — master ( 9ecb5e...97e3cb )
by Sherif
13:30
created

UsersController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace App\Modules\V1\Acl\Http\Controllers;
3
4
use Illuminate\Foundation\Http\FormRequest;
5
use App\Modules\V1\Core\Http\Controllers\BaseApiController;
6
use App\Modules\V1\Acl\Proxy\LoginProxy;
7
use Illuminate\Http\Request;
8
9
class UsersController extends BaseApiController
10
{
11
    /**
12
     * The name of the model that is used by the base api controller 
13
     * to preform actions like (add, edit ... etc).
14
     * @var string
15
     */
16
    protected $model               = 'users';
17
18
    /**
19
     * List of all route actions that the base api controller
20
     * will skip permissions check for them.
21
     * @var array
22
     */
23
    protected $skipPermissionCheck = ['account', 'logout', 'changePassword'];
24
25
    /**
26
     * List of all route actions that the base api controller
27
     * will skip login check for them.
28
     * @var array
29
     */
30
    protected $skipLoginCheck      = ['login', 'loginSocial', 'register', 'sendreset', 'resetpassword', 'refreshtoken'];
31
32
    /**
33
     * The validations rules used by the base api controller
34
     * to check before add.
35
     * @var array
36
     */
37
    protected $validationRules     = [
38
        'name'     => 'nullable|string', 
39
        'email'    => 'required|email|unique:users,email,{id}', 
40
        'password' => 'nullable|min:6'
41
    ];
42
43
    /**
44
     * The loginProxy implementation.
45
     * 
46
     * @var array
47
     */
48
    protected $loginProxy;
49
50
    public function __construct(LoginProxy $loginProxy)
51
    {        
52
        $this->loginProxy = $loginProxy;
0 ignored issues
show
Documentation Bug introduced by
It seems like $loginProxy of type object<App\Modules\V1\Acl\Proxy\LoginProxy> is incompatible with the declared type array of property $loginProxy.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
53
        parent::__construct();
54
    }
55
56
    /**
57
     * Return the logged in user account.
58
     * 
59
     * @return \Illuminate\Http\Response
60
     */
61
    public function account()
62
    {
63
        return \Response::json($this->repo->account($this->relations), 200);
64
    }
65
66
    /**
67
     * Block the user.
68
     *
69
     * @param  integer  $id Id of the user.
70
     * @return \Illuminate\Http\Response
71
     */
72
    public function block($id)
73
    {
74
        return \Response::json($this->repo->block($id), 200);
75
    }
76
77
    /**
78
     * Unblock the user.
79
     *
80
     * @param  integer  $id Id of the user.
81
     * @return \Illuminate\Http\Response
82
     */
83
    public function unblock($id)
84
    {
85
        return \Response::json($this->repo->unblock($id), 200);
86
    }
87
88
    /**
89
     * Logout the user.
90
     * 
91
     * @return \Illuminate\Http\Response
92
     */
93
    public function logout()
94
    {
95
        return \Response::json($this->loginProxy->logout(), 200);
0 ignored issues
show
Bug introduced by
The method logout cannot be called on $this->loginProxy (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
96
    }
97
98
    /**
99
     * Handle a registration request.
100
     *
101
     * @param  \Illuminate\Http\Request  $request
102
     * @return \Illuminate\Http\Response
103
     */
104 View Code Duplication
    public function register(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
105
    {
106
        $this->validate($request, [
107
            'name'     => 'nullable|string', 
108
            'email'    => 'required|email|unique:users,email,{id}', 
109
            'password' => 'required|min:6'
110
            ]);
111
112
        return \Response::json($this->repo->register($request->only('email', 'password')), 200);
113
    }
114
115
    /**
116
     * Handle a login request to the application.
117
     *
118
     * @param  \Illuminate\Http\Request  $request
119
     * @return \Illuminate\Http\Response
120
     */
121
    public function login(Request $request)
122
    {
123
        $this->validate($request, [
124
            'email'    => 'required|email', 
125
            'password' => 'required|min:6',
126
            'admin'    => 'boolean'
127
            ]);
128
129
        return \Response::json($this->loginProxy->login($request->only('email', 'password'), $request->get('admin')), 200);
0 ignored issues
show
Bug introduced by
The method login cannot be called on $this->loginProxy (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
130
    }
131
132
    /**
133
     * Handle a social login request of the none admin to the application.
134
     *
135
     * @param  \Illuminate\Http\Request  $request
136
     * @return \Illuminate\Http\Response
137
     */
138
    public function loginSocial(Request $request)
139
    {
140
        $this->validate($request, [
141
            'auth_code'    => 'required_without:access_token',
142
            'access_token' => 'required_without:auth_code',
143
            'type'         => 'required|in:facebook,google'
144
            ]);
145
146
        return \Response::json($this->repo->loginSocial($request->only('auth_code', 'access_token', 'type')), 200);
147
    }
148
149
    /**
150
     * Assign the given groups to the given user.
151
     *
152
     * @param  \Illuminate\Http\Request  $request
153
     * @return \Illuminate\Http\Response
154
     */
155
    public function assigngroups(Request $request)
156
    {
157
        $this->validate($request, [
158
            'group_ids' => 'required|exists:groups,id', 
159
            'user_id'   => 'required|exists:users,id'
160
            ]);
161
162
        return \Response::json($this->repo->assignGroups($request->get('user_id'), $request->get('group_ids')), 200);
163
    }
164
165
    /**
166
     * Send a reset link to the given user.
167
     *
168
     * @param  \Illuminate\Http\Request  $request
169
     * @return \Illuminate\Http\Response
170
     */
171
    public function sendreset(Request $request)
172
    {
173
        $this->validate($request, ['email' => 'required|email']);
174
175
        return \Response::json($this->repo->sendReset($request->only('email')), 200);
176
    }
177
178
    /**
179
     * Reset the given user's password.
180
     *
181
     * @param  \Illuminate\Http\Request  $request
182
     * @return \Illuminate\Http\Response
183
     */
184
    public function resetpassword(Request $request)
185
    {
186
        $this->validate($request, [
187
            'token'                 => 'required',
188
            'email'                 => 'required|email',
189
            'password'              => 'required|confirmed|min:6',
190
            'password_confirmation' => 'required',
191
        ]);
192
193
        return \Response::json($this->repo->resetPassword($request->only('email', 'password', 'password_confirmation', 'token')), 200);
194
    }
195
196
    /**
197
     * Change the logged in user password.
198
     *
199
     * @param  \Illuminate\Http\Request  $request
200
     * @return \Illuminate\Http\Response
201
     */
202 View Code Duplication
    public function changePassword(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
203
    {
204
        $this->validate($request, [
205
            'old_password'          => 'required',
206
            'password'              => 'required|confirmed|min:6',
207
            'password_confirmation' => 'required',
208
        ]);
209
210
        return \Response::json($this->repo->changePassword($request->only('old_password', 'password', 'password_confirmation')), 200);
211
    }
212
213
    /**
214
     * Refresh the expired login token.
215
     *
216
     * @param  string $refreshToken
217
     * @return \Illuminate\Http\Response
218
     */
219
    public function refreshtoken($refreshToken)
220
    {
221
        return \Response::json($this->loginProxy->refreshtoken($refreshToken), 200);
0 ignored issues
show
Bug introduced by
The method refreshtoken cannot be called on $this->loginProxy (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
222
    }
223
224
    /**
225
     * Paginate all users with inthe given group.
226
     * 
227
     * @param  \Illuminate\Http\Request  $request
228
     * @param  string $groupName The name of the requested group.
229
     * @param  integer $perPage  Number of rows per page default 15.
230
     * @param  string  $sortBy   The name of the column to sort by.
231
     * @param  boolean $desc     Sort ascending or descinding (1: desc, 0: asc).
232
     * @return \Illuminate\Http\Response
233
     */
234
    public function group(Request $request, $groupName, $perPage = false, $sortBy = 'created_at', $desc = 1)
235
    {
236
        return \Response::json($this->repo->group($request->all(), $groupName, $this->relations, $perPage, $sortBy, $desc), 200);
237
    }
238
239
    /**
240
     * Save the given data to the logged in user.
241
     *
242
     * @param  \Illuminate\Http\Request  $request
243
     * @return \Illuminate\Http\Response
244
     */
245 View Code Duplication
    public function saveProfile(Request $request) 
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
246
    {
247
        foreach ($this->validationRules as &$rule) 
248
        {
249
            if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) 
250
            {
251
                $rule .= ',deleted_at,NULL';
252
            }
253
254
            if ($request->has('id')) 
255
            {
256
                $rule = str_replace('{id}', $request->get('id'), $rule);
257
            }
258
            else
259
            {
260
                $rule = str_replace(',{id}', '', $rule);
261
            }
262
        }
263
264
        $this->validate($request, $this->validationRules);
265
266
        if ($this->model)
267
        {
268
            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->saveProfile($request->all()), 200);
269
        }
270
    }
271
}
272