/** * Signs user up and store it in related tenant if is setted up or '1' by default. * * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new User(); if ($this->tenant_id != null) { $user->tenant_id = $this->tenant_id; } $user->username = $this->username; $user->email = $this->email; $user->setPassword($this->password); $user->generateAuthKey(); $user->save(); $group = new User_to_user_group(); $group->user_id = $user->id; $group->user_group_id = $this->groups; $group->save(); return $user; } return null; }
public function actionLogin() { $params = $_REQUEST; $tenantName = ''; $tenant = new Tenant(); $username = ''; $user = ''; if (isset($params['username']) && isset($params['tenantName']) && isset($params['token'])) { $username = $params['username']; $user = User::findByUsername($username); //User::find()->where(['username' => $username]); $tenantName = $params['tenantName']; $tenant = Tenant::find()->where(['name' => $tenantName]); $token = $params['token']; $code = "fromblakpearl4todaduubyantoniochiriaco"; $decoded_parameters = $username . $tenantName . $code; //$encoded_parameters = crypt($decoded_parameters, '$2y$15$R.gJb2U2N.FmZ4hPp1y2CN$'); //'$6$sawhmo1pMjf7$'); //echo $token; //echo " "; //echo $encoded_parameters; //exit; if (crypt($decoded_parameters, $token) != $token) { //($token != $encoded_parameters) throw new NotFoundHttpException('Le credenziali d\'accesso non sono corrette!'); } if ($tenant->exists() && $user != null) { if ($tenant->scalar() != $user->tenant_id) { throw new NotFoundHttpException('Le credenziali d\'accesso non sono corrette!'); } else { Yii::$app->user->login($user); return $this->goHome(); } } if (!$tenant->exists()) { $tenant = new Tenant(); $tenant->autologin = true; $tenant->name = $tenantName; $tenant->username = $username; $tenant->save(); //$tenant = Tenant::find()->where(['name' => $tenantName]); } else { if ($user == null) { $user = new User(); $user->username = $username; $user->email = '*****@*****.**'; $user->tenant_id = $tenant->scalar(); $user->setPassword('admin'); $user->generateAuthKey(); $user->save(); // Inserimento dell'utente neli'ultimo gruppo del tenant /*$group = User_group::find()->orderBy('id desc')->one(); $user_to_user_group = new User_to_user_group(); $user_to_user_group->user_id = $user->id; $user_to_user_group->user_group_id = $group['id']; $user_to_user_group->save(); */ $group = User_group::find()->where(['name' => 'Standard', 'tenant_id' => $user->tenant_id])->one(); $user_to_user_group = new User_to_user_group(); $user_to_user_group->user_id = $user->id; $user_to_user_group->user_group_id = $group['id']; $user_to_user_group->save(); Yii::$app->user->login($user); return $this->goHome(); } } return $this->goHome(); } else { throw new NotFoundHttpException('Le credenziali d\'accesso non sono corrette!'); } /* $model = new LoginForm(); if ($model->load(Yii::$app->request->post()) && $model->login()) { return $this->goBack(); } else { return $this->render('login', [ 'model' => $model, ]); }*/ }
/** * @return \yii\db\ActiveQuery */ public function getUsersgroups() { return $this->hasMany(User_to_user_group::className(), ['group_id' => 'id']); }
/** * 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); } } }