backport some unused new-style auth stuff from master for development purposes, shoul...
[freeside.git] / FS / FS / CurrentUser.pm
1 package FS::CurrentUser;
2
3 use vars qw($CurrentUser $upgrade_hack);
4
5 #not at compile-time, circular dependancey causes trouble
6 #use FS::Record qw(qsearchs);
7 #use FS::access_user;
8
9 $upgrade_hack = 0;
10
11 =head1 NAME
12
13 FS::CurrentUser - Package representing the current user
14
15 =head1 SYNOPSIS
16
17 =head1 DESCRIPTION
18
19 =cut
20
21 sub load_user {
22   my( $class, $user ) = @_; #, $pass
23
24   if ( $upgrade_hack ) {
25     return $CurrentUser = new FS::CurrentUser::BootstrapUser;
26   }
27
28   #return "" if $user =~ /^fs_(queue|selfservice)$/;
29
30   #not the best thing in the world...
31   eval "use FS::Record qw(qsearchs);";
32   die $@ if $@;
33   eval "use FS::access_user;";
34   die $@ if $@;
35
36   $CurrentUser = qsearchs('access_user', {
37     'username' => $user,
38     #'_password' =>
39     'disabled' => '',
40   } );
41
42   die "unknown user: $user" unless $CurrentUser; # or bad password
43
44   $CurrentUser;
45 }
46
47 =item new_session
48
49 Creates a new session for the current user and returns the session key
50
51 =cut
52
53 use vars qw( @saltset );
54 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '+' , '/' );
55
56 sub new_session {
57   my( $class ) = @_;
58
59   #not the best thing in the world...
60   eval "use FS::access_user_session;";
61   die $@ if $@;
62
63   my $sessionkey = join('', map $saltset[int(rand(scalar @saltset))], 0..39);
64
65   my $access_user_session = new FS::access_user_session {
66     'sessionkey' => $sessionkey,
67     'usernum'    => $CurrentUser->usernum,
68     'start_date' => time,
69   };
70   my $error = $access_user_session->insert;
71   die $error if $error;
72
73   return $sessionkey;
74
75 }
76
77 =item load_user_session SESSION_KEY
78
79 Sets the current user via the provided session key
80
81 =cut
82
83 sub load_user_session {
84   my( $class, $sessionkey ) = @_;
85
86   #not the best thing in the world...
87   eval "use FS::Record qw(qsearchs);";
88   die $@ if $@;
89   eval "use FS::access_user_session;";
90   die $@ if $@;
91
92   $CurrentSession = qsearchs('access_user_session', {
93     'sessionkey' => $sessionkey,
94     #XXX check for timed out but not-yet deleted sessions here
95   }) or return '';
96
97   $CurrentSession->touch_last_date;
98
99   $CurrentUser = $CurrentSession->access_user;
100
101 }
102
103 =head1 BUGS
104
105 Creepy crawlies
106
107 =head1 SEE ALSO
108
109 =cut
110
111 package FS::CurrentUser::BootstrapUser;
112
113 sub new {
114   my $proto = shift;
115   my $class = ref($proto) || $proto;
116   my $self = {};
117   bless ($self, $class);
118 }
119
120 sub AUTOLOAD { 1 };
121
122 1;
123