3

On my project, I have a custom table for some custom permissions for users. I want all permissions for a user loaded on each page like $user. I created a custom module and set the global variable $cperms in hook_init() which contains all custom permissions for current logged in user.

These values are available on site pages but I am not sure it is good to use hook_init() to set global data. Is there any proper method?

  • 1
    First, why the custom table? Why not use Drupal permissions and roles? The Drupal user permissions and roles is one of the easiest and most extendable user/permissions scheme that I've seen out there, I highly doubt the need for anything custom vs using the Drupal user/role/permission base. They also integrate with most contrib modules easily. That being said you would want to actually use a custom access function to use in either a preprocessing function or if you are wanting to restrict a view then you would use the access function in a views access plugin. Many ways to use it this way. – burnsjeremy Nov 8 '15 at 4:12
0

It sounds like you're reinventing the wheel here; as @burnsjeremy mentions, there's already an effective system built into Drupal for storing permissions.

hook_permission() allows any module to define its own custom permissions that can be set per-role. Once implemented, user_access() can be called to check a user for custom permissions.

If you need some data that is more granular (i.e. at the per-user level), the global $user object has an array property called data. $user->data is loaded/saved with the user object and should always be available to you.

  • I am trying to maintain user base permissions but not role based that also depends upon current page user is on thats why I had to maintain them in a custom table. – Vikram Nov 10 '15 at 3:09
0

from a custom module you can add global variables by prefixing the variable name with global keyword.

global $my_global_var;

another way to use the variables from anywhere is by using the variable_get() and variable_set() functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.