400843bbad87e9d45bad5c39c0f2067538a6f515
[kivitendo-erp.git] / SL / DB / Manager / Employee.pm
1 package SL::DB::Manager::Employee;
2
3 use strict;
4
5 use SL::DB::Helper::Manager;
6 use SL::DB::Helper::Sorted;
7 use base qw(SL::DB::Helper::Manager);
8
9 sub object_class { 'SL::DB::Employee' }
10
11 __PACKAGE__->make_manager_methods;
12
13 sub _sort_spec {
14   (
15     default  => [ 'name', 1 ],
16     columns  => {
17       SIMPLE => 'ALL',
18       map { +($_ => "lower(employee.$_)") } qw(deleted_email deleted_fax deleted_signature deleted_tel login name)
19     },
20   );
21 }
22
23 sub current {
24   return undef unless $::myconfig{login};
25   return $::request->cache('current')->{object} //= shift->find_by(login => $::myconfig{login});
26 }
27
28 sub update_entries_for_authorized_users {
29   my ($class) = @_;
30
31   my %employees_by_login = map { ($_->login => $_) } @{ $class->get_all };
32
33   require SL::DB::AuthClient;
34   no warnings 'once';
35
36   foreach my $user (@{ SL::DB::AuthClient->new(id => $::auth->client->{id})->load->users || [] }) {
37     my $user_config = $user->config_values;
38     my $employee    = $employees_by_login{$user->login} || SL::DB::Employee->new(login => $user->login);
39
40     $employee->update_attributes(
41       name      => $user_config->{name},
42       deleted   => 0,
43     );
44   }
45 }
46
47 1;
48 __END__
49
50 =pod
51
52 =encoding utf8
53
54 =head1 NAME
55
56 SL::DB::Manager::Employee - RDBO manager for the C<employee> table
57
58 =head1 SYNOPSIS
59
60   my $logged_in_employee = SL::DB::Manager::Employee->current;
61
62 =head1 FUNCTIONS
63
64 =over 4
65
66 =item C<current>
67
68 Returns an RDBO instance corresponding to the currently logged-in user.
69
70 =item C<update_entries_for_authorized_users>
71
72 For each user created by the administrator in the admin section an
73 entry only exists in the authentication table, but not in the employee
74 table. This is where this function comes in: It iterates over all
75 authentication users that have access to the current client and ensures
76 that an entry for them exists in the table C<employee>. The matching
77 is done via the login name which must be the same in both tables.
78
79 The only other properties that will be copied from the authentication
80 table into the C<employee> row are C<name> and C<workphone>. In
81 addition C<deleted> is always set to 0.
82
83 The intention is that this function is called automatically during the
84 login process.
85
86 =back
87
88 =head1 BUGS
89
90 Nothing here yet.
91
92 =head1 AUTHOR
93
94 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
95
96 =cut