/** * @return boolean */ public function changeTenant() { $tenant = TenantUser::find()->andWhere(['tenant_id' => $this->tenant_id])->andWhere(['user_id' => Yii::$app->user->id])->one()->tenant; if (isset($tenant)) { Yii::$app->tenant->logout(); return Yii::$app->tenant->login($tenant); } return false; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = TenantUser::find(); $dataProvider = new ActiveDataProvider(['query' => $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, 'tenant_id' => $this->tenant_id, 'user_id' => $this->user_id, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, 'create_user_id' => $this->create_user_id, 'update_user_id' => $this->update_user_id]); return $dataProvider; }
/** * @param mixed $tenant * @param mixed $user * @return mixed */ public function create($tenant, $user) { $model = TenantUser::find()->where(['tenant_id' => $tenant->id, 'user_id' => $user->id])->one(); if (!$model) { $model = new TenantUser(); $model->tenant_id = $tenant->id; $model->user_id = $user->id; if ($model->save()) { return $model; } return null; } return $model; }
/** * @return \yii\db\ActiveQuery */ public function getTenantUsers() { return $this->hasMany(TenantUser::className(), ['tenant_id' => 'id']); }
/** * @param integer $id * @param string $email * @return mixed */ public function actionRemoveAuth0User($id, $email, $username, $auth0UserId) { Yii::$app->auth0->updateAppMetadata($auth0UserId, Yii::$app->auth0->getNewAppMetadata($auth0UserId, Tenant::findOne($id)->name, false)); TenantUser::find()->joinWith('user')->where(['{{%tenant_user}}.tenant_id' => $id])->where(['{{%user}}.email' => $email])->one()->delete(); }
/** * @return array */ public function getColumns() { \anli\helper\assets\SweetAlertAsset::register(Yii::$app->controller->view); $js = <<<JS \$(document).on('click', '.ajaxUpdate', function () { var pjaxId = '#' + \$(this).attr('pjaxId'); var url = \$(this).attr('data-url'); swal({ title: "Are you sure?", text: "You will not be able to undo this!", type: "warning", showCancelButton: true, confirmButtonColor: "#DD6B55", confirmButtonText: "Yes", closeOnConfirm: true, showLoaderOnConfirm: true, }, function() { \$.ajax({ url: url, type: 'GET', cache: false, error: function(xhr, status, error) { alert('There was an error with your request.' + xhr.responseText); } }) .done(function(result) { \$.pjax.reload({container: pjaxId, async:false}); }); }); }); JS; Yii::$app->controller->view->registerJs($js); $tenant = $this->tenant; return ['email', ['header' => 'Auth0', 'format' => 'raw', 'value' => function ($model) { if (isset($model['app_metadata']['permissions']) && isset($model['app_metadata']['permissions'][Yii::$app->auth0->serviceId]) && isset($model['app_metadata']['permissions'][Yii::$app->auth0->serviceId][$this->tenant->name])) { return Html::encode($model['app_metadata']['permissions'][Yii::$app->auth0->serviceId][$this->tenant->name]['role']); } return ''; }], ['header' => 'Local', 'format' => 'raw', 'value' => function ($model) { $model = TenantUser::find()->joinWith('user')->where(['email' => $model['email']])->one(); return isset($model) ? 'Yes' : ''; }], ['class' => ActionColumn::className(), 'controller' => 'tenant', 'template' => '{ajax-add} {ajax-remove}', 'buttons' => ['ajax-add' => function ($url, $model) use($tenant) { return Html::a(Html::tag('i', '', ['class' => 'glyphicon glyphicon-plus']), false, ['data-toggle' => 'tooltip', 'title' => 'Add to tenant', 'class' => 'ajaxUpdate', 'pjaxId' => 'list-pjax', 'data-url' => Url::to(['tenant/add-auth0-user', 'id' => $tenant->id, 'email' => $model['email'], 'username' => isset($model['username']) ? $model['username'] : $model['email'], 'auth0UserId' => $model['user_id']])]); }, 'ajax-remove' => function ($url, $model) use($tenant) { return Html::a(Html::tag('i', '', ['class' => 'glyphicon glyphicon-trash']), false, ['data-toggle' => 'tooltip', 'title' => 'Remove from tenant (This does not delete the user)', 'class' => 'ajaxUpdate', 'pjaxId' => 'list-pjax', 'data-url' => Url::to(['tenant/remove-auth0-user', 'id' => $tenant->id, 'email' => $model['email'], 'username' => isset($model['username']) ? $model['username'] : $model['email'], 'auth0UserId' => $model['user_id']])]); }], 'contentOptions' => ['class' => 'text-right']]]; }