/** * Create init user */ public function actionAdmin() { echo "创建一个新用户 ...\n"; // 提示当前操作 $username = $this->prompt('User Name:'); // 接收用户名 $password = $this->prompt('Password:'); // 接收密码 $password_hash = Yii::$app->security->generatePasswordHash($password); $model = new Admin(); // 创建一个新用户 $model->admin_account = $username; // 完成赋值 $model->admin_pwd = $password_hash; if (!$model->save()) { foreach ($model->getErrors() as $error) { foreach ($error as $e) { echo "{$e}\n"; } } return 1; // 命令行返回1表示有异常 } return 0; // 返回0表示一切OK }
/** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new Admin(); $user->fname = $this->fname; $user->lname = $this->lname; $user->contact_no = $this->contact_no; $user->username = $this->username; $user->email = $this->email; $user->setPassword($this->password); $user->company_name = $this->company_name; $user->company_description = $this->company_description; $user->shipping_address = $this->shipping_address; $user->generateAuthKey(); $user1 = new User(); $user1->fname = $this->fname; $user1->lname = $this->lname; $user1->contact_no = $this->contact_no; $user1->username = $this->username; $user1->email = $this->email; $user1->setPassword($this->password); $user1->company_name = $this->company_name; $user1->company_description = $this->company_description; $user1->shipping_address = $this->shipping_address; $user1->generateAuthKey(); if ($user->save() && $user1->save()) { return $user; return $user1; } } return null; }
/** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new Admin(); $user->username = $this->username; $user->email = $this->email; $user->setPassword($this->password); $user->generateAuthKey(); if ($user->save()) { return $user; } } return null; }
/** * Finds user by [[username]] * * @return User|null */ public function getUser() { if ($this->_user === false) { $this->_user = Admin::findByUsername($this->username); } return $this->_user; }
/** * Finds user by [[username]] * * @return User|null */ protected function getUser() { if ($this->_user === null) { $this->_user = Admin::findByUsername($this->username); } return $this->_user; }
/** * Finds admin by [[username]] * * @return Admin|null */ public function getAdmin() { if ($this->_admin === null) { $this->_admin = Admin::findByUsername($this->username); } return $this->_admin; }
public function actionChangePassword() { $model = new \backend\models\Admin(['scenario' => 'admin-change-password']); if ($model->load(Yii::$app->request->post()) && $model->validate()) { $admin = Admin::findOne(Yii::$app->user->identity->id); $admin->setPassword($model->password); $admin->generateAuthKey(); if ($admin->save()) { Yii::$app->getSession()->setFlash('success', Yii::t('app', 'New password was saved.')); } return $this->redirect(['change-password']); } return $this->render('changePassword', ['model' => $model]); }
public function search($params) { $query = Admin::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]], 'pagination' => ['pageSize' => 20]]); // load the seach form data and validate if (!($this->load($params) && $this->validate())) { return $dataProvider; } $dateBegin = strtotime($this->date); $dateEnd = $dateBegin + 86400; // adjust the query by adding the filters $query->andFilterWhere(['like', 'username', $this->username])->andFilterWhere(['like', 'real_name', $this->real_name])->andFilterWhere(['gender' => $this->gender])->andFilterWhere(['like', 'email', $this->email])->andFilterWhere(['like', 'mobile', $this->mobile])->andFilterWhere(['status' => $this->status])->andFilterWhere(['>=', 'created_at', $this->date ? $dateBegin : null])->andFilterWhere(['<', 'created_at', $this->date ? $dateEnd : null]); return $dataProvider; }
/** * 登录 * @return string|\yii\web\Response */ public function actionLogin() { if (isset($_SESSION['admin_id'])) { return $this->goHome(); } if ($post = Yii::$app->request->post()) { $result = Admin::validateIdentify($post['username'], md5($post['password'])); if (!$result) { return $this->render('login', ['message' => '账号或密码错误']); } $_SESSION['admin_id'] = $result['id']; return $this->goBack(); } return $this->render('login', ['message' => '']); }
public function actionStatus($id) { Yii::$app->response->format = Response::FORMAT_JSON; $status = Yii::$app->request->post('status'); $model = Admin::findOne($id); if (!$model || !in_array($status, [Admin::STATUS_ACTIVE, Admin::STATUS_BLOCKED])) { throw new BadRequestHttpException('请求错误!'); } $model->status = $status; if ($model->save(false)) { return ['status' => 'success', 'data' => []]; } else { return ['status' => 'fail', 'data' => ['message' => '更新出错!']]; } }
/** * @inheritdoc */ public function beforeSave($insert) { if (parent::beforeSave($insert)) { if ($this->isNewRecord || !$this->isNewRecord && $this->password) { $this->setPassword($this->password); $this->generateAuthKey(); $this->generatePasswordResetToken(); } return true; } return false; }
echo $form->field($model, 'store_id')->dropDownList($model->isNewRecord ? [] : Store::getKeyValuePairs($model->store->school_id)); ?> <?php echo $form->field($model, 'username')->textInput(['autocomplete' => 'off']); ?> <?php echo $form->field($model, 'password')->passwordInput(['autocomplete' => 'off']); ?> <?php echo $form->field($model, 'email')->input('email'); ?> <?php echo $form->field($model, 'real_name'); ?> <?php echo $form->field($model, 'gender')->dropDownList(Admin::getGenderList()); ?> <?php echo $form->field($model, 'mobile'); ?> <div class="form-group"> <?php echo Html::submitButton('<i class="fa fa-floppy-o"></i> 保存', ['class' => 'btn btn-primary']); ?> </div> <?php ActiveForm::end(); ?> </div> </div> <?php
?> <p> <?php echo Html::a('<i class="fa fa-plus"></i> 添加后台用户', ['admin/add'], ['class' => 'btn btn-primary']); ?> </p> <div class="row"> <div class="col-lg-12"> <?php Pjax::begin(); ?> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'tableOptions' => ['class' => 'table table-striped table-bordered table-center'], 'summaryOptions' => ['tag' => 'p', 'class' => 'text-right text-info'], 'columns' => [['class' => 'yii\\grid\\SerialColumn', 'headerOptions' => ['class' => 'col-md-1']], ['attribute' => 'username', 'headerOptions' => ['class' => 'col-md-2'], 'filterInputOptions' => ['class' => 'form-control input-sm']], ['attribute' => 'gender', 'filter' => Admin::getGenderList(), 'filterInputOptions' => ['class' => 'form-control input-sm'], 'headerOptions' => ['class' => 'col-md-1'], 'value' => function ($model, $key, $index, $column) { return $model->genderMsg; }], ['attribute' => 'email', 'headerOptions' => ['class' => 'col-md-2'], 'filterInputOptions' => ['class' => 'form-control input-sm']], ['attribute' => 'mobile', 'headerOptions' => ['class' => 'col-md-2'], 'filterInputOptions' => ['class' => 'form-control input-sm']], ['attribute' => 'status', 'format' => 'raw', 'filter' => Admin::getStatusList(), 'filterInputOptions' => ['class' => 'form-control input-sm'], 'headerOptions' => ['class' => 'col-md-1'], 'value' => function ($model, $key, $index, $column) { return Html::dropDownList('status', $model->status, Admin::getStatusList(), ['data-id' => $model->id]); }], ['attribute' => 'created_at', 'format' => ['date', 'php:Y-m-d H:i'], 'filter' => DatePicker::widget(['model' => $searchModel, 'type' => DatePicker::TYPE_COMPONENT_APPEND, 'attribute' => 'date', 'options' => ['class' => 'input-sm'], 'pluginOptions' => ['autoclose' => true, 'format' => 'yyyy-mm-dd']]), 'headerOptions' => ['class' => 'col-md-2']], ['class' => 'yii\\grid\\ActionColumn', 'header' => '操作', 'headerOptions' => ['class' => 'col-md-1'], 'template' => '{update}']]]); ?> <?php Pjax::end(); ?> </div> </div> <?php $url = Url::to(['/admin/status']); $js = <<<JS var handle = function () { var id = \$(this).attr('data-id'); var status = \$(this).val(); \$.ajax({ url: '{$url}?id=' + id ,
/** * @return \yii\db\ActiveQuery */ public function getAdmin() { return $this->hasOne(Admin::className(), ['id' => 'admin_id']); }