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