@@ -28,6 +28,8 @@ final class ApiUserProviderTest extends TestCase
2828 private string $ testMallAdminUsername ;
2929 private string $ testAdminId ;
3030 private string $ testAdminUsername ;
31+ private string $ testSubshopAdminId ;
32+ private string $ testSubshopAdminUsername ;
3133
3234 protected function setUp (): void
3335 {
@@ -37,16 +39,16 @@ protected function setUp(): void
3739 $ this ->queryBuilderFactory = $ container ->get (QueryBuilderFactoryInterface::class);
3840 $ context = $ container ->get (ContextInterface::class);
3941
40- $ roleResolver = new RoleResolver ([
42+ $ roleResolver = new RoleResolver ($ context , [
4143 'malladmin ' => ['ROLE_ADMIN ' , 'ROLE_ADMIN_MALL ' ],
42- '1 ' => ['ROLE_ADMIN ' ],
4344 ]);
4445 $ this ->userProvider = new ApiUserProvider ($ this ->queryBuilderFactory , $ roleResolver , $ context );
4546
4647 $ timestamp = uniqid ('' , true );
4748 $ this ->testUsername = "test-user-provider- {$ timestamp }@example.com " ;
4849 $ this ->testMallAdminUsername = "test-malladmin- {$ timestamp }@example.com " ;
4950 $ this ->testAdminUsername = "test-admin- {$ timestamp }@example.com " ;
51+ $ this ->testSubshopAdminUsername = "test-subshopadmin- {$ timestamp }@example.com " ;
5052
5153 $ this ->createTestUsers ();
5254 }
@@ -127,6 +129,62 @@ public function testRefreshUserWithMallAdmin(): void
127129 $ this ->assertContains ('ROLE_ADMIN_MALL ' , $ refreshedUser ->getRoles ());
128130 }
129131
132+ public function testSubshopAdminDoesNotGetAdminRoleOnDifferentShop (): void
133+ {
134+ $ user = $ this ->userProvider ->loadUserByIdentifier ($ this ->testSubshopAdminUsername );
135+
136+ $ this ->assertContains ('ROLE_USER ' , $ user ->getRoles ());
137+ $ this ->assertNotContains ('ROLE_ADMIN ' , $ user ->getRoles ());
138+ }
139+
140+ public function testMallAdminIsFoundRegardlessOfShopId (): void
141+ {
142+ $ connection = $ this ->queryBuilderFactory ->create ()->getConnection ();
143+ $ id = uniqid ('malladmin_other_ ' , true );
144+ $ username = "test-malladmin-othershop- {$ id }@example.com " ;
145+
146+ $ connection ->insert ('oxuser ' , [
147+ 'OXID ' => $ id ,
148+ 'OXUSERNAME ' => $ username ,
149+ 'OXPASSWORD ' => hash ('sha512 ' , 'testpassword ' ),
150+ 'OXRIGHTS ' => 'malladmin ' ,
151+ 'OXACTIVE ' => 1 ,
152+ 'OXSHOPID ' => 999 ,
153+ ]);
154+
155+ try {
156+ $ user = $ this ->userProvider ->loadUserByIdentifier ($ username );
157+
158+ $ this ->assertContains ('ROLE_ADMIN ' , $ user ->getRoles ());
159+ $ this ->assertContains ('ROLE_ADMIN_MALL ' , $ user ->getRoles ());
160+ } finally {
161+ $ connection ->delete ('oxuser ' , ['OXID ' => $ id ]);
162+ }
163+ }
164+
165+ public function testRegularUserFromDifferentShopIsNotFound (): void
166+ {
167+ $ connection = $ this ->queryBuilderFactory ->create ()->getConnection ();
168+ $ id = uniqid ('user_other_ ' , true );
169+ $ username = "test-user-othershop- {$ id }@example.com " ;
170+
171+ $ connection ->insert ('oxuser ' , [
172+ 'OXID ' => $ id ,
173+ 'OXUSERNAME ' => $ username ,
174+ 'OXPASSWORD ' => hash ('sha512 ' , 'testpassword ' ),
175+ 'OXRIGHTS ' => 'user ' ,
176+ 'OXACTIVE ' => 1 ,
177+ 'OXSHOPID ' => 999 ,
178+ ]);
179+
180+ try {
181+ $ this ->expectException (UserNotFoundException::class);
182+ $ this ->userProvider ->loadUserByIdentifier ($ username );
183+ } finally {
184+ $ connection ->delete ('oxuser ' , ['OXID ' => $ id ]);
185+ }
186+ }
187+
130188 public function testRefreshUserWithNumericAdmin (): void
131189 {
132190 $ originalUser = new ApiUser ($ this ->testAdminId , $ this ->testAdminUsername , ['ROLE_USER ' , 'ROLE_ADMIN ' ]);
@@ -169,16 +227,26 @@ private function createTestUsers(): void
169227 'OXACTIVE ' => 1 ,
170228 'OXSHOPID ' => 1 ,
171229 ]);
230+
231+ $ this ->testSubshopAdminId = uniqid ('subshopadmin_ ' , true );
232+ $ connection ->insert ('oxuser ' , [
233+ 'OXID ' => $ this ->testSubshopAdminId ,
234+ 'OXUSERNAME ' => $ this ->testSubshopAdminUsername ,
235+ 'OXPASSWORD ' => hash ('sha512 ' , 'testpassword ' ),
236+ 'OXRIGHTS ' => '2 ' ,
237+ 'OXACTIVE ' => 1 ,
238+ 'OXSHOPID ' => 1 ,
239+ ]);
172240 }
173241
174242 private function deleteTestUsers (): void
175243 {
176- if (isset ($ this ->testUserId , $ this ->testMallAdminId , $ this ->testAdminId )) {
244+ if (isset ($ this ->testUserId , $ this ->testMallAdminId , $ this ->testAdminId , $ this -> testSubshopAdminId )) {
177245 $ queryBuilder = $ this ->queryBuilderFactory ->create ();
178246 $ queryBuilder
179247 ->delete ('oxuser ' )
180248 ->where ('OXID IN (:ids) ' )
181- ->setParameter ('ids ' , [$ this ->testUserId , $ this ->testMallAdminId , $ this ->testAdminId ], \Doctrine \DBAL \Connection::PARAM_STR_ARRAY )
249+ ->setParameter ('ids ' , [$ this ->testUserId , $ this ->testMallAdminId , $ this ->testAdminId , $ this -> testSubshopAdminId ], \Doctrine \DBAL \Connection::PARAM_STR_ARRAY )
182250 ->execute ();
183251 }
184252 }
0 commit comments