/** * Finds tenant by user and tenant model * Create a tenant if it does not exists. * * @return mixed */ public static function findByTenantUser($tenant, $user) { $query = self::find()->andWhere(['tenant_id' => $tenant->id])->andWhere(['user_id' => $user->id]); if (!$query->exists()) { $model = new TenantUser(); $model->user_id = $user->id; $model->tenant_id = $tenant->id; $model->save(); $model->refresh(); return $model; } return $query->one(); }
/** * @return \yii\db\ActiveQuery */ public function getTenantUsers() { return $this->hasMany(TenantUser::className(), ['user_id' => 'id']); }
/** * @param string $userId * @return mixed */ public function actionRemoveTenant($userId, $tenantId = null) { if (!isset($tenantId)) { $tenant = Yii::$app->tenant->identity; } else { $tenant = Tenant::findOne($tenantId); } $model = ApiUser::findOne($userId); if ($model) { $data = ['app_metadata' => $model['app_metadata']]; unset($data['app_metadata']['permissions'][Yii::$app->getModule('auth0')->serviceId][$tenant->name]); if ($this->update($userId, $data)) { $user = User::findByAuth0($model); $tenantUser = TenantUser::findByTenantUser($tenant, $user); $tenantUser->delete(); $msg = 'Successfully removed the selected user from the current tenant'; return $this->goBack(); } } throw new HttpException(404, 'The requested user cannot be found.'); }