a8c690ade9950cf52da5d4e5dec372f3e6a93f0a
[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 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   foreach my $user (@{ SL::DB::AuthClient->new(id => $::auth->client->{id})->load->users || [] }) {
35     my $user_config = $user->config_values;
36     my $employee    = $employees_by_login{$user->login} || SL::DB::Employee->new(login => $user->login);
37
38     $employee->update_attributes(
39       name      => $user_config->{name},
40       deleted   => 0,
41     );
42   }
43 }
44
45 1;
46 __END__
47
48 =pod
49
50 =encoding utf8
51
52 =head1 NAME
53
54 SL::DB::Manager::Employee - RDBO manager for the C<employee> table
55
56 =head1 SYNOPSIS
57
58   my $logged_in_employee = SL::DB::Manager::Employee->current;
59
60 =head1 FUNCTIONS
61
62 =over 4
63
64 =item C<current>
65
66 Returns an RDBO instance corresponding to the currently logged-in user.
67
68 =item C<update_entries_for_authorized_users>
69
70 For each user created by the administrator in the admin section an
71 entry only exists in the authentication table, but not in the employee
72 table. This is where this function comes in: It iterates over all
73 authentication users that have access to the current client and ensures
74 that an entry for them exists in the table C<employee>. The matching
75 is done via the login name which must be the same in both tables.
76
77 The only other properties that will be copied from the authentication
78 table into the C<employee> row are C<name> and C<workphone>. In
79 addition C<deleted> is always set to 0.
80
81 The intention is that this function is called automatically during the
82 login process.
83
84 =back
85
86 =head1 BUGS
87
88 Nothing here yet.
89
90 =head1 AUTHOR
91
92 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
93
94 =cut