Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 84
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserController
0.00% covered (danger)
0.00%
0 / 84
0.00% covered (danger)
0.00%
0 / 8
240
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 index
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
42
 create
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 store
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 edit
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 update
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 destroy
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 activity_logs
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace App\Http\Controllers\Dashboard;
4
5use App\Http\Controllers\Controller;
6use App\Http\Requests\users\createRequest;
7use App\Http\Requests\users\editRequest;
8use App\Models\Role;
9use App\Models\User;
10use App\Services\ActivityLogsService;
11use App\Services\UsersService;
12use Illuminate\Support\Facades\Hash;
13use Faker\Factory as Faker;
14use Illuminate\Database\Eloquent\Factories\Sequence;
15use Illuminate\Http\Request;
16use Yajra\DataTables\Facades\DataTables;
17
18class UserController extends Controller
19{
20    protected $UsersService;
21    protected $ActivityLogsService;
22
23    public function __construct(UsersService $UsersService, ActivityLogsService $ActivityLogsService) {
24        $this->UsersService = $UsersService;
25        $this->ActivityLogsService = $ActivityLogsService;
26
27        $this->middleware('permissionMiddleware:read-users')->only('index');
28        $this->middleware('permissionMiddleware:delete-users')->only('destroy');
29        $this->middleware('permissionMiddleware:update-users')->only(['edit', 'update', 'activity_logs']);
30        $this->middleware('permissionMiddleware:create-users')->only(['create', 'store']);
31    }
32
33    public function index(Request $request){
34        $roles = Role::get();
35
36        if ($request->ajax()) {
37            $data = User::where('super', '!=', 1);
38
39            if($request->role){
40                $data->whereRoleIs($request->role);
41            }
42
43
44            return DataTables::of($data)
45                    ->addIndexColumn()
46                    ->addColumn('action', function($row){
47                        $btn =  '<div class="btn-group"><button type="button" class="btn btn-success">'. trans('admin.Actions') .'</button><button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown"></button><div class="dropdown-menu" role="menu">';
48
49                        //my menu
50                        if (auth('user')->user()->has_permission('update-users')) {
51                            $btn .= '<a class="dropdown-item" href="' . route('dashboard.users.edit', $row->id).'">' . trans("admin.Edit") . '</a>';
52                        }
53
54                        if (auth('user')->user()->has_permission('update-users')) {
55                            $btn .= '<a class="dropdown-item" href="' . route('dashboard.users.activityLogs', $row->id) .'">' . trans('admin.Activity logs') . '</a>';
56                        }
57
58                        if (auth('user')->user()->has_permission('delete-users')) {
59                            $btn .= '<a class="dropdown-item" href="#" data-toggle="modal" data-target="#modal-default-' . $row->id.'">' . trans('admin.Delete') . '</a>';
60                        }
61                        
62                        $btn.= '</div></div>';
63
64                        //delete alert
65                        $btn .= view("Dashboard.partials.delete_confirmation", [
66                            'url' =>  route('dashboard.users.destroy', $row->id),
67                            'modal_id'  => 'modal-default-' . $row->id,
68                        ]);
69                        return $btn;
70                    })
71                    ->addColumn('role', function($row){
72                        return $row->getRole();
73                    })
74                    ->rawColumns(['action'])
75                    ->make(true);
76        }
77
78        return view('Dashboard.users.index')->with([
79            'roles' => $roles,
80        ]);
81    }
82
83    public function create(){
84        $roles = Role::all();
85        return view('Dashboard.users.create')->with('roles', $roles);
86    }
87
88    public function store(createRequest $request){
89        $user = $this->UsersService->insert($request);
90
91        $this->ActivityLogsService->insert([
92            'subject_id'      => $user->id,
93            'subject_type'    => 'App\Models\User',
94            'description'     => 'create',
95            'causer_id'       => auth('user')->user()->id,
96            'causer_type'     => 'App\Models\User',
97            'properties'      => null,
98        ]);
99
100        return redirect('dashboard/users')->with('success', 'success');
101    }
102
103    public function edit($id){
104        $roles = Role::all();
105        $user = User::findOrFail($id);
106
107        if($user->super == 1)
108            return redirect('dashboard/users')->with('error', trans('admin.you can\'t update this user'));
109        
110        return view('Dashboard.users.edit')->with([
111            'roles' => $roles,
112            'data' => $user,
113        ]);
114    }
115
116    public function update($id, editRequest $request){
117        $user = User::findOrFail($id);
118
119        $this->UsersService->update($user, $request);
120
121        $this->ActivityLogsService->insert([
122            'subject_id'      => $id,
123            'subject_type'    => 'App\Models\User',
124            'description'     => 'update',
125            'causer_id'       => auth('user')->user()->id,
126            'causer_type'     => 'App\Models\User',
127            'properties'      => null,
128        ]);
129
130        return redirect('dashboard/users')->with('success', 'success');
131    }
132
133    public function destroy($user_id){
134        $user = User::findOrFail($user_id);
135
136        if($user->super == 1)
137            return redirect('dashboard/users')->with('error', trans('admin.you can\'t delete this user'));
138        
139        $user->delete();
140
141        $this->ActivityLogsService->insert([
142            'subject_id'      => $user_id,
143            'subject_type'    => 'App\Models\User',
144            'description'     => 'delete',
145            'causer_id'       => auth('user')->user()->id,
146            'causer_type'     => 'App\Models\User',
147            'properties'      => null,
148        ]);
149
150        return redirect()->back()->with('success', trans('admin.success'));
151    }
152
153    public function activity_logs($id){
154        $user = User::findOrFail($id);
155
156        return view('Dashboard.users.activity_logs')->with([
157            'user' => $user,
158        ]);
159    }
160}