| 1 | package Apache2::SiteControl::User; | ||
| 2 | |||
| 3 | use 5.008; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Carp; | ||
| 7 | |||
| 8 | # This object represents a transient view of a persistent user. The UserManager | ||
| 9 | # is responsible for loading/saving these things. | ||
| 10 | sub new($$$$) { | ||
| 11 | my $proto = shift; | ||
| 12 | my $username = shift; | ||
| 13 | my $sessionid = shift; | ||
| 14 | my $usermanager = shift; | ||
| 15 | my $class = ref($proto) || $proto; | ||
| 16 | my $this = { username => $username, | ||
| 17 | sessionid => $sessionid, | ||
| 18 | manager => $usermanager, | ||
| 19 | attributes => {} }; | ||
| 20 | bless ($this, $class); | ||
| 21 | return $this; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub getUsername | ||
| 25 | { | ||
| 26 | my $this = shift; | ||
| 27 | return $this->{username}; | ||
| 28 | } | ||
| 29 | |||
| 30 | # user, request, name, value | ||
| 31 | sub setAttribute | ||
| 32 | { | ||
| 33 | my $this = shift; | ||
| 34 | my $r = shift; | ||
| 35 | my $name = shift; | ||
| 36 | my $value = shift; | ||
| 37 | |||
| 38 | $this->{attributes}{$name} = $value; | ||
| 39 | eval("$this->{manager}" . '->saveAttribute($r, $this, $name)'); | ||
| 40 | if($@) { | ||
| 41 | $r->log_error("ERROR! FAILED TO SAVE ATTRIBUTE. SESSION WILL NOT WORK: $@"); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | sub getAttribute | ||
| 46 | { | ||
| 47 | my $this = shift; | ||
| 48 | my $name = shift; | ||
| 49 | |||
| 50 | return $this->{attributes}{$name} if defined($this->{attributes}{$name}); | ||
| 51 | |||
| 52 | return undef; | ||
| 53 | } | ||
| 54 | |||
| 55 | # user object, apache request | ||
| 56 | sub logout | ||
| 57 | { | ||
| 58 | my $this = shift; | ||
| 59 | my $r = shift; | ||
| 60 | |||
| 61 | if(!defined($this) || !defined($r)) { | ||
| 62 | croak "INVALID CALL TO LOGOUT. You forgot to use OO syntax, or you forgot to pass the request object."; | ||
| 63 | } | ||
| 64 | eval("$this->{manager}" . '->invalidate($r, $this)'); | ||
| 65 | if($@) { | ||
| 66 | $r->log_error("Logout failed: $@"); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | 1; | ||
| 71 | |||
| 72 | __END__ |