Mini Lesson Notes on hook_perm

You are viewing a wiki page. You are welcome to join the group and then edit it. Be bold!
dipen's picture
public
dipen - Fri, 2007-06-01 23:39

INTRODUCTION


Another Mini Lesson from Dimitri's famous hook Series ,(I hope he keep giving more of his MiNi hook charm to drupal-dojo) this one is about permissions and will concentrate on hook_perm. The lesson is effective n prompt as the other two and will armour developer with the basics of hook_perm in hands on approach. All you need to know at this point of class notes is that hook_perm has something to do with authorization and permissions in drupal, Rest everything you will learn as you progress in this class notes.

Concepts Around hook_perm

hook_perm is like any other hook which gets fired when modules have to interact with the drupal core system. This particular hook is used to create new sets of permissions which can be used by programmers to accustom drupal to their organization needs (Talking about flexibility of Drupal). hook_perm as mentioned above is used for creating new permissions hence it has to return an array of permissions .

Come Along

Now we will start with an imaginary module called dojo, this module doesn't have much sense in functionality but should prove helpful to understand hook_perm. So lets see the code involved which is gonna surprise you...

<?php
function dojo_perm($form_id,&$form) {
  return array(
'be a student','be a sensai','administer the dojo');
}

/**
*  dojo.module is a test module to display the functionality of hook_form_alter
*  hook_perm is implemented by dojo module
*  by defining a function of the form dojo_perm
*  Note for Beginners: You dont close Php tag when you write modules
*  in drupal
*/
?>

Enable the dojo module from administrator section (obviously after uploading dojo.module folder in modules directory). Once you enable dojo module, you Have 3 permissions in dojo Module and you can see it by navigating to admin/user/access in your drupal test environment.

Setting a test role for demonstration

D5 (drupal 5) has 2 access roles in built viz anonymous and authorized, You can create other roles according to your needs by navigating to /admin/user and selecting Roles which should take you to /admin/user/roles. Create a new role named dojo_roll and save the configuration.
Now dojo_roll can be seen as the third column in /admin/user/access ..
In /admin/user/access you can see the three roles which you created above under dojo module, Enable any of the three permissions in dojo_roll section, Lets say "Be a student".

Demonstration of hook_perm

Now create a story page or any other kind of content that supports php code, Enable php code in your content and write the below code to see hook_perm in action ..

<?php
if(user_access('Be a student')) {
  print
"hello welcome student";
}
else
  print
" I dont know u ";
?>

And Press submit ...
If you are logged in as administrator of your D5 then You will see "Hello Welcome Student" as administrator has all access and is not limited by role types. Therefore to test the working of hook_perm, I suggest that you create two users, One with role dojo_roll and one without this role ..

You can Place the difference by the two messages when you try to access that page ..

What is user_access()

Now another interesting drupal API function used in this lesson is user_access('Be a student'), This function takes 2 arguments which are:

  • the permission you wanna check
  • the account against which you wanna check

If the second argument is omitted then currently logged in user is assumed as in the case above.
User_access returns boolean value and hence most of the time used with if clause.

Closing

Well this was a short lesson but apt and a good point to start with permissions, I request other developers to add to tricks and concepts of this lesson. It will be a great help to others. The video is completely transcribed with all codes involved.

Further Reading And Tricks

Please Add here !!