From 015e7d5651044f01a84d3d4b6931e7ca764d4b84 Mon Sep 17 00:00:00 2001 From: othermachines Date: Sat, 2 Nov 2013 13:22:12 -0600 Subject: [PATCH] Issue #1327326 by othermachines, mrfelton, ioskevich et al: Added Rules condition and actions related to OG group roles. --- og.rules.inc | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) diff --git a/og.rules.inc b/og.rules.inc index 4f95deb..c7eab2e 100644 --- a/og.rules.inc +++ b/og.rules.inc @@ -221,6 +221,58 @@ function og_rules_action_info() { 'access callback' => 'og_rules_integration_access', ); + $items['og_grant_og_role'] = array( + 'label' => t('Grant OG role'), + 'group' => t('Organic groups'), + 'parameter' => array( + 'account' => array( + 'type' => 'user', + 'label' => t('User'), + 'description' => t('The user who will be granted this role.'), + 'wrapped' => TRUE, + ), + 'group' => array( + 'type' => array_keys(og_get_all_group_entity()), + 'label' => t('Group'), + 'wrapped' => TRUE, + ), + 'roles' => array( + 'type' => 'list', + 'label' => t('Group roles'), + 'options list' => 'og_rules_group_roles_options_list', + 'restriction' => 'input', + ), + ), + 'base' => 'og_rules_grant_og_role', + 'access callback' => 'og_rules_integration_access', + ); + + $items['og_revoke_og_role'] = array( + 'label' => t('Revoke OG role'), + 'group' => t('Organic groups'), + 'parameter' => array( + 'account' => array( + 'type' => 'user', + 'label' => t('User'), + 'description' => t('The user who will have the role revoked.'), + 'wrapped' => TRUE, + ), + 'group' => array( + 'type' => array_keys(og_get_all_group_entity()), + 'label' => t('Group'), + 'wrapped' => TRUE, + ), + 'roles' => array( + 'type' => 'list', + 'label' => t('Group roles'), + 'options list' => 'og_rules_group_roles_options_list', + 'restriction' => 'input', + ), + ), + 'base' => 'og_rules_revoke_og_role', + 'access callback' => 'og_rules_integration_access', + ); + return $items; } @@ -315,6 +367,46 @@ function og_rules_remove_entity_from_group(EntityDrupalWrapper $entity, EntityDr } /** + * Action: Grant OG role. + */ +function og_rules_grant_og_role(EntityDrupalWrapper $account, EntityDrupalWrapper $group, $roles) { + $group_roles = og_roles($group->type(), NULL, $group->getIdentifier()); + foreach ($roles as $role) { + $rid = array_search($role, $group_roles); + if ($rid !== FALSE) { + og_role_grant($group->type(), $group->getIdentifier(), $account->getIdentifier(), $rid); + } + } +} + +/** + * Action: Revoke OG role. + */ +function og_rules_revoke_og_role(EntityDrupalWrapper $account, EntityDrupalWrapper $group, $roles) { + $group_roles = og_roles($group->type(), NULL, $group->getIdentifier()); + foreach ($roles as $role) { + $rid = array_search($role, $group_roles); + if ($rid !== FALSE) { + og_role_revoke($group->type(), $group->getIdentifier(), $account->getIdentifier(), $rid); + } + } +} + +/** + * Options list callback for group roles. + */ +function og_rules_group_roles_options_list($element) { + $og_roles = array(); + $unique_roles = array_unique(og_get_user_roles_name()); + foreach ($unique_roles as $role) { + if (!in_array($role, array(OG_ANONYMOUS_ROLE, OG_AUTHENTICATED_ROLE))) { + $og_roles[$role] = $role; + } + } + return $og_roles; +} + +/** * OG Rules integration access callback. */ function og_rules_integration_access($type, $name) { @@ -426,6 +518,30 @@ function og_rules_condition_info() { 'base' => 'og_rules_entity_is_group_content', 'access callback' => 'og_rules_integration_access', ); + $items['og_user_has_role'] = array( + 'label' => t('User has group role'), + 'group' => t('Organic groups'), + 'parameter' => array( + 'account' => array( + 'type' => 'user', + 'label' => t('User'), + 'wrapped' => TRUE, + ), + 'group' => array( + 'type' => array_keys(og_get_all_group_entity()), + 'label' => t('Group'), + 'wrapped' => TRUE, + ), + 'roles' => array( + 'type' => 'list', + 'label' => t('Group roles'), + 'options list' => 'og_rules_group_roles_options_list', + 'restriction' => 'input', + ), + ), + 'base' => 'og_rules_condition_user_has_role', + 'access callback' => 'og_rules_integration_access', + ); return $items; } @@ -499,5 +615,13 @@ function og_rules_entity_is_group_content_help() { } /** + * Condition: User has group role. + */ +function og_rules_condition_user_has_role(EntityDrupalWrapper $account, EntityDrupalWrapper $group, $roles) { + $user_roles = og_get_user_roles($group->type(), $group->getIdentifier(), $account->getIdentifier()); + return !array_diff($roles, $user_roles); +} + +/** * @} */ -- 1.8.3.msysgit.0