Passed
Pull Request — master (#77)
by Ron
41:51 queued 13:39
created

AdminController::submitRoleSettings()   B

Complexity

Conditions 9
Paths 5

Size

Total Lines 50
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 9

Importance

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