1 package FS::CurrentUser;
3 use vars qw($CurrentUser $CurrentSession $upgrade_hack);
5 #not at compile-time, circular dependancey causes trouble
6 #use FS::Record qw(qsearchs);
13 FS::CurrentUser - Package representing the current user (and session)
23 =item load_user USERNAME
25 Sets the current user to the provided username
30 my( $class, $username, %opt ) = @_;
32 if ( $upgrade_hack ) {
33 return $CurrentUser = new FS::CurrentUser::BootstrapUser;
36 #return "" if $username =~ /^fs_(queue|selfservice)$/;
38 #not the best thing in the world...
39 eval "use FS::Record qw(qsearchs);";
41 eval "use FS::access_user;";
44 my %hash = ( 'username' => $username,
48 $CurrentUser = qsearchs('access_user', \%hash) and return $CurrentUser;
50 die "unknown user: $username" unless $opt{'autocreate'};
52 $CurrentUser = new FS::access_user \%hash;
53 $CurrentUser->set($_, $opt{$_}) foreach qw( first last );
54 my $error = $CurrentUser->insert;
55 die $error if $error; #better way to handle this error?
59 || FS::Conf->new->config('external_auth-access_group-template_user');
61 if ( $template_user ) {
63 my $tmpl_access_user =
64 qsearchs('access_user', { 'username' => $template_user } );
66 if ( $tmpl_access_user ) {
67 eval "use FS::access_usergroup;";
70 foreach my $tmpl_access_usergroup
71 ($tmpl_access_user->access_usergroup) {
72 my $access_usergroup = new FS::access_usergroup {
73 'usernum' => $CurrentUser->usernum,
74 'groupnum' => $tmpl_access_usergroup->groupnum,
76 my $error = $access_usergroup->insert;
78 #shouldn't happen, but seems better to proceed than to die
79 warn "error inserting access_usergroup: $error";
84 warn "template username $template_user not found\n";
88 warn "no access template user for autocreated user $username\n";
96 Creates a new session for the current user and returns the session key
100 use vars qw( @saltset );
101 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '+' , '/' );
106 #not the best thing in the world...
107 eval "use FS::access_user_session;";
110 my $sessionkey = join('', map $saltset[int(rand(scalar @saltset))], 0..39);
112 my $access_user_session = new FS::access_user_session {
113 'sessionkey' => $sessionkey,
114 'usernum' => $CurrentUser->usernum,
115 'start_date' => time,
117 my $error = $access_user_session->insert;
118 die $error if $error;
124 =item load_user_session SESSION_KEY
126 Sets the current user via the provided session key
130 sub load_user_session {
131 my( $class, $sessionkey ) = @_;
133 #not the best thing in the world...
134 eval "use FS::Record qw(qsearchs);";
136 eval "use FS::access_user_session;";
139 $CurrentSession = qsearchs('access_user_session', {
140 'sessionkey' => $sessionkey,
141 #XXX check for timed out but not-yet deleted sessions here
144 $CurrentSession->touch_last_date;
146 $CurrentUser = $CurrentSession->access_user;
158 package FS::CurrentUser::BootstrapUser;
162 my $class = ref($proto) || $proto;
164 bless ($self, $class);