Completed
Push — master ( ab60f2...c901a1 )
by Ricardo
07:00
created

UserController::sendMailForResetpwd()   C

Complexity

Conditions 16
Paths 27

Size

Total Lines 68

Duplication

Lines 12
Ratio 17.65 %

Importance

Changes 0
Metric Value
dl 12
loc 68
rs 5.5666
c 0
b 0
f 0
cc 16
nc 27
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Fabrica\Http\Api;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Event;
7
8
use Fabrica\Http\Requests;
9
use Fabrica\Http\Api\Controller;
10
use Fabrica\Events\DelUserEvent;
11
use Fabrica\Acl\Eloquent\Group;
12
13
use Fabrica\ActiveDirectory\Eloquent\Directory;
14
15
use Maatwebsite\Excel\Facades\Excel;
16
use Cartalyst\Sentinel\Users\EloquentUser;
17
use Sentinel;
18
use Activation; 
19
20
use Fabrica\System\Eloquent\SysSetting;
21
use Fabrica\System\Eloquent\ResetPwdCode;
22
use Mail;
23
use Config;
24
25
class UserController extends Controller
26
{
27
    use ExcelTrait;
28
29
    public function __construct()
30
    {
31
        $this->middleware('privilege:sys_admin', [ 'except' => [ 'login', 'register', 'search', 'show', 'sendMailForResetpwd', 'showResetpwd', 'doResetpwd' ] ]);
32
        parent::__construct();
33
    }
34
35
    /**
36
     * user login.
37
     *
38
     * @return \Illuminate\Http\Response
39
     */
40
    public function login(Request $request)
41
    {
42
        $email = $request->input('email');
43
        $password = $request->input('password');
44
        if (!$email || !$password)
45
        {
46
            throw new \UnexpectedValueException('email or password cannot be empty.', -10003);
47
        }
48
49
        if (strpos($email, '@') === false)
50
        {
51
            $setting = SysSetting::first();
52 View Code Duplication
            if ($setting && isset($setting->properties) && isset($setting->properties['login_mail_domain']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
53
            {
54
                $email = $email . '@' . $setting->properties['login_mail_domain'];
55
            }
56
        }
57
58
        $user = Sentinel::authenticate([ 'email' => $email, 'password' => $password ]);
59 View Code Duplication
        if ($user)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
60
        {
61
            return Response()->json([ 'ecode' => 0, 'data' => $user ]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
62
        }
63
        else
64
        {
65
            return Response()->json([ 'ecode' => -10000, 'data' => [] ]);
66
        }
67
    }
68
69
    /**
70
     * Display a listing of the resource.
71
     *
72
     * @return \Illuminate\Http\Response
73
     */
74
    public function search(Request $request)
75
    {
76
        $s = $request->input('s');
77
        $users = [];
78
        if ($s)
79
        {
80
            $search_users = EloquentUser::Where('first_name', 'like', '%' . $s .  '%')
81
                                ->orWhere('email', 'like', '%' . $s .  '%')
82
                                ->get([ 'first_name', 'last_name', 'email', 'invalid_flag' ]);
83
84
            $i = 0;
85
            foreach ($search_users as $key => $user)
86
            {
87
                if ((isset($user->invalid_flag) && $user->invalid_flag === 1) || Activation::completed($user) === false || $user->email === '[email protected]')
88
                {
89
                    continue;
90
                }
91
92
                $users[$i]['id'] = $user->id;
93
                $users[$i]['name'] = $user->first_name ?: '';
94
                $users[$i]['email'] = $user->email;
95
                if (++$i >= 10)
96
                {
97
                    break;
98
                }
99
            }
100
        }
101
        return Response()->json([ 'ecode' => 0, 'data' => $users ]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
102
    }
103
104
    /**
105
     * Display a listing of the resource.
106
     *
107
     * @return \Illuminate\Http\Response
108
     */
109
    public function index(Request $request)
110
    {
111
        $query = EloquentUser::where('email', '<>', '')->where('email', '<>', '[email protected]');
112
113
        if ($name = $request->input('name'))
114
        {
115
            $query->where(function ($query) use ($name) {
116
                $query->where('email', 'like', '%' . $name . '%')->orWhere('name', 'like', '%' . $name . '%');
117
            });
118
        }
119
120
        if ($group_id = $request->input('group'))
121
        {
122
            $group = Group::find($group_id);
123
            if ($group)
124
            {
125
                $query->whereIn('_id', $group->users ?: []);
126
            }
127
        }
128
129
        if ($directory = $request->input('directory'))
130
        {
131
            $query->where('directory', $directory);
132
        }
133
134
        // get total
135
        $total = $query->count();
136
137
        $query->orderBy('_id', 'asc');
138
139
        $page_size = 50;
140
        $page = $request->input('page') ?: 1;
141
        $query = $query->skip($page_size * ($page - 1))->take($page_size);
142
        $all_users = $query->get([ 'first_name', 'last_name', 'email', 'phone', 'directory', 'invalid_flag' ]);
143
144
        $users = [];
145
        foreach ($all_users as $user)
146
        {
147
            $tmp = [];
148
            $tmp['id'] = $user->id;
149
            $tmp['first_name'] = $user->first_name;
150
            $tmp['email'] = $user->email;
151
            $tmp['phone'] = $user->phone ?: '';
152
            $tmp['groups'] = array_column(Group::whereRaw([ 'users' => $user->id ])->get([ 'name' ])->toArray() ?: [], 'name');
153
            $tmp['directory'] = $user->directory ?: 'self';
154
            $tmp['status'] = $user->invalid_flag === 1 ? 'invalid' : (Activation::completed($user) ? 'active' : 'unactivated');
155
156
            $users[] = $tmp;
157
        }
158
        return Response()->json([ 'ecode' => 0, 'data' => $users, 'options' => [ 'total' => $total, 'sizePerPage' => $page_size, 'groups' => Group::all(), 'directories' => Directory::all() ] ]); 
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
159
    }
160
161
    /**
162
     * Store a newly created resource in storage.
163
     *
164
     * @param  \Illuminate\Http\Request  $request
165
     * @return \Illuminate\Http\Response
166
     */
167
    public function register(Request $request)
168
    {
169
        if (!($first_name = $request->input('first_name')))
170
        {
171
            throw new \UnexpectedValueException('the name can not be empty.', -10100);
172
        }
173
174
        if (!($email = $request->input('email')))
175
        {
176
            throw new \UnexpectedValueException('the email can not be empty.', -10101);
177
        }
178
179
        if (Sentinel::findByCredentials([ 'email' => $email ]))
180
        {
181
            throw new \InvalidArgumentException('the email has already been registered.', -10102);
182
        }
183
184
        if (!$password = $request->input('password'))
185
        {
186
            throw new \UnexpectedValueException('the password can not be empty.', -10103);
187
        }
188
189
        $user = Sentinel::register([ 'first_name' => $first_name, 'email' => $email, 'password' => $password ], true);
190
        return Response()->json([ 'ecode' => 0, 'data' => $user ]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
191
    }
192
193
    /**
194
     * Store a newly created resource in storage.
195
     *
196
     * @param  \Illuminate\Http\Request  $request
197
     * @return \Illuminate\Http\Response
198
     */
199
    public function store(Request $request)
200
    {
201
        if (!($first_name = $request->input('first_name')))
202
        {
203
            throw new \UnexpectedValueException('the name can not be empty.', -10100);
204
        }
205
206
        if (!($email = $request->input('email')))
207
        {
208
            throw new \UnexpectedValueException('the email can not be empty.', -10101);
209
        }
210
211
        if (Sentinel::findByCredentials([ 'email' => $email ]))
212
        {
213
            throw new \InvalidArgumentException('email has already existed.', -10102);
214
        }
215
216
        $phone = $request->input('phone') ? $request->input('phone') : '';
217
218
        $user = Sentinel::register([ 'first_name' => $first_name, 'email' => $email, 'password' => 'actionview', 'phone' => $phone ], true);
219
        $user->status = Activation::completed($user) ? 'active' : 'unactivated';
220
221
        return Response()->json([ 'ecode' => 0, 'data' => $user ]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
222
    }
223
224
    /**
225
     * import the users.
226
     *
227
     * @param  \Illuminate\Http\Request  $request
228
     * @return \Illuminate\Http\Response
229
     */
230
    public function imports(Request $request)
231
    {
232
        if (!($fid = $request->input('fid')))
233
        {
234
            throw new \UnexpectedValueException('the user file ID can not be empty.', -11140);
235
        }
236
237
        $pattern = $request->input('pattern');
238
        if (!isset($pattern))
239
        {
240
            $pattern = '1';
241
        }
242
243
        $file = config('filesystems.disks.local.root', '/tmp') . '/' . substr($fid, 0, 2) . '/' . $fid;
244
        if (!file_exists($file))
245
        {
246
            throw new \UnexpectedValueException('the file cannot be found.', -11141);
247
        }
248
249
        Excel::load($file, function($reader) use($pattern) {
250
            $reader = $reader->getSheet(0);
251
            $data = $reader->toArray();
252
253
            $fields = [ 'first_name' => '姓名', 'email' => '邮箱', 'phone' => '手机号' ];
254
            $data = $this->arrangeExcel($data, $fields);
255
256
            foreach ($data as $value) 
257
            {
258
                if (!isset($value['first_name']) || !$value['first_name'])
259
                {
260
                    throw new \UnexpectedValueException('there is empty value in the name column', -10110);
261
                }
262
263
                if (!isset($value['email']) || !$value['email'])
264
                {
265
                    throw new \UnexpectedValueException('there is empty value in the email column', -10111);
266
                }
267
            }
268
269
            foreach ($data as $value)
270
            {
271
                $old_user = Sentinel::findByCredentials([ 'email' => $value['email'] ]);
272
                if ($old_user)
273
                {
274
                    if ($pattern == '1')
275
                    {
276
                        continue;
277
                    }
278
                    else
279
                    {
280
                        Sentinel::update($old_user, $value + [ 'password' => 'actionview' ]); 
281
                    }
282
283
                }
284
                else
285
                {
286
                    Sentinel::register($value + [ 'password' => 'actionview' ], true);
287
                }
288
            }
289
        });
290
291
        return Response()->json([ 'ecode' => 0, 'data' => [ 'ok' => true ] ]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
292
    }
293
294
    /**
295
     * Display the specified resource.
296
     *
297
     * @param  int  $id
298
     * @return \Illuminate\Http\Response
299
     */
300
    public function show($id)
301
    {
302
        return Response()->json([ 'ecode' => 0, 'data' => Sentinel::findById($id) ]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
303
    }
304
305
    /**
306
     * Update the specified resource in storage.
307
     *
308
     * @param  \Illuminate\Http\Request  $request
309
     * @param  int  $id
310
     * @return \Illuminate\Http\Response
311
     */
312
    public function update(Request $request, $id)
313
    {
314
        $first_name = $request->input('first_name');
315
        if (isset($first_name))
316
        {
317
            if (!$first_name)
318
            {
319
                throw new \UnexpectedValueException('the name can not be empty.', -10100);
320
            }
321
        }
322
323
        $email = $request->input('email');
324 View Code Duplication
        if (isset($email))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
325
        {
326
            if (!$email)
327
            {
328
                throw new \UnexpectedValueException('the email can not be empty.', -10101);
329
            }
330
            if ($user = Sentinel::findByCredentials([ 'email' => $email ]))
331
            {
332
                if ($user->id !== $id) {
333
                    throw new \InvalidArgumentException('email has already existed.', -10102);
334
                }
335
            }
336
        }
337
338
        $user = Sentinel::findById($id);
339
        if (!$user)
340
        {
341
            throw new \UnexpectedValueException('the user does not exist.', -10106);
342
        }
343
        if (isset($user->diectory) && $user->directory && $user->diectory != 'self')
344
        {
345
            throw new \UnexpectedValueException('the user come from external directroy.', -10109);
346
        }
347
348
        $valid = Sentinel::validForUpdate($user, array_only($request->all(), ['first_name', 'email', 'phone', 'invalid_flag']));
349
        if (!$valid)
350
        {
351
            throw new \UnexpectedValueException('updating the user does fails.', -10107);
352
        }
353
354
        $user = Sentinel::update($user, array_only($request->all(), ['first_name', 'email', 'phone', 'invalid_flag']));
355
        $user->status = $user->invalid_flag === 1 ? 'invalid' : (Activation::completed($user) ? 'active' : 'unactivated');
356
357
        $user->groups = array_column(Group::whereRaw([ 'users' => $user->id ])->get([ 'name' ])->toArray() ?: [], 'name');
358
359
        return Response()->json([ 'ecode' => 0, 'data' => $user ]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
360
    }
361
362
    /**
363
     * Remove the specified resource from storage.
364
     *
365
     * @param  int  $id
366
     * @return \Illuminate\Http\Response
367
     */
368 View Code Duplication
    public function destroy($id)
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...
369
    {
370
        $user = Sentinel::findById($id);
371
        if (!$user)
372
        {
373
            throw new \UnexpectedValueException('the user does not exist.', -10106);
374
        }
375
        if (isset($user->diectory) && $user->directory && $user->diectory != 'self')
376
        {
377
            throw new \UnexpectedValueException('the user come from external directroy.', -10109);
378
        }
379
380
        $user->delete();
381
        Event::fire(new DelUserEvent($id));
382
        return Response()->json([ 'ecode' => 0, 'data' => [ 'id' => $id ] ]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
383
    }
384
385
    /**
386
     * delete all selected users.
387
     *
388
     * @return \Illuminate\Http\Response
389
     */
390 View Code Duplication
    public function delMultiUsers(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...
391
    {
392
        $ids = $request->input('ids');
393
        if (!isset($ids) || !$ids)
394
        {
395
            throw new \InvalidArgumentException('the selected users cannot been empty.', -10108);
396
        }
397
398
        $deleted_ids = [];
399
        foreach ($ids as $id)
400
        {
401
            $user = Sentinel::findById($id);
402
            if ($user)
403
            {
404
                if (isset($user->directory) && $user->directory && $user->directory != 'self')
405
                {
406
                    continue;
407
                }
408
409
                $user->delete();
410
                Event::fire(new DelUserEvent($id));
411
                $deleted_ids[] = $id;
412
            }
413
        }
414
        return Response()->json([ 'ecode' => 0, 'data' => [ 'ids' => $deleted_ids ] ]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
415
    }
416
417
    /**
418
     * valid/invalid all selected users.
419
     *
420
     * @return \Illuminate\Http\Response
421
     */
422
    public function InvalidateMultiUsers(Request $request)
423
    {
424
        $ids = $request->input('ids');
425
        if (!isset($ids) || !$ids)
426
        {
427
            throw new \InvalidArgumentException('the selected users cannot been empty.', -10108);
428
        }
429
430
        $flag = $request->input('flag') ?: 1;
431
432
        $new_ids = [];
433
        foreach ($ids as $id)
434
        {
435
            $user = Sentinel::findById($id);
436
            if ($user)
437
            {
438
                if (isset($user->directory) && $user->directory && $user->directory != 'self')
439
                {
440
                    continue;
441
                }
442
                Sentinel::update($user, [ 'invalid_flag' => $flag ]);
443
                $new_ids[] = $id;
444
            }
445
        }
446
        return Response()->json([ 'ecode' => 0, 'data' => [ 'ids' => $new_ids ] ]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
447
    }
448
449
    /**
450
     * reset the user password.
451
     *
452
     * @param  \Illuminate\Http\Request  $request
453
     * @param  int  $id
454
     * @return \Illuminate\Http\Response
455
     */
456
    public function renewPwd(Request $request, $id)
457
    {
458
        $user = Sentinel::findById($id);
459
        if (!$user)
460
        {
461
            throw new \UnexpectedValueException('the user does not exist.', -10106);
462
        }
463
464
        $valid = Sentinel::validForUpdate($user, [ 'password' => 'actionview' ]);
465
        if (!$valid)
466
        {
467
            throw new \UnexpectedValueException('updating the user does fails.', -10107);
468
        }
469
470
        $user = Sentinel::update($user, [ 'password' => 'actionview' ]);
471
        return Response()->json([ 'ecode' => 0, 'data' => $user ]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
472
    }
473
474
    /**
475
     * send the reset password link to the mail.
476
     *
477
     * @param  \Illuminate\Http\Request  $request
478
     * @return \Illuminate\Http\Response
479
     */
480
    public function sendMailForResetpwd(Request $request)
481
    {
482
        $email = $request->input('email');
483
        if (!isset($email) || !$email)
484
        {
485
            throw new \UnexpectedValueException('the email can not be empty.', -10019);
486
        }
487
488
        $obscured_email = $sendto_email = $email;
489
490
        $last_reset_times = ResetPwdCode::where('requested_at', '>=', time() - 10 * 60)->count();
491
        if ($last_reset_times >= 10)
492
        {
493
            throw new \UnexpectedValueException('sending the email is too often.', -10016);
494
        }
495
496
        $last_reset_times = ResetPwdCode::where('requested_at', '>=', time() - 10 * 60)->where('email', $email)->count();
497
        if ($last_reset_times >= 3)
498
        {
499
            throw new \UnexpectedValueException('sending the email is too often.', -10016);
500
        }
501
502
        $user = Sentinel::findByCredentials([ 'email' => $email ]);
503 View Code Duplication
        if (!$user)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
504
        {
505
            throw new \UnexpectedValueException('the user is not exists.', -10010);
506
        }
507
        else if ($user->invalid_flag === 1)
508
        {
509
            throw new \UnexpectedValueException('the user has been disabled.', -10011);
510
        }
511
        else if ($user->directory && $user->directory != 'self')
512
        {
513
            throw new \UnexpectedValueException('the user is external sync user.', -10012);
514
        }
515
516
        if ($email === '[email protected]')
517
        {
518
            if (isset($user->bind_email) && $user->bind_email)
519
            {
520
                $sendto_email = $user->bind_email;
521
                $sections = explode('@', $user->bind_email);
522
                $sections[0] = substr($sections[0], 0, 1) . '***' . substr($sections[0], -1, 1);
523
                $obscured_email = implode('@', $sections);
524
            }
525
            else
526
            {
527
                throw new \UnexpectedValueException('the related email is not bound.', -10013);
528
            }
529
        }
530
531
        $data = [];
532
        $data['email'] = $email;
533
        $rand_code = md5($email . mt_rand() . microtime());
534
        $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
535
        $data['url'] = $http_type . $_SERVER['HTTP_HOST'] . '/actionview/resetpwd?code=' . $rand_code;
536
537
        $this->sendMail($sendto_email, $data);
538
539
        ResetPwdCode::create([
540
            'email' => $email,
541
            'code' => $rand_code,
542
            'requested_at' => time(),
543
            'expired_at' => time() + 24 * 60 * 60,
544
        ]);
545
546
        return Response()->json([ 'ecode' => 0, 'data' => [ 'sendto_email' => $obscured_email ] ]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
547
    }
548
549
    /**
550
     * send the reset link to the address.
551
     *
552
     * @param  string $to
553
     * @param  array $data
554
     * @return \Illuminate\Http\Response
555
     */
556
    public function sendMail($to, $data)
557
    {
558
        $syssetting = SysSetting::first()->toArray();
559
        if (isset($syssetting['mailserver'])
560
            && isset($syssetting['mailserver']['send'])
561
            && isset($syssetting['mailserver']['smtp'])
562
            && isset($syssetting['mailserver']['send']['from'])
563
            && isset($syssetting['mailserver']['smtp']['host'])
564
            && isset($syssetting['mailserver']['smtp']['port'])
565
            && isset($syssetting['mailserver']['smtp']['username'])
566
            && isset($syssetting['mailserver']['smtp']['password']))
567
        {
568
            Config::set('mail.from', $syssetting['mailserver']['send']['from']);
569
            Config::set('mail.host', $syssetting['mailserver']['smtp']['host']);
570
            Config::set('mail.port', $syssetting['mailserver']['smtp']['port']);
571
            Config::set('mail.encryption', isset($syssetting['mailserver']['smtp']['encryption']) && $syssetting['mailserver']['smtp']['encryption'] ? $syssetting['mailserver']['smtp']['encryption'] : null);
572
            Config::set('mail.username', $syssetting['mailserver']['smtp']['username']);
573
            Config::set('mail.password', $syssetting['mailserver']['smtp']['password']);
574
        }
575
        else
576
        {
577
            throw new \UnexpectedValueException('the smtp server is not configured.', -10014);
578
        }
579
580
        $mail_prefix = 'ActionView';
581
        if (isset($syssetting['mailserver']['send']['prefix'])
582
            && $syssetting['mailserver']['send']['prefix'])
583
        {
584
            $mail_prefix = $syssetting['mailserver']['send']['prefix'];
585
        }
586
587
        $subject = '[' . $mail_prefix . ']重置密码';
588
589
        try {
590
            Mail::send('emails.resetpwdlink', $data, function($message) use($to, $subject) {
591
                $message->from(Config::get('mail.from'), 'master')
592
                    ->to($to)
593
                    ->subject($subject);
594
            });
595
        } catch (Exception $e){
0 ignored issues
show
Bug introduced by
The class Fabrica\Http\Api\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
596
            throw new Exception('send mail failed.', -15200);
597
        }
598
    }
599
600
    /**
601
     * show the reset password link.
602
     *
603
     * @param  \Illuminate\Http\Request  $request
604
     * @return \Illuminate\Http\Response
605
     */
606
    public function showResetpwd(Request $request)
607
    {
608
        $code = $request->input('code');
609
        if (!isset($code) || !$code)
610
        {
611
            throw new \UnexpectedValueException('the link is not exists.', -10018);
612
        }
613
614
        $reset_code = ResetPwdCode::where('code', $code)->first();
615
        if (!$reset_code)
616
        {
617
            throw new \UnexpectedValueException('the link is not exists.', -10018);
618
        }
619
620 View Code Duplication
        if ($reset_code->invalid_flag == 1)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
621
        {
622
            throw new \UnexpectedValueException('the link has been invalid.', -10020);
623
        }
624
        else if ($reset_code->expired_at < time())
625
        {
626
            throw new \UnexpectedValueException('the link has been expired.', -10017);
627
        }
628
629
        $email = $reset_code->email;
630
        $user = Sentinel::findByCredentials([ 'email' => $email ]);
631 View Code Duplication
        if (!$user)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
632
        {
633
            throw new \UnexpectedValueException('the user is not exists.', -10010);
634
        }
635
        else if ($user->invalid_flag === 1)
636
        {
637
            throw new \UnexpectedValueException('the user has been disabled.', -10011);
638
        }
639
        else if ($user->directory && $user->directory != 'self')
640
        {
641
            throw new \UnexpectedValueException('the user is external sync user.', -10012);
642
        }
643
644
        return Response()->json([ 'ecode' => 0, 'data' => [ 'email' => $reset_code['email'] ] ]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
645
    }
646
647
    /**
648
     * reset the password.
649
     *
650
     * @param  \Illuminate\Http\Request  $request
651
     * @return \Illuminate\Http\Response
652
     */
653
    public function doResetpwd(Request $request)
654
    {
655
        $code = $request->input('code');
656
        if (!isset($code) || !$code)
657
        {
658
            throw new \UnexpectedValueException('the link is not exists.', -10018);
659
        }
660
661
        $password = $request->input('password');
662
        if (!isset($password) || !$password)
663
        {
664
            throw new \UnexpectedValueException('the password can not be empty.', -10103);
665
        }
666
667
        $reset_code = ResetPwdCode::where('code', $code)->first();
668
        if (!$reset_code)
669
        {
670
            throw new \UnexpectedValueException('the link is not exists.', -10018);
671
        }
672
673 View Code Duplication
        if ($reset_code->invalid_flag == 1)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
674
        {
675
            throw new \UnexpectedValueException('the link has been invalid.', -10020);
676
        }
677
        else if ($reset_code->expired_at < time())
678
        {
679
            throw new \UnexpectedValueException('the link has been expired.', -10017);
680
        }
681
682
        $email = $reset_code->email;
683
        $user = Sentinel::findByCredentials([ 'email' => $email ]);
684 View Code Duplication
        if (!$user)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
685
        {
686
            throw new \UnexpectedValueException('the user is not exsits.', -10010);
687
        }
688
        else if ($user->invalid_flag === 1)
689
        {
690
            throw new \UnexpectedValueException('the user has been disabled.', -10011);
691
        }
692
        else if ($user->directory && $user->directory != 'self')
693
        {
694
            throw new \UnexpectedValueException('the user is external sync user.', -10012);
695
        }
696
697
        $valid = Sentinel::validForUpdate($user, [ 'password' => $password ]);
698
        if (!$valid)
699
        {
700
            throw new \UnexpectedValueException('updating the user does fails.', -10107);
701
        }
702
703
        $user = Sentinel::update($user, [ 'password' => $password ]);
704
705
        $reset_code->invalid_flag = 1;
706
        $reset_code->save();
707
        
708
        return Response()->json([ 'ecode' => 0, 'data' => $user ]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
709
    }
710
711
    /**
712
     * Download user template file.
713
     *
714
     * @param  \Illuminate\Http\Request  $request
715
     */
716
    public function downloadUserTpl(Request $request)
717
    {
718
        $output = fopen('php://output', 'w') or die("can't open php://output");  
719
720
        header("Content-type:text/csv;charset=utf-8");
721
        header("Content-Disposition:attachment;filename=import-user-template.csv");
722
723
        fputcsv($output, [ 'name', 'email', 'phone' ]);  
724
        fputcsv($output, [ 'Tom', '[email protected]', '13811111111' ]);  
725
        fputcsv($output, [ 'Alice', '[email protected]', '13611111111' ]);  
726
        fclose($output) or die("can't close php://output"); 
727
        exit;
728
    }
729
}
730