Completed
Push — develop ( 1eb861...f7494f )
by Neil
10s
created

UpdateUserRequest::authorize()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 6
nc 3
nop 0
1
<?php
2
/**
3
 * UpdateUserRequest.php
4
 *
5
 * Authorize and validate requests to update users.
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * @package    LibreNMS
21
 * @link       http://librenms.org
22
 * @copyright  2016 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace App\Http\Requests;
27
28
use App\Models\User;
29
use Auth;
30
31
class UpdateUserRequest extends Request
32
{
33
    /**
34
     * Determine if the user is authorized to make this request.
35
     *
36
     * @return bool
37
     */
38
    public function authorize()
39
    {
40
        if (Auth::user()->isAdmin()) {
41
            return true;
42
        }
43
        if (Auth::id() == $this->input('user_id')) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return \Auth::id() == $this->input('user_id');.
Loading history...
44
            return true;
45
        }
46
        return false;
47
    }
48
49
    /**
50
     * Get the validation rules that apply to the request.
51
     *
52
     * @return array
53
     */
54
    public function rules()
55
    {
56
        if ($this->input('update') == 'password') {
57
            $user_id = $this->input('user_id');
58
            $rules = ['password'              => 'required|min:6|max:255',
59
                      'password_confirmation' => 'required|same:password',
60
            ];
61
            if (!Auth::user()->isAdmin() || Auth::id() == $user_id) {
62
                $rules['current_password'] = 'required|password:'.$user_id;
63
            }
64
            return $rules;
65
        }
66
        else {
67
            $user = User::find($this->input('user_id'));
68
            return ['username'    => 'required|max:20|unique:users,username,'.$user->username.',username',
69
                    'email'       => 'required|email|max:60|unique:users,email,'.$user->username.',username',
70
                    'realname'    => 'max:60',
71
                    'description' => 'min:3|max:1024',
72
            ];
73
        }
74
    }
75
}
76