1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace flipbox\saml\core\models; |
5
|
|
|
|
6
|
|
|
use craft\elements\User; |
7
|
|
|
use yii\base\InvalidArgumentException; |
8
|
|
|
use yii\base\Model; |
9
|
|
|
|
10
|
|
|
class GroupOptions extends Model implements \JsonSerializable |
11
|
|
|
{ |
12
|
|
|
const OPTION_SYNC = 'sync'; |
13
|
|
|
const OPTION_ALLOW = 'allow'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var int[] |
17
|
|
|
*/ |
18
|
|
|
private $sync = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int[] |
22
|
|
|
*/ |
23
|
|
|
private $allow = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param array $groups |
27
|
|
|
* @return GroupOptions |
28
|
|
|
*/ |
29
|
|
|
public function setSync(array $groups) |
30
|
|
|
{ |
31
|
|
|
return $this->setOption(self::OPTION_SYNC, $groups); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return int[] |
36
|
|
|
*/ |
37
|
|
|
public function getSync() |
38
|
|
|
{ |
39
|
|
|
return $this->sync; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param $id |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
|
|
public function shouldSync($id): bool |
47
|
|
|
{ |
48
|
|
|
return in_array($id, $this->sync); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param array $groups |
53
|
|
|
* @return GroupOptions |
54
|
|
|
*/ |
55
|
|
|
public function setAllow(array $groups) |
56
|
|
|
{ |
57
|
|
|
return $this->setOption(self::OPTION_ALLOW, $groups); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return int[] |
63
|
|
|
*/ |
64
|
|
|
public function getAllow() |
65
|
|
|
{ |
66
|
|
|
return $this->allow; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $id |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function shouldAllow($id): bool |
74
|
|
|
{ |
75
|
|
|
return in_array($id, $this->allow); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param User $user |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public function shouldDenyNoGroupAssigned(User $user): bool |
83
|
|
|
{ |
84
|
|
|
return empty($user->getGroups()) && in_array('nogroup', $this->allow); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array $options |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
|
|
public function setOptions(array $options) |
92
|
|
|
{ |
93
|
|
|
|
94
|
|
|
if (isset($options[self::OPTION_SYNC])) { |
95
|
|
|
$this->setSync($options[self::OPTION_SYNC]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (isset($options[self::OPTION_ALLOW])) { |
99
|
|
|
$this->setAllow($options[self::OPTION_ALLOW]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param $option |
107
|
|
|
* @param array $groups |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
private function setOption($option, array $groups) |
111
|
|
|
{ |
112
|
|
|
if (! in_array($option, [static::OPTION_SYNC, static::OPTION_ALLOW])) { |
113
|
|
|
throw new InvalidArgumentException('Option not valid.'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
foreach ($groups as $group) { |
117
|
|
|
if (empty($group)) { |
118
|
|
|
continue; |
119
|
|
|
} |
120
|
|
|
$this->{$option}[] = is_numeric($group) ? (int)$group : $group; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
public function jsonSerialize() |
130
|
|
|
{ |
131
|
|
|
return [ |
132
|
|
|
'sync' => $this->sync, |
133
|
|
|
'allow' => $this->allow, |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|