How to modify a new Drupal 6 user programmatically

The trick here is using the $op “insert” as it is only called once upon user creation.

For a very basic example of using hook_user(), here is how you would create a new node for the new user. Useful for automatically creating a new user’s content_profile node. This is pretty much just programmatic node creation inside of hook_user’s insert $op.

[pastacode lang=”php” manual=”%2F*%0A%20*%20Implementation%20of%20hook_user%0A%20*%2F%0Afunction%20mymodule_user(%24op%2C%20%26%24edit%2C%20%26%24account%2C%20%24category%20%3D%20NULL)%0A%7B%0A%20%20if(%24op%20%3D%3D%20%22insert%22)%7B%0A%20%20%20%20%2F%2F%20created%20an%20empty%20object%0A%20%20%20%20%24node%20%3D%20new%20stdClass()%3B%0A%20%20%20%20%24node-%3Euid%20%3D%20%24account-%3Euid%3B%0A%20%20%20%20%24node-%3Etype%20%3D%20’profile’%3B%20%20%20%20%20%20%20%2F%2F%20the%20content%20type’s%20machine%20name%0A%20%20%20%20%24node-%3Etitle%20%3D%20%24account-%3Ename%3B%20%2F%2F%20title%20the%20node%20after%20the%20author%0A%20%20%20%20%24node-%3Ebody%20%3D%20”%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20empty%20body%20field%2C%20or%20populate%20with%20example%20data%0A%20%20%20%20%24node-%3Estatus%20%3D%201%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20published%0A%0A%20%20%20%20%2F%2F%20save%20the%20new%20node%0A%20%20%20%20node_save(%24node)%3B%20%20%0A%20%20%7D%0A%7D” message=”” highlight=”” provider=”manual”/]

Applying a new role to the user. Useful for many things. You could potentially have a profile question on user registration and use the given answer to determine the role the user should receive.

[pastacode lang=”php” manual=”%2F*%0A%20*%20Implementation%20of%20hook_user%0A%20*%2F%0Afunction%20mymodule_user(%24op%2C%20%26%24edit%2C%20%26%24account%2C%20%24category%20%3D%20NULL)%0A%7B%0A%20%20if(%24op%20%3D%3D%20%22insert%22)%7B%0A%20%20%20%20%2F%2F%20set%20the%20role%20name%20and%20query%20for%20the%20role%20id%0A%20%20%20%20%24role_name%20%3D%20’test%20role’%3B%20%20%20%20%0A%20%20%20%20%24role_id%20%3D%20db_result(db_query(%22SELECT%20id%20FROM%20%7Brole%7D%20WHERE%20name%20%3D%20’%25s’%22%2C%20%24role_name))%3B%0A%20%20%20%20%0A%20%20%20%20%2F%2F%20apply%20the%20new%20role%0A%20%20%20%20%24edit%5B’roles’%5D%5B%24role_id%5D%20%3D%20%24role_name%3B%20%20%0A%20%20%7D%0A%7D” message=”” highlight=”” provider=”manual”/]

Now, let’s give the user a role based on a profile field.

I’ve created a profile field named profile_role and made it a select list with the options being Normal User and Business Owner. This case is only slightly different from the previous, in that we need to load the account’s profile information, and look at the profile_role field to decide what to do.

[pastacode lang=”php” manual=”%2F*%0A%20*%20Implementation%20of%20hook_user%0A%20*%2F%0Afunction%20mymodule_user(%24op%2C%20%26%24edit%2C%20%26%24account%2C%20%24category%20%3D%20NULL)%0A%7B%0A%20%20if(%24op%20%3D%3D%20%22insert%22)%7B%0A%20%20%20%20%2F%2F%20add%20profile%20data%20to%20the%20user%20object%0A%20%20%20%20profile_load_profile(%24account)%3B%0A%20%20%20%20%0A%20%20%20%20%2F%2F%20check%20for%20the%20selected%20role%0A%20%20%20%20if(%24account-%3Eprofile_role%20%3D%3D%20%22Business%20Owner%22)%7B%0A%20%20%20%20%20%20%2F%2F%20set%20the%20role%20name%20and%20query%20for%20the%20role%20id%0A%20%20%20%20%20%20%24role_name%20%3D%20’business%20owner’%3B%0A%20%20%20%20%20%20%24role_id%20%3D%20db_result(db_query(%22SELECT%20id%20FROM%20%7Brole%7D%20WHERE%20name%20%3D%20’%25s’%22%2C%20%24role_name))%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%2F%2F%20apply%20the%20new%20role%0A%20%20%20%20%20%20%24edit%5B’roles’%5D%5B%24role_id%5D%20%3D%20%24role_name%3B%20%20%20%20%20%20%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D” message=”” highlight=”” provider=”manual”/]

For the final example, we will block new business owners so that they require admin approval. This is something I’ve found myself doing on multiple sites recently.

Assume we have a website where the user base is divided into two primary roles. The first being the standard authenticated users, and the other users having a business owner role. A business owner has way more permissions than an authenticated user, so we want to require all business owners to be approved, while allowing standard users to skip approval.

[pastacode lang=”php” manual=”%2F*%0A%20*%20Implementation%20of%20hook_user%0A%20*%20Handle%20admin%20approval%20for%20the%20business%20owner%20role%0A%20*%2F%0Afunction%20mymodule_user(%24op%2C%20%26%24edit%2C%20%26%24account%2C%20%24category%20%3D%20NULL)%0A%7B%0A%20%20if(%24op%20%3D%3D%20%22insert%22)%7B%0A%20%20%20%20%2F%2F%20add%20profile%20data%20to%20the%20user%20object%0A%20%20%20%20profile_load_profile(%24account)%3B%0A%20%20%20%20%0A%20%20%20%20%2F%2F%20check%20for%20the%20selected%20role%0A%20%20%20%20if(%24account-%3Eprofile_role%20%3D%3D%20%22Business%20Owner%22)%7B%0A%20%20%20%20%20%20%2F%2F%20set%20the%20role%20name%20and%20query%20for%20the%20role%20id%0A%20%20%20%20%20%20%24role_name%20%3D%20’business%20owner’%3B%0A%20%20%20%20%20%20%24role_id%20%3D%20db_result(db_query(%22SELECT%20id%20FROM%20%7Brole%7D%20WHERE%20name%20%3D%20’%25s’%22%2C%20%24role_name))%3B%0A%20%20%20%20%0A%20%20%20%20%20%20%2F%2F%20apply%20the%20new%20role%0A%20%20%20%20%20%20%24edit%5B’roles’%5D%5B%24role_id%5D%20%3D%20%24role_name%3B%20%20%20%20%20%0A%0A%20%20%20%20%20%20%2F%2F%20update%20the%20database%20to%20set%20user%20status%20to%20zero%0A%20%20%20%20%20%20db_query(%22UPDATE%20%7Busers%7D%20SET%20status%20%3D%200%20WHERE%20uid%20%3D%20%25d%22%2C%20%24account-%3Euid)%3B%0A%20%20%20%20%20%20%2F%2F%20set%20the%20’changed’%20data%20to%20NULL%20so%20it%20doesn’t%20save%20over%20out%20database%20modification%0A%20%20%20%20%20%20%24edit%5B’status’%5D%20%3D%20NULL%3B%20%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D” message=”” highlight=”” provider=”manual”/]

There we have it. Programmatically giving a new user a role based on a profile field, and requiring that user to be approved by the admin.

0 Thoughts

Discussion

Leave a Reply

Your email address will not be published. Required fields are marked *