5 * Example policy commandfile. Modify as desired.
7 * Validates commands as they are issued and returns an error
8 * or changes options when policy is violated.
10 * You can copy this file to any of the following
11 * 1. A .drush folder in your HOME folder.
12 * 2. Anywhere in a folder tree below an active module on your site.
13 * 3. /usr/share/drush/commands (configurable)
14 * 4. In an arbitrary folder specified with the --include option.
15 * 5. Drupal's /drush or sites/all/drush folder, or in the /drush
16 * folder in the directory above the Drupal root (note: sql-sync
17 * validation won't work in any of these locations).
21 * Implements drush_hook_COMMAND_validate().
23 * Prevent catastrophic braino. Note that this file has to be local to the
24 * machine that intitiates sql-sync command.
26 function drush_policy_sql_sync_validate($source = NULL, $destination = NULL) {
27 if ($destination == '@prod') {
28 return drush_set_error('POLICY_DENY', dt('Per examples/policy.drush.inc, you may never overwrite the production database.'));
33 * Implements drush_hook_COMMAND_validate().
35 * We can also limit rsync operations to production sites.
37 function drush_policy_core_rsync_validate($source = NULL, $destination = NULL) {
38 if (preg_match("/^@prod/", $destination)) {
39 return drush_set_error('POLICY_DENY', dt('Per examples/policy.drush.inc, you may never rsync to the production site.'));
44 * Implements hook_drush_sitealias_alter
46 * Alter alias record data in code.
48 function policy_drush_sitealias_alter(&$alias_record) {
49 // A duplicate of the old implementation of the 'parent' element.
50 // Keep this if you want to keep using 'parent', but do not want
51 // to be nagged (or worse, break when it is removed).
52 if (isset($alias_record['parent'])) {
53 // Fetch and merge in each parent
54 foreach (explode(',', $alias_record['parent']) as $parent) {
55 $parent_record = drush_sitealias_get_record($parent);
56 unset($parent_record['#name']);
57 unset($parent_record['#file']);
58 unset($parent_record['#hidden']);
59 $array_based_keys = array_merge(drush_get_special_keys(), array('path-aliases'));
60 foreach ($array_based_keys as $array_based_key) {
61 if (isset($alias_record[$array_based_key]) && isset($parent_record[$array_based_key])) {
62 $alias_record[$array_based_key] = array_merge($parent_record[$array_based_key], $alias_record[$array_based_key]);
65 $alias_record = array_merge($parent_record, $alias_record);
67 unset($alias_record['parent']);
72 * Implements drush_hook_COMMAND_validate().
74 * Encourage folks to use `composer` instead of Drush pm commands
76 function drush_policy_pm_updatecode_validate() {
77 return _deny_message();
80 function drush_policy_pm_update_validate() {
81 return _deny_message();
84 function drush_policy_pm_download_validate() {
85 return _deny_message();
88 function _deny_message() {
89 if (!drush_get_option('pm-force')) {
90 $msg = 'This codebase is assembled with Composer instead of Drush. Use `composer update` and `composer require` instead of `drush pm-updatecode` and `drush pm-download`. You may override this error by using the --pm-force option.';
91 return drush_set_error('POLICY_PM_DENY', dt($msg));
96 * Implements hook_drush_help_alter().
98 * When a hook extends a command with additional options, it must
99 * implement help alter and declare the option(s). Doing so will add
100 * the option to the help text for the modified command, and will also
101 * allow the new option to be specified on the command line. Without
102 * this, Drush will fail with an error when a user attempts to use
105 function policy_drush_help_alter($command) {
106 if ($command['command'] == 'updatedb') {
107 $command['options']['token'] = 'Per site policy, you must specify a token in the --token option for all commands.';
109 elseif (in_array($command['command'], array('pm-updatecode', 'pm-update', 'pm-download'))) {
110 $command['options']['pm-force'] = 'Override site policy and allow Drush codebase management (pm-* commands)';
115 * Implements drush_hook_COMMAND_validate().
117 * To test this example without copying, execute
118 * `drush --include=./examples updatedb` from within your drush directory.
120 * Unauthorized users may view pending updates but not execute them.
122 function drush_policy_updatedb_validate() {
123 // Check for a token in the request. In this case, we require --token=secret.
124 if (!drush_get_option('token') == 'secret') {
125 drush_log(dt('Per site policy, you must add a secret --token complete this command. See examples/policy.drush.inc. If you are running a version of drush prior to 4.3 and are not sure why you are seeing this message, please see http://drupal.org/node/1024824.'), 'warning');
126 drush_set_context('DRUSH_AFFIRMATIVE', FALSE);
127 drush_set_context('DRUSH_NEGATIVE', TRUE);
132 * Implements drush_hook_COMMAND_validate().
134 * Only sudo tells me to make a sandwich: http://xkcd.com/149/
136 function drush_policy_make_me_a_sandwich_validate() {
137 if (drush_is_windows()) {
138 // $name = drush_get_username();
139 // TODO: implement check for elevated process using w32api
140 // as sudo is not available for Windows
141 // @see http://php.net/manual/en/book.w32api.php
142 // @see http://social.msdn.microsoft.com/Forums/en/clr/thread/0957c58c-b30b-4972-a319-015df11b427d
145 $name = posix_getpwuid(posix_geteuid());
146 if ($name['name'] !== 'root') {
147 return drush_set_error('POLICY_MAKE_IT_YOUSELF', dt('What? Make your own sandwich.'));