| 1 |
<?php |
<?php |
| 2 |
|
|
| 3 |
|
/** |
| 4 |
|
* Implementation of hook_install(). |
| 5 |
|
*/ |
| 6 |
function nodeaccess_install() { |
function nodeaccess_install() { |
| 7 |
|
// Create tables. |
| 8 |
switch ($GLOBALS['db_type']) { |
switch ($GLOBALS['db_type']) { |
| 9 |
case 'mysql': |
case 'mysql': |
| 10 |
case 'mysqli': |
case 'mysqli': |
| 11 |
db_query("CREATE TABLE {nodeaccess} ( |
db_query("CREATE TABLE {nodeaccess} ( |
| 12 |
nid int(10) unsigned NOT NULL default '0', |
nid int(10) unsigned NOT NULL default '0', |
| 13 |
gid int(10) unsigned NOT NULL default '0', |
gid int(10) unsigned NOT NULL default '0', |
| 14 |
realm varchar(255) NOT NULL default '', |
realm varchar(255) NOT NULL default '', |
| 15 |
grant_view tinyint(1) unsigned NOT NULL default '0', |
grant_view tinyint(1) unsigned NOT NULL default '0', |
| 16 |
grant_update tinyint(1) unsigned NOT NULL default '0', |
grant_update tinyint(1) unsigned NOT NULL default '0', |
| 17 |
grant_delete tinyint(1) unsigned NOT NULL default '0', |
grant_delete tinyint(1) unsigned NOT NULL default '0', |
| 18 |
PRIMARY KEY (nid,gid,realm) |
PRIMARY KEY (nid,gid,realm) |
| 19 |
) /*!40100 DEFAULT CHARACTER SET utf8 */;"); |
)"); /*!40100 DEFAULT CHARACTER SET utf8 */ |
| 20 |
break; |
db_query("CREATE TABLE {nodeaccess_role_alias} ( |
| 21 |
|
rid int(10) unsigned NOT NULL default '0', |
| 22 |
|
name varchar(50) NOT NULL default '', |
| 23 |
|
weight int(3) NOT NULL default '0', |
| 24 |
|
PRIMARY KEY (rid) |
| 25 |
|
)"); |
| 26 |
|
break; |
| 27 |
case 'pgsql': |
case 'pgsql': |
| 28 |
db_query("CREATE TABLE {nodeaccess} ( |
db_query("CREATE TABLE {nodeaccess} ( |
| 29 |
nid int_unsigned NOT NULL default '0', |
nid int_unsigned NOT NULL default '0', |
| 34 |
grant_delete smallint_unsigned NOT NULL default '0', |
grant_delete smallint_unsigned NOT NULL default '0', |
| 35 |
PRIMARY KEY (nid,gid,realm) |
PRIMARY KEY (nid,gid,realm) |
| 36 |
)"); |
)"); |
| 37 |
|
db_query("CREATE TABLE {nodeaccess_role_alias} ( |
| 38 |
|
rid int_unsigned NOT NULL default '0', |
| 39 |
|
name varchar(50) NOT NULL default '', |
| 40 |
|
weight smallint NOT NULL default '0', |
| 41 |
|
PRIMARY KEY (rid) |
| 42 |
|
)"); |
| 43 |
break; |
break; |
| 44 |
} |
} |
| 45 |
// set up default permissions to be view for authenticated and anonymouse users |
// Set up default permissions to be view for authenticated and |
| 46 |
|
// anonymous users, and all permissions for author. |
| 47 |
$grants = array(); |
$grants = array(); |
| 48 |
$grants[] = array('gid' => 1, 'realm' => 'nodeaccess_rid', 'grant_view' => 1, |
$grants[] = array('gid' => 1, 'realm' => 'nodeaccess_rid', |
| 49 |
'grant_update' => 0, 'grant_delete' => 0); |
'grant_view' => 1, 'grant_update' => 0, 'grant_delete' => 0); |
| 50 |
$grants[] = array('gid' => 2, 'realm' => 'nodeaccess_rid', 'grant_view' => 1, |
$grants[] = array('gid' => 2, 'realm' => 'nodeaccess_rid', |
| 51 |
'grant_update' => 0, 'grant_delete' => 0); |
'grant_view' => 1, 'grant_update' => 0, 'grant_delete' => 0); |
| 52 |
$author = array(); |
$author_prefs = array(); |
| 53 |
foreach (node_get_types() as $type => $name) { |
foreach (node_get_types() as $type => $name) { |
| 54 |
variable_set('nodeaccess_' . $type, $grants); |
variable_set('nodeaccess_' . $type, $grants); |
| 55 |
$author[$type] = array('grant_view' => 1, 'grant_update' => 1, 'grant_delete' => 1); |
$author_prefs[$type] = array('grant_view' => 1, 'grant_update' => 1, 'grant_delete' => 1); |
| 56 |
} |
} |
| 57 |
variable_set('nodeaccess_authors', $author); |
variable_set('nodeaccess_authors', $author_prefs); |
| 58 |
|
// Set up all permissions to be editable by default. |
| 59 |
|
$grant_prefs = array('view' => 1, 'edit' => 1, 'delete' => 1); |
| 60 |
|
variable_set('nodeaccess-grants', $grant_prefs); |
| 61 |
} |
} |
| 62 |
|
|
| 63 |
|
/** |
| 64 |
|
* Implementations of hook_update_N(). |
| 65 |
|
*/ |
| 66 |
function nodeaccess_update_1() { |
function nodeaccess_update_1() { |
| 67 |
|
// Create new nodeaccess table. |
| 68 |
switch ($GLOBALS['db_type']) { |
switch ($GLOBALS['db_type']) { |
| 69 |
case 'mysql': |
case 'mysql': |
| 70 |
case 'mysqli': |
case 'mysqli': |
| 71 |
db_query("CREATE TABLE {nodeaccess} ( |
db_query("CREATE TABLE {nodeaccess} ( |
| 72 |
nid int(10) unsigned NOT NULL default '0', |
nid int(10) unsigned NOT NULL default '0', |
| 73 |
gid int(10) unsigned NOT NULL default '0', |
gid int(10) unsigned NOT NULL default '0', |
| 74 |
realm varchar(255) NOT NULL default '', |
realm varchar(255) NOT NULL default '', |
| 75 |
grant_view tinyint(1) unsigned NOT NULL default '0', |
grant_view tinyint(1) unsigned NOT NULL default '0', |
| 76 |
grant_update tinyint(1) unsigned NOT NULL default '0', |
grant_update tinyint(1) unsigned NOT NULL default '0', |
| 77 |
grant_delete tinyint(1) unsigned NOT NULL default '0', |
grant_delete tinyint(1) unsigned NOT NULL default '0', |
| 78 |
PRIMARY KEY (nid,gid,realm) |
PRIMARY KEY (nid,gid,realm) |
| 79 |
) /*!40100 DEFAULT CHARACTER SET utf8 */;"); |
)"); /*!40100 DEFAULT CHARACTER SET utf8 */ |
| 80 |
break; |
break; |
| 81 |
case 'pgsql': |
case 'pgsql': |
| 82 |
db_query("CREATE TABLE {nodeaccess} ( |
db_query("CREATE TABLE {nodeaccess} ( |
| 83 |
nid int_unsigned NOT NULL default '0', |
nid int_unsigned NOT NULL default '0', |
| 91 |
break; |
break; |
| 92 |
} |
} |
| 93 |
|
|
| 94 |
// migrate variables if we're updating |
// Update format of content type specific variables. |
| 95 |
foreach (node_get_types() as $type => $name) { |
foreach (node_get_types() as $type => $name) { |
| 96 |
$perm = variable_get('nodeaccess_' . $type, array()); |
$perm = variable_get('nodeaccess_' . $type, array()); |
| 97 |
if (count($perm) > 0) { |
if (count($perm) > 0) { |
| 98 |
foreach ($perm['rid'] as $role => $grants) { |
foreach ($perm['rid'] as $role => $grants) { |
| 99 |
$new[] = array('realm' => 'nodeaccess_rid', |
$new[] = array('gid' => $grants[0], |
| 100 |
'gid' => $grants[0], |
'realm' => 'nodeaccess_rid', |
| 101 |
'grant_view' => $grants['grant_view'], |
'grant_view' => $grants['grant_view'], |
| 102 |
'grant_update' => $grants['grant_update'], |
'grant_update' => $grants['grant_update'], |
| 103 |
'grant_delete' => $grants['grant_delete'] |
'grant_delete' => $grants['grant_delete'] |
| 104 |
); |
); |
|
// save variable, once this works... |
|
| 105 |
} |
} |
| 106 |
variable_set('nodeaccess_' . $type, $new); |
variable_set('nodeaccess_' . $type, $new); |
| 107 |
} |
} |
| 108 |
} |
} |
| 109 |
|
|
| 110 |
// populate our nodeaccess table with data from node_access |
// Populate the new nodeaccess table with data from node_access. |
| 111 |
$result = db_query("SELECT na.nid, na.gid, na.realm, na.grant_view, na.grant_update, na.grant_delete, n.type FROM {node_access} na LEFT JOIN {node} n on n.nid=na.nid WHERE na.realm = 'nodeaccess_uid' OR na.realm = 'nodeaccess_rid'"); |
$result = db_query("SELECT na.nid, na.gid, na.realm, na.grant_view, na.grant_update, na.grant_delete, n.type FROM {node_access} na LEFT JOIN {node} n ON n.nid = na.nid WHERE na.realm = 'nodeaccess_uid' OR na.realm = 'nodeaccess_rid'"); |
| 112 |
while ($row = db_fetch_object($result)) { |
while ($row = db_fetch_object($result)) { |
| 113 |
$default = variable_get('nodeaccess_' . $row->type, array()); |
$default = variable_get('nodeaccess_' . $row->type, array()); |
| 114 |
if ($default['grant_view'] != $row->grant_view && |
if ($default['grant_view'] != $row->grant_view && |
| 115 |
$default['grant_update'] != $row->grant_update && |
$default['grant_update'] != $row->grant_update && |
| 116 |
$default['grant_delete'] != $row->grant_delete) { |
$default['grant_delete'] != $row->grant_delete) { |
| 117 |
db_query("INSERT into {nodeaccess} (nid, gid, realm, grant_view, grant_update, grant_delete) values (%d, %d, '%s', %d, %d, %d)", |
db_query("INSERT INTO {nodeaccess} (nid, gid, realm, grant_view, grant_update, grant_delete) VALUES (%d, %d, '%s', %d, %d, %d)", |
| 118 |
$row->nid, $row->gid, $row->realm, $row->grant_view, $row->grant_update, $row->grant_delete); |
$row->nid, $row->gid, $row->realm, $row->grant_view, $row->grant_update, $row->grant_delete); |
| 119 |
} |
} |
| 120 |
} |
} |
| 121 |
} |
} |
| 122 |
|
|
| 123 |
function nodeaccess_update_2() { |
function nodeaccess_update_2() { |
| 124 |
// changed menu location, need to clear menu cache |
// Clear menu cache because of changed menu location. |
| 125 |
cache_clear_all(NULL, 'cache_menu'); |
cache_clear_all(NULL, 'cache_menu'); |
| 126 |
} |
} |
| 127 |
|
|
| 128 |
|
function nodeaccess_update_3() { |
| 129 |
|
// Create new nodeaccess_role_alias table. |
| 130 |
|
switch ($GLOBALS['db_type']) { |
| 131 |
|
case 'mysql': |
| 132 |
|
case 'mysqli': |
| 133 |
|
db_query("CREATE TABLE {nodeaccess_role_alias} ( |
| 134 |
|
rid int(10) unsigned NOT NULL default '0', |
| 135 |
|
name varchar(50) NOT NULL default '', |
| 136 |
|
weight int(3) NOT NULL default '0', |
| 137 |
|
PRIMARY KEY (rid) |
| 138 |
|
)"); |
| 139 |
|
break; |
| 140 |
|
case 'pgsql': |
| 141 |
|
db_query("CREATE TABLE {nodeaccess_role_alias} ( |
| 142 |
|
rid int_unsigned NOT NULL default '0', |
| 143 |
|
name varchar(50) NOT NULL default '', |
| 144 |
|
weight smallint NOT NULL default '0', |
| 145 |
|
PRIMARY KEY (rid) |
| 146 |
|
)"); |
| 147 |
|
break; |
| 148 |
|
} |
| 149 |
|
// Set up default alias names to match role names and default |
| 150 |
|
// weights to 0. Do this for allowed roles only. |
| 151 |
|
$allowedrole = variable_get('nodeaccess-roles', array()); |
| 152 |
|
foreach ($allowedrole as $rid => $value) { |
| 153 |
|
if ($value) { |
| 154 |
|
db_query("INSERT INTO {nodeaccess_role_alias} SELECT rid, name, 0 FROM {role} WHERE rid = %d", $rid); |
| 155 |
|
} |
| 156 |
|
} |
| 157 |
|
// Set up all permissions to be editable by default. |
| 158 |
|
$grant_prefs = array('view' => 1, 'edit' => 1, 'delete' => 1); |
| 159 |
|
variable_set('nodeaccess-grants', $grant_prefs); |
| 160 |
|
} |
| 161 |
|
|
| 162 |
|
/** |
| 163 |
|
* Implementation of hook_uninstall(). |
| 164 |
|
*/ |
| 165 |
function nodeaccess_uninstall() { |
function nodeaccess_uninstall() { |
| 166 |
db_query("DELETE FROM {nodeaccess}"); |
// Remove variables. |
|
variable_del('nodeaccess-types'); |
|
|
variable_del('nodeaccess-roles'); |
|
| 167 |
variable_del('nodeaccess-priority'); |
variable_del('nodeaccess-priority'); |
| 168 |
|
variable_del('nodeaccess-preserve'); |
| 169 |
|
variable_del('nodeaccess-grants'); |
| 170 |
|
variable_del('nodeaccess-roles'); |
| 171 |
|
variable_del('nodeaccess-types'); |
| 172 |
variable_del('nodeaccess_authors'); |
variable_del('nodeaccess_authors'); |
| 173 |
foreach (node_get_types() as $type => $name) { |
foreach (node_get_types() as $type => $name) { |
| 174 |
variable_del('nodeaccess_' . $type); |
variable_del('nodeaccess_' . $type); |
| 175 |
} |
} |
| 176 |
db_query('DROP TABLE {nodeaccess}'); |
// Remove tables. |
| 177 |
|
db_query("DROP TABLE {nodeaccess}"); |
| 178 |
|
db_query("DROP TABLE {nodeaccess_role_alias}"); |
| 179 |
} |
} |
| 180 |
|
|
| 181 |
?> |
?> |