diff --git a/protected/humhub/modules/space/components/ActiveQuerySpace.php b/protected/humhub/modules/space/components/ActiveQuerySpace.php index 74f8a5dadf..8409a76e22 100644 --- a/protected/humhub/modules/space/components/ActiveQuerySpace.php +++ b/protected/humhub/modules/space/components/ActiveQuerySpace.php @@ -9,8 +9,12 @@ namespace humhub\modules\space\components; +use humhub\modules\space\models\Membership; use humhub\modules\user\components\ActiveQueryUser; +use humhub\modules\user\models\User; +use Yii; use yii\db\ActiveQuery; +use yii\db\Expression; /** @@ -25,10 +29,32 @@ class ActiveQuerySpace extends ActiveQuery /** * Only returns spaces which are visible for this user * + * @param User|null $user * @return ActiveQuerySpace the query */ - public function visible() + public function visible(User $user = null) { + if ($user === null && !Yii::$app->user->isGuest) { + try { + $user = Yii::$app->user->getIdentity(); + } catch (\Throwable $e) { + Yii::error($e, 'space'); + } + } + + if ($user !== null) { + + $spaceIds = array_map(function (Membership $membership) { + return $membership->space_id; + }, Membership::findAll(['user_id' => $user->id])); + + $this->andWhere(['OR', + ['space.visibility' => 1], + ['IN', 'space.id', $spaceIds] + ]); + } else { + $this->andWhere(['space.visibility' => 1]); + } return $this; }