/** * Authenticates the password. * This is the 'authenticate' validator as declared in rules(). */ public function authenticate() { if (!$this->hasErrors()) { $user = new User(); $identity = $user->authenticate($this->username, $this->password); switch ($identity->errorCode) { case User::ERROR_NONE: $duration = $this->rememberMe ? Module::getInstance()->rememberMeTime : 0; Yii::$app->user->login($identity, $duration); break; case User::ERROR_EMAIL_INVALID: $this->addError("username", Module::t("Email is incorrect.")); break; case User::ERROR_USERNAME_INVALID: $this->addError("username", Module::t("Username is incorrect.")); break; case User::ERROR_STATUS_NOTACTIV: $this->addError("status", Module::t("Your account is not activated.")); break; case User::ERROR_STATUS_BAN: $this->addError("status", Module::t("Your account is blocked.")); break; case User::ERROR_PASSWORD_INVALID: $this->addError("password", Module::t("Password is incorrect.")); break; } } }
public function checkexists() { if (!$this->hasErrors()) { // we only want to authenticate when no input errors /**@var User $user*/ if (strpos($this->login_or_email, "@")) { $user = User::findOne(['email' => $this->login_or_email]); if ($user) { $this->user_id = $user->id; } } else { $user = User::findOne(['username' => $this->login_or_email]); if ($user) { $this->user_id = $user->id; } } if ($user === null) { if (strpos($this->login_or_email, "@")) { $this->addError("login_or_email", Module::t("Email is incorrect.")); } else { $this->addError("login_or_email", Module::t("Username is incorrect.")); } } } }
public function safeDown() { switch ($this->dbType()) { case "mysql": $this->addColumn(Module::getInstance()->tableUsers, 'createtime', "int(10) NOT NULL"); $this->addColumn(Module::getInstance()->tableUsers, 'lastvisit', "int(10) NOT NULL"); $this->execute("UPDATE " . Module::getInstance()->tableUsers . " SET createtime = UNIX_TIMESTAMP(create_at), lastvisit = UNIX_TIMESTAMP(lastvisit_at)"); $this->dropColumn(Module::getInstance()->tableUsers, 'create_at'); $this->dropColumn(Module::getInstance()->tableUsers, 'lastvisit_at'); break; case "pgsql": $this->addColumn(Module::getInstance()->tableUsers, 'createtime', "int NOT NULL default 0"); $this->addColumn(Module::getInstance()->tableUsers, 'lastvisit', "int NOT NULL default 0"); $this->execute("UPDATE " . Module::getInstance()->tableUsers . " SET createtime = extract(epoch from create_at), lastvisit = extract(epoch from lastvisit_at)"); $this->dropColumn(Module::getInstance()->tableUsers, 'create_at'); $this->dropColumn(Module::getInstance()->tableUsers, 'lastvisit_at'); break; case "sqlite": default: $this->addColumn(Module::getInstance()->tableUsers, 'createtime', "int(10)"); $this->addColumn(Module::getInstance()->tableUsers, 'lastvisit', "int(10)"); $this->execute("UPDATE " . Module::getInstance()->tableUsers . " SET createtime = strftime('%s',create_at), lastvisit = strftime('%s',lastvisit_at)"); $this->execute('ALTER TABLE "' . Module::getInstance()->tableUsers . '" RENAME TO "' . __CLASS__ . '_' . Module::getInstance()->tableUsers . '"'); $this->createTable(Module::getInstance()->tableUsers, ["id" => "pk", "username" => "varchar(20) NOT NULL", "password" => "varchar(128) NOT NULL", "email" => "varchar(128) NOT NULL", "activkey" => "varchar(128) NOT NULL", "createtime" => "int(10) NOT NULL", "lastvisit" => "int(10) NOT NULL", "superuser" => "int(1) NOT NULL", "status" => "int(1) NOT NULL"]); $this->execute('INSERT INTO "' . Module::getInstance()->tableUsers . '" SELECT "id","username","password","email","activkey","createtime","lastvisit","superuser","status" FROM "' . __CLASS__ . '_' . Module::getInstance()->tableUsers . '"'); $this->execute('DROP TABLE "' . __CLASS__ . '_' . Module::getInstance()->tableUsers . '"'); break; } }
/** * Verify Old Password */ public function verifyOldPassword($attribute, $params) { $current = User::find()->notsafe()->findByPk(Yii::$app->user->id)->one()->password; $cond = Yii::$app->security->validatePassword($this->{$attribute}, $current); if (!$cond) { $this->addError($attribute, Module::t("Old Password is incorrect.")); } }
/** * Registration user */ public function actionRegistration() { Profile::$regMode = true; $model = new RegistrationForm(); $profile = new Profile(); $module = Module::getInstance(); // ajax validator // if (Yii::$app->request->isAjax) { // if ($model->load(Yii::$app->request->post()) && $profile->load(Yii::$app->request->post())) { // Yii::$app->response->format = Response::FORMAT_JSON; // return ActiveForm::validateMultiple([$model, $profile]); // } // } if (Yii::$app->user->id) { $this->redirect($module->profileUrl); } else { if ($model->load(Yii::$app->request->post())) { $profile->load(Yii::$app->request->post()); if ($model->validate() && $profile->validate()) { $model->activkey = Module::encrypting(microtime() . $model->password); $model->superuser = 0; $model->status = $module->activeAfterRegister ? User::STATUS_ACTIVE : User::STATUS_NOACTIVE; if ($model->save(false)) { $profile->user_id = $model->id; $profile->save(false); if ($module->sendActivationMail) { $url = Url::to(array_merge($module->activationUrl, ["activkey" => $model->activkey, "email" => $model->email]), true); $activation_url = Html::a($url, $url); Module::sendMail($model->email, Module::t("{site_name} account activation", ['site_name' => Yii::$app->name]), 'register', ['activation_url' => $activation_url]); } if (($module->loginNotActiv || $module->activeAfterRegister && $module->sendActivationMail == false) && $module->autoLogin) { Yii::$app->user->login($model); $this->redirect($module->returnUrl); } else { if (!$module->activeAfterRegister && !$module->sendActivationMail) { Yii::$app->user->setFlash('success', Module::t("Thank you for your registration. Contact Admin to activate your account.")); } elseif ($module->activeAfterRegister && $module->sendActivationMail == false) { Yii::$app->user->setFlash('success', Module::t("Thank you for your registration. Please {{login}}.", ['{{login}}' => Html::a(Module::t('Login'), $module->loginUrl)])); } elseif ($module->loginNotActiv) { Yii::$app->user->setFlash('success', Module::t("Thank you for your registration. Please check your email or login.")); } else { Yii::$app->user->setFlash('success', Module::t("Thank you for your registration. Please check your email.")); } return $this->refresh(); } } } else { $profile->validate(); } } return $this->render('/user/registration', ['model' => $model, 'profile' => $profile]); } }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * * @return \marsoltys\yii2user\models\User */ public function loadUser() { if ($this->model === null) { if (Yii::$app->user->id) { $this->model = Module::getInstance()->user(); } if ($this->model === null) { $this->redirect(Module::getInstance()->loginUrl); } } return $this->model; }
/** * @param string $type * @param null|string $code * @return false|string alias or false if alias is not found */ public static function itemAlias($type, $code = null) { $_items = ['field_type' => ['INTEGER' => Module::t('INTEGER'), 'VARCHAR' => Module::t('VARCHAR'), 'TEXT' => Module::t('TEXT'), 'DATE' => Module::t('DATE'), 'FLOAT' => Module::t('FLOAT'), 'DECIMAL' => Module::t('DECIMAL'), 'BOOL' => Module::t('BOOL'), 'BLOB' => Module::t('BLOB'), 'BINARY' => Module::t('BINARY')], 'required' => [self::REQUIRED_NO => Module::t('No'), self::REQUIRED_NO_SHOW_REG => Module::t('No, but show on registration form'), self::REQUIRED_YES_SHOW_REG => Module::t('Yes and show on registration form'), self::REQUIRED_YES_NOT_SHOW_REG => Module::t('Yes')], 'visible' => [self::VISIBLE_ALL => Module::t('For all'), self::VISIBLE_REGISTER_USER => Module::t('Registered users'), self::VISIBLE_ONLY_OWNER => Module::t('Only owner'), self::VISIBLE_NO => Module::t('Hidden')]]; if (isset($code)) { return isset($_items[$type][$code]) ? $_items[$type][$code] : false; } else { return isset($_items[$type]) ? $_items[$type] : false; } }
/** * Checks if current user is an Administrator * @return boolean */ public function isAdmin() { return $this->module->isAdmin(); }
public function behaviors() { return Module::getInstance()->getBehaviorsFor(get_class($this)); }
<?php $form = ActiveForm::begin(['id' => 'changepassword-form', 'enableAjaxValidation' => true, 'validateOnSubmit' => true, 'options' => ['class' => 'form-horizontal'], 'fieldConfig' => ['labelOptions' => ['class' => 'control-label']]]); ?> <?php echo $form->errorSummary($model); ?> <?php echo $form->field($model, 'oldPassword')->passwordInput(); ?> <?php echo $form->field($model, 'password')->passwordInput()->hint(Module::t("Minimal password length 4 symbols.")); ?> <?php echo $form->field($model, 'verifyPassword')->passwordInput(); ?> <div class="form-group"> <?php echo Html::submitButton(Module::t("Save"), ['class' => 'btn btn-primary']); ?> </div> <?php ActiveForm::end(); ?> </div> </div><!-- form -->
} Module::getInstance()->setMenu($menu); ?> <h1><?php echo Module::t('Your profile'); ?> </h1> <?php $attributes = ['username', 'email:email', 'create_at:date', 'lastvisit_at:date']; $profileFields = ProfileField::find()->forOwner()->sort()->all(); if ($profileFields) { foreach ($profileFields as $field) { $val = ''; if ($field->widgetView($model->profile)) { $val = $field->widgetView($model->profile); } else { if ($field->range) { $val = Profile::range($field->range, $model->profile->getAttribute($field->varname)); } else { $val = $model->profile->getAttribute($field->varname); } } $type = 'html'; if ($field->field_type == "DATE" || $field->widget == "UWjuidate") { $type = 'date'; } array_push($attributes, ['label' => Module::t($field->title), 'name' => $field->varname, 'format' => $type, 'value' => $val]); } } echo DetailView::widget(['model' => $model, 'attributes' => $attributes]);
?> <?php $profileFields = Profile::getFields(); if ($profileFields) { foreach ($profileFields as $field) { echo $field->renderField($profile, $form); } } ?> <?php echo $form->field($model, 'username'); ?> <?php echo $form->field($model, 'email'); ?> <div class="form-group"> <?php echo Html::submitButton($model->isNewRecord ? Module::t('Create') : Module::t('Save'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?> </div> <?php ActiveForm::end(); ?> </div><!-- form -->
/** Encrypt password before saving to database */ public function beforeSave($insert) { $this->password = Module::encrypting($this->password); return parent::beforeSave($insert); }
</h1> <p><?php echo Module::t("You may optionally enter a comparison operator (<b><</b>, <b><=</b>, <b>></b>, <b>>=</b>, <b><></b> or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done."); ?> </p> <?php echo Html::a(Module::t('Advanced Search'), '#', ['class' => 'search-button']); ?> <div class="search-form" style="display:none"> <?php echo $this->render('_search', ['model' => $searchModel]); ?> </div><!-- search-form --> <?php \yii\widgets\Pjax::begin(); ?> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => ['id', ['attribute' => 'varname', 'format' => 'raw', 'value' => function ($data) { return UHtml::markSearch($data, "varname"); }], ['attribute' => 'title', 'value' => function ($data) { return Module::t($data->title); }], ['attribute' => 'field_type', 'filter' => ProfileField::itemAlias("field_type")], 'field_size', ['attribute' => 'required', 'value' => function ($data) { return ProfileField::itemAlias("required", $data->required); }, 'filter' => ProfileField::itemAlias("required")], 'position', ['attribute' => 'visible', 'value' => function ($data) { return ProfileField::itemAlias("visible", $data->visible); }, 'filter' => ProfileField::itemAlias("visible")], ['class' => 'yii\\grid\\ActionColumn']]]); \yii\widgets\Pjax::end();
public function initMenu() { if (Module::isAdmin()) { $this->menu = [['label' => Module::t('Create User'), 'url' => ['/user/admin/create']], ['label' => Module::t('Manage Users'), 'url' => ['/user/admin/admin']], ['label' => Module::t('Manage Profile Field'), 'url' => ['/user/profile-field/admin']], ['label' => Module::t('List User'), 'url' => ['/user/user/index']]]; } }
} elseif ($field->range) { echo $input->dropDownList(Profile::range($field->range)); } elseif ($field->field_type == "TEXT") { echo $input->textarea(['rows' => 6, 'cols' => 50]); } else { echo $input->textInput(['size' => 60, 'maxlength' => $field->field_size ? $field->field_size : 255]); } } } ?> <?php if (Module::doCaptcha('registration')) { echo $form->field($model, 'captcha')->widget(\yii\captcha\Captcha::classname(), ['captchaAction' => '/site/captcha'])->hint(Module::t("Please enter the letters as they are shown in the image above.") . "<br/>" . Module::t("Letters are not case-sensitive.")); } ?> <div class="form-group"> <?php echo Html::submitButton(Module::t('Register'), ['class' => 'btn btn-success']); ?> </div> <?php ActiveForm::end(); ?> </div> </div><!-- form --> <?php }
<?php use marsoltys\yii2user\Module; use yii\widgets\DetailView; /** @var $model \marsoltys\yii2user\models\ProfileField */ /** @var $this \yii\web\View*/ $this->params['breadcrumbs'] = [['label' => Module::t('Profile Fields'), 'url' => ['admin']], Module::t($model->title)]; Module::getInstance()->setMenu([['label' => Module::t('Create Profile Field'), 'url' => ['create']], ['label' => Module::t('Update Profile Field'), 'url' => ['update', 'id' => $model->id]], ['label' => Module::t('Delete Profile Field'), 'url' => ['delete', 'id' => $model->id], 'linkOptions' => ['data' => ['method' => 'POST', 'confirm' => Module::t('Are you sure to delete this item?')]]], ['label' => Module::t('Manage Profile Field'), 'url' => ['admin']], ['label' => Module::t('Manage Users'), 'url' => ['/user/admin']]]); ?> <h1><?php echo Module::t('View Profile Field #') . $model->varname; ?> </h1> <?php echo DetailView::widget(['model' => $model, 'attributes' => ['id', 'varname', 'title', 'field_type', 'field_size', 'field_size_min', 'required', 'match', 'range', 'error_message', 'other_validator', 'widget', 'widgetparams', 'default', 'position', 'visible']]);
public function safeDown() { $this->dropTable(Module::getInstance()->tableProfileFields); $this->dropTable(Module::getInstance()->tableProfiles); $this->dropTable(Module::getInstance()->tableUsers); }
<?php echo $form->field($model, 'username'); ?> <?php echo $form->field($model, 'password')->passwordInput(); ?> <div class="form-group"> <div class="col-lg-offset-2 col-lg-11"> <?php echo Html::a(Module::t("Register"), Module::getInstance()->registrationUrl); ?> | <?php echo Html::a(Module::t("Lost Password?"), Module::getInstance()->recoveryUrl); ?> </div> </div> <?php echo $form->field($model, 'rememberMe')->checkbox(['template' => "<div class=\"col-lg-offset-2 col-lg-3\">{input} {label}</div>\n<div class=\"col-lg-8\">{error}</div>"]); ?> <div class="form-group"> <div class="col-lg-offset-2 col-lg-11"> <?php echo Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']); ?> </div> </div>
/** * Widget initialization * @return array */ public function init() { return ['name' => __CLASS__, 'label' => Module::t('DropDown List Dependent', [], __CLASS__), 'fieldType' => ['INTEGER'], 'params' => $this->params, 'paramsLabels' => ['modelName' => Module::t('Model Name', [], __CLASS__), 'optionName' => Module::t('Lable field name', [], __CLASS__), 'emptyField' => Module::t('Empty item name', [], __CLASS__), 'relationName' => Module::t('Profile model relation name', [], __CLASS__), 'modelDestName' => Module::t('Model Dest Name', [], __CLASS__), 'destField' => Module::t('Dest Field', [], __CLASS__), 'optionDestName' => Module::t('Label Dest field name', [], __CLASS__)]]; }
/** * @return array customized attribute labels (name=>label) */ public function attributeLabels() { $labels = ['user_id' => Module::t('User ID')]; $model = self::getFields(); foreach ($model as $field) { if (Module::getInstance()->fieldsMessage) { $l = Module::t($field->title, [], Module::getInstance()->fieldsMessage); } else { $l = Module::t($field->title); } $labels[$field->varname] = $l; } return $labels; }
/** * @param ActiveRecord $model * @return string */ public function editAttribute($model, $field, $params = []) { if (!isset($params['options'])) { $params['options'] = []; } $options = $params['options']; unset($params['options']); /** @var \yii\widgets\ActiveField $form */ $form = $params['formField']; unset($params['formField']); $return = $form->fileInput($options); $file = $model->getAttribute($field->varname); if ($file) { $return .= "<div class='form-group'><fieldset>"; $finfo = finfo_open(FILEINFO_MIME_TYPE); $root = Yii::getAlias('@webroot/' . $file); $type = explode("/", finfo_file($finfo, $root)); $return .= Html::activeCheckBox($model, '[uwfdel]' . $field->varname, ['label' => Module::t('Delete file')]) . "<br>"; if ($type[0] == 'image') { $return .= Html::img(Url::base() . "/" . $file, ['class' => 'UWfile-image-preview']); } else { $assetsPath = Yii::$app->assetManager->getBundle('marsoltys\\yii2user\\assets\\UserAssets')->basePath; $assetsUrl = Yii::$app->assetManager->getBundle('marsoltys\\yii2user\\assets\\UserAssets')->baseUrl; $img = "/img/" . pathinfo($file, PATHINFO_EXTENSION) . ".png"; if (file_exists($assetsPath . $img)) { $return .= Html::img($assetsUrl . $img, ['class' => 'UWfile-image-preview']); } else { $return .= Html::img($assetsUrl . "/img/file.png", ['class' => 'UWfile-image-preview']); } $return .= pathinfo($file, PATHINFO_BASENAME) . " "; $return .= Html::a(pathinfo($file, PATHINFO_BASENAME) . " ", Url::base() . "/" . $file); } $return .= "</fieldset></div>"; } return $return; }
<?php use marsoltys\yii2user\Module; use yii\grid\GridView; use yii\helpers\Html; /* @var $this yii\web\View */ /* @var $dataProvider yii\data\ActiveDataProvider */ $this->params['breadcrumbs'] = [Module::t("Users")]; if (!Module::isAdmin()) { $this->context->layout = 'main'; } ?> <h1><?php echo Module::t("List User"); ?> </h1> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'columns' => [['format' => 'html', 'value' => function ($data) { return Html::a(Html::encode($data->username), ["user/view", "id" => $data->id]); }], 'create_at', 'lastvisit_at']]);
<?php use marsoltys\yii2user\models\ProfileField; use marsoltys\yii2user\Module; use yii\widgets\DetailView; $this->params['breadcrumbs'] = [['label' => Module::t('Users'), 'url' => ['index']], $model->username]; Module::getInstance()->setMenu([['label' => Module::t('List User'), 'url' => ['index']]]); ?> <h1><?php echo Module::t('View User') . ' "' . $model->username . '"'; ?> </h1> <?php // For all users $attributes = ['username']; $profileFields = ProfileField::find()->forAll()->sort()->all(); if ($profileFields) { foreach ($profileFields as $field) { array_push($attributes, ['label' => Module::t($field->title), 'attribute' => $field->varname, 'value' => $field->widgetView($model->profile) ? $field->widgetView($model->profile) : ($field->range ? Profile::range($field->range, $model->profile->getAttribute($field->varname)) : $model->profile->getAttribute($field->varname))]); } } array_push($attributes, 'create_at', ['attribute' => 'lastvisit_at', 'value' => $model->lastvisit_at != '0000-00-00 00:00:00' ? $model->lastvisit_at : Module::t('Not visited')]); echo DetailView::widget(['model' => $model, 'attributes' => $attributes]);
<?php use marsoltys\yii2user\Module; use yii\bootstrap\Nav; Nav::widget(['items' => [['label' => Module::t('Create User'), 'url' => ['create']], ['label' => Module::t('Manage Users'), 'url' => ['admin']], ['label' => Module::t('Manage Profile Field'), 'url' => ['profileField/admin']], ['label' => Module::t('List User'), 'url' => ['/user']]], 'options' => ['class' => 'nav-pills']]);
/** * Widget initialization * @return array */ public function init() { return ['name' => __CLASS__, 'label' => Module::t('jQueryUI autocomplete', [], __CLASS__), 'fieldType' => ['VARCHAR'], 'params' => $this->params, 'paramsLabels' => ['modelName' => Module::t('Model Name', [], __CLASS__), 'optionName' => Module::t('Lable field name', [], __CLASS__), 'emptyFieldLabel' => Module::t('Empty item name', [], __CLASS__), 'emptyFieldValue' => Module::t('Empty item value', [], __CLASS__), 'relationName' => Module::t('Profile model relation name', [], __CLASS__), 'minLength' => Module::t('minimal start research length', [], __CLASS__)]]; }
?> <?php echo $form->field($model, 'create_at'); ?> <?php echo $form->field($model, 'lastvisit_at'); ?> <?php echo $form->field($model, 'superuser')->dropDownList($model->itemAlias('AdminStatus')); ?> <?php echo $form->field($model, 'status')->dropDownList($model->itemAlias('UserStatus')); ?> <div class="form-group"> <div class="col-lg-offset-1 col-lg-11"> <?php echo Html::submitButton(\marsoltys\yii2user\Module::t('search'), ['class' => 'btn btn-primary']); ?> </div> </div> <?php ActiveForm::end(); ?> </div><!-- search-form -->
<?php use marsoltys\yii2user\Module; $this->title = Yii::$app->name . ' - ' . Module::t("Login"); ?> <h1><?php echo $title; ?> </h1> <div class="form"> <?php echo $content; ?> </div><!-- yiiForm -->
/** * Widget initialization * @return array */ public function init() { return ['name' => __CLASS__, 'label' => Module::t('Relation Belongs To', [], __CLASS__), 'fieldType' => ['INTEGER'], 'params' => $this->params, 'paramsLabels' => ['modelName' => Module::t('Model Name', [], __CLASS__), 'optionName' => Module::t('Lable field name', [], __CLASS__), 'emptyField' => Module::t('Empty item name', [], __CLASS__), 'relationName' => Module::t('Profile model relation name', [], __CLASS__)]]; }
<?php use marsoltys\yii2user\Module; /** * @var $this yii\web\View * @var \marsoltys\yii2user\models\User $model * @var \marsoltys\yii2user\models\Profile $profile */ $this->params['breadcrumbs'] = [['label' => Module::t('Users'), 'url' => ['admin']], ['label' => $model->username, 'url' => ['view', 'id' => $model->id]], Module::t('Update')]; Module::getInstance()->addMenu(['label' => Module::t('View User'), 'url' => ['view', 'id' => $model->id]]); ?> <h1><?php echo Module::t('Update User') . " " . $model->id; ?> </h1> <?php echo $this->render('_form', ['model' => $model, 'profile' => $profile]);