Commit | Line | Data |
---|---|---|
87d3a054 MS |
1 | <?php |
2 | namespace wcf\system\condition; | |
3 | use wcf\data\condition\Condition; | |
4 | use wcf\data\user\User; | |
e62dacea | 5 | use wcf\data\user\UserList; |
23d57595 | 6 | use wcf\data\DatabaseObjectList; |
87d3a054 MS |
7 | use wcf\system\exception\UserInputException; |
8 | use wcf\system\WCF; | |
9 | ||
10 | /** | |
11 | * Condition implementation for the state (banned, enabled) of a user. | |
12 | * | |
13 | * @author Matthias Schmidt | |
f2a4a429 | 14 | * @copyright 2001-2016 WoltLab GmbH |
87d3a054 MS |
15 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
16 | * @package com.woltlab.wcf | |
17 | * @subpackage system.condition | |
18 | * @category Community Framework | |
19 | */ | |
23d57595 MS |
20 | class UserStateCondition extends AbstractSingleFieldCondition implements IContentCondition, IObjectListCondition, IUserCondition { |
21 | use TObjectListUserCondition; | |
22 | ||
87d3a054 | 23 | /** |
f2a4a429 | 24 | * @inheritDoc |
87d3a054 MS |
25 | */ |
26 | protected $label = 'wcf.user.condition.state'; | |
27 | ||
28 | /** | |
29 | * true if the the user has to be banned | |
30 | * @var integer | |
31 | */ | |
32 | protected $userIsBanned = 0; | |
33 | ||
34 | /** | |
35 | * true if the user has to be disabled | |
36 | * @var integer | |
37 | */ | |
38 | protected $userIsDisabled = 0; | |
39 | ||
40 | /** | |
41 | * true if the user has to be enabled | |
42 | * @var integer | |
43 | */ | |
44 | protected $userIsEnabled = 0; | |
45 | ||
46 | /** | |
47 | * true if the the user may not be banned | |
48 | * @var integer | |
49 | */ | |
50 | protected $userIsNotBanned = 0; | |
51 | ||
52 | /** | |
f2a4a429 | 53 | * @inheritDoc |
87d3a054 | 54 | */ |
23d57595 | 55 | public function addObjectListCondition(DatabaseObjectList $objectList, array $conditionData) { |
f2a4a429 | 56 | if (!($objectList instanceof UserList)) { |
a92f7b96 | 57 | throw new \InvalidArgumentException("Object list is no instance of '".UserList::class."', instance of '".get_class($objectList)."' given."); |
f2a4a429 | 58 | } |
23d57595 MS |
59 | |
60 | if (isset($conditionData['userIsBanned'])) { | |
f2a4a429 | 61 | $objectList->getConditionBuilder()->add('user_table.banned = ?', [$conditionData['userIsBanned']]); |
87d3a054 MS |
62 | } |
63 | ||
23d57595 MS |
64 | if ($conditionData['userIsEnabled']) { |
65 | if ($conditionData['userIsEnabled']) { | |
f2a4a429 | 66 | $objectList->getConditionBuilder()->add('user_table.activationCode = ?', [0]); |
87d3a054 MS |
67 | } |
68 | else { | |
f2a4a429 | 69 | $objectList->getConditionBuilder()->add('user_table.activationCode <> ?', [0]); |
87d3a054 MS |
70 | } |
71 | } | |
72 | } | |
73 | ||
74 | /** | |
f2a4a429 | 75 | * @inheritDoc |
87d3a054 MS |
76 | */ |
77 | public function checkUser(Condition $condition, User $user) { | |
78 | if ($condition->userIsBanned !== null && $user->banned != $condition->userIsBanned) { | |
79 | return false; | |
80 | } | |
81 | ||
82 | if ($condition->userIsEnabled !== null) { | |
83 | if ($condition->userIsEnabled && $user->activationCode) { | |
84 | return false; | |
85 | } | |
86 | else if (!$condition->userIsEnabled && !$user->activationCode) { | |
87 | return false; | |
88 | } | |
89 | } | |
90 | ||
91 | return true; | |
92 | } | |
93 | ||
94 | /** | |
f2a4a429 | 95 | * @inheritDoc |
87d3a054 MS |
96 | */ |
97 | public function getData() { | |
f2a4a429 | 98 | $data = []; |
87d3a054 MS |
99 | |
100 | if ($this->userIsBanned) { | |
101 | $data['userIsBanned'] = 1; | |
102 | } | |
103 | else if ($this->userIsNotBanned) { | |
104 | $data['userIsBanned'] = 0; | |
105 | } | |
106 | if ($this->userIsEnabled) { | |
107 | $data['userIsEnabled'] = 1; | |
108 | } | |
109 | else if ($this->userIsDisabled) { | |
110 | $data['userIsEnabled'] = 0; | |
111 | } | |
112 | ||
113 | if (!empty($data)) { | |
114 | return $data; | |
115 | } | |
116 | ||
117 | return null; | |
118 | } | |
119 | ||
120 | /** | |
121 | * Returns the "checked" attribute for an input element. | |
122 | * | |
123 | * @param string $propertyName | |
124 | * @return string | |
125 | */ | |
126 | protected function getCheckedAttribute($propertyName) { | |
7c9f2241 | 127 | /** @noinspection PhpVariableVariableInspection */ |
87d3a054 MS |
128 | if ($this->$propertyName) { |
129 | return ' checked="checked"'; | |
130 | } | |
131 | ||
132 | return ''; | |
133 | } | |
134 | ||
135 | /** | |
f2a4a429 | 136 | * @inheritDoc |
87d3a054 MS |
137 | */ |
138 | protected function getFieldElement() { | |
139 | $userIsNotBanned = WCF::getLanguage()->get('wcf.user.condition.state.isNotBanned'); | |
140 | $userIsBanned = WCF::getLanguage()->get('wcf.user.condition.state.isBanned'); | |
141 | $userIsDisabled = WCF::getLanguage()->get('wcf.user.condition.state.isDisabled'); | |
142 | $userIsEnabled = WCF::getLanguage()->get('wcf.user.condition.state.isEnabled'); | |
143 | ||
144 | return <<<HTML | |
ef1f5b4a MW |
145 | <label><input type="checkbox" name="userIsBanned" value="1"{$this->getCheckedAttribute('userIsBanned')} /> {$userIsBanned}</label> |
146 | <label><input type="checkbox" name="userIsNotBanned" value="1"{$this->getCheckedAttribute('userIsNotBanned')} /> {$userIsNotBanned}</label> | |
147 | <label><input type="checkbox" name="userIsEnabled" value="1"{$this->getCheckedAttribute('userIsEnabled')} /> {$userIsEnabled}</label> | |
148 | <label><input type="checkbox" name="userIsDisabled" value="1"{$this->getCheckedAttribute('userIsDisabled')} /> {$userIsDisabled}</label> | |
87d3a054 MS |
149 | HTML; |
150 | } | |
151 | ||
152 | /** | |
f2a4a429 | 153 | * @inheritDoc |
87d3a054 MS |
154 | */ |
155 | public function readFormParameters() { | |
156 | if (isset($_POST['userIsBanned'])) $this->userIsBanned = 1; | |
157 | if (isset($_POST['userIsDisabled'])) $this->userIsDisabled = 1; | |
158 | if (isset($_POST['userIsEnabled'])) $this->userIsEnabled = 1; | |
159 | if (isset($_POST['userIsNotBanned'])) $this->userIsNotBanned = 1; | |
160 | } | |
161 | ||
162 | /** | |
f2a4a429 | 163 | * @inheritDoc |
87d3a054 MS |
164 | */ |
165 | public function reset() { | |
166 | $this->userIsBanned = 0; | |
167 | $this->userIsDisabled = 0; | |
168 | $this->userIsEnabled = 0; | |
169 | $this->userIsNotBanned = 0; | |
170 | } | |
171 | ||
172 | /** | |
f2a4a429 | 173 | * @inheritDoc |
87d3a054 MS |
174 | */ |
175 | public function setData(Condition $condition) { | |
176 | if ($condition->userIsBanned !== null) { | |
177 | $this->userIsBanned = $condition->userIsBanned; | |
178 | $this->userIsNotBanned = !$condition->userIsBanned; | |
179 | } | |
180 | if ($condition->userIsEnabled !== null) { | |
181 | $this->userIsEnabled = $condition->userIsEnabled; | |
182 | $this->userIsDisabled = !$condition->userIsEnabled; | |
183 | } | |
184 | } | |
185 | ||
186 | /** | |
f2a4a429 | 187 | * @inheritDoc |
87d3a054 MS |
188 | */ |
189 | public function validate() { | |
190 | if ($this->userIsBanned && $this->userIsNotBanned) { | |
191 | $this->errorMessage = 'wcf.user.condition.state.isBanned.error.conflict'; | |
192 | ||
193 | throw new UserInputException('userIsBanned', 'conflict'); | |
194 | } | |
195 | ||
196 | if ($this->userIsDisabled && $this->userIsEnabled) { | |
197 | $this->errorMessage = 'wcf.user.condition.state.isEnabled.error.conflict'; | |
198 | ||
2b755412 | 199 | throw new UserInputException('userIsEnabled', 'conflict'); |
87d3a054 MS |
200 | } |
201 | } | |
20933e61 MS |
202 | |
203 | /** | |
f2a4a429 | 204 | * @inheritDoc |
20933e61 | 205 | */ |
1bf25f6b | 206 | public function showContent(Condition $condition) { |
64571a65 | 207 | if (!WCF::getUser()->userID) return false; |
20933e61 MS |
208 | |
209 | return $this->checkUser($condition, WCF::getUser()); | |
210 | } | |
87d3a054 | 211 | } |