UserController::pending()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Helpers\Helper;
6
use App\Http\Requests\UserSearchRequest;
7
use App\Http\Requests\UserStoreRequest;
8
use App\Models\User;
9
use App\Services\CountryService;
10
use App\Services\TeamService;
11
use App\Services\UserService;
12
use App\Traits\CheckPermission;
13
use Illuminate\Http\RedirectResponse;
14
use Illuminate\Support\Facades\Auth;
15
use Illuminate\View\View;
16
use Session;
17
18
class UserController extends Controller
19
{
20
    use CheckPermission;
0 ignored issues
show
Bug introduced by
The trait App\Traits\CheckPermission requires the property $user_id which is not provided by App\Http\Controllers\UserController.
Loading history...
21
22
    private UserService $userService;
23
    private TeamService $teamService;
24
    private CountryService $countryService;
25
26
    /**
27
     * UserController constructor.
28
     *
29
     * @param \App\Services\UserService $userService
30
     * @param \App\Services\TeamService $teamService
31
     * @param \App\Services\CountryService $countryService
32
     */
33
    public function __construct(
34
        UserService $userService,
35
        TeamService $teamService,
36
        CountryService $countryService
37
    ) {
38
        $this->userService = $userService;
39
        $this->teamService = $teamService;
40
        $this->countryService = $countryService;
41
    }
42
43
    /**
44
     * Display a listing of the users.
45
     *
46
     * @param \App\Http\Requests\UserSearchRequest $request
47
     *
48
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\View\View
49
     */
50
    public function index(UserSearchRequest $request): View
51
    {
52
        $this->checkPermission('users.view');
53
54
        $searchParameters = Helper::getSearchParameters($request, User::SEARCH_PARAMETERS);
55
56
        $users = $this->userService->getUsers(20, $searchParameters);
57
        $userLevels =  $this->teamService->getAllAdminRoles();
58
        $teams = $this->teamService->getAllTeamRoles();
59
        $countries = $this->countryService->getCountries();
60
        $statuses = User::STATUS;
61
62
        return view('users.index', [
63
            'users' => $users,
64
            'userLevels' => $userLevels,
65
            'teams' => $teams,
66
            'countries' => $countries,
67
            'searchParameters' => $searchParameters,
68
            'statuses' => $statuses,
69
        ]);
70
    }
71
72
    /**
73
     * Show to the form for creating a new user.
74
     *
75
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\View\View
76
     */
77
    public function create(): View
78
    {
79
        $this->checkPermission('users.create');
80
81
        $countries = $this->countryService->getCountries();
82
        $roles = $this->teamService->getAllUserRoles();
83
        $userLevels = $this->teamService->getAllAdminRoles();
84
        $allTeams = $this->teamService->getAllTeamRoles();
85
86
        return view('users.create', [
87
            'countries' => $countries,
88
            'roles' => $roles,
89
            'userLevels' => $userLevels,
90
            'allTeams' => $allTeams,
91
        ]);
92
    }
93
94
    /**
95
     * Store a user created by an admin
96
     *
97
     * @param \App\Http\Requests\UserStoreRequest $request
98
     *
99
     * @return \Illuminate\Http\RedirectResponse
100
     */
101
    public function store(UserStoreRequest $request): RedirectResponse
102
    {
103
        $this->checkPermission('users.create');
104
105
        $user = $this->userService->createUser($request);
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
106
        //$user->notify(new MemberResetPasswordNotification($user));
107
108
        return redirect()->route('users.index')
109
            ->with('success', 'User created successfully');
110
    }
111
112
    /**
113
     * Show the form for editing the specified resource.
114
     *
115
     * @param  int  $userId
116
     *
117
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\View\View
118
     */
119
    public function edit(int $userId): View
120
    {
121
        if (Auth::id() != $userId) {
122
            $this->checkPermission('users.edit');
123
        }
124
125
        $user = $this->userService->getById($userId);
126
        $countries = $this->countryService->getCountries();
127
        $roles = $this->teamService->getAllUserRoles();
128
        $userLevels = $this->teamService->getAllAdminRoles();
129
        $allTeams = $this->teamService->getAllTeamRoles();
130
131
        return view('users.edit', [
132
            'user' => $user,
133
            'countries' => $countries,
134
            'roles' => $roles,
135
            'userLevels' => $userLevels,
136
            'allTeams' => $allTeams,
137
        ]);
138
    }
139
140
    /**
141
     * Update the specified resource in storage.
142
     *
143
     * @param \App\Http\Requests\UserStoreRequest $request
144
     * @param int $userId
145
     *
146
     * @return \Illuminate\Http\RedirectResponse
147
     * @throws \Spatie\ModelStatus\Exceptions\InvalidStatus
148
     */
149
    public function update(UserStoreRequest $request, int $userId): RedirectResponse
150
    {
151
        if (Auth::id() != $userId) {
152
            $this->checkPermission('users.edit');
153
        }
154
155
        $this->userService->updateUser($request, $userId);
156
157
        if (Auth::user()->hasPermissionTo('users.edit')) {
0 ignored issues
show
Bug introduced by
The method hasPermissionTo() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

157
        if (Auth::user()->/** @scrutinizer ignore-call */ hasPermissionTo('users.edit')) {
Loading history...
158
            return redirect()->route('users.index')
159
                ->with('success', __('ui.users.admin_updated_member_profile'));
160
        }
161
        if (Session::get('completeProfile')) {
162
            return redirect()->back()
163
                ->with('success', __('ui.users.first_time_updated_profile'));
164
        }
165
        return redirect()->back()
166
            ->with('success', __('ui.users.updated_profile'));
167
    }
168
169
    /**
170
     * Remove the specified resource from storage.
171
     *
172
     * @param  int  $userId
173
     *
174
     * @return \Illuminate\Http\RedirectResponse
175
     */
176
    public function destroy(int $userId): RedirectResponse
177
    {
178
        $this->checkPermission('users.delete');
179
180
        $this->userService->deleteUser($userId);
181
182
        return redirect()->route('users.index')
183
            ->with('success', __('ui.users.admin_deleted_member_profile'));
184
    }
185
186
    /**
187
     * Show to the user a notice that his/her status is still pending
188
     *
189
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\View\View
190
     */
191
    public function pending(): View
192
    {
193
        return view('users.status.pending');
194
    }
195
196
    /**
197
     * Show to the user a notice that his/her status is refused
198
     *
199
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\View\View
200
     */
201
    public function refused(): View
202
    {
203
        return view('users.status.refused');
204
    }
205
}
206