/** * Creates a new User_group model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new User_group(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Generate the first user when create a tenant * @return */ public function afterSave($insert, $changedAttributes) { $connection = \Yii::$app->db; $tenantUsers = $this->tenantUsers; $user = User::findByUsername($this->username); if (!$user) { // Creazione primo utente del tenant $user = new User(); $user->username = $this->username; $user->email = '*****@*****.**'; $user->tenant_id = $this->id; $user->setPassword('admin'); $user->generateAuthKey(); $user->save(); // Creazione gruppo Administrator $userGroup = new User_group(); $userGroup->name = 'Administrator'; $userGroup->changeTenant = true; $userGroup->tenant_id = $user->tenant_id; $userGroup->save(); // Inserimento del primo utente nel gruppo Administrator del tenant $user_to_user_group = new User_to_user_group(); $user_to_user_group->user_id = $user->id; $user_to_user_group->user_group_id = $userGroup->id; $user_to_user_group->save(); // Creazione gruppo Standard $userGroup = new User_group(); $userGroup->name = 'Standard'; $userGroup->changeTenant = true; $userGroup->tenant_id = $user->tenant_id; $userGroup->save(); if ($this->autologin == true) { Yii::$app->user->login($user); } else { // Invio email con username e password $to = \Yii::$app->user->identity->email; $subject = 'daduu registration'; $message = 'Hello from daduu. User registration is successful!!!' . "\r\n" . 'username: '******' password: admin'; $headers = 'From: daduu42@localhost.it' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); $m = mail($to, $subject, $message, $headers); if (!$m) { throw new NotFoundHttpException('Non è stato possibile inviare l\'email con le credenziali di registrazione!'); } } } elseif ($this->autologin == true) { if ($user->tenant_id != $this->id) { throw new NotFoundHttpException('ATTENZIONE! L\'utente fa parte di un altro tenant!'); } else { Yii::$app->user->login($user); } } }