Révision a2baadd1
Ajouté par Assos Assos il y a environ 10 ans
drupal7/sites/all/modules/cas/cas.drush.inc | ||
---|---|---|
10 | 10 |
*/ |
11 | 11 |
function cas_drush_command() { |
12 | 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 |
); |
|
13 | 26 |
$items['cas-user-create'] = array( |
14 | 27 |
'callback' => 'cas_drush_user_create', |
15 | 28 |
'description' => dt('Create a CAS user account with the specified CAS username.'), |
... | ... | |
25 | 38 |
} |
26 | 39 |
|
27 | 40 |
/** |
28 |
* Implementats hook_drush_help().
|
|
41 |
* Implements hook_drush_help(). |
|
29 | 42 |
*/ |
30 | 43 |
function cas_drush_help($section) { |
31 | 44 |
switch ($section) { |
32 | 45 |
case 'drush:cas-user-create': |
33 | 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.'); |
|
34 | 49 |
} |
35 | 50 |
} |
36 | 51 |
|
... | ... | |
57 | 72 |
drush_set_error(dt('There is already a user account with CAS username @cas_name.', array('@cas_name' => $cas_name))); |
58 | 73 |
} |
59 | 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 |
} |
Formats disponibles : Unified diff
Weekly update of contrib modules