Space db search handle visibility and membership

This commit is contained in:
Lucas Bartholemy 2019-09-26 19:03:20 +02:00
parent d45d23f7f6
commit 050b79f753

View File

@ -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;
}