| | 1 | = Validation of employee authorization before making a product change |
| | 2 | |
| | 3 | === Description |
| | 4 | This trigger verifies that a member of the personnel has the required authorization before they are allowed to make a change to a product. |
| | 5 | |
| | 6 | The makes_change relationship contains information about the permission, type, and authorization associated with a product change. The trigger compares the authorization provided for the change with the authorization of the employee recorded in PERSONAL. |
| | 7 | |
| | 8 | If the employee does not have the required authorization, the change is rejected. |
| | 9 | |
| | 10 | ==== Tables involved |
| | 11 | - Personal |
| | 12 | - Change |
| | 13 | - makes_change |
| | 14 | |
| | 15 | ==== Type of trigger |
| | 16 | - BEFORE INSERT |
| | 17 | - BEFORE UPDATE |
| | 18 | |
| | 19 | Activated on the `makes_change` table. |
| | 20 | |
| | 21 | ==== SQL Code |
| | 22 | {{{#!sql |
| | 23 | CREATE OR REPLACE FUNCTION validate_employee_authorization() |
| | 24 | RETURNS TRIGGER |
| | 25 | LANGUAGE plpgsql |
| | 26 | AS $$ |
| | 27 | DECLARE v_authorisation TEXT; |
| | 28 | BEGIN |
| | 29 | -- Get the employee's authorization |
| | 30 | SELECT authorisation |
| | 31 | INTO v_authorisation |
| | 32 | FROM personal |
| | 33 | WHERE id = NEW.id; |
| | 34 | -- Check whether the employee exists |
| | 35 | IF v_authorisation IS NULL THEN |
| | 36 | RAISE EXCEPTION 'Employee % does not have valid authorization.', NEW.id; |
| | 37 | END IF; |
| | 38 | -- Check whether the authorization matches |
| | 39 | IF NEW.authorisation IS DISTINCT FROM v_authorisation THEN |
| | 40 | RAISE EXCEPTION 'Employee % is not authorized to make this product change.', NEW.id; |
| | 41 | END IF; |
| | 42 | RETURN NEW; |
| | 43 | END; |
| | 44 | $$; |
| | 45 | |
| | 46 | |
| | 47 | CREATE TRIGGER trg_validate_employee_authorization |
| | 48 | BEFORE INSERT OR UPDATE |
| | 49 | ON makes_change |
| | 50 | FOR EACH ROW |
| | 51 | EXECUTE FUNCTION validate_employee_authorization(); |
| | 52 | |
| | 53 | }}} |
| | 54 | |
| | 55 | === Logic explanation |
| | 56 | |
| | 57 | Before a record is inserted or updated in makes_change, the trigger: |
| | 58 | |
| | 59 | **1.** Identifies the employee using NEW.id. |
| | 60 | **2.** Retrieves the employee's authorization from PERSONAL. |
| | 61 | **3.** Checks whether the employee has valid authorization. |
| | 62 | **4.** Compares it with the authorization specified for the product change. |
| | 63 | **5.** If the authorization matches, the operation continues. |
| | 64 | **6.** If it does not match, the database raises an exception and rejects the change. |
| | 65 | |
| | 66 | === Reason for trigger |
| | 67 | |
| | 68 | This trigger is useful because: |
| | 69 | |
| | 70 | - It prevents unauthorized personnel from modifying products. |
| | 71 | - It enforces access-control rules at the database level. |
| | 72 | - It protects product information from unauthorized changes. |
| | 73 | - It prevents the application from bypassing authorization rules. |
| | 74 | - It centralizes authorization validation in the database. |