Skip to content
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
8 changes: 5 additions & 3 deletions plugins/baser-core/src/Service/UserGroupsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function getIndex($queryParams = []): Query
'finder' => 'all',
'exclude_admin' => false,
'order' => null,
'contain' => ['Users'],
'contain' => Configure::read('BcApp.isDisplayUserListInUserGroup') ? ['Users'] : [],
], $queryParams);

$query = $this->UserGroups->find($queryParams['finder']);
Expand Down Expand Up @@ -180,8 +180,10 @@ public function update(EntityInterface $target, array $postData): ?EntityInterfa
*/
public function delete(int $id): bool
{
$userGroup = $this->UserGroups->get($id, ['contain' => ['Users']]);
if (!empty($userGroup->users)) {
$userGroup = $this->UserGroups->get($id);
/** @var \Cake\ORM\Association\BelongsToMany $assoc */
$assoc = $this->UserGroups->getAssociation('Users');
if ($assoc->junction()->exists(['user_group_id' => $id])) {
Comment thread
kaburk marked this conversation as resolved.
throw new BcException(__d('baser_core', 'ユーザーが所属しているユーザーグループは削除できません。'));
}
return $this->UserGroups->delete($userGroup);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use BaserCore\Test\Scenario\UserGroupsScenario;
use BaserCore\TestSuite\BcTestCase;
use CakephpFixtureFactories\Scenario\ScenarioAwareTrait;
use Cake\Core\Configure;

/**
* Class UserGroupsServiceTest
Expand Down Expand Up @@ -93,6 +94,28 @@ public function testGetIndex()
$this->assertEquals(3, $userGroups->count());
}

/**
* Test getIndex with isDisplayUserListInUserGroup false
*/
public function testGetIndex_ContainCheck()
{
Configure::write('BcApp.isDisplayUserListInUserGroup', true);
$userGroups = $this->UserGroups->getIndex();
$this->assertEquals(3, $userGroups->count());

foreach ($userGroups as $userGroup) {
$this->assertTrue($userGroup->has('users'));
}

Configure::write('BcApp.isDisplayUserListInUserGroup', false);
$userGroups = $this->UserGroups->getIndex();
$this->assertEquals(3, $userGroups->count());

foreach ($userGroups as $userGroup) {
$this->assertFalse($userGroup->has('users'));
}
}

/**
* Test create
* @dataProvider createDataProvider
Expand Down
Loading