Projet

Général

Profil

Paste
Télécharger (3,68 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / cas / cas.drush.inc @ 87dbc3bf

1
<?php
2

    
3
/**
4
 * @file
5
 * Drush commands for CAS.
6
 */
7

    
8
/**
9
 * Implements hook_drush_command().
10
 */
11
function cas_drush_command() {
12
  $items = array();
13
  $items['cas-user-add-role'] = array(
14
    'callback' => 'cas_drush_user_add_role',
15
    'description' => 'Add a role to the specified CAS usernames.',
16
    'arguments' => array(
17
      'role' => 'The name of the role to add',
18
      'users' => '(optional) A comma delimited list of CAS user names.',
19
    ),
20
    'required-arguments' => 1,
21
    'examples' => array(
22
      'drush cas-user-add-role "power user" casuser1,casuser2' =>
23
        'Add the "power user" role to the accounts with CAS user names casuser1 and casuser2.',
24
    ),
25
  );
26
  $items['cas-user-create'] = array(
27
    'callback' => 'cas_drush_user_create',
28
    'description' => dt('Create a CAS user account with the specified CAS username.'),
29
    'arguments' => array(
30
      'cas_name' => 'The CAS username of the account to add',
31
    ),
32
    'required-arguments' => TRUE,
33
    'examples' => array(
34
      'drush cas-user-create newcasuser' => 'Create a new user with CAS username newcasuser',
35
    ),
36
  );
37
  return $items;
38
}
39

    
40
/**
41
 * Implements hook_drush_help().
42
 */
43
function cas_drush_help($section) {
44
  switch ($section) {
45
    case 'drush:cas-user-create':
46
      return dt('Create a CAS user account with the specified CAS username.');
47
    case 'drush:cas-user-add-role':
48
      return dt('Add a role to the specified CAS usernames.');
49
  }
50
}
51

    
52
/**
53
 * Creates a new CAS user account.
54
 */
55
function cas_drush_user_create($cas_name) {
56
  // @todo: Handle additional options for setting other user attributes.
57
  $account = cas_user_load_by_name($cas_name);
58
  if ($account === FALSE) {
59
    if (!drush_get_context('DRUSH_SIMULATE')) {
60
      $options = array('invoke_cas_user_presave' => TRUE);
61
      $new_user_object = cas_user_register($cas_name, $options);
62
      if ($new_user_object !== FALSE) {
63
        _drush_user_print_info($new_user_object->uid);
64
        // return $new_user_object->uid;
65
      }
66
      else {
67
        drush_set_error(dt('Could not create a new user account with CAS username @cas_name.', array('@cas_name' => $cas_name)));
68
      }
69
    }
70
  }
71
  else {
72
    drush_set_error(dt('There is already a user account with CAS username @cas_name.', array('@cas_name' => $cas_name)));
73
  }
74
}
75

    
76
/**
77
 * Add a role to the specified CAS user accounts.
78
 */
79
function cas_drush_user_add_role($role, $users = '') {
80
  $uids = cas_drush_user_get_users_from_arguments($users);
81
  if (drush_drupal_major_version() >= 7) {
82
    $rid_query = db_query("SELECT rid FROM {role} WHERE name = :role", array(':role' => $role));
83
  }
84
  else {
85
    $rid_query = db_query("SELECT rid FROM {role} WHERE name = '%s'", $role);
86
  }
87
  if (!empty($uids)) {
88
    if ($rid = drush_db_result($rid_query)) {
89
      drush_op('user_multiple_role_edit', $uids, 'add_role', $rid);
90
      foreach ($uids as $uid) {
91
        drush_log(dt("Added the !role role to uid !uid", array('!role' => $role, '!uid' => $uid)), 'success');
92
      }
93
    }
94
    else {
95
      return drush_set_error(dt("There is no role named: !role", array('!role' => $role)));
96
    }
97
  }
98
  else {
99
    return drush_set_error("Could not find any valid uids!");
100
  }
101
}
102

    
103
/**
104
 * Look up user ids from a comma-separated list of CAS usernames.
105
 *
106
 * @param $users string
107
 *   Comma separated list of CAS usernames.
108
 *
109
 * @return array
110
 *   An array of user ids corresponding to the given CAS usernames. Unknown
111
 *   CAS usernames are ignored.
112
 */
113
function cas_drush_user_get_users_from_arguments($users) {
114
  $uids = array();
115
  if ($users !== '') {
116
    $users = explode(',', $users);
117
    foreach ($users as $user) {
118
      $account = cas_user_load_by_name($user);
119
      if ($account) {
120
        $uids[] = $account->uid;
121
      }
122
    }
123
  }
124
  return $uids;
125
}