/** * Authenticates a user based on {@link username} and {@link password}. * Derived classes should override this method, or an exception will be thrown. * This method is required by {@link IUserIdentity}. * @return boolean whether authentication succeeds. */ public function authenticate() { /** @var \Modules\User\Components\Auth $auth */ $auth = Mindy::app()->auth; if (strpos($this->username, "@")) { $model = User::objects()->get(['email' => strtolower($this->username)]); } else { $model = User::objects()->get(['username' => $this->username]); } if ($model === null) { if (strpos($this->username, "@")) { $this->errorCode = self::ERROR_EMAIL_INVALID; } else { $this->errorCode = self::ERROR_USERNAME_INVALID; } } else { if (!$model->is_active) { $this->errorCode = self::ERROR_INACTIVE; } else { if (!$auth->getPasswordHasher($model->hash_type)->verifyPassword($this->password, $model->password)) { $this->errorCode = self::ERROR_PASSWORD_INVALID; } else { $this->errorCode = self::ERROR_NONE; $this->setModel($model); } } } return !$this->errorCode; }
public static function preConfigure() { $tpl = Mindy::app()->template; $tpl->addHelper('cart', function () { return Mindy::app()->getModule('Cart')->getCart(); }); }
protected function getModels($module = null) { $checkModule = $module !== null; $models = []; $modelFiles = glob(Alias::get('Modules') . '/*/Models/*.php'); foreach (Alias::find('Contrib.*') as $alias) { $contribModels = glob($alias . '/*/Models/*.php'); if ($contribModels) { $modelFiles = array_merge($modelFiles, $contribModels); } } $modules = []; foreach ($modelFiles as $file) { $moduleName = basename(dirname(dirname($file))); if ($checkModule && $module == $moduleName && Mindy::app()->hasModule($moduleName) || !$checkModule && Mindy::app()->hasModule($moduleName)) { $modules[] = $moduleName; $class = str_replace('.php', '', substr($file, strpos($file, 'Modules'))); $class = str_replace('/', '\\', $class); if (is_subclass_of($class, Model::className())) { $reflectClass = new ReflectionClass($class); if ($reflectClass->isAbstract()) { continue; } $models[$class] = new $class(); } } } echo "Modules:" . PHP_EOL; echo implode(PHP_EOL, array_unique($modules)) . PHP_EOL; return $models; }
protected function getClient() { if ($this->_client === null) { $this->_client = new Client(Mindy::app()->getModule('Core')->repositoryUrl); } return $this->_client; }
public function makeChoices() { $choices = []; $all = []; $fieldsConfig = Mindy::app()->getModule('CustomRelation')->fields; $fieldConfig = $fieldsConfig[$this->field]; foreach ($fieldConfig as $relatedClass => $config) { $qs = $relatedClass::objects()->getQuerySet(); if (isset($config['filter'])) { $qs->filter($config['filter']); } if (isset($config['exclude'])) { $qs->exclude($config['exclude']); } if (isset($config['order'])) { $qs->order($config['order']); } $objects = $qs->all(); $title = $config['title']; $data = []; foreach ($objects as $object) { $data[$relatedClass . ':' . $object['id']] = (string) $object; } $all += $data; $choices[] = ['title' => $title, 'objects' => $data]; } return array($choices, $all); }
/** * @param $canonical string */ public function setCanonical($canonical) { if ($canonical instanceof Model) { $canonical = $canonical->getAbsoluteUrl(); } $this->canonical = Mindy::app()->request->http->absoluteUrl($canonical); }
/** * @param Request $request * @throws \Mindy\Exception\HttpException */ public function processRequest(Request $request) { if (Console::isCli() === false) { $authKey = $this->getToken($request); if (!empty($authKey)) { $key = Key::objects()->get(['key' => $authKey]); if ($key !== null) { $user = $key->user; $app = Mindy::app(); /** @var \Modules\Sites\SitesModule $siteModule */ $siteModule = $app->getModule('Sites'); if ($this->useSubDomains) { $site = $siteModule->getSite(); if ($user && $user->is_active && ($site->id == $user->site_id || $user->is_superuser)) { $app->auth->login($user); } } else { if ($user && $user->is_active) { $app->auth->login($user); $siteModule->setSite($user->site); } } } } } }
protected static function fetchMeta($uri) { $qs = Meta::objects()->filter(['url' => $uri]); if (Mindy::app()->getModule('Meta')->onSite) { $qs = $qs->currentSite(); } return $qs->limit(1)->get(); }
public function getColumns() { $columns = ['title', 'url']; if (Mindy::app()->getModule('Meta')->onSite) { $columns[] = 'site'; } return $columns; }
public function render($view, array $data = []) { $site = null; if (Mindy::app()->hasModule('Sites')) { $site = Mindy::app()->getModule('Sites')->getSite(); } return $this->renderTemplate($view, array_merge(['this' => $this, 'site' => $site, 'locale' => Mindy::app()->locale], $data)); }
public function init() { parent::init(); $onSite = Mindy::app()->getModule('Meta')->onSite; if (is_null($onSite) || $onSite === false) { $this->exclude[] = 'site'; } }
/** * Apply discount to CartItem position. If new prices is equal old price - return old price. * @param Cart $cart * @param CartItem $item * @return int|float new price with discount */ public function applyDiscount(Cart $cart, CartItem $item) { if (Mindy::app()->user->isGuest === false) { // Дарим скидку зарегистрированным пользователям return $item->getPrice() - 200; } else { return $item->getPrice(); } }
public function send() { if ($this->_model && $this->_model->objects()->changeActivationKey()) { $app = Mindy::app(); $recoverUrl = $app->urlManager->reverse('user:recover_activate', ['key' => $this->_model->activation_key]); return $app->mail->fromCode('user.recover', $this->_model->email, ['data' => $this->_model, 'username' => $this->_model->username, 'site' => $app->getModule('Sites')->getSite(), 'activation_url' => $app->request->http->absoluteUrl($recoverUrl)]); } return false; }
public function __construct($id, $module = null, Request $request) { parent::__construct($id, $module, $request); $this->pageTitle = CoreModule::t('Control panel'); $user = Mindy::app()->user; if ($user === null || $user && ($user->is_superuser === false || $user->is_staff === false)) { $this->r->redirect('admin:login'); } }
/** * Logout the current user and redirect to returnLogoutUrl. */ public function actionLogout() { $auth = Mindy::app()->auth; if ($auth->isGuest) { $this->redirectNext(); $this->getRequest()->redirect(Mindy::app()->homeUrl); } $auth->logout($this->getModule()->destroySessionAfterLogout); $this->getRequest()->redirect('user:login'); }
public function processRequest(Request $request) { if ($meta = Meta::objects()->filter(['url' => $request->http->requestUri])->limit(1)->get()) { $metaInfo = ['title' => $meta->title, 'keywords' => $meta->keywords, 'description' => $meta->description, 'canonical' => $meta->url]; $controller = Mindy::app()->controller; foreach ($metaInfo as $key => $value) { $controller->set[ucfirst($key)] = $value; } } }
public function actionSmsConfirm() { $this->setBreadcrumbs([['name' => UserModule::t('Login'), 'url' => Mindy::app()->urlManager->reverse('user:login')], ['name' => 'Повторная отправка SMS для активации учетной записи', 'url' => Mindy::app()->urlManager->reverse('user:activation_sms')], ['name' => 'Подтверждение учетной записи по смс']]); $form = new SmsConfirmForm(); $request = $this->getRequest(); if ($request->getIsPost() && $form->populate($request->post->all())->isValid() && $form->activate()) { $request->redirect(Mindy::app()->urlManager->reverse('user:activation_sms_success')); } echo $this->render('user/activation/sms_confirm.html', ['form' => $form]); }
public function beforeSave($owner, $isNew) { $onSite = Mindy::app()->getModule('Meta')->onSite; if ($onSite) { $sitesModule = Mindy::app()->getModule('Sites'); if (($isNew || empty($owner->site)) && $sitesModule) { $owner->site = $sitesModule->getSite(); } } }
public function actionLogout() { /** @var \Modules\User\Components\Auth $auth */ $auth = Mindy::app()->auth; if ($auth->getIsGuest() === false) { $this->clearKeys($auth->getModel()); $auth->logout($this->getModule()->destroySessionAfterLogout); } echo $this->json(['status' => true]); $this->end(); }
public function send() { $user = $this->getUser(); if ($user === null) { return false; } $smsKey = mt_rand(1000, 9999); $user->setAttributes(['sms_key' => $smsKey])->save(['sms_key']); Mindy::app()->sms->fromCode('user.activation_sms', $user->phone, ['sms_key' => $smsKey]); return true; }
public function cleanPassword_current($value) { $model = $this->getModel(); $auth = Mindy::app()->auth; $hasher = $auth->getPasswordHasher($model->hash_type); if ($hasher->verifyPassword($this->password_current->getValue(), $model->password) === false) { $this->addError('password_current', 'Incorrect password'); return null; } return $value; }
public static function getCache() { if (self::$_cache === null) { if (class_exists('\\Mindy\\Base\\Mindy')) { self::$_cache = \Mindy\Base\Mindy::app()->getComponent('cache'); } else { self::$_cache = new \Mindy\Cache\DummyCache(); } } return self::$_cache; }
public static function getFields() { $parent = 'Parent'; if (class_exists('\\Mindy\\Base\\Mindy')) { if (\Mindy\Base\Mindy::app()->hasModule(self::getModuleName())) { $module = \Mindy\Base\Mindy::app()->getModule(self::getModuleName()); $parent = $module->t('Parent'); } } return ['parent' => ['class' => TreeForeignField::className(), 'modelClass' => get_called_class(), 'null' => true, 'verboseName' => $parent], 'lft' => ['class' => IntField::className(), 'editable' => false, 'null' => true], 'rgt' => ['class' => IntField::className(), 'editable' => false, 'null' => true], 'level' => ['class' => IntField::className(), 'editable' => false, 'null' => true], 'root' => ['class' => IntField::className(), 'null' => true, 'editable' => false]]; }
/** * @param $name * @return mixed */ public static function delete($name) { $module = Mindy::app()->getModule($name); if ($module) { $module->delete(); $path = $module->getModulePath(); } else { $path = Alias::get('Modules.' . $name); } FileHelper::removeDirectory($path); return !is_dir($path); }
public static function recordActionInternal($owner, $text) { if ($owner->classNameShort() == UserLog::classNameShort() || $owner->classNameShort() == Session::classNameShort() || Mindy::app()->getUser()->is_staff === false) { return; } else { $url = method_exists($owner, 'getAbsoluteUrl') ? $owner->getAbsoluteUrl() : null; $message = strtr('{model} {url} ' . $text, ['{model}' => $owner->classNameShort()]); $app = Mindy::app(); $module = $owner->getModule(); UserLog::objects()->create(['user' => $app->getUser()->getIsGuest() ? null : $app->getUser(), 'module' => $owner->getModuleName(), 'model' => $owner->classNameShort(), 'url' => $url, 'ip' => $app->getUser()->getIp(), 'name' => (string) $owner, 'message' => $module->t($message, ['{url}' => $url ? "<a href='" . $owner->getAbsoluteUrl() . "'>" . (string) $owner . "</a>" : (string) $owner])]); } }
public function getLinkModels(array $attributes) { $model = array_shift($attributes); if (!$model->getIsNewRecord() && method_exists($model, 'getAbsoluteUrl')) { $qs = Meta::objects()->filter(['url' => $model->getAbsoluteUrl()]); if (Mindy::app()->getModule('Meta')->onSite) { $qs = $qs->currentSite(); } return $qs; } return []; }
/** * @return array|bool */ public function send() { $user = $this->getUser(); if ($user === null) { return false; } /** @var \Modules\Mail\Components\DbMailer $mail */ $mail = Mindy::app()->mail; $activationKey = User::objects()->generateActivationKey(); $user->setAttributes(['activation_key' => $activationKey])->save(['activation_key']); return $mail->fromCode('user.activation_email', $user->email, ['activation_key' => $activationKey]); }
public function testResize() { $field = new ImageField(['uploadTo' => function () { return ''; }, 'sizes' => ['preview' => [120, 160, 'method' => 'resize'], 'mini' => [60, 50, 'method' => 'adaptiveResize'], 'big' => [400, 300, 'method' => 'adaptiveResize'], 'watermarked' => [400, 400, 'method' => 'adaptiveResize', 'watermark' => ['file' => '/mock/watermark.png', 'position' => 'repeat']]]]); $image = dirname(Mindy::app()->basePath) . '/mock/lena.png'; $imageName = $field->setFile(new LocalFile($image)); $this->assertFileExists($this->media . '/' . $imageName); $this->assertFileExists($this->media . '/' . $field->sizeStoragePath('mini', $imageName)); $this->assertFileExists($this->media . '/' . $field->sizeStoragePath('preview', $imageName)); $this->assertFileExists($this->media . '/' . $field->sizeStoragePath('big', $imageName)); $this->assertFileExists($this->media . '/' . $field->sizeStoragePath('watermarked', $imageName)); }
public function actionChangePassword() { $user = Mindy::app()->getUser(); $form = new ChangePasswordForm(); $form->setModel($user); $r = $this->getRequest(); if ($r->getIsPost() && $form->populate($_POST)->isValid() && $form->save()) { echo $this->json(['status' => true, 'message' => UserModule::t('Password changed')]); $this->end(); } else { echo $this->json(['errors' => $form->getJsonErrors()]); $this->end(); } }
public function actionIndex() { $this->addBreadcrumb(UserModule::t("Recover password")); $form = new RecoverForm(); if ($this->getRequest()->isPost) { if ($form->populate($_POST)->isValid() && $form->send()) { $this->getRequest()->flash->success(UserModule::t("Message was sended to your email")); echo $this->render('user/recover_form_success.html'); Mindy::app()->end(); } else { $this->getRequest()->flash->error(UserModule::t("An error has occurred. Please try again later.")); } } echo $this->render('user/recover_form.html', ['form' => $form]); }