diff --git a/src/di.php b/src/di.php index 28c10f79a..0004388ba 100644 --- a/src/di.php +++ b/src/di.php @@ -436,8 +436,10 @@ $di['api'] = $di->protect(function ($role) use ($di) { $isApiRequest = str_starts_with($routePath, '/api/'); $isAllowedClientApi = str_starts_with($routePath, '/api/client/client/') || str_starts_with($routePath, '/api/client/profile/'); + $isAllowedClientPage = str_starts_with($routePath, '/client/profile') + || str_starts_with($routePath, '/client/logout'); - if (($isApiRequest && !$isAllowedClientApi) || (!$isApiRequest && !str_starts_with($routePath, '/client'))) { + if (($isApiRequest && !$isAllowedClientApi) || (!$isApiRequest && !$isAllowedClientPage)) { throw new EmailValidationRequiredException(); } } diff --git a/src/modules/Order/Api/Client.php b/src/modules/Order/Api/Client.php index 0a0473c38..dc133ef98 100644 --- a/src/modules/Order/Api/Client.php +++ b/src/modules/Order/Api/Client.php @@ -82,7 +82,11 @@ class Client extends \Api_Abstract { $order = $this->_getOrder($data); - return $this->getService()->getOrderServiceData($order, $data['id'], $this->getIdentity()); + if ($order->status !== \Model_ClientOrder::STATUS_ACTIVE) { + throw new \FOSSBilling\InformationException('Order is not active'); + } + + return $this->getService()->getOrderServiceData($order, $this->getIdentity()); } /** diff --git a/src/modules/Serviceapikey/Service.php b/src/modules/Serviceapikey/Service.php index f0f23b046..a925e934f 100644 --- a/src/modules/Serviceapikey/Service.php +++ b/src/modules/Serviceapikey/Service.php @@ -163,6 +163,10 @@ class Service implements InjectionAwareInterface throw new \FOSSBilling\Exception('API key does not exist'); } + if (!$this->isActive($model)) { + throw new \FOSSBilling\InformationException('Order is not active'); + } + $config = json_decode($model->config ?? '', true); $model->api_key = $this->generateKey($config); diff --git a/src/modules/Servicecustom/Service.php b/src/modules/Servicecustom/Service.php index 2acfb25df..55fb7a228 100644 --- a/src/modules/Servicecustom/Service.php +++ b/src/modules/Servicecustom/Service.php @@ -42,14 +42,14 @@ class Service implements \FOSSBilling\InjectionAwareInterface $formbuilderService = $this->di['mod_service']('formbuilder'); $form = $formbuilderService->getForm((int) $product['form_id']); foreach ($form['fields'] as $field) { - if ($field['required'] == 1) { + if (($field['required'] ?? 0) == 1) { $field_name = $field['name']; if (!isset($data[$field_name]) || empty($data[$field_name])) { throw new \FOSSBilling\InformationException('You must fill in all required fields. ' . $field['label'] . ' is missing', null, 9684); } } - if ($field['readonly'] == 1) { + if (($field['readonly'] ?? 0) == 1) { $field_name = $field['name']; if ($data[$field_name] != $field['default_value']) { throw new \FOSSBilling\InformationException('Field ' . $field['label'] . ' is read only. You cannot change its value', null, 5468); @@ -248,6 +248,10 @@ class Service implements \FOSSBilling\InjectionAwareInterface if (!$order instanceof \Model_ClientOrder) { throw new \FOSSBilling\Exception('Order not found'); } + + if ($order->status !== \Model_ClientOrder::STATUS_ACTIVE) { + throw new \FOSSBilling\InformationException('Order is not activated'); + } } else { $order = $this->di['db']->getExistingModelById('ClientOrder', $orderId, 'Order not found'); } diff --git a/src/modules/Servicelicense/Api/Client.php b/src/modules/Servicelicense/Api/Client.php index d7569be7b..8dede26b2 100644 --- a/src/modules/Servicelicense/Api/Client.php +++ b/src/modules/Servicelicense/Api/Client.php @@ -47,6 +47,10 @@ class Client extends \Api_Abstract throw new \FOSSBilling\Exception('Order not found'); } + if ($order->status !== \Model_ClientOrder::STATUS_ACTIVE) { + throw new \FOSSBilling\InformationException('Order is not activated'); + } + $orderService = $this->di['mod_service']('order'); $s = $orderService->getOrderService($order); if (!$s instanceof \Model_ServiceLicense) { diff --git a/src/modules/Staff/Api/Admin.php b/src/modules/Staff/Api/Admin.php index 7016fb001..b740f6481 100644 --- a/src/modules/Staff/Api/Admin.php +++ b/src/modules/Staff/Api/Admin.php @@ -178,7 +178,11 @@ class Admin extends \Api_Abstract { $model = $this->di['db']->getExistingModelById('Admin', $data['id'], 'Staff member not found'); - $this->getService()->setPermissions($model->id, $data['permissions']); + if (!is_array($data['permissions'])) { + throw new \FOSSBilling\InformationException('Parameter "permissions" must be an array'); + } + + $this->getService()->setPermissions($model, $data['permissions']); $this->di['logger']->info('Changed staff member %s permissions', $model->id); diff --git a/src/modules/Staff/Service.php b/src/modules/Staff/Service.php index 790d15cfe..5095ab11e 100644 --- a/src/modules/Staff/Service.php +++ b/src/modules/Staff/Service.php @@ -138,9 +138,28 @@ class Service implements InjectionAwareInterface return $this->di['db']->getAssoc($sql, $params); } - public function setPermissions($member_id, $array): bool + public function setPermissions(\Model_Admin $model, array $array): bool { - $this->checkPermissionsAndThrowException('staff', 'create_and_edit_staff'); + $caller = $this->di['loggedin_admin']; + + if ($caller->id == $model->id) { + throw new \FOSSBilling\InformationException('You cannot modify your own permissions'); + } + + if ($model->role === \Model_Admin::ROLE_ADMIN) { + $this->checkPermissionsAndThrowException('staff', 'create_and_edit_admin'); + } else { + $this->checkPermissionsAndThrowException('staff', 'create_and_edit_staff'); + } + + if ($caller->role !== \Model_Admin::ROLE_ADMIN) { + $callerPerms = $this->getPermissions($caller->id); + $callerHasWildcard = !empty($callerPerms['default']['all']); + + if (!$callerHasWildcard) { + $this->enforcePermissionCeiling($array, $callerPerms); + } + } $array = array_filter($array); @@ -150,14 +169,57 @@ class Service implements InjectionAwareInterface ->set('permissions', ':p') ->where('id = :id') ->setParameter('p', json_encode($array)) - ->setParameter('id', $member_id) + ->setParameter('id', $model->id) ->executeStatement(); - $this->permissionCache[(string) $member_id] = $array; + $this->permissionCache[(string) $model->id] = $array; return true; } + private function enforcePermissionCeiling(array $submitted, array $callerPerms): void + { + foreach ($submitted as $module => $modulePerms) { + if ($module === 'default') { + if (!empty($submitted['default']['all']) && empty($callerPerms['default']['all'])) { + throw new \FOSSBilling\InformationException('You cannot grant wildcard access that you do not have'); + } + + continue; + } + + if (!is_array($modulePerms)) { + continue; + } + + $hasGrantedPermissions = false; + foreach ($modulePerms as $value) { + if (!empty($value)) { + $hasGrantedPermissions = true; + break; + } + } + + if (!$hasGrantedPermissions) { + continue; + } + + if (!isset($callerPerms[$module])) { + throw new \FOSSBilling\InformationException('You cannot grant permissions for a module you do not have access to'); + } + + foreach ($modulePerms as $key => $value) { + if (empty($value)) { + continue; + } + + if (!isset($callerPerms[$module][$key]) || !$callerPerms[$module][$key]) { + throw new \FOSSBilling\InformationException('You cannot grant a permission that you do not have'); + } + } + } + } + public function getPermissions($member_id) { $cachedPermissions = $this->getPermissionsFromCache($member_id); diff --git a/src/modules/System/Service.php b/src/modules/System/Service.php index b454b431b..e79d43fb7 100644 --- a/src/modules/System/Service.php +++ b/src/modules/System/Service.php @@ -302,6 +302,12 @@ class Service { $this->di['events_manager']->fire(['event' => 'onBeforeAdminSettingsUpdate', 'params' => $data]); + foreach ($data as $key => $val) { + if (!$this->canUpdateParam($key)) { + throw new \FOSSBilling\InformationException('You do not have permission to update the parameter :param', [':param' => $key]); + } + } + foreach ($data as $key => $val) { $this->setParamValue($key, $val, true); } @@ -1115,6 +1121,10 @@ class Service 'company_number', 'company_vat_number', 'company_account_number', + 'company_bank_name', + 'company_bic', + 'company_display_bank_info', + 'company_bank_info_pagebottom', 'hide_company_public', 'company_signature', ]; diff --git a/tests-legacy/library/FOSSBilling/DiApiAccessTest.php b/tests-legacy/library/FOSSBilling/DiApiAccessTest.php index abdb96a45..a32fa608e 100644 --- a/tests-legacy/library/FOSSBilling/DiApiAccessTest.php +++ b/tests-legacy/library/FOSSBilling/DiApiAccessTest.php @@ -42,6 +42,32 @@ final class DiApiAccessTest extends PHPUnit\Framework\TestCase $di['api']('client'); } + public function testClientPageAllowsUnvalidatedClientProfilePage(): void + { + $di = $this->createContainerForRoute('/client/profile'); + + $api = $di['api']('client'); + + $this->assertInstanceOf(Api_Handler::class, $api); + } + + public function testClientPageAllowsUnvalidatedClientLogoutPage(): void + { + $di = $this->createContainerForRoute('/client/logout'); + + $api = $di['api']('client'); + + $this->assertInstanceOf(Api_Handler::class, $api); + } + + public function testClientPageBlocksUnvalidatedClientBalancePage(): void + { + $di = $this->createContainerForRoute('/client/balance'); + + $this->expectException(EmailValidationRequiredException::class); + $di['api']('client'); + } + private function createContainerForRoute(string $routePath): Pimple\Container { global $request; diff --git a/tests-legacy/modules/Order/Api/Api_ClientTest.php b/tests-legacy/modules/Order/Api/Api_ClientTest.php index 02cb8d5ab..32cffc7d1 100644 --- a/tests-legacy/modules/Order/Api/Api_ClientTest.php +++ b/tests-legacy/modules/Order/Api/Api_ClientTest.php @@ -162,6 +162,7 @@ final class Api_ClientTest extends \BBTestCase { $order = new \Model_ClientOrder(); $order->loadBean(new \DummyBean()); + $order->status = \Model_ClientOrder::STATUS_ACTIVE; $apiMock = $this->getMockBuilder(\Box\Mod\Order\Api\Client::class)->onlyMethods(['_getOrder'])->disableOriginalConstructor()->getMock(); $apiMock->expects($this->atLeastOnce()) @@ -187,6 +188,25 @@ final class Api_ClientTest extends \BBTestCase $this->assertIsArray($result); } + public function testServiceNotActiveException(): void + { + $order = new \Model_ClientOrder(); + $order->loadBean(new \DummyBean()); + $order->status = \Model_ClientOrder::STATUS_PENDING_SETUP; + + $apiMock = $this->getMockBuilder(\Box\Mod\Order\Api\Client::class)->onlyMethods(['_getOrder'])->disableOriginalConstructor()->getMock(); + $apiMock->expects($this->atLeastOnce()) + ->method('_getOrder') + ->willReturn($order); + + $data = [ + 'id' => 1, + ]; + + $this->expectException(\FOSSBilling\InformationException::class); + $apiMock->service($data); + } + public function testUpgradables(): void { $order = new \Model_ClientOrder(); diff --git a/tests-legacy/modules/Servicelicense/Api/ClientTest.php b/tests-legacy/modules/Servicelicense/Api/ClientTest.php index 850d51cd6..dded2dbd9 100644 --- a/tests-legacy/modules/Servicelicense/Api/ClientTest.php +++ b/tests-legacy/modules/Servicelicense/Api/ClientTest.php @@ -45,6 +45,10 @@ final class ClientTest extends \BBTestCase { $data['order_id'] = 1; + $orderModel = new \Model_ClientOrder(); + $orderModel->loadBean(new \DummyBean()); + $orderModel->status = \Model_ClientOrder::STATUS_ACTIVE; + $orderServiceMock = $this->createMock(\Box\Mod\Order\Service::class); $orderServiceMock->expects($this->atLeastOnce()) ->method('getOrderService') @@ -54,7 +58,7 @@ final class ClientTest extends \BBTestCase $dbMock->expects($this->atLeastOnce()) ->method('findOne') ->with('ClientOrder') - ->willReturn(new \Model_ClientOrder()); + ->willReturn($orderModel); $di = $this->getDi(); $di['db'] = $dbMock; @@ -74,20 +78,18 @@ final class ClientTest extends \BBTestCase { $data['order_id'] = 1; - $orderServiceMock = $this->createMock(\Box\Mod\Order\Service::class); - $orderServiceMock->expects($this->atLeastOnce()) - ->method('getOrderService') - ->willReturn(null); + $orderModel = new \Model_ClientOrder(); + $orderModel->loadBean(new \DummyBean()); + $orderModel->status = \Model_ClientOrder::STATUS_SUSPENDED; $dbMock = $this->createMock('\Box_Database'); $dbMock->expects($this->atLeastOnce()) ->method('findOne') ->with('ClientOrder') - ->willReturn(new \Model_ClientOrder()); + ->willReturn($orderModel); $di = $this->getDi(); $di['db'] = $dbMock; - $di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $orderServiceMock); $this->api->setDi($di); @@ -95,7 +97,7 @@ final class ClientTest extends \BBTestCase $clientModel->loadBean(new \DummyBean()); $this->api->setIdentity($clientModel); - $this->expectException(\FOSSBilling\Exception::class); + $this->expectException(\FOSSBilling\InformationException::class); $this->expectExceptionMessage('Order is not activated'); $this->api->_getService($data); } diff --git a/tests-legacy/modules/Staff/Api/AdminTest.php b/tests-legacy/modules/Staff/Api/AdminTest.php index 43ba90e87..52977d970 100644 --- a/tests-legacy/modules/Staff/Api/AdminTest.php +++ b/tests-legacy/modules/Staff/Api/AdminTest.php @@ -283,17 +283,18 @@ final class AdminTest extends \BBTestCase { $data = [ 'id' => '1', - 'permissions' => 'default', + 'permissions' => ['client' => ['access' => 1]], ]; + $staffModel = new \Model_Admin(); + $staffModel->loadBean(new \DummyBean()); + $serviceMock = $this->createMock(\Box\Mod\Staff\Service::class); $serviceMock->expects($this->atLeastOnce()) ->method('setPermissions') + ->with($staffModel, $data['permissions']) ->willReturn(true); - $staffModel = new \Model_Admin(); - $staffModel->loadBean(new \DummyBean()); - $dbMock = $this->createMock('\Box_Database'); $dbMock->expects($this->atLeastOnce()) ->method('getExistingModelById') @@ -476,7 +477,7 @@ final class AdminTest extends \BBTestCase $model = new \Model_ActivityAdminHistory(); $model->loadBean(new \DummyBean()); - $dbMock = $this->createMock('\Box_Database'); + $dbMock = $this->createStub('\Box_Database'); $dbMock->method('getExistingModelById') ->willReturn($model); @@ -524,7 +525,7 @@ final class AdminTest extends \BBTestCase { $data['id'] = '1'; - $serviceMock = $this->createMock(\Box\Mod\Staff\Service::class); + $serviceMock = $this->createStub(\Box\Mod\Staff\Service::class); $serviceMock->method('toActivityAdminHistoryApiArray') ->willReturn([]); diff --git a/tests-legacy/modules/Staff/Api/GuestTest.php b/tests-legacy/modules/Staff/Api/GuestTest.php index 5ea056c95..751987903 100644 --- a/tests-legacy/modules/Staff/Api/GuestTest.php +++ b/tests-legacy/modules/Staff/Api/GuestTest.php @@ -58,9 +58,7 @@ final class GuestTest extends \BBTestCase ->method('login') ->willReturn([]); - $sessionMock = $this->getMockBuilder(\FOSSBilling\Session::class) - ->disableOriginalConstructor() - ->getMock(); + $sessionMock = $this->createStub(\FOSSBilling\Session::class); $di = $this->getDi(); @@ -132,7 +130,7 @@ final class GuestTest extends \BBTestCase $dbMock->expects($this->never()) ->method('findOne'); - $rateLimiterMock = $this->createMock(\FOSSBilling\Security\RateLimiter::class); + $rateLimiterMock = $this->createStub(\FOSSBilling\Security\RateLimiter::class); $rateLimiterMock->method('consumeOrThrow')->willReturn(new RateLimitResult('test', false, 10, 9)); $di = $this->getDi(); @@ -199,10 +197,10 @@ final class GuestTest extends \BBTestCase ->with('staff@example.com') ->willReturn('staff@example.com'); - $rateLimiterMock = $this->createMock(\FOSSBilling\Security\RateLimiter::class); + $rateLimiterMock = $this->createStub(\FOSSBilling\Security\RateLimiter::class); $rateLimiterMock->method('consume')->willReturn(new RateLimitResult('test', false, 10, 9)); - $extensionServiceMock = $this->createMock(\Box\Mod\Extension\Service::class); + $extensionServiceMock = $this->createStub(\Box\Mod\Extension\Service::class); $extensionServiceMock->method('isExtensionActive')->willReturn(false); $di = $this->getDi(); @@ -210,12 +208,12 @@ final class GuestTest extends \BBTestCase $di['events_manager'] = $eventMock; $di['validator'] = new \FOSSBilling\Validate(); $di['tools'] = $toolsMock; - $di['mod_service'] = $di->protect(fn (string $name): \PHPUnit\Framework\MockObject\MockObject => match ($name) { + $di['mod_service'] = $di->protect(fn (string $name) => match ($name) { 'email' => $emailServiceMock, 'extension' => $extensionServiceMock, default => $this->createMock(\Box\Mod\Extension\Service::class), }); - $di['logger'] = $this->createMock('\Box_Log'); + $di['logger'] = $this->createStub('\Box_Log'); $di['rate_limiter'] = $rateLimiterMock; $guestApi = new \Box\Mod\Staff\Api\Guest(); @@ -263,7 +261,7 @@ final class GuestTest extends \BBTestCase $passwordMock->expects($this->never()) ->method('hashIt'); - $rateLimiterMock = $this->createMock(\FOSSBilling\Security\RateLimiter::class); + $rateLimiterMock = $this->createStub(\FOSSBilling\Security\RateLimiter::class); $rateLimiterMock->method('consumeOrThrow')->willReturn(new RateLimitResult('test', false, 10, 9)); $di = $this->getDi(); @@ -272,7 +270,7 @@ final class GuestTest extends \BBTestCase $di['validator'] = new \FOSSBilling\Validate(); $di['password'] = $passwordMock; $di['rate_limiter'] = $rateLimiterMock; - $di['logger'] = $this->createMock('\Box_Log'); + $di['logger'] = $this->createStub('\Box_Log'); $guestApi = new \Box\Mod\Staff\Api\Guest(); $guestApi->setDi($di); diff --git a/tests-legacy/modules/Staff/ServiceTest.php b/tests-legacy/modules/Staff/ServiceTest.php index 9e27b0659..08b0fffd7 100644 --- a/tests-legacy/modules/Staff/ServiceTest.php +++ b/tests-legacy/modules/Staff/ServiceTest.php @@ -999,7 +999,7 @@ final class ServiceTest extends \BBTestCase $eventsMock->expects($this->atLeastOnce()) ->method('fire'); - $logMock = $this->createMock('\Box_Log'); + $logMock = $this->createStub('\Box_Log'); $dbMock = $this->createMock('\Box_Database'); $dbMock->expects($this->atLeastOnce()) @@ -1031,7 +1031,7 @@ final class ServiceTest extends \BBTestCase $eventsMock->expects($this->atLeastOnce()) ->method('fire'); - $logMock = $this->createMock('\Box_Log'); + $logMock = $this->createStub('\Box_Log'); $dbMock = $this->createMock('\Box_Database'); $dbMock->expects($this->atLeastOnce()) @@ -1077,7 +1077,7 @@ final class ServiceTest extends \BBTestCase $eventsMock->expects($this->atLeastOnce()) ->method('fire'); - $logMock = $this->createMock('\Box_Log'); + $logMock = $this->createStub('\Box_Log'); $dbMock = $this->createMock('\Box_Database'); $dbMock->expects($this->atLeastOnce()) @@ -1088,7 +1088,7 @@ final class ServiceTest extends \BBTestCase ->method('hashIt') ->with($plainTextPassword); - $profileService = $this->createMock(\Box\Mod\Profile\Service::class); + $profileService = $this->createStub(\Box\Mod\Profile\Service::class); $serviceMock = $this->getMockBuilder(Service::class) ->onlyMethods(['hasPermission'])->getMock(); @@ -1101,7 +1101,7 @@ final class ServiceTest extends \BBTestCase $di['logger'] = $logMock; $di['db'] = $dbMock; $di['password'] = $passwordMock; - $di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $profileService); + $di['mod_service'] = $di->protect(fn () => $profileService); $serviceMock->setDi($di); @@ -1140,7 +1140,7 @@ final class ServiceTest extends \BBTestCase ->method('store') ->willReturn($newId); - $logMock = $this->createMock('\Box_Log'); + $logMock = $this->createStub('\Box_Log'); $passwordMock = $this->createMock(\FOSSBilling\PasswordManager::class); $passwordMock->expects($this->atLeastOnce()) @@ -1199,7 +1199,7 @@ final class ServiceTest extends \BBTestCase ->method('store') ->willThrowException(new \RedBeanPHP\RedException()); - $logMock = $this->createMock('\Box_Log'); + $logMock = $this->createStub('\Box_Log'); $passwordMock = $this->createMock(\FOSSBilling\PasswordManager::class); $passwordMock->expects($this->atLeastOnce()) @@ -1549,15 +1549,114 @@ final class ServiceTest extends \BBTestCase } }; + $callerModel = new \Model_Admin(); + $callerModel->loadBean(new \DummyBean()); + $callerModel->id = 1; + $callerModel->role = \Model_Admin::ROLE_ADMIN; + + $targetModel = new \Model_Admin(); + $targetModel->loadBean(new \DummyBean()); + $targetModel->id = 2; + $targetModel->role = \Model_Admin::ROLE_STAFF; + $di = new \Pimple\Container(); $di['dbal'] = $dbalMock; + $di['loggedin_admin'] = $callerModel; $serviceMock->setDi($di); - $member_id = 1; - $result = $serviceMock->setPermissions($member_id, []); + $result = $serviceMock->setPermissions($targetModel, []); $this->assertTrue($result); } + public function testSetPermissionsSelfEditThrows(): void + { + $service = new Service(); + + $callerModel = new \Model_Admin(); + $callerModel->loadBean(new \DummyBean()); + $callerModel->id = 1; + $callerModel->role = \Model_Admin::ROLE_STAFF; + + $di = new \Pimple\Container(); + $di['loggedin_admin'] = $callerModel; + $service->setDi($di); + + $this->expectException(\FOSSBilling\InformationException::class); + $this->expectExceptionMessage('You cannot modify your own permissions'); + $service->setPermissions($callerModel, []); + } + + public function testSetPermissionsCeilingEnforced(): void + { + $serviceMock = $this->getMockBuilder(Service::class) + ->onlyMethods(['hasPermission', 'getPermissions'])->getMock(); + + $serviceMock->expects($this->atLeastOnce()) + ->method('hasPermission')->willReturn(true); + + $callerPerms = [ + 'client' => ['access' => 1, 'view' => 1], + ]; + $serviceMock->expects($this->atLeastOnce()) + ->method('getPermissions')->willReturn($callerPerms); + + $callerModel = new \Model_Admin(); + $callerModel->loadBean(new \DummyBean()); + $callerModel->id = 1; + $callerModel->role = \Model_Admin::ROLE_STAFF; + + $targetModel = new \Model_Admin(); + $targetModel->loadBean(new \DummyBean()); + $targetModel->id = 2; + $targetModel->role = \Model_Admin::ROLE_STAFF; + + $di = new \Pimple\Container(); + $di['loggedin_admin'] = $callerModel; + $serviceMock->setDi($di); + + $submitted = [ + 'client' => ['access' => 1, 'view' => 1, 'manage_balance' => 1], + ]; + + $this->expectException(\FOSSBilling\InformationException::class); + $this->expectExceptionMessage('You cannot grant a permission that you do not have'); + $serviceMock->setPermissions($targetModel, $submitted); + } + + public function testSetPermissionsWildcardCeilingEnforced(): void + { + $serviceMock = $this->getMockBuilder(Service::class) + ->onlyMethods(['hasPermission', 'getPermissions'])->getMock(); + + $serviceMock->expects($this->atLeastOnce()) + ->method('hasPermission')->willReturn(true); + + $serviceMock->expects($this->atLeastOnce()) + ->method('getPermissions')->willReturn([]); + + $callerModel = new \Model_Admin(); + $callerModel->loadBean(new \DummyBean()); + $callerModel->id = 1; + $callerModel->role = \Model_Admin::ROLE_STAFF; + + $targetModel = new \Model_Admin(); + $targetModel->loadBean(new \DummyBean()); + $targetModel->id = 2; + $targetModel->role = \Model_Admin::ROLE_STAFF; + + $di = new \Pimple\Container(); + $di['loggedin_admin'] = $callerModel; + $serviceMock->setDi($di); + + $submitted = [ + 'default' => ['all' => 1], + ]; + + $this->expectException(\FOSSBilling\InformationException::class); + $this->expectExceptionMessage('You cannot grant wildcard access that you do not have'); + $serviceMock->setPermissions($targetModel, $submitted); + } + public function testGetPermissionsPermAreEmpty(): void { $statementWithFetchOne = new class { diff --git a/tests-legacy/modules/System/ServiceTest.php b/tests-legacy/modules/System/ServiceTest.php index 353551358..193aaa15a 100644 --- a/tests-legacy/modules/System/ServiceTest.php +++ b/tests-legacy/modules/System/ServiceTest.php @@ -178,6 +178,9 @@ final class ServiceTest extends \BBTestCase $logMock = $this->createStub('\Box_Log'); + $staffServiceMock = $this->createStub(\Box\Mod\Staff\Service::class); + $staffServiceMock->method('hasPermission')->willReturn(true); + $systemServiceMock = $this->getMockBuilder(Service::class)->onlyMethods(['setParamValue'])->getMock(); $systemServiceMock->expects($this->atLeastOnce()) ->method('setParamValue') @@ -186,6 +189,10 @@ final class ServiceTest extends \BBTestCase $di = $this->getDi(); $di['events_manager'] = $eventMock; $di['logger'] = $logMock; + $di['mod_service'] = $di->protect(fn (string $name) => match ($name) { + 'Staff' => $staffServiceMock, + default => throw new \RuntimeException("Unexpected mod_service request: $name"), + }); $systemServiceMock->setDi($di); $result = $systemServiceMock->updateParams($data); @@ -1158,4 +1165,92 @@ final class ServiceTest extends \BBTestCase $result = $this->service->clearPendingMessages(); $this->assertTrue($result); } + + public function testUpdateParamsRejectsBankFieldsWithoutPermission(): void + { + $data = [ + 'company_bank_name' => 'Malicious Bank', + ]; + + $eventMock = $this->createMock('\Box_EventManager'); + $eventMock->expects($this->atLeastOnce()) + ->method('fire'); + + $staffServiceMock = $this->createStub(\Box\Mod\Staff\Service::class); + $staffServiceMock->method('hasPermission')->willReturn(false); + + $di = $this->getDi(); + $di['events_manager'] = $eventMock; + $di['mod_service'] = $di->protect(fn (string $name) => match ($name) { + 'Staff' => $staffServiceMock, + default => throw new \RuntimeException("Unexpected mod_service request: $name"), + }); + + $this->service->setDi($di); + + $this->expectException(\FOSSBilling\InformationException::class); + $this->expectExceptionMessage('You do not have permission to update the parameter company_bank_name'); + $this->service->updateParams($data); + } + + public function testUpdateParamsRejectsBicWithoutPermission(): void + { + $data = [ + 'company_bic' => 'MALICUS33', + ]; + + $eventMock = $this->createMock('\Box_EventManager'); + $eventMock->expects($this->atLeastOnce()) + ->method('fire'); + + $staffServiceMock = $this->createStub(\Box\Mod\Staff\Service::class); + $staffServiceMock->method('hasPermission')->willReturn(false); + + $di = $this->getDi(); + $di['events_manager'] = $eventMock; + $di['mod_service'] = $di->protect(fn (string $name) => match ($name) { + 'Staff' => $staffServiceMock, + default => throw new \RuntimeException("Unexpected mod_service request: $name"), + }); + + $this->service->setDi($di); + + $this->expectException(\FOSSBilling\InformationException::class); + $this->expectExceptionMessage('You do not have permission to update the parameter company_bic'); + $this->service->updateParams($data); + } + + public function testUpdateParamsAllowsBankFieldsWithPermission(): void + { + $data = [ + 'company_bank_name' => 'Safe Bank', + 'company_bic' => 'SAFEUS33', + ]; + + $eventMock = $this->createMock('\Box_EventManager'); + $eventMock->expects($this->atLeastOnce()) + ->method('fire'); + + $logMock = $this->createStub('\Box_Log'); + + $staffServiceMock = $this->createStub(\Box\Mod\Staff\Service::class); + $staffServiceMock->method('hasPermission')->willReturn(true); + + $systemServiceMock = $this->getMockBuilder(Service::class)->onlyMethods(['setParamValue'])->getMock(); + $systemServiceMock->expects($this->exactly(2)) + ->method('setParamValue') + ->willReturn(true); + + $di = $this->getDi(); + $di['events_manager'] = $eventMock; + $di['logger'] = $logMock; + $di['mod_service'] = $di->protect(fn (string $name) => match ($name) { + 'Staff' => $staffServiceMock, + default => throw new \RuntimeException("Unexpected mod_service request: $name"), + }); + + $systemServiceMock->setDi($di); + $result = $systemServiceMock->updateParams($data); + $this->assertTrue($result); + } }