Skip to content

Commit ddbb0ba

Browse files
committed
Add alternate Models with Entities
1 parent c6a603a commit ddbb0ba

9 files changed

+389
-283
lines changed

docs/_changes.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
## Unreleased
44

5-
_No unreleased changes yet_
5+
Enhancements:
6+
7+
- Added alternate authorization Models with stronger typing
68

79
## 1.1.0
810

docs/extending.md

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ Entities with your own casts and class methods.
2626
If you extend the Model and supply your own validation rules you can also enforce those on the
2727
`AuthController` by providing a `$registrationRules` property in **app/Config/Validation.php**.
2828

29+
### Alternate Models
30+
31+
Since version `1.2.0` this library includes an alternate version of the authorization models
32+
that use specific Entities as their return types. These models are not yet used by the library
33+
itself for backwards-compatibility, but they are highly recommended for any implementing
34+
projects or model extensions.
35+
2936
## Views
3037

3138
Myth:Auth uses its own views by default, but you may want to update these in order to change

phpstan.neon.dist

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ parameters:
1010
- src/Config/Routes.php
1111
- src/Views/*
1212
ignoreErrors:
13+
- '#.+deprecated class Myth\\Auth\\Authorization\\(Group|Permission)Model.+#'
1314
- '#.+Mockery.+#'
1415
- '#Call to an undefined method .+::shouldReceive\(\)#'
1516
- '#Call to an undefined static method Config\\Services::[A-Za-z]+\(\)#'

src/Authorization/GroupModel.php

+7-190
Original file line numberDiff line numberDiff line change
@@ -2,196 +2,13 @@
22

33
namespace Myth\Auth\Authorization;
44

5-
use CodeIgniter\Model;
5+
use Myth\Auth\Models\GroupModel as BaseModel;
66

7-
class GroupModel extends Model
7+
/**
8+
* @deprecated 1.2.0 Use Myth\Auth\Models\GroupModel instead
9+
*/
10+
class GroupModel extends BaseModel
811
{
9-
protected $table = 'auth_groups';
10-
protected $primaryKey = 'id';
11-
protected $returnType = 'object';
12-
protected $allowedFields = [
13-
'name', 'description',
14-
];
15-
protected $useTimestamps = false;
16-
protected $validationRules = [
17-
'name' => 'required|max_length[255]|is_unique[auth_groups.name,name,{name}]',
18-
'description' => 'max_length[255]',
19-
];
20-
protected $validationMessages = [];
21-
protected $skipValidation = false;
22-
23-
//--------------------------------------------------------------------
24-
// Users
25-
//--------------------------------------------------------------------
26-
27-
/**
28-
* Adds a single user to a single group.
29-
*
30-
* @return bool
31-
*/
32-
public function addUserToGroup(int $userId, int $groupId)
33-
{
34-
cache()->delete("{$groupId}_users");
35-
cache()->delete("{$userId}_groups");
36-
cache()->delete("{$userId}_permissions");
37-
38-
$data = [
39-
'user_id' => $userId,
40-
'group_id' => $groupId,
41-
];
42-
43-
return (bool) $this->db->table('auth_groups_users')->insert($data);
44-
}
45-
46-
/**
47-
* Removes a single user from a single group.
48-
*
49-
* @param int|string $groupId
50-
*
51-
* @return bool
52-
*/
53-
public function removeUserFromGroup(int $userId, $groupId)
54-
{
55-
cache()->delete("{$groupId}_users");
56-
cache()->delete("{$userId}_groups");
57-
cache()->delete("{$userId}_permissions");
58-
59-
return $this->db->table('auth_groups_users')
60-
->where([
61-
'user_id' => $userId,
62-
'group_id' => (int) $groupId,
63-
])->delete();
64-
}
65-
66-
/**
67-
* Removes a single user from all groups.
68-
*
69-
* @return bool
70-
*/
71-
public function removeUserFromAllGroups(int $userId)
72-
{
73-
cache()->delete("{$userId}_groups");
74-
cache()->delete("{$userId}_permissions");
75-
76-
return $this->db->table('auth_groups_users')
77-
->where('user_id', $userId)
78-
->delete();
79-
}
80-
81-
/**
82-
* Returns an array of all groups that a user is a member of.
83-
*
84-
* @return array
85-
*/
86-
public function getGroupsForUser(int $userId)
87-
{
88-
if (null === $found = cache("{$userId}_groups")) {
89-
$found = $this->builder()
90-
->select('auth_groups_users.*, auth_groups.name, auth_groups.description')
91-
->join('auth_groups_users', 'auth_groups_users.group_id = auth_groups.id', 'left')
92-
->where('user_id', $userId)
93-
->get()->getResultArray();
94-
95-
cache()->save("{$userId}_groups", $found, 300);
96-
}
97-
98-
return $found;
99-
}
100-
101-
/**
102-
* Returns an array of all users that are members of a group.
103-
*
104-
* @return array
105-
*/
106-
public function getUsersForGroup(int $groupId)
107-
{
108-
if (null === $found = cache("{$groupId}_users")) {
109-
$found = $this->builder()
110-
->select('auth_groups_users.*, users.*')
111-
->join('auth_groups_users', 'auth_groups_users.group_id = auth_groups.id', 'left')
112-
->join('users', 'auth_groups_users.user_id = users.id', 'left')
113-
->where('auth_groups.id', $groupId)
114-
->get()->getResultArray();
115-
116-
cache()->save("{$groupId}_users", $found, 300);
117-
}
118-
119-
return $found;
120-
}
121-
122-
//--------------------------------------------------------------------
123-
// Permissions
124-
//--------------------------------------------------------------------
125-
126-
/**
127-
* Gets all permissions for a group in a way that can be
128-
* easily used to check against:
129-
*
130-
* [
131-
* id => name,
132-
* id => name
133-
* ]
134-
*/
135-
public function getPermissionsForGroup(int $groupId): array
136-
{
137-
$permissionModel = model(PermissionModel::class);
138-
$fromGroup = $permissionModel
139-
->select('auth_permissions.*')
140-
->join('auth_groups_permissions', 'auth_groups_permissions.permission_id = auth_permissions.id', 'inner')
141-
->where('group_id', $groupId)
142-
->findAll();
143-
144-
$found = [];
145-
146-
foreach ($fromGroup as $permission) {
147-
$found[$permission['id']] = $permission;
148-
}
149-
150-
return $found;
151-
}
152-
153-
/**
154-
* Add a single permission to a single group, by IDs.
155-
*
156-
* @return mixed
157-
*/
158-
public function addPermissionToGroup(int $permissionId, int $groupId)
159-
{
160-
$data = [
161-
'permission_id' => $permissionId,
162-
'group_id' => $groupId,
163-
];
164-
165-
return $this->db->table('auth_groups_permissions')->insert($data);
166-
}
167-
168-
//--------------------------------------------------------------------
169-
170-
/**
171-
* Removes a single permission from a single group.
172-
*
173-
* @return mixed
174-
*/
175-
public function removePermissionFromGroup(int $permissionId, int $groupId)
176-
{
177-
return $this->db->table('auth_groups_permissions')
178-
->where([
179-
'permission_id' => $permissionId,
180-
'group_id' => $groupId,
181-
])->delete();
182-
}
183-
184-
//--------------------------------------------------------------------
185-
186-
/**
187-
* Removes a single permission from all groups.
188-
*
189-
* @return mixed
190-
*/
191-
public function removePermissionFromAllGroups(int $permissionId)
192-
{
193-
return $this->db->table('auth_groups_permissions')
194-
->where('permission_id', $permissionId)
195-
->delete();
196-
}
12+
protected $returnType = 'object';
13+
protected string $permissionModel = PermissionModel::class;
19714
}

src/Authorization/PermissionModel.php

+6-92
Original file line numberDiff line numberDiff line change
@@ -2,98 +2,12 @@
22

33
namespace Myth\Auth\Authorization;
44

5-
use CodeIgniter\Database\BaseResult;
6-
use CodeIgniter\Database\Query;
7-
use CodeIgniter\Model;
5+
use Myth\Auth\Models\PermissionModel as BaseModel;
86

9-
class PermissionModel extends Model
7+
/**
8+
* @deprecated 1.2.0 Use Myth\Auth\Models\PermissionModel instead
9+
*/
10+
class PermissionModel extends BaseModel
1011
{
11-
protected $table = 'auth_permissions';
12-
protected $allowedFields = [
13-
'name', 'description',
14-
];
15-
protected $useTimestamps = false;
16-
protected $validationRules = [
17-
'name' => 'required|max_length[255]|is_unique[auth_permissions.name,name,{name}]',
18-
'description' => 'max_length[255]',
19-
];
20-
21-
/**
22-
* Checks to see if a user, or one of their groups,
23-
* has a specific permission.
24-
*/
25-
public function doesUserHavePermission(int $userId, int $permissionId): bool
26-
{
27-
return array_key_exists($permissionId, $this->getPermissionsForUser($userId));
28-
}
29-
30-
/**
31-
* Adds a single permission to a single user.
32-
*
33-
* @return BaseResult|false|Query
34-
*/
35-
public function addPermissionToUser(int $permissionId, int $userId)
36-
{
37-
cache()->delete("{$userId}_permissions");
38-
39-
return $this->db->table('auth_users_permissions')->insert([
40-
'user_id' => $userId,
41-
'permission_id' => $permissionId,
42-
]);
43-
}
44-
45-
/**
46-
* Removes a permission from a user.
47-
*
48-
* @return mixed
49-
*/
50-
public function removePermissionFromUser(int $permissionId, int $userId)
51-
{
52-
$this->db->table('auth_users_permissions')->where([
53-
'user_id' => $userId,
54-
'permission_id' => $permissionId,
55-
])->delete();
56-
57-
cache()->delete("{$userId}_permissions");
58-
}
59-
60-
/**
61-
* Gets all permissions for a user in a way that can be
62-
* easily used to check against:
63-
*
64-
* [
65-
* id => name,
66-
* id => name
67-
* ]
68-
*/
69-
public function getPermissionsForUser(int $userId): array
70-
{
71-
if (null === $found = cache("{$userId}_permissions")) {
72-
$fromUser = $this->db->table('auth_users_permissions')
73-
->select('id, auth_permissions.name')
74-
->join('auth_permissions', 'auth_permissions.id = permission_id', 'inner')
75-
->where('user_id', $userId)
76-
->get()
77-
->getResultObject();
78-
$fromGroup = $this->db->table('auth_groups_users')
79-
->select('auth_permissions.id, auth_permissions.name')
80-
->join('auth_groups_permissions', 'auth_groups_permissions.group_id = auth_groups_users.group_id', 'inner')
81-
->join('auth_permissions', 'auth_permissions.id = auth_groups_permissions.permission_id', 'inner')
82-
->where('user_id', $userId)
83-
->get()
84-
->getResultObject();
85-
86-
$combined = array_merge($fromUser, $fromGroup);
87-
88-
$found = [];
89-
90-
foreach ($combined as $row) {
91-
$found[$row->id] = strtolower($row->name);
92-
}
93-
94-
cache()->save("{$userId}_permissions", $found, 300);
95-
}
96-
97-
return $found;
98-
}
12+
protected $returnType = 'array';
9913
}

src/Entities/Group.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Myth\Auth\Entities;
4+
5+
use CodeIgniter\Entity\Entity;
6+
7+
/**
8+
* Group Entity
9+
*
10+
* As of version 1.2 this class is used by the new GroupModel
11+
* to allow using a strongly-typed return. Any logic in this
12+
* class should not be relied on within this library.
13+
*
14+
* @since 1.2.0
15+
*/
16+
class Group extends Entity
17+
{
18+
}

src/Entities/Permission.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Myth\Auth\Entities;
4+
5+
use CodeIgniter\Entity\Entity;
6+
7+
/**
8+
* Permission Entity
9+
*
10+
* As of version 1.2 this class is used by the new PermissionModel
11+
* to allow using a strongly-typed return. Any logic in this
12+
* class should not be relied on within this library.
13+
*
14+
* @since 1.2.0
15+
*/
16+
class Permission extends Entity
17+
{
18+
}

0 commit comments

Comments
 (0)