| 1 | # | ||
| 2 | # UserDB.pm - User and Group Management Interface. | ||
| 3 | # | ||
| 4 | # This file is part of DBIx::SearchProfiles. | ||
| 5 | # | ||
| 6 | # Author: Francis J. Lacoste <francis.lacoste@iNsu.COM> | ||
| 7 | # | ||
| 8 | # Copyright (C) 1999 Francis J. Lacoste, iNsu Innovations | ||
| 9 | # | ||
| 10 | # This program is free software; you can redistribute it and/or modify | ||
| 11 | # it under the terms same terms as perl itself. | ||
| 12 | # | ||
| 13 | package DBIx::UserDB; | ||
| 14 | |||
| 15 | use strict; | ||
| 16 | |||
| 17 | use vars qw( $VERSION ); | ||
| 18 | |||
| 19 | BEGIN { | ||
| 20 | ($VERSION) = '$Revision: 1.6 $' =~ /Revision: ([\d.]+)/; | ||
| 21 | } | ||
| 22 | |||
| 23 | use DBIx::SearchProfiles; | ||
| 24 | |||
| 25 | =pod | ||
| 26 | |||
| 27 | =head1 NAME | ||
| 28 | |||
| 29 | DBIx::UserDB - Module to manage a user database using DBIx::SearchProfiles | ||
| 30 | |||
| 31 | =head1 SYNOPSIS | ||
| 32 | |||
| 33 | use DBIx::UserDB; | ||
| 34 | use DBIx::SearchProfiles; | ||
| 35 | |||
| 36 | my $db = new DBIx::SearchProfiles( ... ); | ||
| 37 | my $userdb = new DBIx::UserDB( $db ); | ||
| 38 | |||
| 39 | my $user = { username => $username, password => $password }; | ||
| 40 | $user = $userdb->user_create( $user ); | ||
| 41 | |||
| 42 | # Later on | ||
| 43 | my $user = $userdb->login( $user, $password ); | ||
| 44 | die "Login failed" unless $user; | ||
| 45 | |||
| 46 | # Much later | ||
| 47 | if ( $userdb->allowed( $user, $target, "DELETE" ) ) { | ||
| 48 | ... | ||
| 49 | } | ||
| 50 | |||
| 51 | =head1 DESCRIPTION | ||
| 52 | |||
| 53 | The DBIx::UserDB uses DBIx::SearchProfiles to manage a user and group | ||
| 54 | database and may be also used to manage complex ACL. The user and | ||
| 55 | group schema may be modified for application specific data since only | ||
| 56 | a few fields are required by the UserDB. This is possible thanks to | ||
| 57 | DBIx::SearchProfiles. | ||
| 58 | |||
| 59 | =head1 CONCEPTS | ||
| 60 | |||
| 61 | =head2 Users and Groups | ||
| 62 | |||
| 63 | Users are represented as hash and as one SQL table. They have a unique | ||
| 64 | username and a unique uid. Group have also a unique name and a unique | ||
| 65 | gid. A user may be a members of many groups. | ||
| 66 | |||
| 67 | =head2 ACLs | ||
| 68 | |||
| 69 | UserDB can also be used to manage complex ACL (Acccess Control Lists). | ||
| 70 | Access to resources is determined by the tuple (user,target,privilege) | ||
| 71 | which determines if a I<user> has the required I<privilege> on | ||
| 72 | I<target>. I<Privilege> and I<target> are treated as application | ||
| 73 | specific character strings. | ||
| 74 | |||
| 75 | =head1 CONFIGURATION | ||
| 76 | |||
| 77 | In order to use DBIx::UserDB you will need to create a few tables in | ||
| 78 | your DMBS and to create the approriate DBIx::SearchProfiles. | ||
| 79 | |||
| 80 | Here is the minimal schema required in your DBMS : | ||
| 81 | |||
| 82 | CREATE TABLE userdb ( | ||
| 83 | uid SERIAL PRIMARY KEY, | ||
| 84 | username CHAR(32) UNIQUE, | ||
| 85 | password CHAR(32) | ||
| 86 | ); | ||
| 87 | |||
| 88 | CREATE TABLE groupdb ( | ||
| 89 | gid SERIAL PRIMARY KEY, | ||
| 90 | groupname CHAR(32) UNIQUE | ||
| 91 | ); | ||
| 92 | |||
| 93 | CREATE TABLE groupmembers ( | ||
| 94 | gid INT REFERENCES groupdb, | ||
| 95 | uid INT REFERENCES userdb, | ||
| 96 | PRIMARY KEY (gid,uid) | ||
| 97 | ); | ||
| 98 | |||
| 99 | CREATE TABLE user_acl ( | ||
| 100 | uid INT REFERENCES userdb, | ||
| 101 | target CHAR(128), | ||
| 102 | privilege CHAR(32), | ||
| 103 | negated BOOL DEFAULT 0, | ||
| 104 | PRIMARY KEY (uid,target,privilege) | ||
| 105 | ); | ||
| 106 | |||
| 107 | CREATE TABLE group_acl ( | ||
| 108 | gid INT REFERENCES groupdb, | ||
| 109 | target CHAR(128), | ||
| 110 | privilege CHAR(32), | ||
| 111 | negated BOOL DEFAULT 0, | ||
| 112 | PRIMARY KEY (gid,target,privilege) | ||
| 113 | ); | ||
| 114 | |||
| 115 | CREATE TABLE default_acl ( | ||
| 116 | target CHAR(128), | ||
| 117 | privilege CHAR(32), | ||
| 118 | negated BOOL DEFAULT 0, | ||
| 119 | PRIMARY KEY (target,privilege) | ||
| 120 | ); | ||
| 121 | |||
| 122 | This SQL was tested with PostgreSQL, modify according to your RDBMS. | ||
| 123 | And here is its related DBIx::SearchProfiles profile : | ||
| 124 | |||
| 125 | { | ||
| 126 | userdb => | ||
| 127 | { | ||
| 128 | fields => [qw( username password ) ], | ||
| 129 | keys => [qw( uid )], | ||
| 130 | table => "userdb", | ||
| 131 | }, | ||
| 132 | groupdb => | ||
| 133 | { | ||
| 134 | query => q{ SELECT m.gid,uid,groupname FROM groupdb, groupmembers m | ||
| 135 | WHERE uid = ? }, | ||
| 136 | params => [ qw( uid ) ], | ||
| 137 | fields => [ qw( groupname ) ], | ||
| 138 | keys => [ qw( gid )], | ||
| 139 | table => "groupdb", | ||
| 140 | } , | ||
| 141 | } | ||
| 142 | |||
| 143 | You may add any fields to the groupdb and userdb tables as long as you | ||
| 144 | add them to the profiles. The I<userdb> profile should be a C<record> | ||
| 145 | profile (see DBIx::SearchProfiles(3)) and I<groupdb> should contains | ||
| 146 | both template profile's information (for finding the users associated | ||
| 147 | with a group) and record profile's information (for inserting and | ||
| 148 | updating group's information). Additionaly you may change the fields | ||
| 149 | length of all required fields. | ||
| 150 | |||
| 151 | Passwords are uuencoded for storage (for minimal privacy not for | ||
| 152 | security), so take this into account when setting the password field's | ||
| 153 | length. If you want to store password in plaintext, use the | ||
| 154 | C<scramble_password> method. | ||
| 155 | |||
| 156 | =head1 INITIALIZATION | ||
| 157 | |||
| 158 | Initializing the DBIx::UserDB is as simple as | ||
| 159 | |||
| 160 | my $userdb = new DBIx::UserDB( $DB, "userdb", "groupdb" ); | ||
| 161 | |||
| 162 | The first parameter is a DBIx::SearchProfiles object which will be | ||
| 163 | used to access the database. The second parameter is the name of the | ||
| 164 | profile that should be used to access the users' information (defaults | ||
| 165 | to "userdb"). The third parameter is the name of the profile to use | ||
| 166 | for group access (defaults to "groupdb"). | ||
| 167 | |||
| 168 | =cut | ||
| 169 | |||
| 170 | sub new { | ||
| 171 | my $proto = shift; | ||
| 172 | my $class = ref $proto || $proto; | ||
| 173 | |||
| 174 | my $self = bless {}, $class; | ||
| 175 | |||
| 176 | my $DB = shift | ||
| 177 | or die "Missing Database argument\n"; | ||
| 178 | |||
| 179 | my $user_profile = shift || "userdb"; | ||
| 180 | my $group_profile = shift || "groupdb"; | ||
| 181 | |||
| 182 | die "No user profile named $user_profile\n" | ||
| 183 | unless $DB->has_profile( $user_profile ); | ||
| 184 | die "No group profile named $group_profile\n" | ||
| 185 | unless $DB->has_profile( $group_profile ); | ||
| 186 | |||
| 187 | $self->{DB} = $DB; | ||
| 188 | $self->{user_profile} = $user_profile; | ||
| 189 | $self->{group_profile} = $group_profile; | ||
| 190 | $self->{scramble} = 1; | ||
| 191 | |||
| 192 | $self; | ||
| 193 | } | ||
| 194 | |||
| 195 | =pod | ||
| 196 | |||
| 197 | =head2 scramble_password ( [new_setting] ) | ||
| 198 | |||
| 199 | Return the scramble password setting. You may also change the setting | ||
| 200 | by giving the method a new value. If scramble password is true, user's | ||
| 201 | password will be uuencoded before being stored in the database. | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | sub scramble_password($;$) { | ||
| 206 | $_[0]->{scramble} = $_[1] if @_ == 2; | ||
| 207 | |||
| 208 | $_[0]->{scramble}; | ||
| 209 | } | ||
| 210 | |||
| 211 | =pod | ||
| 212 | |||
| 213 | =head1 USER METHODS | ||
| 214 | |||
| 215 | Here are the methods for managing users in the database. | ||
| 216 | |||
| 217 | =head2 user_create ( \%user ) | ||
| 218 | |||
| 219 | This method creates a user with the information specified in the hash | ||
| 220 | reference in the database. In the user's hash, at least the fields | ||
| 221 | I<username> and I<password> should be set. | ||
| 222 | |||
| 223 | The methods return true on success and false if there is already a | ||
| 224 | username with that name in the database. Exception are thrown on | ||
| 225 | database errors. Additionally, on return, the method will add the UID | ||
| 226 | of the newly created user. | ||
| 227 | |||
| 228 | =cut | ||
| 229 | |||
| 230 | sub user_create { | ||
| 231 | my ( $self, $user ) = @_; | ||
| 232 | |||
| 233 | # Check for a user with the same username | ||
| 234 | my $old_user = $self->{DB}->record_search( $self->{user_profile}, | ||
| 235 | {username => $user->{username}} | ||
| 236 | ); | ||
| 237 | return undef if @$old_user; | ||
| 238 | |||
| 239 | # Scramble the password for persistence | ||
| 240 | $user->{password} = pack "u*", $user->{password} | ||
| 241 | if ( $self->{scramble} ); | ||
| 242 | $self->{DB}->record_insert( $self->{user_profile}, $user ); | ||
| 243 | |||
| 244 | # Load the user back | ||
| 245 | my $new_user = $self->{DB}->record_search( $self->{user_profile}, | ||
| 246 | {username => $user->{username}} | ||
| 247 | ); | ||
| 248 | die "Can't find new user\n" unless @$new_user == 1; | ||
| 249 | |||
| 250 | # Copy the fields of the new user back in this one | ||
| 251 | while ( my ($name,$value) = each %{$new_user->[0]} ) { | ||
| 252 | $user->{$name} = $value; | ||
| 253 | } | ||
| 254 | |||
| 255 | # Unscramble the password | ||
| 256 | $user->{password} = unpack "u*", $user->{password} | ||
| 257 | if $self->{scramble}; | ||
| 258 | |||
| 259 | return $user; | ||
| 260 | } | ||
| 261 | |||
| 262 | sub user_load { | ||
| 263 | my ($self,$user) = @_; | ||
| 264 | |||
| 265 | # Unscramble the password | ||
| 266 | $user->{password} = unpack "u*", $user->{password} | ||
| 267 | if $self->{scramble}; | ||
| 268 | $user->{groups} = | ||
| 269 | $self->{DB}->template_search( $self->{group_profile}, | ||
| 270 | { uid => $user->{uid} } ); | ||
| 271 | return $user; | ||
| 272 | } | ||
| 273 | |||
| 274 | =pod | ||
| 275 | |||
| 276 | =head2 user_search ( \%params ) | ||
| 277 | |||
| 278 | This method will return users matching the DBIx::SearchProfiles query | ||
| 279 | specification in a reference to an array. | ||
| 280 | |||
| 281 | =cut | ||
| 282 | |||
| 283 | sub user_search { | ||
| 284 | my $self = shift; | ||
| 285 | |||
| 286 | my $users = $self->{DB}->record_search( $self->{user_profile}, @_ ); | ||
| 287 | for my $user ( @$users ) { | ||
| 288 | $self->user_load( $user ); | ||
| 289 | } | ||
| 290 | return $users; | ||
| 291 | } | ||
| 292 | |||
| 293 | =pod | ||
| 294 | |||
| 295 | =head2 user_get ( $uid_or_name ) | ||
| 296 | |||
| 297 | This method takes a UID or username and return the corresponding user (as an hash reference) or undef if there is no such user. | ||
| 298 | |||
| 299 | The key I<groups> in the user's hash contains the names of the groups | ||
| 300 | of which this user is a member. | ||
| 301 | |||
| 302 | =cut | ||
| 303 | |||
| 304 | sub user_get { | ||
| 305 | my ( $self, $uidorname ) = @_; | ||
| 306 | |||
| 307 | my $user; | ||
| 308 | if ( $uidorname =~ /\d+/ ) { | ||
| 309 | $user = $self->{DB}->record_get( $self->{user_profile}, $uidorname ); | ||
| 310 | return undef unless $user; | ||
| 311 | } else { | ||
| 312 | my $users = $self->{DB}->record_search( $self->{user_profile}, | ||
| 313 | { username => $uidorname } ); | ||
| 314 | return undef unless @$users; | ||
| 315 | $user = $users->[0]; | ||
| 316 | } | ||
| 317 | $self->user_load( $user ); | ||
| 318 | } | ||
| 319 | |||
| 320 | =pod | ||
| 321 | |||
| 322 | =head2 user_login ( $username, $password ) | ||
| 323 | |||
| 324 | This method will return the user which have the corresponding username | ||
| 325 | and password or undef if the username or password is invalid. | ||
| 326 | |||
| 327 | =cut | ||
| 328 | |||
| 329 | sub user_login { | ||
| 330 | my ( $self, $username, $password) = @_; | ||
| 331 | |||
| 332 | my $user = $self->{DB}->record_search( $self->{user_profile}, | ||
| 333 | {username => $username, | ||
| 334 | password => ($self->{scramble} ? | ||
| 335 | pack( "u*", $password ) : | ||
| 336 | $password) | ||
| 337 | } | ||
| 338 | ); | ||
| 339 | return undef unless @$user == 1; | ||
| 340 | $user = $user->[0]; | ||
| 341 | |||
| 342 | $self->user_load($user); | ||
| 343 | } | ||
| 344 | |||
| 345 | =pod | ||
| 346 | |||
| 347 | =head2 user_delete ( \%user ) | ||
| 348 | |||
| 349 | This method removes the given user from the database. | ||
| 350 | |||
| 351 | =cut | ||
| 352 | |||
| 353 | sub user_delete { | ||
| 354 | my ( $self, $user ) = @_; | ||
| 355 | |||
| 356 | die "Bad user: no uid\n" unless defined $user->{uid}; | ||
| 357 | my $DB = $self->{DB}; | ||
| 358 | $DB->record_delete( $self->{user_profile}, $user ); | ||
| 359 | $DB->sql_delete( "DELETE FROM groupmembers WHERE uid = ?", $user->{uid} ); | ||
| 360 | $DB->sql_delete( "DELETE FROM user_acl WHERE uid = ?", $user->{uid} ); | ||
| 361 | } | ||
| 362 | |||
| 363 | =pod | ||
| 364 | |||
| 365 | =head2 user_update ( \%user ) | ||
| 366 | |||
| 367 | This method updates database information of the given user. This | ||
| 368 | method has no effects on the group information. Use the | ||
| 369 | C<group_add_user> and C<group_remove_user> methods for modifying the | ||
| 370 | groups associated with a user. | ||
| 371 | |||
| 372 | =cut | ||
| 373 | |||
| 374 | sub user_update { | ||
| 375 | my ( $self, $user ) = @_; | ||
| 376 | |||
| 377 | die "Bad user: no uid\n" unless defined $user->{uid}; | ||
| 378 | # Scramble password | ||
| 379 | $user->{password} = pack "u*", $user->{password} | ||
| 380 | if $self->{scramble}; | ||
| 381 | $self->{DB}->record_update( $self->{user_profile}, $user ); | ||
| 382 | # Unscramble | ||
| 383 | $user->{password} = unpack "u*", $user->{password} | ||
| 384 | if $self->{scramble}; | ||
| 385 | } | ||
| 386 | |||
| 387 | =pod | ||
| 388 | |||
| 389 | =head1 GROUP METHODS | ||
| 390 | |||
| 391 | Here are the methods to manage group information | ||
| 392 | |||
| 393 | =head2 group_create ( \%group ) | ||
| 394 | |||
| 395 | This method creates a new group in the database. At least the | ||
| 396 | I<groupname> key should be set in the hash. | ||
| 397 | |||
| 398 | This methods returns false if there is already a group with the same | ||
| 399 | groupname. It returns true if the creation succeeded. Additionnaly, on | ||
| 400 | return, the key I<gid> will be set in the original group's hash. | ||
| 401 | |||
| 402 | =cut | ||
| 403 | |||
| 404 | sub group_create { | ||
| 405 | my ( $self, $group ) = @_; | ||
| 406 | |||
| 407 | my $DB = $self->{DB}; | ||
| 408 | |||
| 409 | # Check for group with same name | ||
| 410 | my $old_group = $DB->record_search( $self->{group_profile}, | ||
| 411 | { groupname => $group->{groupname} } | ||
| 412 | ); | ||
| 413 | return undef if @$old_group; | ||
| 414 | |||
| 415 | $DB->record_insert( $self->{group_profile}, $group ); | ||
| 416 | my $new_group = $DB->record_search( $self->{group_profile}, | ||
| 417 | { groupname => $group->{groupname} } | ||
| 418 | ); | ||
| 419 | die "Failed to find newly created group\n" unless @$new_group == 1; | ||
| 420 | |||
| 421 | # Copy the fields of the new user back in this one | ||
| 422 | while ( my ($name,$value) = each %{$new_group->[0]} ) { | ||
| 423 | $group->{$name} = $value; | ||
| 424 | } | ||
| 425 | |||
| 426 | return $group; | ||
| 427 | } | ||
| 428 | |||
| 429 | sub load_group { | ||
| 430 | my ( $self, $group ) = @_; | ||
| 431 | |||
| 432 | $group->{members} = | ||
| 433 | $self->{DB}->sql_search( q{ SELECT uid FROM groupmembers WHERE gid = ? }, | ||
| 434 | $group->{gid} ); | ||
| 435 | |||
| 436 | return $group; | ||
| 437 | } | ||
| 438 | |||
| 439 | =pod | ||
| 440 | |||
| 441 | =head2 group_search ( \%params ) | ||
| 442 | |||
| 443 | This method will search the database for groups matching the | ||
| 444 | DBIx::SearchProfiles record search and will return its results as a | ||
| 445 | reference to an hash. | ||
| 446 | |||
| 447 | =cut | ||
| 448 | |||
| 449 | sub group_search { | ||
| 450 | my $self = shift; | ||
| 451 | |||
| 452 | my $groups = $self->{DB}->record_search( $self->{group_profile}, @_ ); | ||
| 453 | for my $group( @$groups) { | ||
| 454 | $self->load_group( $group ); | ||
| 455 | } | ||
| 456 | return $groups; | ||
| 457 | } | ||
| 458 | |||
| 459 | =pod | ||
| 460 | |||
| 461 | =head2 group_get ( $gid_or_name ) | ||
| 462 | |||
| 463 | This method takes a gid or groupname and will fetch the corresponding | ||
| 464 | group. It returns the corresponding group or undef if there is no such | ||
| 465 | group. Additionnaly there is a key I<members> defined in the resulting | ||
| 466 | hash which contains in an array the name of all members of the group. | ||
| 467 | |||
| 468 | =cut | ||
| 469 | |||
| 470 | sub group_get { | ||
| 471 | my ( $self, $gidorname ) = @_; | ||
| 472 | |||
| 473 | my $group; | ||
| 474 | if ( $gidorname =~ /\d+/ ) { | ||
| 475 | $group = $self->{DB}->record_get( $self->{group_profile}, $gidorname ); | ||
| 476 | return undef unless $group; | ||
| 477 | } else { | ||
| 478 | my $groups = $self->{DB}->record_search( $self->{group_profile}, | ||
| 479 | { groupname => $gidorname } ); | ||
| 480 | return undef unless @$groups; | ||
| 481 | |||
| 482 | $group = $groups->[0]; | ||
| 483 | } | ||
| 484 | |||
| 485 | $self->load_group( $group ); | ||
| 486 | } | ||
| 487 | |||
| 488 | =pod | ||
| 489 | |||
| 490 | =head2 group_delete ( \%group ) | ||
| 491 | |||
| 492 | This methods removes the given group from the database. | ||
| 493 | |||
| 494 | =cut | ||
| 495 | |||
| 496 | sub group_delete { | ||
| 497 | my ( $self, $group ) = @_; | ||
| 498 | |||
| 499 | my $DB = $self->{DB}; | ||
| 500 | $DB->sql_delete( q{ DELETE FROM groupmembers WHERE gid = ? }, | ||
| 501 | $group->{gid} ); | ||
| 502 | $DB->record_delete( $self->{group_profile}, $group->{gid} ); | ||
| 503 | } | ||
| 504 | |||
| 505 | =pod | ||
| 506 | |||
| 507 | =head2 group_update ( \%group ) | ||
| 508 | |||
| 509 | This methods updates the information associated with the given group | ||
| 510 | in that database. This methods doesn't modify the list of members of | ||
| 511 | this group. User C<group_add_user> and C<group_remove_user> for that. | ||
| 512 | |||
| 513 | =cut | ||
| 514 | |||
| 515 | sub group_update { | ||
| 516 | my ( $self, $group ) = @_; | ||
| 517 | |||
| 518 | my $DB = $self->{DB}; | ||
| 519 | $DB->record_update( $self->{group_profile}, $group ); | ||
| 520 | |||
| 521 | } | ||
| 522 | |||
| 523 | =pod | ||
| 524 | |||
| 525 | =head2 group_add_user ( \%group, \%user ) | ||
| 526 | |||
| 527 | Adds the user to that group. | ||
| 528 | |||
| 529 | =cut | ||
| 530 | |||
| 531 | sub group_add_user { | ||
| 532 | my ( $self, $group, $user ) = @_; | ||
| 533 | |||
| 534 | my $DB = $self->{DB}; | ||
| 535 | $DB->sql_insert( q{ INSERT INTO groupmembers (gid,uid) | ||
| 536 | VALUES (?,?) }, | ||
| 537 | $group->{gid}, $user->{uid} ); | ||
| 538 | push @{$group->{members}}, $user->{uid}; | ||
| 539 | } | ||
| 540 | |||
| 541 | =pod | ||
| 542 | |||
| 543 | =head2 group_remove_user ( \%group, \%user ) | ||
| 544 | |||
| 545 | Removes the user from that group. | ||
| 546 | |||
| 547 | =cut | ||
| 548 | |||
| 549 | sub group_remove_user { | ||
| 550 | my ( $self, $group, $user ) = @_; | ||
| 551 | |||
| 552 | my $DB = $self->{DB}; | ||
| 553 | $DB->sql_insert( q{ DELETE FROM groupmembers WHERE gid = ? AND uid = ?) }, | ||
| 554 | $group->{gid}, $user->{uid} ); | ||
| 555 | |||
| 556 | $group->{members} = [ grep { $_ != $user->{uid} } @{$group->{members} } ]; | ||
| 557 | |||
| 558 | } | ||
| 559 | |||
| 560 | =pod | ||
| 561 | |||
| 562 | =head1 ACL METHODS | ||
| 563 | |||
| 564 | Here are the methods to access the ACL information : | ||
| 565 | |||
| 566 | =head2 grant ( \%user_or_group, $target, $privilege ) | ||
| 567 | |||
| 568 | Grant the specified I<privilege> on I<target> to that group or user. | ||
| 569 | If you want to set the default policy regarding that target and privilege, | ||
| 570 | use undef as the user parameter. | ||
| 571 | |||
| 572 | =cut | ||
| 573 | |||
| 574 | sub grant { | ||
| 575 | $_[0]->update_acl( @_, 1 ); | ||
| 576 | } | ||
| 577 | |||
| 578 | =pod | ||
| 579 | |||
| 580 | =head2 deny ( \%user_or_group, $target, $privilege ) | ||
| 581 | |||
| 582 | Deny the specific I<privilege> on I<target> to that group or user. Use undef | ||
| 583 | if you want the default policy to be deny. | ||
| 584 | |||
| 585 | =cut | ||
| 586 | |||
| 587 | sub deny { | ||
| 588 | $_[0]->update_acl( @_, 0 ); | ||
| 589 | } | ||
| 590 | |||
| 591 | sub update_acl { | ||
| 592 | my ( $self, $whom, $target, $priv, $negated ) = @_; | ||
| 593 | |||
| 594 | my $DB = $self->{DB}; | ||
| 595 | |||
| 596 | # Try to update privilege first in case it was set and not revoked | ||
| 597 | my $rv; | ||
| 598 | if ( not ref $whom) { | ||
| 599 | $rv = $DB->sql_update( q{ UPDATE default_acl SET negated = ? | ||
| 600 | WHERE target = ? AND privilege = ? }, | ||
| 601 | $negated, $target, $priv ); | ||
| 602 | } elsif ( exists $whom->{uid} ) { | ||
| 603 | $rv = $DB->sql_update( q{ UPDATE user_acl SET negated = ? | ||
| 604 | WHERE uid = ? AND target = ? | ||
| 605 | AND privilege = ? }, | ||
| 606 | $negated, $whom->{uid}, $target, $priv ); | ||
| 607 | } else { | ||
| 608 | $rv = $DB->sql_updated( q{ UPDATED group_acl SET negated = ? | ||
| 609 | WHERE gid = ? AND target = ? | ||
| 610 | AND privilege = ? }, | ||
| 611 | $negated, $whom->{gid}, $target, $priv ); | ||
| 612 | } | ||
| 613 | unless ( $rv ) { | ||
| 614 | if ( not ref $whom) { | ||
| 615 | $DB->sql_insert( q{ INSERT INTO default_acl | ||
| 616 | (target,privilege,negated) | ||
| 617 | VALUES (?,?,?) }, | ||
| 618 | $target, $priv, $negated ); | ||
| 619 | } elsif ( exists $whom->{uid} ) { | ||
| 620 | $DB->sql_insert( q{ INSERT INTO user_acl | ||
| 621 | (uid,target,privilege,negated) | ||
| 622 | VALUES (?,?,?,?) }, | ||
| 623 | $whom->{uid}, $target, $priv, $negated ); | ||
| 624 | } else { | ||
| 625 | $DB->sql_insert( q{ INSERT INTO group_acl | ||
| 626 | (gid,target,privilege,negated) | ||
| 627 | VALUES (?,?,?,?) }, | ||
| 628 | $whom->{gid}, $target, $priv, $negated ); | ||
| 629 | } | ||
| 630 | } | ||
| 631 | } | ||
| 632 | |||
| 633 | =pod | ||
| 634 | |||
| 635 | =head2 revoke ( \%user_or_group, $target, $privilege ) | ||
| 636 | |||
| 637 | Removes the specified I<privilege> on I<target> associated with user | ||
| 638 | or group. If you want to remove the default policy, use undef as the | ||
| 639 | user parameter. | ||
| 640 | |||
| 641 | NOTE: Revoking is not the same as denying. Revoking removes the entry | ||
| 642 | from the ACL which means that the resulting policy will be determined | ||
| 643 | by other entry in the ACL (i.e: group or default). When using deny, | ||
| 644 | you are explicitely determining the level of access. | ||
| 645 | |||
| 646 | =cut | ||
| 647 | |||
| 648 | sub revoke { | ||
| 649 | my ( $self, $whom, $target, $priv ) = @_; | ||
| 650 | |||
| 651 | my $DB = $self->{DB}; | ||
| 652 | if ( not ref $whom) { | ||
| 653 | $DB->sql_delete( q{ DELETE FROM default_acl | ||
| 654 | WHERE target = ? AND privilege = ? }, | ||
| 655 | $target, $priv ); | ||
| 656 | } elsif ( exists $whom->{uid} ) { | ||
| 657 | $DB->sql_delete( q{ DELETE FROM user_acl | ||
| 658 | WHERE uid = ? AND target = ? AND privilege = ? }, | ||
| 659 | $whom->{uid}, $target, $priv ); | ||
| 660 | } else { | ||
| 661 | $DB->sql_delete( q{ DELETE FROM group_acl | ||
| 662 | WHERE gid = ? AND target = ? AND privilege = ? }, | ||
| 663 | $whom->{gid}, $target, $priv ); | ||
| 664 | } | ||
| 665 | } | ||
| 666 | |||
| 667 | =pod | ||
| 668 | |||
| 669 | =head2 allowed ( \%user, $target, $privilege ) | ||
| 670 | |||
| 671 | Determine if I<user> has I<prilivege> on I<target>. This how the access is determined : | ||
| 672 | |||
| 673 | =over | ||
| 674 | |||
| 675 | =item 1 | ||
| 676 | |||
| 677 | Determine if there is an entry (user,target,privilege). If an entry is | ||
| 678 | found, true or false will be returned depending whether that privilege | ||
| 679 | was granted or denied. | ||
| 680 | |||
| 681 | =item 2 | ||
| 682 | |||
| 683 | Check for an entry (group,target,privilege) for each group of which | ||
| 684 | the user is a member. For the group policy to apply, all group must | ||
| 685 | share the same result. | ||
| 686 | |||
| 687 | For example, if user A is member of group A and B and group A is | ||
| 688 | granted the requested privilege and group B is denied, the group | ||
| 689 | policy doesn't apply to that particular user. Schematically : | ||
| 690 | |||
| 691 | Group A Granted + Group B Granted = User Granted | ||
| 692 | Group A Granted + Group B Denied = Default policy will apply | ||
| 693 | Group A Denied + Group B Denied = User Denied | ||
| 694 | |||
| 695 | =item 3 | ||
| 696 | |||
| 697 | A entry (target,privilege) will be lookup in the default policy. If | ||
| 698 | one is found, that policy will apply. | ||
| 699 | |||
| 700 | =item 4 | ||
| 701 | |||
| 702 | Access is denied. | ||
| 703 | |||
| 704 | =back | ||
| 705 | |||
| 706 | =cut | ||
| 707 | |||
| 708 | sub allowed { | ||
| 709 | my ( $self, $user, $target, $priv ) = @_; | ||
| 710 | |||
| 711 | my $DB = $self->{DB}; | ||
| 712 | |||
| 713 | # Try to see if there is a policy for this particular | ||
| 714 | # user | ||
| 715 | my $user_policy = | ||
| 716 | $DB->sql_get( q{ SELECT negated FROM user_acl | ||
| 717 | WHERE uid = ? AND target = ? AND privilege = ? }, | ||
| 718 | $user->{uid}, $target, $priv | ||
| 719 | ); | ||
| 720 | return not $user_policy->{negated} if $user_policy; | ||
| 721 | |||
| 722 | # Now check the group in which this user is. | ||
| 723 | # All the group policy must match for this to be returned as | ||
| 724 | # a result. If there is a conflict, we use the default policy. | ||
| 725 | my $groups = join ",", map { $_->{gid} } @{$user->{groups}}; | ||
| 726 | my $group_policy = | ||
| 727 | $DB->sql_search( qq{ SELECT DISTINCT negated FROM group_acl | ||
| 728 | WHERE gid IN ( $groups ) AND | ||
| 729 | target = ? AND privilege = ?}, | ||
| 730 | $target, $priv ); | ||
| 731 | return not $group_policy->[0]{negated} if @$group_policy == 1; | ||
| 732 | |||
| 733 | # Use the default policy | ||
| 734 | my $default_policy = | ||
| 735 | $DB->sql_get( q{ SELECT negated FROM default_acl | ||
| 736 | WHERE target = ? AND privilege = ? }, | ||
| 737 | $target, $priv ); | ||
| 738 | |||
| 739 | return not $default_policy->{negated} if $default_policy; | ||
| 740 | |||
| 741 | # Well, the default's default is to default | ||
| 742 | return 0; | ||
| 743 | } | ||
| 744 | |||
| 745 | 1; | ||
| 746 | |||
| 747 | __END__ |