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