Add Amarildo to the barber page and gate workers by service

New barber Amarildo Champimpi is introduced on the barber page in all three
languages, and workers can now be restricted to a subset of the services in
their category.

Amarildo cannot perform beard colouring, any waxing, or the all-in package, and
he is a barber only - but category 1 "Kozmetika" holds 19 barber AND 27 beauty
services, so the category-derived vertical wrongly made him beauty-capable.
Because the worker picker is only ever fetched AFTER services are ticked, one
per-service capability filter solves both problems: excluding him from every
beauty service removes him from that vertical entirely.

worker_services(worker_id, service_id) is an allow-list where an EMPTY set means
UNRESTRICTED. That default is deliberate: a missing migration degrades to the
previous behaviour instead of hiding every worker from the booking flow, and
existing workers keep working untouched. Ticking every box in the admin grid
stores nothing at all, so an unrestricted worker also picks up services added
later; unticking even one makes the worker restricted, and new services must
then be granted explicitly.

Enforcement is in three places. The picker offers only workers who can perform
EVERY selected service, and both booking paths re-check server-side, since the
picker is only a UI affordance - a crafted POST now gets service_not_offered/403
rather than a booking the worker cannot honour.

Fixed alongside, all found while building the above:

- getWorkersByCategorySlug() never filtered is_active, so marking a worker
  inactive had NO effect on the public booking flow. Both of its callers are
  guest-facing. The sibling fallback getActiveWorkers() had always filtered it.
- Worker profile picture uploads failed SILENTLY above PHP's upload_max_filesize.
  Both upload blocks gated on tmp_name alone, which cannot distinguish a rejected
  upload from "no file chosen" - PHP empties tmp_name in both cases - so the
  worker was saved with an empty worker_profile_img and no error shown. The
  upload error code is now read and reported, and a separate guard catches
  post_max_size overflow, where $_POST and $_FILES both arrive empty and the form
  silently did nothing at all.
- createWorker() omitted is_active from its INSERT, so the column default (1)
  always won and a worker created as inactive silently came back active.

Migration - the table MUST be created before this code is deployed, because the
picker query subselects it whenever service ids are passed:

    CREATE TABLE worker_services (
      worker_id  INT NOT NULL,
      service_id INT NOT NULL,
      PRIMARY KEY (worker_id, service_id),
      KEY idx_worker_services_worker (worker_id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Already applied on test, dev and prod. Server-side, upload_max_filesize/
post_max_size were raised to 8M/12M on all three environments (php.ini on test,
.user.ini on the shared-host dev and prod docroots) - not carried by this commit.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TYGSbK1erKv7VG1pvdPEjG
This commit is contained in:
Ubuntu
2026-08-24 12:07:07 +00:00
co-authored by Claude Opus 5
parent 4c823f92ca
commit 13e1b1a672
16 changed files with 638 additions and 13 deletions
+184 -5
View File
@@ -127,20 +127,28 @@ class Service_model extends CI_Model {
$this->_applyLegacyVerticalFlags($workerArray, $workerArray['service_category_id']);
// is_active was previously omitted from this INSERT, so the column
// default (1) always won and a worker created as inactive silently
// came back active. The admin's choice is honoured here; callers that
// do not supply it still get the old default.
$is_active = isset($workerArray['is_active']) ? (int)$workerArray['is_active'] : 1;
$query = $this->db->query('INSERT INTO workers (
worker_name,
worker_profile_img,
worker_info,
is_beauty,
is_barber,
service_category_id
) VALUES(?, ?, ?, ?, ?, ?)', array(
service_category_id,
is_active
) VALUES(?, ?, ?, ?, ?, ?, ?)', array(
$workerArray['worker_name'],
$workerArray['worker_profile_img'],
$workerArray['worker_info'],
$workerArray['is_beauty'],
$workerArray['is_barber'],
$workerArray['service_category_id']
$workerArray['service_category_id'],
$is_active
));
}
@@ -243,6 +251,25 @@ class Service_model extends CI_Model {
}
}
/**
* Live services belonging to one category, ordered for display as the
* admin capability grid (sub-heading, then name).
*
* @param int $service_category_id
* @return array
*/
public function getServicesByCategoryId($service_category_id){
$this->load->database();
$query = $this->db->query(
'SELECT * FROM services
WHERE service_category_id = ? AND is_deleted != "1"
ORDER BY service_type ASC, service_category_no ASC, service_id ASC',
array($service_category_id)
);
return $query->result();
}
public function getAllServiceCategories(){
$this->load->database();
$query = $this->db->query('SELECT * FROM service_categories WHERE is_deleted != "1" ORDER BY service_category_id;');
@@ -300,9 +327,161 @@ class Service_model extends CI_Model {
return $query->result();
}
public function getWorkersByCategorySlug($serv_cat_slug){
/**
* The service ids one worker is allowed to perform.
*
* An EMPTY result means "unrestricted" - the worker performs every live
* service in their category, which is how every worker behaved before
* worker_services existed. Restrictions are therefore opt-in per worker,
* and a missing migration degrades to the old behaviour instead of
* hiding everybody from the booking flow.
*
* @param int $worker_id
* @return array list of service_id ints (empty = unrestricted)
*/
public function getWorkerServiceIds($worker_id){
$this->load->database();
$query = $this->db->query('SELECT * FROM workers JOIN service_categories ON workers.service_category_id = service_categories.service_category_id WHERE workers.is_deleted != "1" AND serv_cat_slug = ? ORDER BY worker_name ASC', array($serv_cat_slug));
$query = $this->db->query(
'SELECT service_id FROM worker_services WHERE worker_id = ? ORDER BY service_id ASC',
array($worker_id)
);
$ids = array();
foreach($query->result() as $row){
$ids[] = (int)$row->service_id;
}
return $ids;
}
/**
* Replace a worker's allowed-service set.
*
* Passing an empty array clears every row, which restores the worker to
* the unrestricted default rather than making them unable to do anything.
*
* @param int $worker_id
* @param array $service_ids
* @return void
*/
public function setWorkerServiceIds($worker_id, $service_ids){
$this->load->database();
$this->db->query('DELETE FROM worker_services WHERE worker_id = ?', array($worker_id));
if(!is_array($service_ids) || empty($service_ids)){
return;
}
// De-duplicate: the PK would reject repeats and abort the whole save.
$clean = array();
foreach($service_ids as $service_id){
$service_id = (int)$service_id;
if($service_id > 0){
$clean[$service_id] = TRUE;
}
}
foreach(array_keys($clean) as $service_id){
$this->db->query(
'INSERT INTO worker_services (worker_id, service_id) VALUES (?, ?)',
array($worker_id, $service_id)
);
}
}
/**
* Can this worker perform EVERY one of these services?
*
* Used as the server-side guard behind the worker picker: the picker only
* offers capable workers, but that is a UI affordance and a crafted POST
* must not get past it.
*
* @param int $worker_id
* @param array $service_ids
* @return bool
*/
public function workerCanPerformServices($worker_id, $service_ids){
if(!is_array($service_ids) || empty($service_ids)){
return TRUE;
}
$allowed = $this->getWorkerServiceIds($worker_id);
// No rows at all = unrestricted worker.
if(empty($allowed)){
return TRUE;
}
foreach($service_ids as $service_id){
if(!in_array((int)$service_id, $allowed, TRUE)){
return FALSE;
}
}
return TRUE;
}
/**
* Bookable workers for one service category, optionally narrowed to those
* able to perform a specific set of services.
*
* Both callers are guest-facing (the booking worker picker and the
* manage-booking worker list), so an INACTIVE worker must not appear:
* this used to filter on is_deleted alone, which meant switching a
* worker to inactive in the admin had no effect on the public flow.
* getActiveWorkers() - the fallback used by the very same caller when
* no category is known - has always filtered on is_active; the two
* paths now agree.
*
* @param string $serv_cat_slug
* @param array $service_ids selected services; empty = no capability filter
* @return array
*/
public function getWorkersByCategorySlug($serv_cat_slug, $service_ids = array()){
$this->load->database();
$sql = 'SELECT w.* FROM workers w
JOIN service_categories sc ON w.service_category_id = sc.service_category_id
WHERE w.is_deleted != "1" AND w.is_active = "1" AND sc.serv_cat_slug = ?';
$params = array($serv_cat_slug);
// Capability filter. A worker qualifies when they are unrestricted (no
// worker_services rows at all) OR their allowed set covers EVERY
// selected service - counting matched rows against the number asked
// for, so a worker who can do 2 of the 3 chosen services is excluded.
$clean = array();
if(is_array($service_ids)){
foreach($service_ids as $service_id){
$service_id = (int)$service_id;
if($service_id > 0){
$clean[$service_id] = TRUE;
}
}
}
$clean = array_keys($clean);
if(!empty($clean)){
$placeholders = implode(',', array_fill(0, count($clean), '?'));
$sql .= ' AND (
NOT EXISTS (SELECT 1 FROM worker_services wsx WHERE wsx.worker_id = w.worker_id)
OR (
SELECT COUNT(*) FROM worker_services ws
WHERE ws.worker_id = w.worker_id
AND ws.service_id IN ('.$placeholders.')
) = ?
)';
foreach($clean as $service_id){
$params[] = $service_id;
}
$params[] = count($clean);
}
$sql .= ' ORDER BY w.worker_name ASC';
$query = $this->db->query($sql, $params);
return $query->result();
}