Adds a scope-based access control layer to all 81 existing API routes. Sits alongside existing session-cookie auth (does not replace it). What this PR does: - Audits every existing route and documents required scope per route (see documentation/auth/route-scope-audit.md) - Adds classes/auth/scope.php with 10 scope constants and role→scope defaults - Adds classes/auth/scope_middleware.php with requireScope/requireAnyScope/requireRole - Applies require*() calls to all 81 existing routes - Adds ScopeMiddlewareTest (unit, 178 lines) and RouteScopeTest (integration, 212 lines) Coexistence note: This branch's classes/auth/scope.php is a stub that will be replaced by classes/auth/scope_registry.php (from TRU-145 / PR #396) when that PR merges first. The two have compatible APIs. Refs: TRU-149
132 lines
5.5 KiB
PHP
132 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace routes;
|
|
|
|
use classes\authentication;
|
|
use objects\customer_notes_o;
|
|
use objects\logs_o;
|
|
use objects\users_o;
|
|
use traits\route_t;
|
|
|
|
use app\auth\Scope;
|
|
use app\auth\ScopeMiddleware;
|
|
|
|
class customerNotes
|
|
{
|
|
use route_t;
|
|
|
|
public function run(): void
|
|
{
|
|
$this->get('/customer/notes', function () {
|
|
ScopeMiddleware::requireScope(Scope::CUSTOMER_READ, '/customer/notes');
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('list_customer_notes');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Get the query parameters from the URL
|
|
$data = $_GET;
|
|
// Check if the required fields are set
|
|
if (!isset($data['customer_number']) && !isset($data['user_id'])) {
|
|
$response->error('User ID or Customer Number is required', 400);
|
|
}
|
|
// Log the incident
|
|
(new logs_o())->add('customer_notes', 'global', 1, $user->id, 'LIST_CUSTOMER_NOTES', 'Successfully listed customer notes');
|
|
// Check if the user exists
|
|
$targetUser = (new users_o())->automaticGetTargetUserFromRequest();
|
|
if (!$targetUser->exists()) {
|
|
$response->error('Customer not found', 400);
|
|
}
|
|
// Return the list of customer notes
|
|
$response->success(
|
|
($targetUser->getNotes())
|
|
);
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('customer_notes', 'global', 1, 0, 'LIST_CUSTOMER_NOTES', 'No user found, or invalid session');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
},
|
|
[
|
|
'list_customer_notes' => 'List all customer notes'
|
|
]
|
|
);
|
|
|
|
$this->post('/customer/notes', function () {
|
|
ScopeMiddleware::requireScope(Scope::CUSTOMER_WRITE, '/customer/notes');
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('add_customer_note');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Get the post data
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
// Check if the required fields are set
|
|
if (!isset($data['user_id']) && !isset($data['customer_number'])) {
|
|
$response->error('User ID or Customer Number is required', 400);
|
|
}
|
|
if (!isset($data['note'])) {
|
|
$response->error('Note is required', 400);
|
|
}
|
|
// Add the note to the customer
|
|
$customer = (new users_o())->automaticGetTargetUserFromRequest();
|
|
// Check if the customer exists
|
|
if (!$customer->exists()) {
|
|
$response->error('Customer not found', 400);
|
|
}
|
|
$note = $customer->addNote((int)$customer->id, (string)$data['note'], (int)$user->id);
|
|
// Log the incident
|
|
(new logs_o())->add('customer_notes', 'global', 1, $user->id, 'ADD_CUSTOMER_NOTE', 'Successfully added a customer note');
|
|
// Return a success message
|
|
$response->success($note->asArray());
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('customer_notes', 'global', 1, 0, 'ADD_CUSTOMER_NOTE', 'No user found, or invalid session');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
},
|
|
[
|
|
'add_customer_note' => 'Add a customer note'
|
|
]
|
|
);
|
|
|
|
$this->delete('/customer/notes', function () {
|
|
ScopeMiddleware::requireScope(Scope::CUSTOMER_WRITE, '/customer/notes');
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('delete_customer_note');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Get the query parameters from the URL
|
|
$data = $_GET;
|
|
// Check if the required fields are set
|
|
if (!isset($data['id'])) {
|
|
$response->error('Note ID is required', 400);
|
|
}
|
|
// Delete the note from the customer
|
|
(new customer_notes_o())->delete((int)$data['id']);
|
|
// Log the incident
|
|
(new logs_o())->add('customer_notes', 'global', 1, $user->id, 'DELETE_CUSTOMER_NOTE', 'Successfully deleted a customer note');
|
|
// Return a success message
|
|
$response->success(['message' => 'Customer note deleted']);
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('customer_notes', 'global', 1, 0, 'DELETE_CUSTOMER_NOTE', 'No user found, or invalid session');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
},
|
|
[
|
|
'delete_customer_note' => 'Delete a customer note'
|
|
]
|
|
);
|
|
}
|
|
} |