assign trainers to user; add email to jwt token
This commit is contained in:
@@ -2,10 +2,13 @@
|
||||
|
||||
namespace backend\controllers;
|
||||
|
||||
use common\models\Trainer;
|
||||
use common\models\UserTrainerAssignment;
|
||||
use Yii;
|
||||
use common\models\User;
|
||||
use backend\models\UserSearch;
|
||||
use backend\models\UserCreate;
|
||||
use yii\web\BadRequestHttpException;
|
||||
use yii\web\Controller;
|
||||
use yii\web\NotFoundHttpException;
|
||||
use yii\filters\VerbFilter;
|
||||
@@ -20,7 +23,7 @@ use common\components\RoleDefinition;
|
||||
*/
|
||||
class UserController extends \backend\controllers\BackendController
|
||||
{
|
||||
|
||||
|
||||
|
||||
public function behaviors()
|
||||
{
|
||||
@@ -45,8 +48,8 @@ class UserController extends \backend\controllers\BackendController
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Lists all User models.
|
||||
* @return mixed
|
||||
@@ -55,8 +58,8 @@ class UserController extends \backend\controllers\BackendController
|
||||
{
|
||||
$searchModel = new UserSearch();
|
||||
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return $this->render('index', [
|
||||
'searchModel' => $searchModel,
|
||||
@@ -84,35 +87,48 @@ class UserController extends \backend\controllers\BackendController
|
||||
public function actionCreate()
|
||||
{
|
||||
$model = new UserCreate();
|
||||
|
||||
|
||||
$accounts = Account::readAccounts();
|
||||
$trainers = Trainer::find()->all();
|
||||
|
||||
if ($model->load(Yii::$app->request->post()) && $model->save()) {
|
||||
|
||||
|
||||
$this->updateAccountAssignments($model);
|
||||
|
||||
$this->updateTrainerAssignments($model);
|
||||
|
||||
return $this->redirect(['index' ]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
return $this->render('create', [
|
||||
'model' => $model,
|
||||
'accounts' => $accounts,
|
||||
'trainers' => $trainers,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
public function updateAccountAssignments($model){
|
||||
|
||||
echo "saving accounts";
|
||||
|
||||
UserAccountAssignment::deleteAll(['id_user' => $model->id]);
|
||||
foreach ( $model->selected_accounts as $id_account ){
|
||||
echo "saving account";
|
||||
$uaa = new UserAccountAssignment();
|
||||
$uaa->id_user = $model->id;
|
||||
$uaa->id_account = $id_account;
|
||||
$uaa->save();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function updateTrainerAssignments($model){
|
||||
|
||||
UserTrainerAssignment::deleteAll(['id_user' => $model->id]);
|
||||
foreach ( $model->selected_trainers as $id_trainer ){
|
||||
$uaa = new UserTrainerAssignment();
|
||||
$uaa->id_user = $model->id;
|
||||
$uaa->id_trainer = $id_trainer;
|
||||
$uaa->save();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,7 +140,7 @@ class UserController extends \backend\controllers\BackendController
|
||||
public function actionUpdate($id)
|
||||
{
|
||||
$model = UserUpdate::findOne(['id' => $id]);
|
||||
|
||||
|
||||
if ( Yii::$app->authManager->checkAccess($model->id, 'admin')){
|
||||
$model->role = 'admin';
|
||||
} else if ( Yii::$app->authManager->checkAccess($model->id, 'employee')){
|
||||
@@ -132,28 +148,31 @@ class UserController extends \backend\controllers\BackendController
|
||||
}else if ( Yii::$app->authManager->checkAccess($model->id, 'reception')){
|
||||
$model->role = 'reception';
|
||||
}
|
||||
|
||||
|
||||
if ( $model == null ){
|
||||
throw new NotFoundHttpException('The requested page does not exist.');
|
||||
}
|
||||
|
||||
|
||||
$accounts = Account::readAccounts();
|
||||
|
||||
$this->applyAccounts($model);
|
||||
|
||||
|
||||
$trainers = Trainer::find()->all();
|
||||
$this->applyTrainers($model);
|
||||
|
||||
if ($model->load(Yii::$app->request->post()) && $model->save()) {
|
||||
|
||||
$this->updateAccountAssignments($model);
|
||||
$this->updateTrainerAssignments($model);
|
||||
return $this->redirect(['view', 'id' => $model->id]);
|
||||
} else {
|
||||
}
|
||||
return $this->render('update', [
|
||||
'model' => $model,
|
||||
'accounts' => $accounts,
|
||||
'trainers' => $trainers,
|
||||
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
private function applyAccounts($model ){
|
||||
$assignedAccounts = $model->userAccountAssignments;
|
||||
foreach ($assignedAccounts as $acc ){
|
||||
@@ -161,6 +180,13 @@ class UserController extends \backend\controllers\BackendController
|
||||
}
|
||||
}
|
||||
|
||||
private function applyTrainers($model ){
|
||||
$assignedTrainers = $model->userTrainerAssignments;
|
||||
foreach ($assignedTrainers as $acc ){
|
||||
$model->selected_trainers[] = $acc->id_trainer;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes an existing User model.
|
||||
* If deletion is successful, the browser will be redirected to the 'index' page.
|
||||
@@ -171,12 +197,12 @@ class UserController extends \backend\controllers\BackendController
|
||||
{
|
||||
|
||||
$user = $this->findModel($id);
|
||||
|
||||
|
||||
$user->updateAttributes(['status' => User::STATUS_DELETED]);
|
||||
|
||||
return $this->redirect(['index']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new User model.
|
||||
* If creation is successful, the browser will be redirected to the 'view' page.
|
||||
@@ -185,17 +211,17 @@ class UserController extends \backend\controllers\BackendController
|
||||
public function actionRole()
|
||||
{
|
||||
$model = new \backend\models\RoleForm();
|
||||
|
||||
|
||||
$model->availablePermissions = [
|
||||
[
|
||||
'name' => "reception.transfers",
|
||||
'description' => 'Tranzakciók'
|
||||
]
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if ($model->load(Yii::$app->request->post()) ) {
|
||||
if ( $model->validate() && $model->save()){
|
||||
Yii::$app->session->setFlash('success', 'Jogosultságok elmentve');
|
||||
@@ -209,7 +235,7 @@ class UserController extends \backend\controllers\BackendController
|
||||
$model->permissions[] = $child->name;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $this->render('role', [
|
||||
'model' => $model,
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user