public function createHash($user_id, $action, $config = null) { $hash = Str::random($this->hashLen()); if (ConfirmUser::findHash($hash)->exists()) { $hash = self::createHash($user_id, $action, $config); } else { $data = compact('user_id', 'hash', 'action', 'config'); //$record = ConfirmUser::firstOrNew(compact('user_id','action')); $record = ConfirmUser::CheckRecord($user_id, $action); if ($record->exists() && !$record->first()->update($data) || !$record->exists() && !ConfirmUser::create($data)) { throw new Exception\ConfirmModelSaveException('Error save or update model'); } } return $hash; }
public function check($hash) { $out = null; if (is_scalar($hash)) { $confirumObj = ConfirmUser::findHash($hash); if (!$confirumObj->exists()) { throw new Exception\ConfirmHashNotFoundException('Confirm hash ' . $hash . ' not found'); } $confirumObj = $confirumObj->first(); if (is_callable($confirumObj->getCallback())) { $out = call_user_func($confirumObj->getCallback(), $confirumObj->user, $confirumObj->getConfigField()); $confirumObj->delete(); } else { throw new Exception\ConfirmCallbackException('Callback ' . $confirumObj->action . ' not found'); } } else { throw new Exception\ConfirmIncorrectHashException('Incorrect confirm hash'); } return $out; }