Changeset 2438
- Timestamp:
- 09/04/08 03:47:39 (3 months ago)
- Location:
- branches/schema06/system
- Files:
-
- 4 modified
-
classes/acl.php (modified) (11 diffs)
-
schema/mysql/schema.sql (modified) (1 diff)
-
schema/pgsql/schema.sql (modified) (1 diff)
-
schema/sqlite/schema.sql (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
branches/schema06/system/classes/acl.php
r1688 r2438 44 44 } 45 45 Plugins::act('permission_create_before', $name, $description); 46 $result= DB::query('INSERT INTO {permissions} (name, description) VALUES (?, ?)', array( $name, $description) ); 46 $result= DB::query('INSERT INTO {tokens} (name, description) VALUES (?, ?)', array( $name, $description) ); 47 47 48 if ( ! $result ) { 48 49 // if it didn't work, don't bother trying to log it … … 66 67 } 67 68 68 // Use ids internall for permissions69 $permission = ACL::permission_id( $permission );70 71 $allow = true;69 // grab permission ID 70 $permission = ACL::permission_id( $permission ); 71 72 $allow = true; 72 73 // plugins have the opportunity to prevent deletion 73 $allow = Plugins::filter('permission_destroy_allow', $allow, $permission);74 $allow = Plugins::filter('permission_destroy_allow', $allow, $permission); 74 75 if ( ! $allow ) { 75 76 return false; … … 77 78 Plugins::act('permission_destroy_before', $permission ); 78 79 // capture the permission name 79 $name = DB::get_value( 'SELECT name FROM {permissions} WHERE id=?', array( $permission ) );80 $name = DB::get_value( 'SELECT name FROM {tokens} WHERE id=?', array( $permission ) ); 80 81 // remove all references to this permissions 81 $result= DB::query( 'DELETE FROM {groups_permissions} WHERE permission_id=?', array( $permission ) ); 82 $result = DB::query( 'DELETE FROM {group_token_permissions} WHERE permission_id=?', array( $permission ) ); 83 $result = DB::query( 'DELETE FROM {user_token_permissions} WHERE permission_id=?', array( $permission ) ); 82 84 // remove this permission 83 $result = DB::query( 'DELETE FROM {permissions} WHERE permissions_id=?', array( $permission ) );85 $result = DB::query( 'DELETE FROM {tokens} WHERE id=?', array( $permission ) ); 84 86 if ( ! $result ) { 85 87 // if it didn't work, don't bother trying to log it … … 102 104 $order= 'id'; 103 105 } 104 $permissions= DB::get_results( 'SELECT id, name, description FROM { permissions} ORDER BY ' . $order );106 $permissions= DB::get_results( 'SELECT id, name, description FROM {tokens} ORDER BY ' . $order ); 105 107 return $permissions ? $permissions : array(); 106 108 } … … 116 118 return false; 117 119 } else { 118 return DB::get_value( 'SELECT name FROM { permissions} WHERE id=?', array( $id ) );120 return DB::get_value( 'SELECT name FROM {tokens} WHERE id=?', array( $id ) ); 119 121 } 120 122 } … … 131 133 } 132 134 $name= self::normalize_permission( $name ); 133 return DB::get_value( 'SELECT id FROM { permissions} WHERE name=?', array( $name ) );135 return DB::get_value( 'SELECT id FROM {tokens} WHERE name=?', array( $name ) ); 134 136 } 135 137 … … 147 149 $permission= self::normalize_permission( $permission ); 148 150 } 149 return DB::get_value( "SELECT description FROM { permissions} WHERE $query=?", array( $permission ) );151 return DB::get_value( "SELECT description FROM {tokens} WHERE $query=?", array( $permission ) ); 150 152 } 151 153 … … 164 166 $permission= self::normalize_permission( $permission ); 165 167 } 166 return ( DB::get_value( "SELECT COUNT(id) FROM { permissions} WHERE $query=?", array( $permission ) ) > 0 );168 return ( DB::get_value( "SELECT COUNT(id) FROM {tokens} WHERE $query=?", array( $permission ) ) > 0 ); 167 169 } 168 170 … … 193 195 * @param mixed $group A group ID or name 194 196 * @param mixed $permission An action ID or name 197 * @param string $access Check for 'read', 'write', or 'full' access 195 198 * @return bool Whether the group can perform the action 196 199 **/ 197 public static function group_can( $group, $permission )200 public static function group_can( $group, $permission, $access = 'full' ) 198 201 { 199 202 // Use only numeric ids internally 200 $group= UserGroup::id( $group ); 201 $permission= ACL::permission_id( $permission ); 202 $result= DB::get_value( 'SELECT denied FROM {groups_permissions} WHERE permission_id=? AND group_id=?', array( $permission, $group ) ); 203 if ( 0 === intval($result) ) { 203 $group = UserGroup::id( $group ); 204 $permission = ACL::permission_id( $permission ); 205 $sql = <<<SQL 206 SELECT p.name FROM {group_token_permissions} gp, {permissions} p WHERE 207 gp.group_id=? AND gp.token_id=? AND gp.permission_id=p.id; 208 SQL; 209 $result = DB::get_values( $sql ); 210 if ( $result == $access ) { 204 211 // the permission has been granted to this group 205 212 return true; … … 214 221 * @param mixed $user A user object, user ID or a username 215 222 * @param mixed $permission A permission ID or name 223 * @param string $access Check for 'read', 'write', or 'full' access 216 224 * @return bool Whether the user can perform the action 217 225 **/ 218 public static function user_can( $user, $permission )226 public static function user_can( $user, $permission, $access = 'full' ) 219 227 { 220 228 // Use only numeric ids internally … … 232 240 } 233 241 234 // we select the "denied" value from all the permissions 235 // assigned to all the groups to which this user is a member. 236 // array_unique() should consolidate this down to, at most, 237 // two values: 0 and 1. 238 $permissions= DB::get_column('SELECT gp.denied from {groups_permissions} gp, {users_groups} g where gp.group_id = g.group_id and g.user_id=? and permission_id=?', array( $user_id, $permission ) ); 239 240 // if any group is explicitly denied access to this permission, 241 // this user is denied access to that permission 242 if ( in_array( 1, $permissions ) ) { 243 return false; 244 } 245 // if the permission is not explicitly denied, make sure it's 246 // explicitly granted. If it is, the user can do this. 247 if ( in_array( 0, $permissions, true ) ) { 242 /** 243 * Jay Pipe's explanation of the following SQL 244 * 1) Look into user_permissions for the user and the token. 245 * If exists, use that permission flag for the check. If not, 246 * go to 2) 247 * 248 * 2) Look into the group_permissions joined to 249 * users_groups for the user and the token. Order the results 250 * by the permission_id flag. The lower the flag value, the 251 * fewest permissions that group has. Use the first record's 252 * permission flag to check the ACL. 253 * 254 * This gives the system very fine grained control and grabbing 255 * the permission flag and can be accomplished in a single SQL 256 * call. 257 */ 258 $sql = <<<SQL 259 SELECT COALESCE(permission_id, 0) as permission_id 260 FROM ( 261 ( 262 SELECT permission_id 263 FROM {user_token_permissions} 264 WHERE user_id = :user_id 265 AND token_id = :token_id 266 ) AS up 267 UNION ALL 268 ( 269 SELECT gp.permission_id 270 FROM {users_groups} ug 271 INNER JOIN {group_token_permissions} gp 272 ON ug.group_id = gp.group_id 273 AND ug.user_id = :user_id 274 AND gp.token_id = :token_id 275 ORDER BY permission_id ASC 276 LIMIT 1 277 ) 278 ) 279 LIMIT 1; 280 SQL; 281 $result = DB::get_value( $sql, array( ':user_id' => $user_id, ':token_id' => $permission ); 282 283 // TODO: modify above call to return the permission name rather than the ID 284 // For now, I'll just look for a result > 0 285 if ( $result !== FALSE && intval($result) > 0 ) { 248 286 return true; 249 287 } 288 250 289 // if the permission is neither denied nor granted, they're not 251 290 // allowed to do it. -
branches/schema06/system/schema/mysql/schema.sql
r2409 r2438 242 242 CREATE TABLE {$prefix}permissions ( 243 243 id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT, 244 descriptionVARCHAR(255) NOT NULL,245 PRIMARY KEY (id) 246 ); 247 248 INSERT INTO {$prefix}permissions ( description) VALUES244 name VARCHAR(255) NOT NULL, 245 PRIMARY KEY (id) 246 ); 247 248 INSERT INTO {$prefix}permissions (name) VALUES 249 249 ('denied'), 250 250 ('read'), -
branches/schema06/system/schema/pgsql/schema.sql
r2409 r2438 279 279 CREATE TABLE {$prefix}permissions ( 280 280 id INTEGER NOT NULL DEFAULT nextval('{$prefix}permissions_pkey_seq'), 281 descriptionVARCHAR(255) NOT NULL,281 name VARCHAR(255) NOT NULL, 282 282 PRIMARY KEY (id) 283 283 ); 284 284 ALTER SEQUENCE {$prefix}permissions_pkey_seq OWNED BY {$prefix}permissions.id; 285 285 286 INSERT INTO {$prefix}permissions ( description) VALUES286 INSERT INTO {$prefix}permissions (name) VALUES 287 287 ('denied'), 288 288 ('read'), 289 289 ('write'), 290 290 ('full'); 291 -
branches/schema06/system/schema/sqlite/schema.sql
r2410 r2438 228 228 CREATE TABLE {$prefix}permissions ( 229 229 id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 230 descriptionVARCHAR(255) NOT NULL231 ); 232 233 INSERT INTO {$prefix}permissions ( description) VALUES230 name VARCHAR(255) NOT NULL 231 ); 232 233 INSERT INTO {$prefix}permissions (name) VALUES 234 234 ('denied'); 235 INSERT INTO {$prefix}permissions ( description) VALUES235 INSERT INTO {$prefix}permissions (name) VALUES 236 236 ('read'); 237 INSERT INTO {$prefix}permissions ( description) VALUES237 INSERT INTO {$prefix}permissions (name) VALUES 238 238 ('write'); 239 INSERT INTO {$prefix}permissions ( description) VALUES239 INSERT INTO {$prefix}permissions (name) VALUES 240 240 ('full');
