Skip to content

Commit 1240e87

Browse files
committed
Model/ACL: Rewrite queries with query builder and clean code
1 parent 6a3dd4e commit 1240e87

File tree

1 file changed

+72
-101
lines changed

1 file changed

+72
-101
lines changed

application/models/Acl_model.php

Lines changed: 72 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,23 @@ public function __construct()
3535
/**
3636
* Get the roles for a group by the user ID
3737
*
38-
* @param Int $userId
39-
* @param String $moduleName
38+
* @param Int $userId
39+
* @param false|String $moduleName
4040
* @return Array
4141
*/
42-
public function getGroupRolesByUser($userId, $moduleName = false)
42+
public function getGroupRolesByUser(int $userId, false|string $moduleName = false): array
4343
{
44-
$query = $this->db->table("acl_group_roles agr, acl_account_groups aag")->select("agr.role_name, agr.module")
45-
->where("aag.account_id", $userId)
46-
->where("aag.group_id = agr.group_id");
44+
$query = $this->db->table('acl_group_roles agr')
45+
->select('agr.role_name, agr.module')
46+
->join('acl_account_groups aag', 'aag.group_id = agr.group_id', 'inner')
47+
->where('aag.account_id', $userId);
4748

4849
if ($moduleName) {
4950
$query->where("agr.module", $moduleName);
5051
}
5152

52-
$query = $query->get();
5353

54-
if ($query->getNumRows()) {
55-
return $query->getResultArray();
56-
} else {
57-
return false;
58-
}
54+
return $query->get()->getResultArray() ?? [];
5955
}
6056

6157
/**
@@ -65,7 +61,7 @@ public function getGroupRolesByUser($userId, $moduleName = false)
6561
* @param int $default_group
6662
* @return array
6763
*/
68-
public function getAccountRolesPermissions(int $userId = 0, int $default_group = 1)
64+
public function getAccountRolesPermissions(int $userId = 0, int $default_group = 1): array
6965
{
7066
// Groups: Initialize
7167
$groups = (array)$this->getGroupsByUser($userId);
@@ -122,51 +118,41 @@ public function getAccountRolesPermissions(int $userId = 0, int $default_group =
122118
/**
123119
* Get the account-specific permissions
124120
*
125-
* @param Int $userId
121+
* @param Int $userId
126122
* @return Array
127123
*/
128-
public function getAccountPermissions($userId)
124+
public function getAccountPermissions(int $userId): array
129125
{
130126
$query = $this->db->table('acl_account_permissions')->select("account_id, permission_name, module, value")->where("account_id", $userId)->get();
131127

132-
if ($query->getNumRows() > 0) {
133-
return $query->getResultArray();
134-
} else {
135-
return false;
136-
}
128+
return $query->getResultArray() ?? [];
137129
}
138130

139131
/**
140132
* Get the account-specific roles
141133
*
142-
* @param Int $userId
143-
* @param String $module
134+
* @param Int $userId
135+
* @param false|String $module
144136
* @return Array
145137
*/
146-
public function getAccountRoles($userId, $module = false)
138+
public function getAccountRoles(int $userId, false|string $module = false): array
147139
{
148140
$builder = $this->db->table('acl_account_roles')->select("role_name")->where("account_id", $userId);
149141

150142
if ($module) {
151143
$builder->where("module", $module);
152144
}
153145

154-
$query = $builder->get();
155-
156-
if ($query->getNumRows() > 0) {
157-
return $query->getResultArray();
158-
} else {
159-
return false;
160-
}
146+
return $builder->get()->getResultArray() ?? [];
161147
}
162148

163149
/**
164150
* Get the groups of the given user
165151
*
166-
* @param $accountId
152+
* @param false|int $accountId
167153
* @return Array
168154
*/
169-
public function getGroupsByUser($accountId = false)
155+
public function getGroupsByUser(false|int $accountId = false): array
170156
{
171157
if (!$accountId) {
172158
$accountId = $this->user->getId();
@@ -183,7 +169,7 @@ public function getGroupsByUser($accountId = false)
183169
return $query->getResultArray();
184170
} else {
185171
// No group found; default to player
186-
return array($this->getGroup($this->config->item('default_player_group')));
172+
return [$this->getGroup($this->config->item('default_player_group'))];
187173
}
188174
}
189175

@@ -192,65 +178,55 @@ public function getGroupsByUser($accountId = false)
192178
*
193179
* @return Array
194180
*/
195-
public function getGroups()
181+
public function getGroups(): array
196182
{
197183
$query = $this->db->table('acl_groups ag')->select('ag.id, ag.priority, ag.name, ag.color, ag.description')->get();
198184

199185
if ($query->getNumRows() > 0) {
200186
return $query->getResultArray();
201187
} else {
202-
return false;
188+
return [];
203189
}
204190
}
205191

206192
/**
207193
* Get member count of a group
208194
*
209-
* @param Int $groupId
195+
* @param Int $groupId
210196
* @return Int
211197
*/
212-
public function getGroupMemberCount($id)
198+
public function getGroupMemberCount(int $groupId): int
213199
{
214-
$query = $this->db->query("SELECT COUNT(*) `memberCount` FROM acl_account_groups WHERE group_id=?", array($id));
215-
216-
if ($query->getNumRows() > 0) {
217-
$result = $query->getResultArray();
218-
219-
return $result[0]['memberCount'];
220-
} else {
221-
return 0;
222-
}
200+
return (int) $this->db->table('acl_account_groups')->where('group_id', $groupId)->countAllResults();
223201
}
224202

225-
/*
203+
/**
226204
* Get the members of a group
227205
* @param Int $groupId
228206
* @return Array
229207
*/
230-
public function getGroupMembers($id)
208+
public function getGroupMembers(int $groupId): array
231209
{
232-
$query = $this->db->query("SELECT account_id FROM acl_account_groups WHERE group_id=?", array($id));
233-
234-
if ($query->getNumRows()) {
235-
$result = $query->getResultArray();
236-
237-
foreach ($result as $k => $v) {
238-
$result[$k]['username'] = $this->user->getUsername($v['account_id']);
239-
}
240-
241-
return $result;
242-
} else {
243-
return false;
210+
$result = $this->db->table('acl_account_groups')
211+
->select('account_id')
212+
->where('group_id', $groupId)
213+
->get()
214+
->getResultArray();
215+
216+
foreach ($result as &$row) {
217+
$row['username'] = $this->user->getUsername($row['account_id']);
244218
}
219+
220+
return $result ?: [];
245221
}
246222

247223
/**
248224
* Get the group by the given id.
249225
*
250-
* @param $groupId
251-
* @return Array
226+
* @param int $groupId
227+
* @return false|array
252228
*/
253-
public function getGroup($groupId)
229+
public function getGroup(int $groupId): false|array
254230
{
255231
$query = $this->db->table('acl_groups')->select('id, priority, name, color, description')->where('id', $groupId)->get();
256232

@@ -266,10 +242,10 @@ public function getGroup($groupId)
266242
/**
267243
* Get the group by the given name
268244
*
269-
* @param $groupName
245+
* @param string $groupName
270246
* @return Boolean
271247
*/
272-
public function getGroupByName($groupName)
248+
public function getGroupByName(string $groupName)
273249
{
274250
$query = $this->db->table('acl_groups')->select('id, priority, name, color, description')->where('name', $groupName)->get();
275251

@@ -285,39 +261,34 @@ public function getGroupByName($groupName)
285261
/**
286262
* Check if a group has a specific role
287263
*
288-
* @param Int $id
289-
* @param String $name
290-
* @param String $module
291-
* @return Boolean
264+
* @param Int $groupId
265+
* @param String $name
266+
* @param String $module
267+
* @return int
292268
*/
293-
public function groupHasRole($groupId, $name, $module)
269+
public function groupHasRole(int $groupId, string $name, string $module): int
294270
{
295-
$query = $this->db->query("SELECT COUNT(*) `total` FROM acl_group_roles WHERE role_name=? AND module=? AND group_id=?", array($name, $module, $groupId));
296-
297-
if ($query->getNumRows()) {
298-
$result = $query->getResultArray();
299-
300-
return $result[0]['total'];
301-
} else {
302-
return false;
303-
}
271+
return (int) $this->db->table('acl_group_roles')
272+
->where('role_name', $name)
273+
->where('module', $module)
274+
->where('group_id', $groupId)
275+
->countAllResults();
304276
}
305277

306278
/**
307279
* Get the database roles for a module
308280
*
309-
* @param String $moduleName
281+
* @param String $moduleName
282+
* @param int $groupId
310283
* @return Array
311284
*/
312-
public function getRolesByModule($moduleName, $groupId)
285+
public function getRolesByModule(string $moduleName, int $groupId): array
313286
{
314-
$query = $this->db->query("SELECT * FROM acl_group_roles WHERE module = ? AND group_id = ?", [$moduleName, $groupId]);
315-
316-
if ($query->getNumRows() > 0) {
317-
return $query->getResultArray();
318-
} else {
319-
return false;
320-
}
287+
return $this->db->table('acl_group_roles')
288+
->where('module', $moduleName)
289+
->where('group_id', $groupId)
290+
->get()
291+
->getResultArray() ?? [];
321292
}
322293

323294
/**
@@ -326,7 +297,7 @@ public function getRolesByModule($moduleName, $groupId)
326297
* @param $data
327298
* @return int
328299
*/
329-
public function createGroup($data)
300+
public function createGroup($data): int
330301
{
331302
$this->db->table('acl_groups')->insert($data);
332303

@@ -338,7 +309,7 @@ public function createGroup($data)
338309
*
339310
* @param Int $groupId
340311
*/
341-
public function deleteGroup($groupId)
312+
public function deleteGroup(int $groupId): void
342313
{
343314
$this->db->table('acl_groups')->delete(['id' => $groupId]);
344315
}
@@ -349,7 +320,7 @@ public function deleteGroup($groupId)
349320
* @param Int $groupId
350321
* @param Int $accountId
351322
*/
352-
public function assignGroupToUser($groupId, $accountId)
323+
public function assignGroupToUser(int $groupId, int $accountId): void
353324
{
354325
$data = [
355326
"account_id" => $accountId,
@@ -365,7 +336,7 @@ public function assignGroupToUser($groupId, $accountId)
365336
* @param Int $groupId
366337
* @param Int $accountId
367338
*/
368-
public function removeGroupFromUser($groupId, $accountId)
339+
public function removeGroupFromUser(int $groupId, int $accountId): void
369340
{
370341
$data = [
371342
"account_id" => $accountId,
@@ -380,7 +351,7 @@ public function removeGroupFromUser($groupId, $accountId)
380351
*
381352
* @param Int $accountId
382353
*/
383-
public function removeGroupsFromUser($accountId)
354+
public function removeGroupsFromUser(int $accountId): void
384355
{
385356
$data = [
386357
"account_id" => $accountId
@@ -394,7 +365,7 @@ public function removeGroupsFromUser($accountId)
394365
*
395366
* @param Int $accountId
396367
*/
397-
public function removePermissionsFromUser($accountId)
368+
public function removePermissionsFromUser(int $accountId): void
398369
{
399370
$data = [
400371
"account_id" => $accountId
@@ -406,15 +377,15 @@ public function removePermissionsFromUser($accountId)
406377
/**
407378
* Assign a permission to a user
408379
*
409-
* @param $accontId
380+
* @param int $accountId
410381
* @param String $permissionName
411382
* @param string $moduleName
412383
* @param $value
413384
*/
414-
public function assignPermissionToUser($accontId, $permissionName, $moduleName, $value)
385+
public function assignPermissionToUser(int $accountId, string $permissionName, string $moduleName, $value): void
415386
{
416387
$data = [
417-
"account_id" => $accontId,
388+
"account_id" => $accountId,
418389
"permission_name" => $permissionName,
419390
"module" => $moduleName,
420391
"value" => $value
@@ -430,7 +401,7 @@ public function assignPermissionToUser($accontId, $permissionName, $moduleName,
430401
* @param String $name
431402
* @param String $module
432403
*/
433-
public function addRoleToGroup($groupId, $name, $module)
404+
public function addRoleToGroup(int $groupId, string $name, string $module): void
434405
{
435406
$data = [
436407
'group_id' => $groupId,
@@ -448,7 +419,7 @@ public function addRoleToGroup($groupId, $name, $module)
448419
* @param String $name
449420
* @param String $module
450421
*/
451-
public function deleteRoleFromGroup($groupId, $name, $module)
422+
public function deleteRoleFromGroup(int $groupId, string $name, string $module): void
452423
{
453424
$this->db->table('acl_group_roles')->delete(['group_id' => $groupId, 'role_name' => $name, 'module' => $module]);
454425
}
@@ -459,7 +430,7 @@ public function deleteRoleFromGroup($groupId, $name, $module)
459430
* @param Int $id
460431
* @param Array $data
461432
*/
462-
public function saveGroup($id, $data)
433+
public function saveGroup(int $id, array $data): void
463434
{
464435
$this->db->table('acl_groups')->where('id', $id)->update($data);
465436
}
@@ -469,7 +440,7 @@ public function saveGroup($id, $data)
469440
*
470441
* @param Int $groupId
471442
*/
472-
public function deleteAllRoleFromGroup($groupId)
443+
public function deleteAllRoleFromGroup(int $groupId): void
473444
{
474445
$this->db->table('acl_group_roles')->delete(['group_id' => $groupId]);
475446
}

0 commit comments

Comments
 (0)