Projet

Général

Profil

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

root / drupal7 / sites / all / modules / cas / cas.drush.inc @ 4f315dab

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
        $user_fields = array('cas_name', 'uid', 'name', 'mail', 'status');
64
        $new_user_object = (array) $new_user_object;
65
        $new_user_object = array_intersect_key($new_user_object, array_flip($user_fields));
66
        $new_user_object = drush_key_value_to_array_table($new_user_object);
67
        drush_print_table($new_user_object);
68
      }
69
      else {
70
        drush_set_error(dt('Could not create a new user account with CAS username @cas_name.', array('@cas_name' => $cas_name)));
71
      }
72
    }
73
  }
74
  else {
75
    drush_set_error(dt('There is already a user account with CAS username @cas_name.', array('@cas_name' => $cas_name)));
76
  }
77
}
78

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

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