Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 57 | class LostController extends Controller { |
||
| 58 | |||
| 59 | /** @var IURLGenerator */ |
||
| 60 | protected $urlGenerator; |
||
| 61 | /** @var IUserManager */ |
||
| 62 | protected $userManager; |
||
| 63 | /** @var Defaults */ |
||
| 64 | protected $defaults; |
||
| 65 | /** @var IL10N */ |
||
| 66 | protected $l10n; |
||
| 67 | /** @var string */ |
||
| 68 | protected $from; |
||
| 69 | /** @var IManager */ |
||
| 70 | protected $encryptionManager; |
||
| 71 | /** @var IConfig */ |
||
| 72 | protected $config; |
||
| 73 | /** @var ISecureRandom */ |
||
| 74 | protected $secureRandom; |
||
| 75 | /** @var IMailer */ |
||
| 76 | protected $mailer; |
||
| 77 | /** @var ITimeFactory */ |
||
| 78 | protected $timeFactory; |
||
| 79 | /** @var ICrypto */ |
||
| 80 | protected $crypto; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param string $appName |
||
| 84 | * @param IRequest $request |
||
| 85 | * @param IURLGenerator $urlGenerator |
||
| 86 | * @param IUserManager $userManager |
||
| 87 | * @param Defaults $defaults |
||
| 88 | * @param IL10N $l10n |
||
| 89 | * @param IConfig $config |
||
| 90 | * @param ISecureRandom $secureRandom |
||
| 91 | * @param string $defaultMailAddress |
||
| 92 | * @param IManager $encryptionManager |
||
| 93 | * @param IMailer $mailer |
||
| 94 | * @param ITimeFactory $timeFactory |
||
| 95 | * @param ICrypto $crypto |
||
| 96 | */ |
||
| 97 | View Code Duplication | public function __construct($appName, |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Someone wants to reset their password: |
||
| 126 | * |
||
| 127 | * @PublicPage |
||
| 128 | * @NoCSRFRequired |
||
| 129 | * |
||
| 130 | * @param string $token |
||
| 131 | * @param string $userId |
||
| 132 | * @return TemplateResponse |
||
| 133 | */ |
||
| 134 | public function resetform($token, $userId) { |
||
| 135 | if ($this->config->getSystemValue('lost_password_link', '') !== '') { |
||
| 136 | return new TemplateResponse('core', 'error', [ |
||
| 137 | 'errors' => [['error' => $this->l10n->t('Password reset is disabled')]] |
||
| 138 | ], |
||
| 139 | 'guest' |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | |||
| 143 | try { |
||
| 144 | $this->checkPasswordResetToken($token, $userId); |
||
| 145 | } catch (\Exception $e) { |
||
| 146 | return new TemplateResponse( |
||
| 147 | 'core', 'error', [ |
||
| 148 | "errors" => array(array("error" => $e->getMessage())) |
||
| 149 | ], |
||
| 150 | 'guest' |
||
| 151 | ); |
||
| 152 | } |
||
| 153 | |||
| 154 | return new TemplateResponse( |
||
| 155 | 'core', |
||
| 156 | 'lostpassword/resetpassword', |
||
| 157 | array( |
||
| 158 | 'link' => $this->urlGenerator->linkToRouteAbsolute('core.lost.setPassword', array('userId' => $userId, 'token' => $token)), |
||
| 159 | ), |
||
| 160 | 'guest' |
||
| 161 | ); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param string $token |
||
| 166 | * @param string $userId |
||
| 167 | * @throws \Exception |
||
| 168 | */ |
||
| 169 | protected function checkPasswordResetToken($token, $userId) { |
||
| 170 | $user = $this->userManager->get($userId); |
||
| 171 | if($user === null || !$user->isEnabled()) { |
||
| 172 | throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
||
| 173 | } |
||
| 174 | |||
| 175 | try { |
||
| 176 | $encryptedToken = $this->config->getUserValue($userId, 'core', 'lostpassword', null); |
||
| 177 | $mailAddress = !is_null($user->getEMailAddress()) ? $user->getEMailAddress() : ''; |
||
| 178 | $decryptedToken = $this->crypto->decrypt($encryptedToken, $mailAddress.$this->config->getSystemValue('secret')); |
||
| 179 | } catch (\Exception $e) { |
||
| 180 | throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
||
| 181 | } |
||
| 182 | |||
| 183 | $splittedToken = explode(':', $decryptedToken); |
||
| 184 | if(count($splittedToken) !== 2) { |
||
| 185 | throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
||
| 186 | } |
||
| 187 | |||
| 188 | if ($splittedToken[0] < ($this->timeFactory->getTime() - 60*60*12) || |
||
| 189 | $user->getLastLogin() > $splittedToken[0]) { |
||
| 190 | throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is expired')); |
||
| 191 | } |
||
| 192 | |||
| 193 | if (!hash_equals($splittedToken[1], $token)) { |
||
| 194 | throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param $message |
||
| 200 | * @param array $additional |
||
| 201 | * @return array |
||
| 202 | */ |
||
| 203 | private function error($message, array $additional=array()) { |
||
| 204 | return array_merge(array('status' => 'error', 'msg' => $message), $additional); |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @return array |
||
| 209 | */ |
||
| 210 | private function success() { |
||
| 211 | return array('status'=>'success'); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @PublicPage |
||
| 216 | * @BruteForceProtection(action=passwordResetEmail) |
||
| 217 | * @AnonRateThrottle(limit=10, period=300) |
||
| 218 | * |
||
| 219 | * @param string $user |
||
| 220 | * @return JSONResponse |
||
| 221 | */ |
||
| 222 | public function email($user){ |
||
| 223 | if ($this->config->getSystemValue('lost_password_link', '') !== '') { |
||
| 224 | return new JSONResponse($this->error($this->l10n->t('Password reset is disabled'))); |
||
| 225 | } |
||
| 226 | |||
| 227 | \OCP\Util::emitHook( |
||
| 228 | '\OCA\Files_Sharing\API\Server2Server', |
||
| 229 | 'preLoginNameUsedAsUserName', |
||
| 230 | ['uid' => &$user] |
||
| 231 | ); |
||
| 232 | |||
| 233 | // FIXME: use HTTP error codes |
||
| 234 | try { |
||
| 235 | $this->sendEmail($user); |
||
| 236 | } catch (\Exception $e){ |
||
| 237 | $response = new JSONResponse($this->error($e->getMessage())); |
||
| 238 | $response->throttle(); |
||
| 239 | return $response; |
||
| 240 | } |
||
| 241 | |||
| 242 | $response = new JSONResponse($this->success()); |
||
| 243 | $response->throttle(); |
||
| 244 | return $response; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @PublicPage |
||
| 249 | * @param string $token |
||
| 250 | * @param string $userId |
||
| 251 | * @param string $password |
||
| 252 | * @param boolean $proceed |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | public function setPassword($token, $userId, $password, $proceed) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param string $input |
||
| 287 | * @throws \Exception |
||
| 288 | */ |
||
| 289 | protected function sendEmail($input) { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param string $input |
||
| 350 | * @return IUser |
||
| 351 | * @throws \InvalidArgumentException |
||
| 352 | */ |
||
| 353 | protected function findUserByIdOrMail($input) { |
||
| 374 | } |
||
| 375 |
If you suppress an error, we recommend checking for the error condition explicitly: