mirror of
https://github.com/opensourcepos/opensourcepos.git
synced 2026-09-13 05:47:23 -04:00
fix(sales): gate per-record endpoints behind reports_sales grant (REDACTED) Cashiers holding only the base `sales` grant could reach per-sale endpoints (getRow, getEdit, postSave, getReceipt, getInvoice, getSendPdf, getSendReceipt) that require `reports_sales`. getManage() enforced this at the list level, but individual endpoints did not re-check. Regression tests added. Auth: - Introduce `IsLoggedIn` filter to centralize login checks across controllers - Replace custom `AccessDeniedRedirectException` with built-in `RedirectException` Employees: - Add `DISALLOW_PASSWORD_CHANGE` and `DISALLOW_GRANT_CHANGE` env vars to restrict credential and permission changes in locked-down environments - Extract `hasGrantsChanged()` to streamline `postSave` Refactor: - Rename snake_case variables to camelCase in Sales, Items, and Employees controllers for PSR-12 compliance - Use explicit `db_connect()` for transaction clarity in Items controller Fixes: - SMTP config entries fall back to defaults via null coalescing - Migration uses `DROP FOREIGN KEY` instead of `DROP CONSTRAINT` - Password hash upgrade only sets session on successful `hash_version` update - Correct lang key for unknown error in Module model Language: - Translate `error_grant_change_disallowed` / `error_password_change_disallowed` across all 44 supported locales with => alignment matching en reference - Fix "cannot be deleted" messages and misc typos across ~15 language files Tests: - Bootstrap seeder only once in ItemsCsvImportTest; close connection after - Restore `DISALLOW_GRANT_CHANGE` in teardown to prevent side effects - Use `uniqid()` for test user data to avoid collisions Signed-off-by: 17935339+objecttothis@users.noreply.github.com
293 lines
9.1 KiB
PHP
293 lines
9.1 KiB
PHP
<?php
|
|
|
|
namespace Tests\Models;
|
|
|
|
use CodeIgniter\Test\CIUnitTestCase;
|
|
use CodeIgniter\Test\DatabaseTestTrait;
|
|
use App\Models\Employee;
|
|
|
|
class EmployeeTest extends CIUnitTestCase
|
|
{
|
|
use DatabaseTestTrait;
|
|
|
|
protected $migrate = true;
|
|
protected $migrateOnce = true;
|
|
protected $refresh = false;
|
|
protected $namespace = null;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
}
|
|
|
|
public function testIsAdminReturnsTrueForPersonId1(): void
|
|
{
|
|
$employeeModel = model(Employee::class);
|
|
|
|
$result = $employeeModel->isAdmin(1);
|
|
|
|
$this->assertTrue($result);
|
|
}
|
|
|
|
public function testIsAdminReturnsTrueForEmployeeWithAllPermissions(): void
|
|
{
|
|
$employeeModel = $this->getMockBuilder(Employee::class)
|
|
->onlyMethods(['has_grant'])
|
|
->getMock();
|
|
|
|
$employeeModel->method('has_grant')
|
|
->willReturn(true);
|
|
|
|
$result = $employeeModel->isAdmin(2);
|
|
|
|
$this->assertTrue($result);
|
|
}
|
|
|
|
public function testIsAdminReturnsFalseWhenMissingPermissions(): void
|
|
{
|
|
$employeeModel = $this->getMockBuilder(Employee::class)
|
|
->onlyMethods(['has_grant'])
|
|
->getMock();
|
|
|
|
$employeeModel->method('has_grant')
|
|
->willReturnCallback(function($permissionId, $personId) {
|
|
return $permissionId !== 'config';
|
|
});
|
|
|
|
$result = $employeeModel->isAdmin(3);
|
|
|
|
$this->assertFalse($result);
|
|
}
|
|
|
|
public function testCanModifyEmployeeReturnsTrueForOwnAccount(): void
|
|
{
|
|
$employeeModel = $this->getMockBuilder(Employee::class)
|
|
->onlyMethods(['isAdmin'])
|
|
->getMock();
|
|
|
|
$employeeModel->method('isAdmin')
|
|
->willReturn(false);
|
|
|
|
$result = $employeeModel->canModifyEmployee(1, 1);
|
|
|
|
$this->assertTrue($result);
|
|
}
|
|
|
|
public function testCanModifyEmployeeReturnsTrueForOwnAdminAccount(): void
|
|
{
|
|
$employeeModel = $this->getMockBuilder(Employee::class)
|
|
->onlyMethods(['isAdmin'])
|
|
->getMock();
|
|
|
|
$employeeModel->method('isAdmin')
|
|
->willReturn(true);
|
|
|
|
$result = $employeeModel->canModifyEmployee(1, 1);
|
|
|
|
$this->assertTrue($result);
|
|
}
|
|
|
|
public function testCanModifyEmployeeReturnsFalseWhenNonAdminModifiesAdmin(): void
|
|
{
|
|
$employeeModel = $this->getMockBuilder(Employee::class)
|
|
->onlyMethods(['isAdmin'])
|
|
->getMock();
|
|
|
|
$employeeModel->method('isAdmin')
|
|
->willReturnCallback(function($personId) {
|
|
return $personId === 1;
|
|
});
|
|
|
|
$result = $employeeModel->canModifyEmployee(1, 2);
|
|
|
|
$this->assertFalse($result);
|
|
}
|
|
|
|
public function testCanModifyEmployeeReturnsTrueWhenAdminModifiesNonAdmin(): void
|
|
{
|
|
$employeeModel = $this->getMockBuilder(Employee::class)
|
|
->onlyMethods(['isAdmin'])
|
|
->getMock();
|
|
|
|
$employeeModel->method('isAdmin')
|
|
->willReturnCallback(function($personId) {
|
|
return $personId === 1;
|
|
});
|
|
|
|
$result = $employeeModel->canModifyEmployee(2, 1);
|
|
|
|
$this->assertTrue($result);
|
|
}
|
|
|
|
public function testCanModifyEmployeeReturnsTrueWhenNonAdminModifiesNonAdmin(): void
|
|
{
|
|
$employeeModel = $this->getMockBuilder(Employee::class)
|
|
->onlyMethods(['isAdmin'])
|
|
->getMock();
|
|
|
|
$employeeModel->method('isAdmin')
|
|
->willReturn(false);
|
|
|
|
$result = $employeeModel->canModifyEmployee(2, 3);
|
|
|
|
$this->assertTrue($result);
|
|
}
|
|
|
|
public function testCanModifyEmployeeReturnsFalseForNonAdminEditingAdmin(): void
|
|
{
|
|
$employeeModel = $this->getMockBuilder(Employee::class)
|
|
->onlyMethods(['isAdmin'])
|
|
->getMock();
|
|
|
|
$employeeModel->method('isAdmin')
|
|
->willReturnCallback(function($personId) {
|
|
return $personId === 1;
|
|
});
|
|
|
|
$result = $employeeModel->canModifyEmployee(1, 2);
|
|
|
|
$this->assertFalse($result);
|
|
}
|
|
|
|
public function testHasGrantReturnsTrueForActualGrant(): void
|
|
{
|
|
$employeeModel = model(Employee::class);
|
|
|
|
$result = $employeeModel->has_grant('employees', 1);
|
|
|
|
$this->assertTrue($result);
|
|
}
|
|
|
|
public function testHasGrantReturnsFalseForMissingGrant(): void
|
|
{
|
|
$employeeModel = model(Employee::class);
|
|
|
|
$result = $employeeModel->has_grant('nonexistent_permission', 1);
|
|
|
|
$this->assertFalse($result);
|
|
}
|
|
|
|
protected function createEmployeeWithGrants(array $grantsData): int
|
|
{
|
|
$uniqueSuffix = uniqid();
|
|
|
|
$personData = [
|
|
'first_name' => 'Grant',
|
|
'last_name' => 'Tester',
|
|
'email' => "granttester{$uniqueSuffix}@test.com",
|
|
'phone_number' => '555-5678'
|
|
];
|
|
|
|
$employeeData = [
|
|
'username' => "granttester{$uniqueSuffix}",
|
|
'password' => password_hash('password123', PASSWORD_DEFAULT),
|
|
'hash_version' => 2,
|
|
'language_code' => 'en',
|
|
'language' => 'english'
|
|
];
|
|
|
|
$employeeModel = model(Employee::class);
|
|
$result = $employeeModel->save_employee($personData, $employeeData, $grantsData, NEW_ENTRY);
|
|
|
|
$this->assertTrue($result);
|
|
$this->assertArrayHasKey('person_id', $personData);
|
|
|
|
return $personData['person_id'];
|
|
}
|
|
|
|
public function testExistingEmployeeKeepsOriginalGrantsWhenGrantChangeDisallowed(): void
|
|
{
|
|
$employeeId = $this->createEmployeeWithGrants([
|
|
['permission_id' => 'customers', 'menu_group' => 'home']
|
|
]);
|
|
|
|
$originalDisallowGrantChange = getenv('DISALLOW_GRANT_CHANGE');
|
|
putenv('DISALLOW_GRANT_CHANGE=true');
|
|
|
|
try {
|
|
$employeeModel = model(Employee::class);
|
|
$personData = ['first_name' => 'Grant', 'last_name' => 'Tester'];
|
|
$employeeData = ['username' => 'granttester', 'language_code' => 'en', 'language' => 'english'];
|
|
$newGrantsData = [['permission_id' => 'sales', 'menu_group' => 'home']];
|
|
|
|
$saveEmployeeResult = $employeeModel->save_employee($personData, $employeeData, $newGrantsData, $employeeId);
|
|
$this->assertTrue($saveEmployeeResult);
|
|
|
|
$this->assertTrue($employeeModel->has_grant('customers', $employeeId));
|
|
$this->assertFalse($employeeModel->has_grant('sales', $employeeId));
|
|
} finally {
|
|
$originalDisallowGrantChange === false
|
|
? putenv('DISALLOW_GRANT_CHANGE')
|
|
: putenv("DISALLOW_GRANT_CHANGE={$originalDisallowGrantChange}");
|
|
}
|
|
}
|
|
|
|
public function testNewEmployeeCreationWithGrantsRejectedWhenGrantChangeDisallowed(): void
|
|
{
|
|
$originalDisallowGrantChange = getenv('DISALLOW_GRANT_CHANGE');
|
|
putenv('DISALLOW_GRANT_CHANGE=true');
|
|
|
|
try {
|
|
$result = $this->createEmployeeWithGrantsExpectingFailure([
|
|
['permission_id' => 'customers', 'menu_group' => 'home']
|
|
]);
|
|
|
|
$this->assertFalse($result);
|
|
} finally {
|
|
$originalDisallowGrantChange === false
|
|
? putenv('DISALLOW_GRANT_CHANGE')
|
|
: putenv("DISALLOW_GRANT_CHANGE={$originalDisallowGrantChange}");
|
|
}
|
|
}
|
|
|
|
protected function createEmployeeWithGrantsExpectingFailure(array $grantsData): bool
|
|
{
|
|
$uniqueSuffix = uniqid();
|
|
|
|
$personData = [
|
|
'first_name' => 'Rejected',
|
|
'last_name' => 'Tester',
|
|
'email' => "rejectedtester{$uniqueSuffix}@test.com",
|
|
'phone_number' => '555-9999'
|
|
];
|
|
|
|
$employeeData = [
|
|
'username' => "rejectedtester{$uniqueSuffix}",
|
|
'password' => password_hash('password123', PASSWORD_DEFAULT),
|
|
'hash_version' => 2,
|
|
'language_code' => 'en',
|
|
'language' => 'english'
|
|
];
|
|
|
|
$employeeModel = model(Employee::class);
|
|
|
|
return $employeeModel->save_employee($personData, $employeeData, $grantsData, NEW_ENTRY);
|
|
}
|
|
|
|
public function testExistingEmployeeGrantsUpdateWhenGrantChangeAllowed(): void
|
|
{
|
|
$employeeId = $this->createEmployeeWithGrants([
|
|
['permission_id' => 'customers', 'menu_group' => 'home']
|
|
]);
|
|
|
|
$employeeModel = model(Employee::class);
|
|
$personData = ['first_name' => 'Grant', 'last_name' => 'Tester'];
|
|
$employeeData = ['username' => 'granttester', 'language_code' => 'en', 'language' => 'english'];
|
|
$newGrantsData = [['permission_id' => 'sales', 'menu_group' => 'home']];
|
|
|
|
$employeeModel->save_employee($personData, $employeeData, $newGrantsData, $employeeId);
|
|
|
|
$this->assertFalse($employeeModel->has_grant('customers', $employeeId));
|
|
$this->assertTrue($employeeModel->has_grant('sales', $employeeId));
|
|
}
|
|
|
|
public function testNewEmployeeCreationWithGrantsSucceedsWhenGrantChangeAllowed(): void
|
|
{
|
|
$result = $this->createEmployeeWithGrantsExpectingFailure([
|
|
['permission_id' => 'customers', 'menu_group' => 'home']
|
|
]);
|
|
|
|
$this->assertTrue((bool) $result);
|
|
}
|
|
}
|