1
|
|
|
<?php |
2
|
|
|
namespace Germania\Permissions; |
3
|
|
|
|
4
|
|
|
use Psr\Log\LoggerInterface; |
5
|
|
|
use Psr\Log\LoggerAwareTrait; |
6
|
|
|
use Psr\Log\NullLogger; |
7
|
|
|
|
8
|
|
|
class PdoUpdatePermissionRoles |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
use LoggerAwareTrait; |
12
|
|
|
|
13
|
|
|
public $permissions; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var \PDO |
17
|
|
|
*/ |
18
|
|
|
public $pdo; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \PDOStatement |
22
|
|
|
*/ |
23
|
|
|
public $stmt; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $permissions_table = "permissions"; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public $permissions_roles_table = "permissions_roles"; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Seperator string for roles in SELECT statement |
37
|
|
|
*/ |
38
|
|
|
protected $separator = ","; |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var PdoAssignRoleToPermission |
43
|
|
|
*/ |
44
|
|
|
public $assigner; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var PdoAssignRoleToPermission |
48
|
|
|
*/ |
49
|
|
|
public $unassigner; |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param \PDO $pdo PDO instance |
55
|
|
|
* @param string $permissions_table Permissions table name |
56
|
|
|
* @param string $permissions_roles_table Permissions/Roles table name |
57
|
|
|
* @param LoggerInterface|null $logger Optional: PSR-3 Logger |
58
|
|
|
*/ |
59
|
|
|
public function __construct( \PDO $pdo, $permissions_table, $permissions_roles_table, LoggerInterface $logger = null ) |
60
|
|
|
{ |
61
|
|
|
$this->setLogger( $logger ?: new NullLogger ); |
62
|
|
|
|
63
|
|
|
// Prerequisites |
64
|
|
|
$this->pdo = $pdo; |
65
|
|
|
$this->permissions_table = $permissions_table; |
66
|
|
|
$this->permissions_roles_table = $permissions_roles_table; |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
$this->assigner = new PdoAssignRoleToPermission( $pdo, $permissions_roles_table, $logger); |
70
|
|
|
$this->unassigner = new PdoUnassignRoleFromPermission( $pdo, $permissions_roles_table, $logger); |
|
|
|
|
71
|
|
|
$this->permissions = new PdoAllPermissions($pdo, $permissions_table, $permissions_roles_table, $logger); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return int Last Insert ID or FALSE when something errored |
77
|
|
|
* @throws PermissionNameExistsException On duplicate name |
78
|
|
|
*/ |
79
|
|
|
public function __invoke( $permission_name, $role_ids ) |
80
|
|
|
{ |
81
|
|
|
|
82
|
|
|
try { |
83
|
|
|
$permission = $this->permissions->get( $permission_name ); |
84
|
|
|
} |
85
|
|
|
catch (PermissionNotFoundException $e) { |
86
|
|
|
throw $e; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
$permission_id = $permission['id']; |
91
|
|
|
$assigned_roles = explode( $this->separator, $permission['assigned_roles']); |
92
|
|
|
|
93
|
|
|
$assigner = $this->assigner; |
94
|
|
|
$unassigner = $this->unassigner; |
95
|
|
|
|
96
|
|
|
$to_add = array_diff($role_ids, $assigned_roles); |
97
|
|
|
foreach( $to_add as $role_id): |
98
|
|
|
try { |
99
|
|
|
$assigner( $permission_id, $role_id); |
100
|
|
|
} |
101
|
|
|
catch (RolePermissionAssignmentExistsException $e) { |
102
|
|
|
// noop |
103
|
|
|
} |
104
|
|
|
endforeach; |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
$to_remove = array_diff($assigned_roles, $role_ids); |
108
|
|
|
foreach( $to_remove as $role_id): |
109
|
|
|
$unassigner( $permission_id, $role_id); |
110
|
|
|
endforeach; |
111
|
|
|
|
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..