Changes between Initial Version and Version 1 of Trigger7


Ignore:
Timestamp:
08/21/26 08:13:02 (27 hours ago)
Author:
235018
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Trigger7

    v1 v1  
     1= Validation of employee authorization before making a product change
     2
     3=== Description
     4This trigger verifies that a member of the personnel has the required authorization before they are allowed to make a change to a product.
     5
     6The 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
     8If 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
     19Activated on the `makes_change` table.
     20
     21==== SQL Code
     22{{{#!sql
     23CREATE OR REPLACE FUNCTION validate_employee_authorization()
     24RETURNS TRIGGER
     25LANGUAGE plpgsql
     26AS $$
     27DECLARE v_authorisation TEXT;
     28BEGIN
     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;
     43END;
     44$$;
     45
     46
     47CREATE TRIGGER trg_validate_employee_authorization
     48BEFORE INSERT OR UPDATE
     49ON makes_change
     50FOR EACH ROW
     51EXECUTE FUNCTION validate_employee_authorization();
     52
     53}}}
     54
     55=== Logic explanation
     56
     57Before 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
     68This 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.