From 8f18b4fd3416fe42bd14aaccb019fc0f0a14f00b Mon Sep 17 00:00:00 2001
From: "Frederic G. MARAND" <fgm@osinet.fr>
Date: Mon, 4 Mar 2013 23:41:47 +0100
Subject: [PATCH 3/3] Add role dependencies.

---
 workflow_access/workflow_access.features.inc |   30 ++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/workflow_access/workflow_access.features.inc b/workflow_access/workflow_access.features.inc
index 2962ec1..ed3913d 100644
--- a/workflow_access/workflow_access.features.inc
+++ b/workflow_access/workflow_access.features.inc
@@ -5,6 +5,30 @@
  */
 
 /**
+ * Helper to find the roles needs by an export.
+ *
+ * @return array
+ *   A workflow_name-indexed hash of non-default roles in workflow_access for
+ *   that workflow.
+ */
+function _workflow_access_get_affected_roles() {
+  $sql = <<<SQL
+SELECT DISTINCT
+  w.name AS wname,
+  r.name AS rname
+FROM {workflow_access} wa
+  INNER JOIN {workflow_states} ws ON wa.sid = ws.sid
+  INNER JOIN {workflows} w ON ws.wid = w.wid
+  INNER JOIN {role} r ON wa.rid = r.rid
+SQL;
+  $roles = array();
+  foreach (db_query($sql) as $row) {
+    $roles[$row->wname][$row->rname] = $row->rname;
+  }
+  return $roles;
+}
+
+/**
  * Implements hook_features_export_options().
  *
  * Provide a list of all workflows as selectable Feature components.
@@ -27,6 +51,7 @@ function workflow_access_features_export_options() {
  * Inform Features about dependencies.
  */
 function workflow_access_features_export($data, &$export, $module_name = '') {
+  $roles = _workflow_access_get_affected_roles();
   $export['dependencies']['workflow_access'] = 'workflow_access';
   $export['dependencies']['workflow'] = 'workflow';
   $pipe = array();
@@ -35,6 +60,11 @@ function workflow_access_features_export($data, &$export, $module_name = '') {
 
     // Access configuration for a workflow needs that worfklow to exist.
     $pipe['workflow'][$component] = $component;
+
+    // And it needs the roles to which rights are granted.
+    foreach ($roles[$component] as $rname) {
+      $pipe['user_role'][$rname] = $rname;
+    }
   }
 
   return $pipe;
-- 
1.7.9.5

