User login

Providing a default user in an installation profile in Drupal 7

Found some great clues in a test

http://api.drupal.org/api/drupal/modules--simpletest--drupal_web_test_case.php/7/source

Putting that to work in our installation profile's .install file, shown beneath a modification of standard.install's role creation to create an additional 'content manager' role and to name the 'administrator' role more appropriately, as 'developer':

  // Create a role for developers, with all available permissions assigned.
  $devel_role = new stdClass();
  $devel_role->name = 'developer';
  $devel_role->weight = 3;
  user_role_save($devel_role);
  user_role_grant_permissions($devel_role->rid, array_keys(module_invoke_all('permission')));
  // Set this as the administrator role.
  variable_set('user_admin_role', $devel_role->rid);

  // Assign user 1 the "developer" role.
  db_insert('users_roles')
    ->fields(array('uid' => 1, 'rid' => $devel_role->rid))
    ->execute();

  // Create a role for site content administrators, or managers.
  $admin_role = new stdClass();
  $admin_role->name = 'content manager';
  $admin_role->weight = 2;
  user_role_save($admin_role);
  // Additional permissions beyond what the authenticated user receives.
  user_role_grant_permissions($admin_role->rid, array(
    // redundant - 'access content overview',
    'administer nodes',
    'administer users',
  ));

  // Create a user for content administration assigned to the "content manager" role.
  $edit = array();
  $edit['name']   = 'contentadmin';
  $edit['mail']   = 'contentadmin@example.com';
  $edit['roles']  = array($admin_role->rid => $admin_role->rid);
  $edit['pass']   = 'dream1939'; // Publication year of Finnegans Wake.
  $edit['status'] = 1;

  $account = user_save(drupal_anonymous_user(), $edit);

(Note: In our custom installation profile for migrating a site to Drupal, we used the content administrator's name and e-mail address. Not sure it would ever make sense to create a dummy user in an installation profile for distribution, maybe for testing, but more likely this would only make sense in an installation profile wizard that could gather information from the person installing.)

Then when it comes to creating a node attributed to this user, a couple tricks:

<?php
  node_object_prepare
($node);
 
// We have to set the uid here, because node_object_prepare() is a discrace.
 
$node->uid = $uid;
 
node_save($node);
?>

Anything else set in node_object_prepare() put to a custom setting will have to be likewise set after node_object_prepare() runs.

(If you're counting, one tip was to use node_object_prepare, and the other was how to work around the sad fact that it uses the global $user for setting the user ID.)

And the code to create a user was building the $edit array and then using user_save(drupal_anonymous_user(), $edit);.

Searched words: 
drupal 7 create user during installation programatic programmatically make user account D7

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.