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 | |
2b6cb5c2 | 14 | * @copyright 2001-2015 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 MS |
23 | /** |
24 | * @see \wcf\system\condition\AbstractSingleFieldCondition::$label | |
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 | /** | |
23d57595 | 53 | * @see \wcf\system\condition\IObjectListCondition::addObjectListCondition() |
87d3a054 | 54 | */ |
23d57595 MS |
55 | public function addObjectListCondition(DatabaseObjectList $objectList, array $conditionData) { |
56 | if (!($objectList instanceof UserList)) return; | |
57 | ||
58 | if (isset($conditionData['userIsBanned'])) { | |
e90198a7 | 59 | $objectList->getConditionBuilder()->add('user_table.banned = ?', array($conditionData['userIsBanned'])); |
87d3a054 MS |
60 | } |
61 | ||
23d57595 MS |
62 | if ($conditionData['userIsEnabled']) { |
63 | if ($conditionData['userIsEnabled']) { | |
e90198a7 | 64 | $objectList->getConditionBuilder()->add('user_table.activationCode = ?', array(0)); |
87d3a054 MS |
65 | } |
66 | else { | |
e90198a7 | 67 | $objectList->getConditionBuilder()->add('user_table.activationCode <> ?', array(0)); |
87d3a054 MS |
68 | } |
69 | } | |
70 | } | |
71 | ||
72 | /** | |
73 | * @see \wcf\system\condition\IUserCondition::checkUser() | |
74 | */ | |
75 | public function checkUser(Condition $condition, User $user) { | |
76 | if ($condition->userIsBanned !== null && $user->banned != $condition->userIsBanned) { | |
77 | return false; | |
78 | } | |
79 | ||
80 | if ($condition->userIsEnabled !== null) { | |
81 | if ($condition->userIsEnabled && $user->activationCode) { | |
82 | return false; | |
83 | } | |
84 | else if (!$condition->userIsEnabled && !$user->activationCode) { | |
85 | return false; | |
86 | } | |
87 | } | |
88 | ||
89 | return true; | |
90 | } | |
91 | ||
92 | /** | |
93 | * @see \wcf\system\condition\ICondition::getData() | |
94 | */ | |
95 | public function getData() { | |
96 | $data = array(); | |
97 | ||
98 | if ($this->userIsBanned) { | |
99 | $data['userIsBanned'] = 1; | |
100 | } | |
101 | else if ($this->userIsNotBanned) { | |
102 | $data['userIsBanned'] = 0; | |
103 | } | |
104 | if ($this->userIsEnabled) { | |
105 | $data['userIsEnabled'] = 1; | |
106 | } | |
107 | else if ($this->userIsDisabled) { | |
108 | $data['userIsEnabled'] = 0; | |
109 | } | |
110 | ||
111 | if (!empty($data)) { | |
112 | return $data; | |
113 | } | |
114 | ||
115 | return null; | |
116 | } | |
117 | ||
118 | /** | |
119 | * Returns the "checked" attribute for an input element. | |
120 | * | |
121 | * @param string $propertyName | |
122 | * @return string | |
123 | */ | |
124 | protected function getCheckedAttribute($propertyName) { | |
125 | if ($this->$propertyName) { | |
126 | return ' checked="checked"'; | |
127 | } | |
128 | ||
129 | return ''; | |
130 | } | |
131 | ||
132 | /** | |
133 | * @see \wcf\system\condition\AbstractSingleFieldCondition::getFieldElement() | |
134 | */ | |
135 | protected function getFieldElement() { | |
136 | $userIsNotBanned = WCF::getLanguage()->get('wcf.user.condition.state.isNotBanned'); | |
137 | $userIsBanned = WCF::getLanguage()->get('wcf.user.condition.state.isBanned'); | |
138 | $userIsDisabled = WCF::getLanguage()->get('wcf.user.condition.state.isDisabled'); | |
139 | $userIsEnabled = WCF::getLanguage()->get('wcf.user.condition.state.isEnabled'); | |
140 | ||
141 | return <<<HTML | |
142 | <label><input type="checkbox" name="userIsBanned" value="1"{$this->getCheckedAttribute('userIsBanned')} /> {$userIsBanned}</label> | |
143 | <label><input type="checkbox" name="userIsNotBanned" value="1"{$this->getCheckedAttribute('userIsNotBanned')} /> {$userIsNotBanned}</label> | |
144 | <label><input type="checkbox" name="userIsEnabled" value="1"{$this->getCheckedAttribute('userIsEnabled')} /> {$userIsEnabled}</label> | |
145 | <label><input type="checkbox" name="userIsDisabled" value="1"{$this->getCheckedAttribute('userIsDisabled')} /> {$userIsDisabled}</label> | |
146 | HTML; | |
147 | } | |
148 | ||
149 | /** | |
150 | * @see \wcf\system\condition\ICondition::readFormParameters() | |
151 | */ | |
152 | public function readFormParameters() { | |
153 | if (isset($_POST['userIsBanned'])) $this->userIsBanned = 1; | |
154 | if (isset($_POST['userIsDisabled'])) $this->userIsDisabled = 1; | |
155 | if (isset($_POST['userIsEnabled'])) $this->userIsEnabled = 1; | |
156 | if (isset($_POST['userIsNotBanned'])) $this->userIsNotBanned = 1; | |
157 | } | |
158 | ||
159 | /** | |
160 | * @see \wcf\system\condition\ICondition::reset() | |
161 | */ | |
162 | public function reset() { | |
163 | $this->userIsBanned = 0; | |
164 | $this->userIsDisabled = 0; | |
165 | $this->userIsEnabled = 0; | |
166 | $this->userIsNotBanned = 0; | |
167 | } | |
168 | ||
169 | /** | |
170 | * @see \wcf\system\condition\ICondition::setData() | |
171 | */ | |
172 | public function setData(Condition $condition) { | |
173 | if ($condition->userIsBanned !== null) { | |
174 | $this->userIsBanned = $condition->userIsBanned; | |
175 | $this->userIsNotBanned = !$condition->userIsBanned; | |
176 | } | |
177 | if ($condition->userIsEnabled !== null) { | |
178 | $this->userIsEnabled = $condition->userIsEnabled; | |
179 | $this->userIsDisabled = !$condition->userIsEnabled; | |
180 | } | |
181 | } | |
182 | ||
183 | /** | |
184 | * @see \wcf\system\condition\ICondition::validate() | |
185 | */ | |
186 | public function validate() { | |
187 | if ($this->userIsBanned && $this->userIsNotBanned) { | |
188 | $this->errorMessage = 'wcf.user.condition.state.isBanned.error.conflict'; | |
189 | ||
190 | throw new UserInputException('userIsBanned', 'conflict'); | |
191 | } | |
192 | ||
193 | if ($this->userIsDisabled && $this->userIsEnabled) { | |
194 | $this->errorMessage = 'wcf.user.condition.state.isEnabled.error.conflict'; | |
195 | ||
2b755412 | 196 | throw new UserInputException('userIsEnabled', 'conflict'); |
87d3a054 MS |
197 | } |
198 | } | |
20933e61 MS |
199 | |
200 | /** | |
1bf25f6b | 201 | * @see \wcf\system\condition\IContentCondition::showContent() |
20933e61 | 202 | */ |
1bf25f6b | 203 | public function showContent(Condition $condition) { |
64571a65 | 204 | if (!WCF::getUser()->userID) return false; |
20933e61 MS |
205 | |
206 | return $this->checkUser($condition, WCF::getUser()); | |
207 | } | |
87d3a054 | 208 | } |