0

I know user can be given a permission to create other users. However, it doesn't seem that user table has any created_by field to store the ID of the creator. Does it?

Is there a way not only to create a field like that (could do it through profiles management) but also to actually put creators ID in it with just using drupal base config or some module?

I simply need to be able to list users created by a particular 'admin'

0

Add a field (e.g. created_by) to the user profile by going to /admin/config/people/accounts/fields.

Then implement hook_user_insert to populate the field whenever a new user is added.

hook_user_insert is called after "A user account was created." So both $edit->uid and $account->uid have the user id present.

If you changed any values in either of these objects you would need to call user_save in order to save your changes.

An alternative to avoid calling user_save again, just after the initial user object has been saved would be to use hook_user_presave that is called when "A user account is about to be created or updated"

The description of hook_user_presave goes on to say:

This hook is primarily intended for modules that want to store properties in the serialized {users}.data column, which is automatically loaded whenever a user account object is loaded, modules may add to $edit['data'] in order to have their data serialized on save.

So another option here would be instead of creating a new field (with a new table), this information could just be stored as a serialized array in the data column of the user table.

This would mean it would be slower to search for users created by a certain admin, but would would have less storage overheads.

|improve this answer|||||
  • ok, as far as I can see a custom field created this way gets a new table created in the db - field_data_xxx (where xxx is the field name). In this particular scenario it requires the entity ID, which would be the ID of the user that was just created. How can I call the ID of newly created user in this hook? – belinea Feb 25 '14 at 17:33
  • I've updated by answer... – Felix Eve Feb 26 '14 at 8:55
  • Thanks. That worked nicely. Is there a way however to allow some admin users to create other users but limit their access to people related admin settings? It seems there's only one option under permissions - Administer users - which seems to dictate either full package (create all different profile2 users, access admin people list view etc) or nothing – belinea Feb 26 '14 at 13:37
  • Yes - however I think that question deserves a whole new thread as opposed to trying to answer it in a comment... – Felix Eve Feb 26 '14 at 14:01

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.