Total Complexity | 48 |
Total Lines | 254 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AdminUserController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AdminUserController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class AdminUserController extends BasePageController |
||
16 | { |
||
17 | /** |
||
18 | * @throws \Throwable |
||
19 | */ |
||
20 | public function index(Request $request): void |
||
21 | { |
||
22 | $this->setAdminPrefs(); |
||
23 | |||
24 | $meta_title = $title = 'User List'; |
||
25 | |||
26 | $roles = []; |
||
27 | $userRoles = Role::cursor()->remember(); |
||
28 | foreach ($userRoles as $userRole) { |
||
29 | $roles[$userRole->id] = $userRole->name; |
||
30 | } |
||
31 | |||
32 | $ordering = getUserBrowseOrdering(); |
||
33 | $orderBy = $request->has('ob') && \in_array($request->input('ob'), $ordering, false) ? $request->input('ob') : ''; |
||
34 | $page = $request->has('page') && is_numeric($request->input('page')) ? $request->input('page') : 1; |
||
35 | $offset = ($page - 1) * config('nntmux.items_per_page'); |
||
36 | |||
37 | $variables = [ |
||
38 | 'username' => $request->has('username') ? $request->input('username') : '', |
||
39 | 'email' => $request->has('email') ? $request->input('email') : '', |
||
40 | 'host' => $request->has('host') ? $request->input('host') : '', |
||
41 | 'role' => $request->has('role') ? $request->input('role') : '', |
||
42 | ]; |
||
43 | |||
44 | $result = User::getRange( |
||
45 | $offset, |
||
46 | config('nntmux.items_per_page'), |
||
47 | $orderBy, |
||
48 | $variables['username'], |
||
49 | $variables['email'], |
||
50 | $variables['host'], |
||
51 | $variables['role'], |
||
52 | true |
||
53 | ); |
||
54 | |||
55 | $results = $this->paginate($result ?? [], User::getCount($variables['role'], $variables['username'], $variables['host'], $variables['email']) ?? 0, config('nntmux.items_per_page'), $page, $request->url(), $request->query()); |
||
56 | |||
57 | // Add country data to each user based on their host IP |
||
58 | foreach ($results as $user) { |
||
59 | $position = null; |
||
60 | if (! empty($user->host) && filter_var($user->host, FILTER_VALIDATE_IP)) { |
||
61 | $position = Location::get($user->host); |
||
62 | } |
||
63 | $user->country_name = $position ? $position->countryName : null; |
||
64 | $user->country_code = $position ? $position->countryCode : null; |
||
65 | } |
||
66 | |||
67 | $this->smarty->assign( |
||
68 | [ |
||
69 | 'username' => $variables['username'], |
||
70 | 'email' => $variables['email'], |
||
71 | 'host' => $variables['host'], |
||
72 | 'role' => $variables['role'], |
||
73 | 'role_ids' => array_keys($roles), |
||
74 | 'role_names' => $roles, |
||
75 | 'userlist' => $results, |
||
76 | ] |
||
77 | ); |
||
78 | |||
79 | foreach ($ordering as $orderType) { |
||
80 | $this->smarty->assign('orderby'.$orderType, url('admin/user-list?ob='.$orderType)); |
||
81 | } |
||
82 | |||
83 | $content = $this->smarty->fetch('user-list.tpl'); |
||
84 | $this->smarty->assign(compact('title', 'meta_title', 'content')); |
||
85 | |||
86 | $this->adminrender(); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @return RedirectResponse|void |
||
91 | * |
||
92 | * @throws \Exception |
||
93 | */ |
||
94 | public function edit(Request $request) |
||
95 | { |
||
96 | $this->setAdminPrefs(); |
||
97 | |||
98 | $user = [ |
||
99 | 'id' => '', |
||
100 | 'username' => '', |
||
101 | 'email' => '', |
||
102 | 'password' => '', |
||
103 | 'role' => User::ROLE_USER, |
||
104 | 'notes' => '', |
||
105 | 'rate_limit' => 60, |
||
106 | ]; |
||
107 | |||
108 | $meta_title = $title = 'View User'; |
||
109 | |||
110 | // set the current action |
||
111 | $action = $request->input('action') ?? 'view'; |
||
112 | |||
113 | // get the user roles |
||
114 | $userRoles = Role::cursor()->remember(); |
||
115 | $roles = []; |
||
116 | $defaultRole = 'User'; |
||
117 | $defaultInvites = Invitation::DEFAULT_INVITES; |
||
118 | foreach ($userRoles as $r) { |
||
119 | $roles[$r->id] = $r->name; |
||
120 | if ($r->isdefault === 1) { |
||
121 | $defaultRole = $r->id; |
||
122 | $defaultInvites = $r->defaultinvites; |
||
123 | } |
||
124 | } |
||
125 | |||
126 | switch ($action) { |
||
127 | case 'add': |
||
128 | $user += [ |
||
129 | 'role' => $defaultRole, |
||
130 | 'notes' => '', |
||
131 | 'invites' => $defaultInvites, |
||
132 | 'movieview' => 0, |
||
133 | 'xxxview' => 0, |
||
134 | 'musicview' => 0, |
||
135 | 'consoleview' => 0, |
||
136 | 'gameview' => 0, |
||
137 | 'bookview' => 0, |
||
138 | ]; |
||
139 | $this->smarty->assign('user', $user); |
||
140 | break; |
||
141 | case 'submit': |
||
142 | if (empty($request->input('id'))) { |
||
143 | $invites = $defaultInvites; |
||
144 | foreach ($userRoles as $role) { |
||
145 | if ($role['id'] === $request->input('role')) { |
||
146 | $invites = $role['defaultinvites']; |
||
147 | } |
||
148 | } |
||
149 | $ret = User::signUp($request->input('username'), $request->input('password'), $request->input('email'), '', $request->input('notes'), $invites, '', true, $request->input('role'), false); |
||
150 | $this->smarty->assign('role', $request->input('role')); |
||
151 | } else { |
||
152 | $editedUser = User::find($request->input('id')); |
||
153 | $ret = User::updateUser($editedUser->id, $request->input('username'), $request->input('email'), $request->input('grabs'), $request->input('role'), $request->input('notes'), $request->input('invites'), ($request->has('movieview') ? 1 : 0), ($request->has('musicview') ? 1 : 0), ($request->has('gameview') ? 1 : 0), ($request->has('xxxview') ? 1 : 0), ($request->has('consoleview') ? 1 : 0), ($request->has('bookview') ? 1 : 0)); |
||
154 | if ($request->input('password') !== null) { |
||
155 | User::updatePassword($editedUser->id, $request->input('password')); |
||
156 | } |
||
157 | if ($request->input('rolechangedate') !== null) { |
||
158 | User::updateUserRoleChangeDate($editedUser->id, $request->input('rolechangedate')); |
||
159 | } |
||
160 | if ($request->input('role') !== null) { |
||
161 | $editedUser->refresh(); |
||
162 | SendAccountChangedEmail::dispatch($editedUser)->onQueue('emails'); |
||
163 | } |
||
164 | } |
||
165 | |||
166 | if ($ret >= 0) { |
||
167 | return redirect()->to('admin/user-list'); |
||
168 | } |
||
169 | |||
170 | switch ($ret) { |
||
171 | case User::ERR_SIGNUP_BADUNAME: |
||
172 | $this->smarty->assign('error', 'Bad username. Try a better one.'); |
||
173 | break; |
||
174 | case User::ERR_SIGNUP_BADPASS: |
||
175 | $this->smarty->assign('error', 'Bad password. Try a longer one.'); |
||
176 | break; |
||
177 | case User::ERR_SIGNUP_BADEMAIL: |
||
178 | $this->smarty->assign('error', 'Bad email.'); |
||
179 | break; |
||
180 | case User::ERR_SIGNUP_UNAMEINUSE: |
||
181 | $this->smarty->assign('error', 'Username in use.'); |
||
182 | break; |
||
183 | case User::ERR_SIGNUP_EMAILINUSE: |
||
184 | $this->smarty->assign('error', 'Email in use.'); |
||
185 | break; |
||
186 | default: |
||
187 | $this->smarty->assign('error', 'Unknown save error.'); |
||
188 | break; |
||
189 | } |
||
190 | $user += [ |
||
191 | 'id' => $request->input('id'), |
||
192 | 'username' => $request->input('username'), |
||
193 | 'email' => $request->input('email'), |
||
194 | 'role' => $request->input('role'), |
||
195 | 'notes' => $request->input('notes'), |
||
196 | ]; |
||
197 | $this->smarty->assign('user', $user); |
||
198 | break; |
||
199 | case 'view': |
||
200 | default: |
||
201 | if ($request->has('id')) { |
||
202 | $title = 'User Edit'; |
||
203 | $id = $request->input('id'); |
||
204 | $user = User::find($id); |
||
205 | |||
206 | $this->smarty->assign('user', $user); |
||
207 | } |
||
208 | |||
209 | break; |
||
210 | } |
||
211 | |||
212 | $this->smarty->assign('yesno_ids', [1, 0]); |
||
213 | $this->smarty->assign('yesno_names', ['Yes', 'No']); |
||
214 | |||
215 | $this->smarty->assign('role_ids', array_keys($roles)); |
||
216 | $this->smarty->assign('role_names', $roles); |
||
217 | $this->smarty->assign('user', $user); |
||
218 | |||
219 | $content = $this->smarty->fetch('user-edit.tpl'); |
||
220 | |||
221 | $this->smarty->assign(compact('title', 'meta_title', 'content')); |
||
222 | |||
223 | $this->adminrender(); |
||
224 | } |
||
225 | |||
226 | public function destroy(Request $request): RedirectResponse |
||
227 | { |
||
228 | if ($request->has('id')) { |
||
229 | $user = User::find($request->input('id')); |
||
230 | $username = $user->username; // Store username before deletion |
||
231 | |||
232 | $user->delete(); |
||
233 | |||
234 | // Redirect with username to display in notification |
||
235 | return redirect()->to('admin/user-list?deleted=1&username='.urlencode($username)); |
||
236 | } |
||
237 | |||
238 | if ($request->has('redir')) { |
||
239 | return redirect()->to($request->input('redir')); |
||
240 | } |
||
241 | |||
242 | return redirect()->to($request->server('HTTP_REFERER')); |
||
|
|||
243 | } |
||
244 | |||
245 | public function resendVerification(Request $request): RedirectResponse |
||
246 | { |
||
247 | if ($request->has('id')) { |
||
248 | $user = User::find($request->input('id')); |
||
249 | UserVerification::generate($user); |
||
250 | |||
251 | UserVerification::send($user, 'User email verification required'); |
||
252 | |||
253 | return redirect()->back()->with('success', 'Email verification for '.$user->username.' sent'); |
||
254 | } |
||
255 | |||
256 | return redirect()->back()->with('error', 'User is invalid'); |
||
257 | } |
||
258 | |||
259 | public function verify(Request $request): RedirectResponse |
||
269 | } |
||
270 | } |
||
271 |