Yaffs site version 1.1
[yaffs-website] / vendor / drush / drush / commands / core / role.drush.inc
1 <?php
2
3 use Drush\Log\LogLevel;
4 use Drush\Role\RoleBase;
5
6 /**
7  * Implementation of hook_drush_help().
8  */
9 function role_drush_help($section) {
10   switch ($section) {
11     case 'meta:role:title':
12       return dt('Role commands');
13     case 'meta:role:summary':
14       return dt('Interact with the role system.');
15   }
16 }
17
18 /**
19  * Implements hook_drush_help_alter().
20  */
21 function role_drush_help_alter(&$command) {
22   // Drupal 8+ has changed role names.
23   if (in_array($command['command'] , array('role-add-perm', 'role-add-perm', 'role-list')) && drush_drupal_major_version() >= 8) {
24     foreach ($command['examples'] as $key => $val) {
25       $newkey = str_replace(array('anonymous user', 'authenticated user'), array('anonymous', 'authenticated'), $key);
26       $command['examples'][$newkey] = $val;
27       unset($command['examples'][$key]);
28     }
29   }
30 }
31
32 /**
33  * Implementation of hook_drush_command().
34  */
35 function role_drush_command() {
36   $items['role-create'] = array(
37     'description' => 'Create a new role.',
38     'examples' => array(
39       "drush role-create 'test role'" => "Create a new role 'test role' on D6 or D7; auto-assign the rid. On D8, 'test role' is the rid, and the human-readable name is set to 'Test role'.",
40       "drush role-create 'test role' 'Test role'" => "Create a new role with a machine name of 'test role', and a human-readable name of 'Test role'. On D6 and D7, behaves as the previous example."
41     ),
42     'arguments' => array(
43       'machine name' => 'The symbolic machine name for the role. Required.',
44       'human-readable name' => 'A descriptive name for the role. Optional; Drupal 8 only.  Ignored in D6 and D7.',
45     ),
46     'aliases' => array('rcrt'),
47   );
48   $items['role-delete'] = array(
49     'description' => 'Delete a role.',
50     'examples' => array(
51       "drush role-delete 'test role'" => "Delete the role 'test role'.",
52     ),
53     'arguments' => array(
54       'machine name' => 'The symbolic machine name for the role. Required.  In D6 and D7, this may also be a numeric role ID.',
55     ),
56     'aliases' => array('rdel'),
57   );
58   $items['role-add-perm'] = array(
59     'description' => 'Grant specified permission(s) to a role.',
60     'examples' => array(
61       "drush role-add-perm 'anonymous user' 'post comments'" => 'Allow anon users to post comments.',
62       "drush role-add-perm 'anonymous user' \"'post comments','access content'\"" => 'Allow anon users to post comments and access content.',
63       "drush role-add-perm 'authenticated user' --module=node" => 'Select a permission from "node" permissions to add to logged in users.'
64     ),
65     'arguments' => array(
66       'role' => 'The role to modify.  Required.',
67       'permissions' => 'The list of permission to grant, delimited by commas. Required, unless the --module option is used.',
68     ),
69     'required-arguments' => 1,
70     'options' => array(
71       'module' => 'Select the permission to modify from an interactive list of all permissions available in the specified module.',
72     ),
73     'global-options' => array(
74       'cache-clear',
75     ),
76     'aliases' => array('rap'),
77   );
78
79   $items['role-remove-perm'] = array(
80     'description' => 'Remove specified permission(s) from a role.',
81     'examples' => array(
82       "drush role-remove-perm 'anonymous user' 'access content'" => 'Hide content from anon users.',
83     ),
84     'arguments' => array(
85       'role' => 'The role to modify.',
86       'permissions' => 'The list of permission to grant, delimited by commas. Required, unless the --module option is used.',
87     ),
88     'required-arguments' => 1,
89     'options' => array(
90       'module' => 'Select the permission to modify from an interactive list of all permissions available in the specified module.',
91     ),
92     'global-options' => array(
93       'cache-clear',
94     ),
95     'aliases' => array('rmp'),
96   );
97
98   $items['role-list'] = array(
99     'description' => 'Display a list of all roles defined on the system.  If a role name is provided as an argument, then all of the permissions of that role will be listed.  If a permission name is provided as an option, then all of the roles that have been granted that permission will be listed.',
100     'examples' => array(
101       "drush role-list --filter='administer nodes'" => 'Display a list of roles that have the administer nodes permission assigned.',
102       "drush role-list 'anonymous user'" => 'Display all of the permissions assigned to the anon user role.'
103     ),
104     'arguments' => array(
105       'role' => 'The role to list.  Optional; if specified, lists all permissions assigned to that role.  If no role is specified, lists all of the roles available on the system.',
106     ),
107     'options' => array(
108       'filter' => 'Limits the list of roles to only those that have been assigned the specified permission. Optional; may not be specified if a role argument is provided.',
109     ),
110     'outputformat' => array(
111       'default' => 'table',
112       'pipe-format' => 'list',
113       'field-labels' => array('rid' => 'ID', 'label' => 'Role Label', 'perm' => "Permission"),
114       'output-data-type' => 'format-table',
115     ),
116     'aliases' => array('rls'),
117   );
118
119   return $items;
120 }
121
122 /**
123  * Create the specified role
124  */
125 function drush_role_create($rid, $role_name = '') {
126   $role = drush_role_get_class();
127   $result = $role->role_create($rid, $role_name);
128   if ($result !== FALSE) {
129     drush_log(dt('Created "!role"', array('!role' => $rid)), LogLevel::SUCCESS);
130   }
131   return $result;
132 }
133
134 /**
135  * Create the specified role
136  */
137 function drush_role_delete($rid) {
138   $role = drush_role_get_class($rid);
139   if ($role === FALSE) {
140     return FALSE;
141   }
142   $result = $role->delete();
143   if ($result !== FALSE) {
144     drush_log(dt('Deleted "!role"', array('!role' => $rid)), LogLevel::SUCCESS);
145   }
146   return $result;
147 }
148
149 /**
150  * Add one or more permission(s) to the specified role.
151  *
152  * @param string $rid machine name for a role
153  * @param null|string $permissions machine names, delimited by commas.
154  *
155  * @return bool
156  */
157 function drush_role_add_perm($rid, $permissions = NULL) {
158   if (is_null($permissions)) {
159     // Assume --module is used thus inject a FALSE
160     $perms = array(FALSE);
161   }
162   else {
163     $perms = _convert_csv_to_array($permissions);
164   }
165
166   $added_perm = FALSE;
167
168   foreach($perms as $perm) {
169     $result = drush_role_perm('add', $rid, $perm);
170     if ($result !== FALSE) {
171       $added_perm = TRUE;
172       drush_log(dt('Added "!perm" to "!role"', array('!perm' => $perm, '!role' => $result->name)), LogLevel::SUCCESS);
173     }
174   }
175
176   if ($added_perm) {
177     drush_drupal_cache_clear_all();
178   }
179
180   return $result;
181 }
182
183 /**
184  * Remove permission(s) from the specified role.
185  *
186  * @param string $rid machine name for a role
187  * @param null|string $permissions machine names, delimited by commas.
188  *
189  * @return bool
190  */
191 function drush_role_remove_perm($rid, $permissions = NULL) {
192   if (is_null($permissions)) {
193     // Assume --module is used thus inject a FALSE
194     $perms = array(FALSE);
195   }
196   else {
197     $perms = _convert_csv_to_array($permissions);
198   }
199
200   $removed_perm = FALSE;
201
202   foreach($perms as $perm) {
203     $result = drush_role_perm('remove', $rid, $perm);
204     if ($result !== FALSE) {
205       $removed_perm = TRUE;
206       drush_log(dt('Removed "!perm" from "!role"', array('!perm' => $perm, '!role' => $result->name)), LogLevel::OK);
207     }
208   }
209
210   if ($removed_perm) {
211     drush_drupal_cache_clear_all();
212   }
213
214   return $result;
215 }
216
217 /**
218  * Implement permission add / remove operations.
219  *
220  * @param string $action 'add' | 'remove'
221  * @param string $rid
222  * @param string|null $permission
223  *
224  * @return bool|RoleBase
225  *
226  * @see drush_set_error()
227  */
228 function drush_role_perm($action, $rid, $permission = NULL) {
229   $role = drush_role_get_class($rid);
230   if (!$role) {
231     return FALSE;
232   }
233
234   // If a permission wasn't provided, but the module option is specified,
235   // provide a list of permissions provided by that module.
236   if (!$permission && $module = drush_get_option('module', FALSE)) {
237     drush_include_engine('drupal', 'environment');
238     if (!drush_module_exists($module)) {
239       return drush_set_error('DRUSH_ROLE_ERROR', dt('!module not enabled!', array('!module' => $module)));
240     }
241     $module_perms = $role->getModulePerms($module);
242     if (empty($module_perms)) {
243       return drush_set_error('DRUSH_ROLE_NO_PERMISSIONS', dt('No permissions found for module !module', array('!module' => $module)));
244     }
245     $choice = drush_choice($module_perms, "Enter a number to choose which permission to $action.");
246     if ($choice === FALSE) {
247       return drush_user_abort();
248     }
249     $permission = $module_perms[$choice];
250   }
251   else {
252     $permissions = $role->getAllModulePerms();
253     if (!in_array($permission, $permissions)) {
254       return drush_set_error(dt('Could not find the permission: !perm', array('!perm' => $permission)));
255     }
256   }
257
258   $result = $role->{$action}($permission);
259   if ($result === FALSE) {
260     return FALSE;
261   }
262   return $role;
263 }
264
265 /**
266  * Get core version specific Role handler class.
267  *
268  * @param string $role_name
269  * @return RoleBase
270  *
271  * @see drush_get_class().
272  */
273 function drush_role_get_class($role_name = DRUPAL_ANONYMOUS_RID) {
274   return drush_get_class('Drush\Role\Role', func_get_args());
275 }
276
277 /**
278  * Displays a list of roles
279  */
280 function drush_role_list($rid = '') {
281   $result = array();
282   if (empty($rid)) {
283     drush_hide_output_fields(array('perm'));
284     // Get options passed.
285     $perm = drush_get_option('filter');
286     $roles = array();
287
288     // Get all roles - if $perm is empty user_roles retrieves all roles.
289     $roles = user_roles(FALSE, $perm);
290     if (empty($roles)) {
291       return drush_set_error('DRUSH_NO_ROLES', dt("No roles found."));
292     }
293     foreach ($roles as $rid => $value) {
294       $role = drush_role_get_class($rid);
295       $result[$role->name] = array(
296         'rid' => $rid,
297         'label' => $role->name,
298       );
299     }
300   }
301   else {
302     drush_hide_output_fields(array('rid', 'label'));
303     $role = drush_role_get_class($rid);
304     if (!$role) {
305       return FALSE;
306     }
307     $perms = $role->getPerms();
308     foreach ($perms as $permission) {
309       $result[$permission] = array(
310         'perm' => $permission);
311     }
312   }
313   return $result;
314 }