diff --git a/plugins/baser-core/src/Service/UserGroupsService.php b/plugins/baser-core/src/Service/UserGroupsService.php index b884740278..bb99268b47 100644 --- a/plugins/baser-core/src/Service/UserGroupsService.php +++ b/plugins/baser-core/src/Service/UserGroupsService.php @@ -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']); @@ -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])) { throw new BcException(__d('baser_core', 'ユーザーが所属しているユーザーグループは削除できません。')); } return $this->UserGroups->delete($userGroup); diff --git a/plugins/baser-core/tests/TestCase/Service/UserGroupsServiceTest.php b/plugins/baser-core/tests/TestCase/Service/UserGroupsServiceTest.php index 73e118cae5..9474229c6a 100644 --- a/plugins/baser-core/tests/TestCase/Service/UserGroupsServiceTest.php +++ b/plugins/baser-core/tests/TestCase/Service/UserGroupsServiceTest.php @@ -18,6 +18,7 @@ use BaserCore\Test\Scenario\UserGroupsScenario; use BaserCore\TestSuite\BcTestCase; use CakephpFixtureFactories\Scenario\ScenarioAwareTrait; +use Cake\Core\Configure; /** * Class UserGroupsServiceTest @@ -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