features.user.inc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /**
  3. * Implements hook_features_api().
  4. */
  5. function user_features_api() {
  6. return array(
  7. 'user_role' => array(
  8. 'name' => t('Roles'),
  9. 'feature_source' => TRUE,
  10. 'default_hook' => 'user_default_roles',
  11. 'default_file' => FEATURES_DEFAULTS_INCLUDED,
  12. ),
  13. 'user_permission' => array(
  14. 'name' => t('Permissions'),
  15. 'feature_source' => TRUE,
  16. 'default_hook' => 'user_default_permissions',
  17. 'default_file' => FEATURES_DEFAULTS_INCLUDED,
  18. ),
  19. );
  20. }
  21. /**
  22. * Implements hook_features_export().
  23. */
  24. function user_permission_features_export($data, &$export, $module_name = '') {
  25. $export['dependencies']['features'] = 'features';
  26. // Ensure the modules that provide the given permissions are included as dependencies.
  27. $map = user_permission_get_modules();
  28. foreach ($data as $perm) {
  29. $perm_name = $perm;
  30. // Export vocabulary permissions using the machine name, instead of
  31. // vocabulary id.
  32. _user_features_change_term_permission($perm_name, 'machine_name');
  33. if (isset($map[$perm_name])) {
  34. $perm_module = $map[$perm_name];
  35. $export['dependencies'][$perm_module] = $perm_module;
  36. $export['features']['user_permission'][$perm] = $perm;
  37. }
  38. }
  39. return array();
  40. }
  41. /**
  42. * Implements hook_features_export_options().
  43. */
  44. function user_permission_features_export_options() {
  45. $modules = array();
  46. $module_info = system_get_info('module');
  47. foreach (module_implements('permission') as $module) {
  48. $modules[$module] = $module_info[$module]['name'];
  49. }
  50. ksort($modules);
  51. $options = array();
  52. foreach ($modules as $module => $display_name) {
  53. if ($permissions = module_invoke($module, 'permission')) {
  54. foreach ($permissions as $perm => $perm_item) {
  55. // Export vocabulary permissions using the machine name, instead of
  56. // vocabulary id.
  57. _user_features_change_term_permission($perm);
  58. $options[$perm] = strip_tags("{$display_name}: {$perm_item['title']}");
  59. }
  60. }
  61. }
  62. return $options;
  63. }
  64. /**
  65. * Implements hook_features_export_render().
  66. */
  67. function user_permission_features_export_render($module, $data) {
  68. $perm_modules = &drupal_static(__FUNCTION__ . '_perm_modules');
  69. if (!isset($perm_modules)) {
  70. $perm_modules = user_permission_get_modules();
  71. }
  72. $code = array();
  73. $code[] = ' $permissions = array();';
  74. $code[] = '';
  75. $permissions = _user_features_get_permissions();
  76. foreach ($data as $perm_name) {
  77. $permission = array();
  78. // Export vocabulary permissions using the machine name, instead of
  79. // vocabulary id.
  80. $perm = $perm_name;
  81. _user_features_change_term_permission($perm_name, 'machine_name');
  82. $permission['name'] = $perm;
  83. if (!empty($permissions[$perm_name])) {
  84. sort($permissions[$perm_name]);
  85. $permission['roles'] = drupal_map_assoc($permissions[$perm_name]);
  86. }
  87. else {
  88. $permission['roles'] = array();
  89. }
  90. if (isset($perm_modules[$perm_name])) {
  91. $permission['module'] = $perm_modules[$perm_name];
  92. }
  93. $perm_identifier = features_var_export($perm);
  94. $perm_export = features_var_export($permission, ' ');
  95. $code[] = " // Exported permission: {$perm_identifier}.";
  96. $code[] = " \$permissions[{$perm_identifier}] = {$perm_export};";
  97. $code[] = "";
  98. }
  99. $code[] = ' return $permissions;';
  100. $code = implode("\n", $code);
  101. return array('user_default_permissions' => $code);
  102. }
  103. /**
  104. * Implements hook_features_revert().
  105. */
  106. function user_permission_features_revert($module) {
  107. user_permission_features_rebuild($module);
  108. }
  109. /**
  110. * Implements hook_features_rebuild().
  111. * Iterate through default permissions and update the permissions map.
  112. *
  113. * @param $module
  114. * The module whose default user permissions should be rebuilt.
  115. */
  116. function user_permission_features_rebuild($module) {
  117. if ($defaults = features_get_default('user_permission', $module)) {
  118. // Make sure the list of available node types is up to date, especially when
  119. // installing multiple features at once, for example from an install profile
  120. // or via drush.
  121. node_types_rebuild();
  122. $modules = user_permission_get_modules();
  123. $roles = _user_features_get_roles();
  124. $permissions_by_role = _user_features_get_permissions(FALSE);
  125. foreach ($defaults as $permission) {
  126. $perm = $permission['name'];
  127. _user_features_change_term_permission($perm, 'machine_name');
  128. if (empty($modules[$perm])) {
  129. $args = array('!name' => $perm, '!module' => $module,);
  130. $msg = t('Warning in features rebuild of !module. No module defines permission "!name".', $args);
  131. drupal_set_message($msg, 'warning');
  132. continue;
  133. }
  134. // Export vocabulary permissions using the machine name, instead of
  135. // vocabulary id.
  136. foreach ($roles as $role) {
  137. if (in_array($role, $permission['roles'])) {
  138. $permissions_by_role[$role][$perm] = TRUE;
  139. }
  140. else {
  141. $permissions_by_role[$role][$perm] = FALSE;
  142. }
  143. }
  144. }
  145. // Write the updated permissions.
  146. foreach ($roles as $rid => $role) {
  147. if (isset($permissions_by_role[$role])) {
  148. user_role_change_permissions($rid, $permissions_by_role[$role]);
  149. }
  150. }
  151. }
  152. }
  153. /**
  154. * Implements hook_features_export().
  155. */
  156. function user_role_features_export($data, &$export, $module_name = '') {
  157. $export['dependencies']['features'] = 'features';
  158. $map = features_get_default_map('user_role', 'name');
  159. foreach ($data as $role) {
  160. // Role is provided by another module. Add dependency.
  161. if (isset($map[$role]) && $map[$role] != $module_name) {
  162. $export['dependencies'][$map[$role]] = $map[$role];
  163. }
  164. // Export.
  165. elseif(user_role_load_by_name($role)) {
  166. $export['features']['user_role'][$role] = $role;
  167. }
  168. }
  169. return array();
  170. }
  171. /**
  172. * Implements hook_features_export_options().
  173. */
  174. function user_role_features_export_options() {
  175. return drupal_map_assoc(_user_features_get_roles(FALSE));
  176. }
  177. /**
  178. * Implements hook_features_export_render().
  179. */
  180. function user_role_features_export_render($module, $data) {
  181. $code = array();
  182. $code[] = ' $roles = array();';
  183. $code[] = '';
  184. foreach ($data as $name) {
  185. if ($role = user_role_load_by_name($name)) {
  186. unset($role->rid);
  187. $role_identifier = features_var_export($name);
  188. $role_export = features_var_export($role , ' ');
  189. $code[] = " // Exported role: {$name}.";
  190. $code[] = " \$roles[{$role_identifier}] = {$role_export};";
  191. $code[] = "";
  192. }
  193. }
  194. $code[] = ' return $roles;';
  195. $code = implode("\n", $code);
  196. return array('user_default_roles' => $code);
  197. }
  198. /**
  199. * Implements hook_features_revert().
  200. */
  201. function user_role_features_revert($module) {
  202. user_role_features_rebuild($module);
  203. }
  204. /**
  205. * Implements hook_features_rebuild().
  206. */
  207. function user_role_features_rebuild($module) {
  208. if ($defaults = features_get_default('user_role', $module)) {
  209. foreach ($defaults as $role) {
  210. $role = (object) $role;
  211. if ($existing = user_role_load_by_name($role->name)) {
  212. $role->rid = $existing->rid;
  213. }
  214. user_role_save($role);
  215. }
  216. }
  217. }
  218. /**
  219. * Generate $rid => $role with role names untranslated.
  220. */
  221. function _user_features_get_roles($builtin = TRUE) {
  222. $roles = array();
  223. foreach (user_roles() as $rid => $name) {
  224. switch ($rid) {
  225. case DRUPAL_ANONYMOUS_RID:
  226. if ($builtin) {
  227. $roles[$rid] = 'anonymous user';
  228. }
  229. break;
  230. case DRUPAL_AUTHENTICATED_RID:
  231. if ($builtin) {
  232. $roles[$rid] = 'authenticated user';
  233. }
  234. break;
  235. default:
  236. $roles[$rid] = $name;
  237. break;
  238. }
  239. }
  240. return $roles;
  241. }
  242. /**
  243. * Represent the current state of permissions as a perm to role name array map.
  244. */
  245. function _user_features_get_permissions($by_role = TRUE) {
  246. $map = user_permission_get_modules();
  247. $roles = _user_features_get_roles();
  248. $permissions = array();
  249. foreach (user_role_permissions($roles) as $rid => $role_permissions) {
  250. if ($by_role) {
  251. foreach (array_keys(array_filter($role_permissions)) as $permission) {
  252. if (isset($map[$permission])) {
  253. $permissions[$permission][] = $roles[$rid];
  254. }
  255. }
  256. }
  257. else {
  258. $permissions[$roles[$rid]] = array();
  259. foreach ($role_permissions as $permission => $status) {
  260. if (isset($map[$permission])) {
  261. $permissions[$roles[$rid]][$permission] = $status;
  262. }
  263. }
  264. }
  265. }
  266. return $permissions;
  267. }