Passed
Push — master ( 1d6d9a...2a90e7 )
by Ron
02:47 queued 12s
created

AdminController::submitPolicy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 19
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 33
ccs 19
cts 19
cp 1
crap 2
rs 9.6333
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\User;
6
use App\Settings;
7
use Carbon\Carbon;
8
use App\UserRoleType;
9
use Illuminate\Http\Request;
10
use App\UserRolePermissions;
11
use App\UserRolePermissionTypes;
12
use Illuminate\Support\Facades\Log;
13
use Illuminate\Support\Facades\Auth;
14
use App\Http\Controllers\Controller;
15
use Illuminate\Support\Facades\Route;
16
use App\Http\Resources\UserCollection;
17
18
class AdminController extends Controller
19
{
20 64
    public function __construct()
21
    {
22
        //  Only Authorized users with specific admin permissions are allowed
23 64
        $this->middleware(['auth', 'can:allow_admin']);
24 64
    }
25
26
    //  Admin landing page
27 10
    public function index()
28
    {
29 10
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name);
30
31 10
        return view('admin.index');
32
    }
33
34
    //  Display all file links
35 2
    public function userLinks()
36
    {
37 2
        Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name);
38
39 2
        $userLinks = new UserCollection(
40 2
            User::withCount([
41 2
                    'FileLinks',
42
                    'FileLinks as expired_file_links_count' => function($query) {
43 2
                        $query->where('expire', '<', Carbon::now());
44 2
                    }
45
                ])
46 2
                ->get()
47 2
                ->makeVisible('user_id')
48
        );
49
50 2
        Log::debug('User link information gathered - ', array($userLinks));
51 2
        return view('admin.userLinks', [
52 2
            'links' => $userLinks,
53
        ]);
54
    }
55
56
    //  Show the links for the selected user
57 2
    public function showLinks($id)
58
    {
59 2
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name);
60
61 2
        $user = User::find($id);
62
63 2
        Log::debug('User link data being gathered for user - ', $user->toArray());
64 2
        return view('admin.linkDetails', [
65 2
            'user' => $user,
66
        ]);
67
    }
68
69
    //  Get the form to change the user password policy
70 4
    public function passwordPolicy()
71
    {
72 4
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name);
73 4
        $this->authorize('hasAccess', 'Manage Users');
74
75 4
        return view('admin.userSecurity', [
76 4
            'passExpire' => config('auth.passwords.settings.expire'),
77
        ]);
78
    }
79
80
    //  Submit the form to change the user password policy
81 6
    public function submitPolicy(Request $request)
82
    {
83 6
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name.'. Submitted Data:', $request->toArray());
84 6
        $this->authorize('hasAccess', 'Manage Users');
85
86 6
        $request->validate([
87 6
            'passExpire' => 'required|numeric'
88
        ]);
89
90 6
        Settings::firstOrCreate(
91 6
            ['key'   => 'auth.passwords.settings.expire'],
92 6
            ['key'   => 'auth.passwords.settings.expire', 'value' => $request->passExpire]
93 6
        )->update(['value' => $request->passExpire]);
94 6
        Log::notice('User '.Auth::user()->full_name.' updated User Password Policy requiring resets every '.$request->passExpire.' days');
95
96
        //  If the setting is changing from never to xx days, update all users
97 6
        if ($request->passExpire == 0)
98
        {
99 2
            User::whereNotNull('password_expires')->update([
100 2
                'password_expires' => null
101
            ]);
102 2
            Log::notice('Password Expires field cleard for all active users');
103
        }
104
        else
105
        {
106 4
            $newExpire = Carbon::now()->addDays($request->passExpire);
107 4
            User::whereNull('password_expires')->update([
108 4
                'password_expires' => $newExpire
109
            ]);
110 4
            Log::notice('Password Expires field updated for all active users to be '.$newExpire);
111
        }
112
113 6
        return redirect()->back()->with('success', 'User Security Updated');
114
    }
115
116
    //  View the current roles that can be assigned to users
117 4
    public function roleSettings()
118
    {
119 4
        Log::debug('Route ' . Route::currentRouteName() . ' visited by ' . Auth::user()->full_name);
120 4
        $this->authorize('hasAccess', 'Manage User Roles');
121
122
        $roles = UserRoleType::with(['UserRolePermissions' => function($query) {
123 4
            $query->join('user_role_permission_types', 'user_role_permission_types.perm_type_id', '=', 'user_role_permissions.perm_type_id');
124 4
        }])->get();
125 4
        $perms = UserRolePermissionTypes::all();
126
127 4
        Log::debug('User Role Data', $roles->toArray());
128 4
        Log::debug('Role Permissions Data', $perms->toArray());
129
130 4
        return view('admin.roleSettings', [
131 4
            'roles' => $roles,
132 4
            'perms' => $perms,
133
        ]);
134
    }
135
136 8
    public function submitRoleSettings(Request $request)
137
    {
138 8
        Log::debug('Route '.Route::currentRouteName().' visited by '.Auth::user()->full_name.'. Submitted Data: ', $request->toArray());
139 8
        $this->authorize('hasAccess', 'Manage User Roles');
140
141 8
        $request->validate([
142 8
            'name'        => 'required',
143
            'description' => 'required',
144
            'permissions' => 'required',
145
        ]);
146
147 8
        if($request->role_id)
148
        {
149 4
            $role = UserRoleType::find($request->role_id);
150 4
            if($role->allow_edit)
151
            {
152 2
                $role->update([
153 2
                    'name'        => $request->name,
154 2
                    'description' => $request->description,
155
                ]);
156 2
                foreach($request->permissions as $perm)
157
                {
158 2
                    UserRolePermissions::where('role_id', $request->role_id)->where('perm_type_id', $perm['perm_type_id'])->update([
159 2
                        'allow' => isset($perm['allow']) && $perm['allow'] ? 1 : 0,
160
                    ]);
161
                }
162
163 2
                Log::notice('Role '.$request->name.' (ID '.$request->role_id.') updated by '.Auth::user()->full_name.'. New Settings - ', array($request->permissions));
164 2
                return response()->json(['success' => true]);
165
            }
166
167 2
            Log::warning('Role '.$request->name.' could not be Edited by '.Auth::user()->full_name);
168 2
            return response()->json(['success' => false, 'reason' => 'Unable to Edit this Role']);
169
        }
170
171
        //  If a role ID was not provided, we create a new one
172 4
        $role = UserRoleType::create(
173
        [
174 4
            'name'        => $request->name,
175 4
            'description' => $request->description,
176
        ]);
177 4
        foreach($request->permissions as $perm)
178
        {
179 4
            UserRolePermissions::create([
180 4
                'role_id'      => $role->role_id,
181 4
                'perm_type_id' => $perm['perm_type_id'],
182 4
                'allow'        => isset($perm['allow']) && $perm['allow'] ? 1 : 0,
183
            ]);
184
        }
185
186 4
        Log::notice('New role "'.$role->name.'" created by '.Auth::user()->full_name.'. New Settings - ', array($request->permissions));
187 4
        return response()->json(['success' => true]);
188
    }
189
}
190