| 1 |
<?php
|
| 2 |
|
| 3 |
// Permissions
|
| 4 |
define('PERM_VIEW_WIKI_CONTENT', 'view wiki content');
|
| 5 |
define('PERM_EDIT_WIKI_CONTENT', 'edit wiki content');
|
| 6 |
define('PERM_MOVE_WIKI_CONTENT', 'move wiki content');
|
| 7 |
define('PERM_DELETE_WIKI_CONTENT', 'delete wiki content');
|
| 8 |
define('PERM_MANAGE_PROTECTED_CONTENT', 'manage protected content');
|
| 9 |
|
| 10 |
// Wiki Access Levels
|
| 11 |
define('WIKI_ACCESS_LEVEL_NONE', 0);
|
| 12 |
define('WIKI_ACCESS_LEVEL_NORMAL', 1);
|
| 13 |
define('WIKI_ACCESS_LEVEL_PROTECTED', 2);
|
| 14 |
|
| 15 |
|
| 16 |
/**
|
| 17 |
* Implementation of hook_help.
|
| 18 |
*/
|
| 19 |
function liquid_access_help($section ='') {
|
| 20 |
$output = '';
|
| 21 |
|
| 22 |
switch($section) {
|
| 23 |
case 'admin/modules#description':
|
| 24 |
$output = t('The access controll module of the Liquid Wiki Engine.');
|
| 25 |
break;
|
| 26 |
case 'admin/help#liquid':
|
| 27 |
$output = t('The access controll module of the Liquid Wiki Engine.');
|
| 28 |
break;
|
| 29 |
}
|
| 30 |
|
| 31 |
return $output;
|
| 32 |
}
|
| 33 |
|
| 34 |
/*
|
| 35 |
* Implementation of hook_perm.
|
| 36 |
*/
|
| 37 |
function liquid_access_perm() {
|
| 38 |
return array(PERM_VIEW_WIKI_CONTENT,
|
| 39 |
PERM_EDIT_WIKI_CONTENT,
|
| 40 |
PERM_MOVE_WIKI_CONTENT,
|
| 41 |
PERM_DELETE_WIKI_CONTENT,
|
| 42 |
PERM_MANAGE_PROTECTED_CONTENT,
|
| 43 |
);
|
| 44 |
}
|
| 45 |
|
| 46 |
|
| 47 |
function liquid_access_enable() {
|
| 48 |
liquid_access_isDisabled(false);
|
| 49 |
node_access_rebuild();
|
| 50 |
liquid_access_rebuild();
|
| 51 |
}
|
| 52 |
|
| 53 |
function liquid_access_disable() {
|
| 54 |
liquid_access_isDisabled(true);
|
| 55 |
node_access_rebuild();
|
| 56 |
liquid_access_rebuild();
|
| 57 |
}
|
| 58 |
|
| 59 |
function liquid_access_isDisabled($set = NULL) {
|
| 60 |
static $_isDisabled = false;
|
| 61 |
if ($set !== null)
|
| 62 |
$_isDisabled = $set;
|
| 63 |
|
| 64 |
return $_isDisabled;
|
| 65 |
}
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
/*
|
| 70 |
* Implementation of hook_form_alter
|
| 71 |
*/
|
| 72 |
|
| 73 |
function liquid_access_form_alter($form_id, &$form) {
|
| 74 |
// Check that this is the settings form and not the add form
|
| 75 |
if (!$form['#showWikiOptions'])
|
| 76 |
return;
|
| 77 |
|
| 78 |
// Add access level settings to the wiki manage form
|
| 79 |
if ($form_id == 'liquid_manage') {
|
| 80 |
$nid = $form['nid']['#value'];
|
| 81 |
$form['access'] = array('#type' => 'fieldset',
|
| 82 |
'#title' => t("Wiki Access Level"),
|
| 83 |
'#collapsible' => true,
|
| 84 |
'#collapsed' => false,
|
| 85 |
'#weight' => 5);
|
| 86 |
|
| 87 |
$level = liquid_access_get_level($nid);
|
| 88 |
if ($level === null)
|
| 89 |
$level = 0;
|
| 90 |
|
| 91 |
$form['access']['level'] = array('#type' => 'radios',
|
| 92 |
'#default_value' => $level,
|
| 93 |
'#options' => array(WIKI_ACCESS_LEVEL_NONE => t('None: <i>No access grants are provided for this node by the wiki module.</i>'),
|
| 94 |
WIKI_ACCESS_LEVEL_NORMAL => t('Normal: <i>Access is granted based wiki access permissions as long as this node stays in the wiki.</i>'),
|
| 95 |
WIKI_ACCESS_LEVEL_PROTECTED => t('Protected: <i>In addition to the normal access permissions for wiki content, the manage protected content permission is required for a grant.</i>')
|
| 96 |
)
|
| 97 |
);
|
| 98 |
}
|
| 99 |
}
|
| 100 |
|
| 101 |
|
| 102 |
function liquid_access_wikiapi($op, $nid, $wid=null, $old_wid=null) {
|
| 103 |
switch($op) {
|
| 104 |
case "validate":
|
| 105 |
break;
|
| 106 |
|
| 107 |
case "update":
|
| 108 |
liquid_access_set_level($nid, $_POST['level']);
|
| 109 |
$level = array('none', 'normal', 'protected');
|
| 110 |
drupal_set_message(t("Wiki access level has been set to %level", array('%level' => $level[$_POST['level']])));
|
| 111 |
break;
|
| 112 |
|
| 113 |
case 'insert':
|
| 114 |
liquid_access_set_level($nid, WIKI_ACCESS_LEVEL_NORMAL);
|
| 115 |
break;
|
| 116 |
|
| 117 |
case 'remove':
|
| 118 |
liquid_access_unset_level($nid);
|
| 119 |
break;
|
| 120 |
}
|
| 121 |
}
|
| 122 |
|
| 123 |
function liquid_access_nodeapi(&$node, $op, $arg = 0) {
|
| 124 |
switch ($op) {
|
| 125 |
case 'load':
|
| 126 |
$level = liquid_access_get_level($node->nid);
|
| 127 |
if (!is_null($level))
|
| 128 |
$node->wiki_access_level = $level;
|
| 129 |
break;
|
| 130 |
|
| 131 |
case 'delete':
|
| 132 |
liquid_access_unset_level($node->nid);
|
| 133 |
break;
|
| 134 |
|
| 135 |
case 'insert':
|
| 136 |
break;
|
| 137 |
|
| 138 |
case 'update':
|
| 139 |
if (isset($node->wiki_access_level) && liquid_in_wiki($node->nid))
|
| 140 |
liquid_access_set_level($node->nid, $node->wiki_access_level);
|
| 141 |
break;
|
| 142 |
}
|
| 143 |
}
|
| 144 |
|
| 145 |
/**
|
| 146 |
* Implementation of hook_node_grants.
|
| 147 |
*/
|
| 148 |
function liquid_access_node_grants($user, $op) {
|
| 149 |
$grants = array();
|
| 150 |
|
| 151 |
switch ($op) {
|
| 152 |
case 'view':
|
| 153 |
if (user_access(PERM_VIEW_WIKI_CONTENT, $user)) {
|
| 154 |
$grants[] = 1;
|
| 155 |
$grants[] = 2;
|
| 156 |
}
|
| 157 |
break;
|
| 158 |
case 'update':
|
| 159 |
if (user_access(PERM_EDIT_WIKI_CONTENT, $user)) {
|
| 160 |
$grants[] = 1;
|
| 161 |
if (user_access(PERM_MANAGE_PROTECTED_CONTENT) || user_access(PERM_MANAGE_WIKI))
|
| 162 |
$grants[] = 2;
|
| 163 |
}
|
| 164 |
break;
|
| 165 |
case 'delete':
|
| 166 |
if (user_access(PERM_DELETE_WIKI_CONTENT, $user)) {
|
| 167 |
$grants[] = 1;
|
| 168 |
if (user_access(PERM_MANAGE_PROTECTED_CONTENT) || user_access(PERM_MANAGE_WIKI))
|
| 169 |
$grants[] = 2;
|
| 170 |
}
|
| 171 |
break;
|
| 172 |
}
|
| 173 |
return array('liquid_access' => $grants);
|
| 174 |
}
|
| 175 |
|
| 176 |
/**
|
| 177 |
* Implementation of hook_wiki_grants.
|
| 178 |
*/
|
| 179 |
function liquid_access_wiki_grants($user, $op) {
|
| 180 |
$grants = array();
|
| 181 |
|
| 182 |
switch ($op) {
|
| 183 |
case 'wiki_move':
|
| 184 |
if (user_access(PERM_MOVE_WIKI_CONTENT, $user)) {
|
| 185 |
$grants[] = 1;
|
| 186 |
if (user_access(PERM_MANAGE_PROTECTED_CONTENT) || user_access(PERM_MANAGE_WIKI))
|
| 187 |
$grants[] = 2;
|
| 188 |
}
|
| 189 |
break;
|
| 190 |
}
|
| 191 |
return array('liquid_access' => $grants);
|
| 192 |
}
|
| 193 |
|
| 194 |
|
| 195 |
function liquid_access_node_access_records($node) {
|
| 196 |
$level = liquid_access_get_level($node->nid);
|
| 197 |
|
| 198 |
if (is_null($level) || liquid_access_isDisabled())
|
| 199 |
return;
|
| 200 |
|
| 201 |
switch ($level) {
|
| 202 |
case WIKI_ACCESS_LEVEL_NONE:
|
| 203 |
return null;
|
| 204 |
|
| 205 |
case WIKI_ACCESS_LEVEL_NORMAL:
|
| 206 |
return array(array('realm' => 'liquid_access',
|
| 207 |
'gid' => 1,
|
| 208 |
'grant_view' => 1,
|
| 209 |
'grant_update' => 1,
|
| 210 |
'grant_delete' => 1,
|
| 211 |
'priority' => 0,
|
| 212 |
));
|
| 213 |
break;
|
| 214 |
case WIKI_ACCESS_LEVEL_PROTECTED:
|
| 215 |
return array(array('realm' => 'liquid_access',
|
| 216 |
'gid' => 2,
|
| 217 |
'grant_view' => 1,
|
| 218 |
'grant_update' => 1,
|
| 219 |
'grant_delete' => 1,
|
| 220 |
'priority' => 0,
|
| 221 |
));
|
| 222 |
break;
|
| 223 |
}
|
| 224 |
}
|
| 225 |
|
| 226 |
function liquid_access_wiki_access_records($node) {
|
| 227 |
$level = liquid_access_get_level($node->nid);
|
| 228 |
|
| 229 |
if (is_null($level) || liquid_access_isDisabled())
|
| 230 |
return;
|
| 231 |
|
| 232 |
switch ($level) {
|
| 233 |
case WIKI_ACCESS_LEVEL_NONE:
|
| 234 |
return null;
|
| 235 |
|
| 236 |
case WIKI_ACCESS_LEVEL_NORMAL:
|
| 237 |
return array(array('realm' => 'liquid_access',
|
| 238 |
'gid' => 1,
|
| 239 |
'grant_wiki_manage' => 0,
|
| 240 |
'grant_wiki_move' => 1,
|
| 241 |
'priority' => 0,
|
| 242 |
));
|
| 243 |
break;
|
| 244 |
case WIKI_ACCESS_LEVEL_PROTECTED:
|
| 245 |
return array(array('realm' => 'liquid_access',
|
| 246 |
'gid' => 2,
|
| 247 |
'grant_wiki_manage' => 0,
|
| 248 |
'grant_wiki_move' => 1,
|
| 249 |
'priority' => 0,
|
| 250 |
));
|
| 251 |
break;
|
| 252 |
}
|
| 253 |
}
|
| 254 |
|
| 255 |
|
| 256 |
function liquid_access_unset_level($nid) {
|
| 257 |
db_query('DELETE FROM {wiki_access_level} WHERE nid = %d', $nid);
|
| 258 |
}
|
| 259 |
|
| 260 |
function liquid_access_set_level($nid, $level) {
|
| 261 |
liquid_access_unset_level($nid);
|
| 262 |
db_query("INSERT INTO {wiki_access_level} VALUES (%d, %d)", $nid, $level);
|
| 263 |
}
|
| 264 |
|
| 265 |
function liquid_access_get_level($nid) {
|
| 266 |
$result = db_query("SELECT level FROM {wiki_access_level} WHERE nid=%d", $nid);
|
| 267 |
|
| 268 |
if (db_num_rows($result) == 0) {
|
| 269 |
if (liquid_in_wiki($nid))
|
| 270 |
return WIKI_ACCESS_LEVEL_NORMAL;
|
| 271 |
else
|
| 272 |
return null;
|
| 273 |
}
|
| 274 |
|
| 275 |
return db_result($result);
|
| 276 |
}
|