|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Itstructure\LaRbac\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Foundation\Auth\User as ParentUser; |
|
7
|
|
|
use Itstructure\LaRbac\Interfaces\RbacUserInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Helper |
|
11
|
|
|
* |
|
12
|
|
|
* @package Itstructure\LaRbac\Helpers |
|
13
|
|
|
* |
|
14
|
|
|
* @author Andrey Girnik <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class Helper |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Check user model for be implemented by RbacUserInterface and extended of parent Auth User model. |
|
20
|
|
|
* @param string|null $userModelClass |
|
21
|
|
|
* @throws Exception |
|
22
|
|
|
*/ |
|
23
|
|
|
public static function checkUserModel(string $userModelClass = null) |
|
24
|
|
|
{ |
|
25
|
|
|
if (empty($userModelClass)) { |
|
26
|
|
|
throw new Exception('User Model class is not defined in "rbac" config file.'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
static::checkUserModelInterface($userModelClass, RbacUserInterface::class); |
|
30
|
|
|
|
|
31
|
|
|
static::checkUserModelParent($userModelClass, ParentUser::class); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Check user model for be implemented by RbacUserInterface. |
|
36
|
|
|
* @param string $userModelClass |
|
37
|
|
|
* @param string $interfaceClass |
|
38
|
|
|
* @throws Exception |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function checkUserModelInterface(string $userModelClass, string $interfaceClass): void |
|
41
|
|
|
{ |
|
42
|
|
|
$userModelInterfaces = class_implements($userModelClass); |
|
43
|
|
|
|
|
44
|
|
|
if (!isset($userModelInterfaces[$interfaceClass])) { |
|
45
|
|
|
throw new Exception('User Model class must be implemented from "'.$interfaceClass.'".'); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Check user model for instance of parent Auth User model. |
|
51
|
|
|
* @param string $userModelClass |
|
52
|
|
|
* @param string $parentClass |
|
53
|
|
|
* @throws Exception |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function checkUserModelParent(string $userModelClass, string $parentClass): void |
|
56
|
|
|
{ |
|
57
|
|
|
$userModelParents = class_parents($userModelClass); |
|
58
|
|
|
|
|
59
|
|
|
if (!isset($userModelParents[$parentClass])) { |
|
60
|
|
|
throw new Exception('User Model class should be extended from "'.$parentClass.'".'); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Check a primary key type of a User model. |
|
66
|
|
|
* @param ParentUser $userModelObject |
|
67
|
|
|
* @throws Exception |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function checkUserModelKeyType(ParentUser $userModelObject): void |
|
70
|
|
|
{ |
|
71
|
|
|
if (!in_array($userModelObject->getKeyType(), ['int', 'integer'])) { |
|
72
|
|
|
throw new Exception('User Model keyType must be type of "int".'); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Check a primary key type of a users table. |
|
78
|
|
|
* @param string $userTablePrimaryType |
|
79
|
|
|
* @param string $userModelKeyName |
|
80
|
|
|
* @param string $userModelTable |
|
81
|
|
|
* @throws Exception |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function checkUserTablePrimaryType(string $userTablePrimaryType, string $userModelKeyName, string $userModelTable): void |
|
84
|
|
|
{ |
|
85
|
|
|
if (!in_array($userTablePrimaryType, ['bigint', 'integer'])) { |
|
86
|
|
|
throw new Exception('Primary key "'.$userModelKeyName.'" in "'.$userModelTable.'" table must be type of "bigint" or "integer"'); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Check for correct defining of an Admin user ID value at the beginning package installation. |
|
92
|
|
|
* @param int|null $adminUserId |
|
93
|
|
|
* @throws Exception |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function checkAdminUserId(int $adminUserId = null): void |
|
96
|
|
|
{ |
|
97
|
|
|
if (empty($adminUserId) || !is_int($adminUserId)) { |
|
98
|
|
|
throw new Exception('Identifier of a desired Admin user is not defined in "rbac" config file.'); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Retrieve user model entity. |
|
104
|
|
|
* @param string $userModelClass |
|
105
|
|
|
* @param int $adminUserId |
|
106
|
|
|
* @return mixed |
|
107
|
|
|
* @throws Exception |
|
108
|
|
|
*/ |
|
109
|
|
|
public static function retrieveUserModel(string $userModelClass, int $adminUserId) |
|
110
|
|
|
{ |
|
111
|
|
|
return call_user_func([ |
|
112
|
|
|
$userModelClass, |
|
113
|
|
|
'findOrFail', |
|
114
|
|
|
], $adminUserId); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|