Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enh: Use the new ContentCreationService to search spaces #74

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions controllers/ShareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace humhub\modules\sharebetween\controllers;

use humhub\modules\content\models\Content;
use humhub\modules\post\permissions\CreatePost;
use humhub\modules\sharebetween\models\ShareForm;
use humhub\modules\sharebetween\services\ShareService;
use humhub\modules\space\helpers\CreateContentPermissionHelper;
use humhub\modules\space\models\Space;
use humhub\modules\user\models\User;
use humhub\modules\user\Module as UserModule;
use humhub\widgets\ModalClose;
use Yii;

Expand Down Expand Up @@ -63,9 +64,11 @@ public function actionSearchSpaces()
return $this->forbidden();
}

$shareService = new ShareService($content->getModel(), Yii::$app->user->getIdentity());

$spaces = $shareService->searchSpaces(Yii::$app->request->get('keyword', ''));
$spaces = CreateContentPermissionHelper::findSpaces(
CreatePost::class,
Yii::$app->request->get('keyword', ''),
Yii::$app->user->identity,
);

return $this->asJson($spaces);
}
Expand Down
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

1.0.11 (Unreleased)
-------------------------
- Enh #74: Use the new ContentCreationService to search spaces

1.0.10 (February 5, 2025)
-------------------------
- Enh #71: Show the list of available spaces to share to without having to enter the 2 first letters
Expand Down
4 changes: 2 additions & 2 deletions module.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"name": "Share content",
"description": "Share content between Spaces in the simplest way possible.",
"keywords": ["share", "between", "content"],
"version": "1.0.10",
"version": "1.0.11",
"humhub": {
"minVersion": "1.14"
"minVersion": "1.17.2"
},
"screenshots": [
"resources/screen1.png",
Expand Down
43 changes: 0 additions & 43 deletions services/ShareService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
use humhub\modules\post\permissions\CreatePost;
use humhub\modules\sharebetween\models\Share;
use humhub\modules\space\models\Space;
use humhub\modules\space\widgets\Chooser;
use humhub\modules\user\models\User;
use Yii;
use yii\db\Expression;
use yii\web\IdentityInterface;

final class ShareService
Expand Down Expand Up @@ -130,47 +128,6 @@ private function getShareByContainer(ContentContainerActiveRecord $container): A
return $this->getShareQuery()->andWhere(['content.contentcontainer_id' => $container->contentcontainer_id]);
}

public function searchSpaces(string $keyword): array
{
$spaces = Space::find()
->visible($this->user)
->filterBlockedSpaces($this->user)
->search($keyword);

if ($this->record->content->container instanceof Space) {
$spaces->andWhere(['!=', 'space.id', $this->record->content->container->id]);
}

if (!$this->user->isSystemAdmin()) {
// Check the User can create a Post in the searched Spaces
$spaces->leftJoin('space_membership', 'space_membership.space_id = space.id')
->leftJoin(
'contentcontainer_permission',
'contentcontainer_permission.contentcontainer_id = space.contentcontainer_id
AND contentcontainer_permission.group_id = space_membership.group_id
AND contentcontainer_permission.permission_id = :permission_id',
)
->andWhere(['space_membership.user_id' => $this->user->id])
->andWhere(['OR',
// Allowed by default
['AND',
['IN', 'space_membership.group_id', $this->getDefaultAllowedGroups()],
['IS', 'contentcontainer_permission.permission_id', new Expression('NULL')],
],
// Set to allow
['contentcontainer_permission.state' => CreatePost::STATE_ALLOW],
])
->addParams(['permission_id' => CreatePost::class]);
}

$result = [];
foreach ($spaces->all() as $space) {
$result[] = Chooser::getSpaceResult($space);
}

return $result;
}

public function getDefaultAllowedGroups(): array
{
$defaultAllowedGroups = (new CreatePost())->defaultAllowedGroups;
Expand Down