Example #1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = User_group::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'tenant_id' => $this->tenant_id]);
     $query->andFilterWhere(['like', 'name', $this->name]);
     return $dataProvider;
 }
Example #2
0
<?php

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use common\models\Tenant;
use common\models\User_group;
/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \frontend\models\SignupForm */
$tenantName = $_REQUEST['tenantname'];
$tenantId = $_REQUEST['tenantid'];
$this->title = 'Create a new user for ' . $tenantName . ' tenant';
$this->params['breadcrumbs'][] = $this->title;
$model->tenant_id = $tenantId;
$groups = User_group::getGroups();
?>
<div class="site-signup">
    <h1><?php 
echo Html::encode($this->title);
?>
</h1>

    <p>Please fill out the following fields to create a new user:</p>

    <div class="row">
        <div class="col-lg-5">
            <?php 
$form = ActiveForm::begin(['id' => 'form-signup']);
?>
                <?php 
echo $form->field($model, 'username');
 /**
  * Finds the User_group 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_group the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = User_group::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 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,
                 ]);
             }*/
 }
Example #5
0
 public static function getGroups()
 {
     $queryGruppi = User_group::find()->asArray()->all();
     $arrayGruppi = ArrayHelper::map($queryGruppi, 'id', 'name');
     return $arrayGruppi;
 }
Example #6
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getGroupId()
 {
     return $this->hasOne(User_group::className(), ['id' => 'user_group_id']);
 }
Example #7
0
 public function getGruppo()
 {
     return $this->hasMany(User_group::className(), ['id' => 'group_id'])->via('user_to_user_group');
 }
Example #8
0
 /**
  * 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);
         }
     }
 }