AdonisJS does not provide an access control list (ACL) feature out of the box. Here’s are a few simple ways to provide the right access to the right user when using AdonisJS.
Use ACL plugins
There are two plugins available for AdonisJS -
The usage is pretty simple. Follow the instructions in the package to install package as a provider.
Create a view based the role/permission data structure for ease-of-use. You could now create specific roles and permissions.
Roles are associated to user, then roles are then mapped to permissions, and permissions are associated to specific operations that a user can do.
Use simple validation within your code
Consider an example where user can update a ‘todo’ record only when she is the owner on the todo, or an admin.
In the controller, we introduce the following code -
|
|
In the normal application flow -
- Route will check access to the route URL and pass the control flow to controller
- Controller will get user details from
auth
andid
fromparams
- Find record
- If
user.role
is notadmin
and user’sid
is not the same asowner_id
, throw exception
Note that InvalidAccessException
is a custom exception. See creating custom exceptions in AdonisJS to know more.
Final Word
ACL is the right way to implement role-based security in data-driven applications. However, implementing plgings may turn out to be an overkill if your requirements do not need granular permissions and roles.
Remember though - your future needs may change and that may bring in whole lot of complexities. I would advise to start with a the ACL plugin if you expect to have ‘> 5’ distinct roles and permission requirements.