diff --git a/backend/components/AdminMenuStructure.php b/backend/components/AdminMenuStructure.php new file mode 100644 index 0000000..505192e --- /dev/null +++ b/backend/components/AdminMenuStructure.php @@ -0,0 +1,74 @@ +menuItems = []; + } + + protected function can($authItem){ + $result = false; + if (\Yii::$app->user->can($authItem)) { + $result = true; + } + return $result; + } + + + + + + protected function addUserMainMenu(){ + + $userMainMenu = null; + $items = []; + + +// if ( $this->can('backend.user.index')){ + $items[] = ['label' => 'Felhasználók', 'url' =>['/user/index']]; +// } + + + if ( count($items) > 0 ){ + $userMainMenu = ['label' => 'Beállítások', 'url' => null, + 'items' => $items + ]; + } + + if ( isset($userMainMenu)){ + $this->menuItems[] = $userMainMenu; + } + + + } + + + protected function addLoginMainMenu(){ + if (Yii::$app->user->isGuest) { + $mainMenuItem= ['label' => 'Login', 'url' => ['/site/login']]; + } else { + $mainMenuItem= [ + 'label' => 'Kijelentkezés (' . Yii::$app->user->identity->username . ')', + 'url' => ['/site/logout'], + 'linkOptions' => ['data-method' => 'post'] + ]; + } + $this->menuItems[] = $mainMenuItem; + } + + + public function run(){ + $this->addUserMainMenu(); + $this->addLoginMainMenu(); + return $this->menuItems; + } + + +} \ No newline at end of file diff --git a/backend/controllers/UserController.php b/backend/controllers/UserController.php new file mode 100644 index 0000000..6fff26f --- /dev/null +++ b/backend/controllers/UserController.php @@ -0,0 +1,132 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['post'], + ], + ], + ]; + } + + /** + * Lists all User models. + * @return mixed + */ + public function actionIndex() + { + $searchModel = new UserSearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single User model. + * @param integer $id + * @return mixed + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new User model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + public function actionCreate() + { + $model = new UserCreate(); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } else { + return $this->render('create', [ + 'model' => $model, + ]); + } + } + + /** + * Updates an existing User model. + * If update is successful, the browser will be redirected to the 'view' page. + * @param integer $id + * @return mixed + */ + public function actionUpdate($id) + { + $model = UserUpdate::findOne(['id' => $id]); + + if ( $model == null ){ + throw new NotFoundHttpException('The requested page does not exist.'); + } + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id]); + } else { + return $this->render('update', [ + 'model' => $model, + ]); + } + } + + /** + * Deletes an existing User model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param integer $id + * @return mixed + */ + public function actionDelete($id) + { +// $this->findModel($id)->delete(); + + $user = $this->findModel($id); + + $user->updateAttributes(['status' => User::STATUS_DELETED]); + + return $this->redirect(['index']); + } + + /** + * Finds the User model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param integer $id + * @return User the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = User::findOne($id)) !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } +} diff --git a/backend/models/UserCreate.php b/backend/models/UserCreate.php new file mode 100644 index 0000000..2e59786 --- /dev/null +++ b/backend/models/UserCreate.php @@ -0,0 +1,67 @@ +6 ], + [['password_repeat'] ,'validatePasswordRepeat' ] + ]; + } + + public function validatePasswordRepeat($attribute,$params){ + + if ( !$this->hasErrors()){ + if ( $this->password_plain != $this->password_repeat ){ + $this->addError($attribute, Yii::t('app', 'Jelszó és jelszó újra nem egyezik!') ); + } + } + } + + public function attributeLabels(){ + return [ + + 'email' =>'E-mail', + 'username' =>'Felhasználónév', + 'created_at' =>'Létrehozás dátuma', + 'password_plain' => Yii::t('app','Jelszó'), + 'password_repeat' => Yii::t('app','Jelszó újra'), + + ]; + } + + public function beforeSave($insert){ + if ( parent::beforeSave($insert)){ + if ( $insert ){ + $this->setPassword($this->password_plain); + $this->generateAuthKey(); + return true; + } + }else{ + return false; + } + } + + public function afterSave($insert, $changedAttributes){ + parent::afterSave($insert, $changedAttributes); +// $am = Yii::$app->authManager; +// $role = $am->getRole('admin'); +// Yii::$app->authManager->assign($role, $this->id); + } + +} \ No newline at end of file diff --git a/backend/models/UserSearch.php b/backend/models/UserSearch.php new file mode 100644 index 0000000..553b9e0 --- /dev/null +++ b/backend/models/UserSearch.php @@ -0,0 +1,73 @@ + $query, + ]); + + $this->load($params); + + if (!$this->validate()) { + // uncomment the following line if you do not want to return any records when validation fails + // $query->where('0=1'); + return $dataProvider; + } + + $query->andFilterWhere([ + 'id' => $this->id, + 'status' => $this->status, + 'created_at' => $this->created_at, + 'updated_at' => $this->updated_at, + ]); + + $query->andFilterWhere(['like', 'username', $this->username]) + ->andFilterWhere(['like', 'auth_key', $this->auth_key]) + ->andFilterWhere(['like', 'password_hash', $this->password_hash]) + ->andFilterWhere(['like', 'password_reset_token', $this->password_reset_token]) + ->andFilterWhere(['like', 'email', $this->email]); + + return $dataProvider; + } +} diff --git a/backend/models/UserUpdate.php b/backend/models/UserUpdate.php new file mode 100644 index 0000000..077d07a --- /dev/null +++ b/backend/models/UserUpdate.php @@ -0,0 +1,69 @@ + User::className(), 'targetAttribute' => 'email'], + ['username' ,'unique', 'targetClass' => User::className(), 'targetAttribute' => 'username'], + [['password_plain' ,'password_repeat'] ,'string','min' =>6 ], + [['password_repeat'] ,'validatePasswordRepeat' ] + ]; + } + /** + * @formatter:on + */ + public function validatePasswordRepeat($attribute, $params) { + if (! $this->hasErrors ()) { + if ( !empty($this->password_plain) || !empty($this->password_repeat) ){ + if ($this->password_plain != $this->password_repeat) { + $this->addError ( $attribute, Yii::t ( 'app', 'Jelszó és jelszó újra nem egyezik!' ) ); + } + } + } + } + public function attributeLabels() { + return [ + + 'email' => 'E-mail', + 'username' => 'Felhasználónév', + 'created_at' => 'Létrehozás dátuma', + 'password_plain' => Yii::t ( 'app', 'Jelszó' ), + 'password_repeat' => Yii::t ( 'app', 'Jelszó újra' ) + ] + ; + } + public function beforeSave($insert) { + if (parent::beforeSave ( $insert )) { + if (! $insert) { + if ( !empty( $this->password_plain ) ) { + $this->setPassword($this->password_plain); + return true; + } + } + return true; + } else { + return false; + } + } + public function afterSave($insert, $changedAttributes) { + parent::afterSave ( $insert, $changedAttributes ); + // $am = Yii::$app->authManager; + // $role = $am->getRole('admin'); + // Yii::$app->authManager->assign($role, $this->id); + } +} \ No newline at end of file diff --git a/backend/views/layouts/main.php b/backend/views/layouts/main.php index d5ded73..a466606 100644 --- a/backend/views/layouts/main.php +++ b/backend/views/layouts/main.php @@ -9,8 +9,12 @@ use yii\bootstrap\Nav; use yii\bootstrap\NavBar; use yii\widgets\Breadcrumbs; use common\widgets\Alert; +use backend\components\AdminMenuStructure; AppAsset::register($this); + +$adminMenu = new AdminMenuStructure(); +$items = $adminMenu->run(); ?> beginPage() ?> @@ -28,27 +32,15 @@ AppAsset::register($this);
+ = Html::a(Yii::t('app', 'Új felhasználó'), ['create'], ['class' => 'btn btn-success']) ?> +
+ + = GridView::widget([ + 'dataProvider' => $dataProvider, + 'columns' => [ + ['class' => 'yii\grid\SerialColumn'], + + 'username', + 'email:email', + 'created_at:datetime', + + ['class' => 'yii\grid\ActionColumn'], + ], + ]); ?> + ++ = Html::a(Yii::t('app', 'Update'), ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?> + = Html::a(Yii::t('app', 'Delete'), ['delete', 'id' => $model->id], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => Yii::t('app', 'Are you sure you want to delete this item?'), + 'method' => 'post', + ], + ]) ?> +
+ + = DetailView::widget([ + 'model' => $model, + 'attributes' => [ + 'username', + 'email:email', + 'statusHuman', + 'created_at:datetime', + ], + ]) ?> + +