summaryrefslogtreecommitdiff
path: root/FS/FS/CurrentUser.pm
blob: 7b0fe28a6f9b40880862596b8c4eca2ddb5c9b52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package FS::CurrentUser;

use vars qw($CurrentUser $CurrentSession $upgrade_hack);

#not at compile-time, circular dependancey causes trouble
#use FS::Record qw(qsearchs);
#use FS::access_user;

$upgrade_hack = 0;

=head1 NAME

FS::CurrentUser - Package representing the current user (and session)

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 CLASS METHODS

=over 4

=item load_user USERNAME

Sets the current user to the provided username

=cut

sub load_user {
  my( $class, $user ) = @_; #, $pass

  if ( $upgrade_hack ) {
    return $CurrentUser = new FS::CurrentUser::BootstrapUser;
  }

  #return "" if $user =~ /^fs_(queue|selfservice)$/;

  #not the best thing in the world...
  eval "use FS::Record qw(qsearchs);";
  die $@ if $@;
  eval "use FS::access_user;";
  die $@ if $@;

  $CurrentUser = qsearchs('access_user', {
    'username' => $user,
    #'_password' =>
    'disabled' => '',
  } );

  die "unknown user: $user" unless $CurrentUser; # or bad password

  $CurrentUser;
}

=item new_session

Creates a new session for the current user and returns the session key

=cut

use vars qw( @saltset );
@saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '+' , '/' );

sub new_session {
  my( $class ) = @_;

  #not the best thing in the world...
  eval "use FS::access_user_session;";
  die $@ if $@;

  my $sessionkey = join('', map $saltset[int(rand(scalar @saltset))], 0..39);

  my $access_user_session = new FS::access_user_session {
    'sessionkey' => $sessionkey,
    'usernum'    => $CurrentUser->usernum,
    'start_date' => time,
  };
  my $error = $access_user_session->insert;
  die $error if $error;

  return $sessionkey;

}

=item load_user_session SESSION_KEY

Sets the current user via the provided session key

=cut

sub load_user_session {
  my( $class, $sessionkey ) = @_;

  #not the best thing in the world...
  eval "use FS::Record qw(qsearchs);";
  die $@ if $@;
  eval "use FS::access_user_session;";
  die $@ if $@;

  $CurrentSession = qsearchs('access_user_session', {
    'sessionkey' => $sessionkey,
    #XXX check for timed out but not-yet deleted sessions here
  }) or return '';

  $CurrentSession->touch_last_date;

  $CurrentUser = $CurrentSession->access_user;

}

=head1 BUGS

Minimal docs

=head1 SEE ALSO

=cut

package FS::CurrentUser::BootstrapUser;

sub new {
  my $proto = shift;
  my $class = ref($proto) || $proto;
  my $self = {};
  bless ($self, $class);
}

sub AUTOLOAD { 1 };

1;