...
/Solution: Enforce Role-Based Access on Service Methods
Solution: Enforce Role-Based Access on Service Methods
Use the get trap to intercept method access and enforce role-based permissions.
We'll cover the following...
We'll cover the following...
Solution explanation
Lines 2–10: We define the
UserServiceclass, which exposes two methods:getUser()for general data access, anddeleteUser()for administrative actions.This represents a real backend service with both safe and restricted operations.
Our goal is to protect certain methods without editing this class.
Lines 13–33: The
createSecureService()function creates aProxythat enforces method-level access control.The variable
restrictedMethodsdefines which methods require admin privileges. ...