From 5f1c3437b60f10a57c38fed7074ff548e5120c20 Mon Sep 17 00:00:00 2001 From: sakaguchi Date: Fri, 22 May 2026 13:49:40 +0900 Subject: [PATCH] =?UTF-8?q?fix=20#4365=20=E3=80=90=E3=83=A6=E3=83=BC?= =?UTF-8?q?=E3=82=B6=E3=83=BC=E3=82=B0=E3=83=AB=E3=83=BC=E3=83=97=E3=80=91?= =?UTF-8?q?=E3=82=B0=E3=83=AB=E3=83=BC=E3=83=97=E3=81=AB=E7=B4=90=E3=81=A5?= =?UTF-8?q?=E3=81=8F=E3=83=A6=E3=83=BC=E3=82=B6=E3=83=BC=E3=81=AE=E3=83=91?= =?UTF-8?q?=E3=83=95=E3=82=A9=E3=83=BC=E3=83=9E=E3=83=B3=E3=82=B9=E6=94=B9?= =?UTF-8?q?=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Service/UserGroupsService.php | 8 ++++--- .../Service/UserGroupsServiceTest.php | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) 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