og_access.module

Tracking 5.x-8.x branch
  1. drupal
    1. 5 contributions/og/og_access.module
    2. 6 contributions/og/modules/og_access/og_access.module
    3. 7 contributions/og/og_access/og_access.module

Functions & methods

NameDescription
og_access_alter_group_form
og_access_alter_nongroup_form
og_access_form_alterImplementation of hook_form_alter().
og_access_menu
og_access_node_access_explainImplementation of hook_node_access_explain.
og_access_node_access_recordsImplementation of hook_node_access_records.
og_access_node_grantsImplementation of hook_node_grants().
og_access_settings

Constants

NameDescription
OG_PRIVATE_GROUPS_ALWAYS
OG_PRIVATE_GROUPS_CHOOSE_FALSE
OG_PRIVATE_GROUPS_CHOOSE_TRUE
OG_PRIVATE_GROUPS_NEVER
OG_VISIBLE_BOTH
OG_VISIBLE_CHOOSE_PRIVATE
OG_VISIBLE_CHOOSE_PUBLIC
OG_VISIBLE_GROUPONLY

File

View source
  1. <?php
  2. // visibility states for private groups. site admin chooses in og_access_settings()
  3. define('OG_PRIVATE_GROUPS_NEVER', 0);
  4. define('OG_PRIVATE_GROUPS_ALWAYS', 1);
  5. define('OG_PRIVATE_GROUPS_CHOOSE_TRUE', 2);
  6. define('OG_PRIVATE_GROUPS_CHOOSE_FALSE', 3);
  7. // visibility states for nodes within groups. site admin chooses in og_settings()
  8. define('OG_VISIBLE_GROUPONLY', 0);
  9. define('OG_VISIBLE_BOTH', 1);
  10. define('OG_VISIBLE_CHOOSE_PUBLIC', 2);
  11. define('OG_VISIBLE_CHOOSE_PRIVATE', 3);
  12. function og_access_menu($may_cache) {
  13. if ($may_cache) {
  14. $items[] = array(
  15. 'path' => 'admin/og/og_access',
  16. 'title' => t('Organic groups access configuration'),
  17. 'description' => t('Choose whether new groups should be private or public'),
  18. 'callback' => 'drupal_get_form',
  19. 'callback arguments' => array('og_access_settings'),
  20. );
  21. }
  22. return $items;
  23. }
  24. function og_access_settings() {
  25. drupal_add_js(drupal_get_path('module', 'og_access'). '/og_access.js'); // load the form javascript to handle private groups / node visibility conflicts in the og access settings form.
  26. $options = array(t('Visible only within the targeted groups.'), t('Visible within the targeted groups and on other pages.'), t('Visibility chosen by author/editor using a checkbox on the posting form.') .' '. t('Checkbox defaults to @pub.', array('@pub' => t('Public'))), t('Visibility chosen by author/editor using a checkbox on the posting form. '). t('Checkbox defaults to @pri.', array('@pri' => t('Private'))));
  27. $form['og_visibility'] = array('#type' => 'radios', '#title' => t('Visibility of posts'), '#default_value' => variable_get('og_visibility', 0), '#description' =>t('Determine how broadly available a given post should be when it is affiliated with a group. OG admins always see the checkbox for making a post @pub. Note that changing this setting has no effect on existing posts. Re-save those posts to acquire this new setting. If the setting you want is disabled here, check the settings under <em>Private Groups</em> setting below. You cannot set node visibility to always be public if private groups are set to always on and vice versa.', array('@pub' => t('Public'))), '#options' => $options);
  28. // private groups control
  29. $options = array(t('New group home pages and default audience are always public.'),
  30. t('New group home pages and default audience are always private.'),
  31. t('Group administrator chooses whether her group homepage and audience are private or not. Defaults to %yes.', array('%yes' => t('private'))),
  32. t('Group administrator chooses whether her group homepage and audience are private or not. Defaults to %no.', array('%no' => t('public'))),
  33. );
  34. $form['og_private_groups'] = array(
  35. '#type' => 'radios',
  36. '#title' => t('Private Groups'),
  37. '#options' => $options,
  38. '#default_value' => variable_get('og_private_groups', OG_PRIVATE_GROUPS_CHOOSE_FALSE),
  39. '#description' => '<p>'. t("A private group's group home page cannot be seen by non-members, and new posts created in the group will default to being private. This setting controls what private groups options can be used when creating a new group or editing an existing group. If you select one of the <em>group administrator chooses</em> options then it will be up to group admins whether their new groups are private or not, with the default you specify here.") .'<p><p>'. t('Note that the privacy of all <em>content</em> in the group is determined as each node is created or edited, according to the <em>Node authoring form / Visibility of Posts</em> setting on this page. Note also that changing this setting only affects the default for new groups being created, not the privacy of any existing groups! To change those you must edit the groups and their individual content nodes directly. If the setting you want is disabled here, check <em>Visibility of Posts</em> above. You cannot choose to only have private groups if node visibility is set to be always public, and vice versa.') .'</p>',
  40. );
  41. return system_settings_form($form);
  42. }
  43. function og_access_alter_group_form(&$form, $node) {
  44. // private groups
  45. $visibility = variable_get('og_private_groups', OG_PRIVATE_GROUPS_CHOOSE_FALSE);
  46. // override setting for admins - get right default
  47. if (user_access('administer nodes')) {
  48. $not = array(OG_PRIVATE_GROUPS_NEVER, OG_PRIVATE_GROUPS_CHOOSE_FALSE);
  49. $visibility = in_array($visibility, $not) ? OG_PRIVATE_GROUPS_CHOOSE_FALSE : OG_PRIVATE_GROUPS_CHOOSE_TRUE;
  50. }
  51. $default = FALSE;
  52. switch ($visibility) {
  53. case OG_PRIVATE_GROUPS_NEVER :
  54. $form['og_private'] = array (
  55. '#type' => 'value',
  56. '#value' => 0
  57. );
  58. break;
  59. case OG_PRIVATE_GROUPS_ALWAYS :
  60. $form['og_private'] = array (
  61. '#type' => 'value',
  62. '#value' => 1
  63. );
  64. break;
  65. case OG_PRIVATE_GROUPS_CHOOSE_TRUE :
  66. $default = TRUE;
  67. // fall through
  68. case OG_PRIVATE_GROUPS_CHOOSE_FALSE :
  69. $form['og_private'] = array (
  70. '#type' => 'checkbox',
  71. '#title' => t('Private group'),
  72. '#default_value' => $node->nid ? $node->og_private : $default,
  73. '#description' => t('Should this group be visible only to its members? Disabled if the group is set to <em>List in Directory</em> or <em>Membership requests: open</em>.'));
  74. break;
  75. }
  76. }
  77. // Add public checkbox to node edit form.
  78. function og_access_alter_nongroup_form(&$form, $node) {
  79. global $user;
  80. if (og_is_group_post_type($node->type)) {
  81. // get the visibility for normal users
  82. $vis = variable_get('og_visibility', 0);
  83. // override visibility for og admins
  84. if (user_access('administer organic groups')) {
  85. if ($vis < 2) {
  86. $vis = $vis == OG_VISIBLE_GROUPONLY ? OG_VISIBLE_CHOOSE_PRIVATE : OG_VISIBLE_CHOOSE_PUBLIC;
  87. }
  88. }
  89. elseif (!og_get_subscriptions($user->uid)) {
  90. // don't show checkbox if no memberships. must be public.
  91. $vis = OG_VISIBLE_BOTH;
  92. }
  93. // We are using this form element to communicate $groups from og to og_access.
  94. $groups = $form['og_initial_groups']['#value'];
  95. // If the post is to a private group, visibility must default to one of the private options.
  96. $selected_groups = isset($form['#post']['og_groups']) ? array_filter($form['#post']['og_groups']) : $groups;
  97. if (count($selected_groups)) {
  98. foreach ($selected_groups as $gid) {
  99. $group_node = new stdClass();
  100. $group_node->nid = $gid;
  101. og_load_group($group_node);
  102. if ($group_node->og_private) {
  103. // Try not to show checkbox if admin likes to reduce decisions for node authors.
  104. $vis = variable_get('og_visibility', 0) == OG_VISIBLE_BOTH ? OG_VISIBLE_GROUPONLY : OG_VISIBLE_CHOOSE_PRIVATE;
  105. break;
  106. }
  107. }
  108. }
  109. else {
  110. // TODOL: No groups. Public must be checked if it is visible.
  111. }
  112. switch ($vis) {
  113. case OG_VISIBLE_BOTH:
  114. $form['og_nodeapi']['og_public'] = array('#type' => 'value', '#value' => 1);
  115. break;
  116. case OG_VISIBLE_GROUPONLY:
  117. $form['og_nodeapi']['og_public'] = array('#type' => 'value', '#value' => 0);
  118. break;
  119. //user decides how public the post is.
  120. case OG_VISIBLE_CHOOSE_PUBLIC:
  121. $form['og_nodeapi']['visible']['og_public'] = array('#type' => 'checkbox', '#title' => t('Public'), '#default_value' => $node->nid ? $node->og_public : 1, '#description' => t('Show this post to everyone, or only to members of the groups checked above. Posts without any groups are always <em>Public</em>.'), '#weight' => 2);
  122. break;
  123. case OG_VISIBLE_CHOOSE_PRIVATE:
  124. $form['og_nodeapi']['visible']['og_public'] = array('#type' => 'checkbox', '#title' => t('Public'), '#default_value' => $node->nid ? $node->og_public : 0, '#description' => t('Show this post to everyone, or only to members of the groups checked above. Posts without any groups are always <em>Public</em>.'), '#weight' => 2);
  125. break;
  126. }
  127. if (count($form['og_nodeapi']['visible']) > 1) {
  128. $form['og_nodeapi']['#type'] = 'fieldset';
  129. $form['og_nodeapi']['#title'] = t('Groups');
  130. $form['og_nodeapi']['#collapsible'] = TRUE;
  131. $form['og_nodeapi']['#collapsed'] = $selected_groups ? TRUE : FALSE;
  132. }
  133. }
  134. }
  135. /**
  136. * Implementation of hook_form_alter().
  137. */
  138. function og_access_form_alter($form_id, &$form) {
  139. if (isset($form['#node']) && $form_id == $form['#node']->type .'_node_form') {
  140. drupal_add_js(drupal_get_path('module', 'og_access'). '/og_access.js');
  141. $node = $form['#node'];
  142. if (og_is_group_type($node->type)) {
  143. og_access_alter_group_form($form, $node);
  144. }
  145. else {
  146. og_access_alter_nongroup_form($form, $node);
  147. }
  148. }
  149. }
  150. /**
  151. * Implementation of hook_node_grants().
  152. */
  153. function og_access_node_grants($account, $op) {
  154. if ($op == 'view') {
  155. $grants['og_public'][] = 0; // everyone can see a public node
  156. }
  157. // Subscribers get an admin or non-admin grant for each subscription
  158. if ($subscriptions = og_get_subscriptions($account->uid)) {
  159. foreach ($subscriptions as $key => $val) {
  160. // Admins don't need to receive the subscriber grant since they can perform all operations.
  161. if ($val['is_admin']) {
  162. $grants['og_admin'][] = $key;
  163. }
  164. else {
  165. $grants['og_subscriber'][] = $key;
  166. }
  167. }
  168. }
  169. return isset($grants) ? $grants : array();
  170. }
  171. /**
  172. * Implementation of hook_node_access_records.
  173. */
  174. function og_access_node_access_records($node) {
  175. // don't write records if the node type is omitted or node is a group
  176. if (og_is_omitted_type($node->type)) {
  177. return;
  178. }
  179. if (og_is_group_type($node->type)) {
  180. // This grant allows group admins to manage their group.
  181. $grants[] = array(
  182. 'realm' => 'og_admin',
  183. 'gid' => $node->nid,
  184. 'grant_view' => 1,
  185. 'grant_update' => 1,
  186. 'grant_delete' => 1
  187. );
  188. // If the group is not marked private, let everyone view the group homepage.
  189. if (!$node->og_private) {
  190. $grants[] = array (
  191. 'realm' => 'og_public',
  192. 'gid' => 0,
  193. 'grant_view' => 1,
  194. 'grant_update' => 0,
  195. 'grant_delete' => 0 );
  196. }
  197. else {
  198. $grants[] = array (
  199. 'realm' => 'og_subscriber',
  200. 'gid' => $node->nid,
  201. 'grant_view' => 1,
  202. 'grant_update' => 0,
  203. 'grant_delete' => 0
  204. );
  205. }
  206. }
  207. elseif (is_array($node->og_groups)) {
  208. // Applies to non group nodes.
  209. if ($node->og_public) {
  210. $grants[] = array('realm' => 'og_public', 'gid' => 0, 'grant_view' => 1, 'grant_update' => 0, 'grant_delete' => 0);
  211. }
  212. foreach ($node->og_groups as $gid) {
  213. // Group administrators get all operations.
  214. $grants[] = array('realm' => 'og_admin', 'gid' => $gid, 'grant_view' => 1, 'grant_update' => 1, 'grant_delete' => 1);
  215. // Normal subscribers just get update operation if node type is a wiki type.
  216. $is_wiki = og_is_wiki_type($node->type);
  217. $grants[] = array('realm' => 'og_subscriber', 'gid' => $gid, 'grant_view' => 1, 'grant_update' => $is_wiki, 'grant_delete' => 0);
  218. }
  219. }
  220. return $grants;
  221. }
  222. /**
  223. * Implementation of hook_node_access_explain.
  224. */
  225. function og_access_node_access_explain($row) {
  226. if ($row->realm == 'og_public') {
  227. return t('All users may view this node.');
  228. }
  229. elseif ($row->realm == 'og_subscriber') {
  230. $node = node_load((int)$row->gid);
  231. return t('Members of !group_n may view this node.', array('!group_n' => l($node->title, "node/$row->gid")));
  232. }
  233. }