I am building a module for farmOS that uses Vue.js to render pages that interact with farmOS via the API using the farmOS.js library. Overall this is working like a charm so far
One thing I would like to be able to do is to change some the available UI elements based on the role (or permissions) of the currently logged in user (e.g. a button to create a new plant type, or a new location, etc).
The challenge is that there does not seem to be a standard way to obtain role/permission information about the currently logged user via API. The admin
user can get role/permission information about their self (and others). However any other user cannot. Note: I only want the currently logged in user to get information about themselves.
I spent some time experimenting with trying to create a view that exposed an API endpoint showing just information about the currently logged user (including their roles and permissions). The effect is the same, the admin
user receives a populated roles
array in the response, while any other user does not. Apart from granting “User Administer…” permissions to other roles (clearly not a good idea), I could not see any standard way to allow a non-admin user to access their own role or permission information via the API.
Some of my thoughts at this point are:
- Inject the role/permission information for the currently logged in user into the html page as global variables in the Drupal
controller
(though I’ve not tried it so not sure if it would work either). - Have the Vue app probe farmOS paths and construct its own permission based on which paths are available or not available. (E.g. try to access the
http://farmos/admin/structure/taxonomy/manage/crop_family/add
page or try to actually create acrop_family
via API and see if it is allowed or not.)
I’m hoping others may be able to point out something I am missing or have other thoughts on how this type of functionality could be implemented. Thanks!