Einträge in employee aus User->login heraus aktualisieren
[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 base qw(SL::DB::Helper::Manager);
7
8 sub object_class { 'SL::DB::Employee' }
9
10 __PACKAGE__->make_manager_methods;
11
12 sub current {
13   return undef unless $::form && $::form->{login};
14   return shift->find_by(login => $::form->{login});
15 }
16
17 sub update_entries_for_authorized_users {
18   my ($class) = @_;
19
20   my %employees_by_login = map { ($_->login => $_) } @{ $class->get_all };
21
22   require SL::DB::AuthClient;
23   foreach my $user (@{ SL::DB::AuthClient->new(id => $::auth->client->{id})->load->users || [] }) {
24     my $user_config = $user->config_values;
25     my $employee    = $employees_by_login{$user->login} || SL::DB::Employee->new(login => $user->login);
26
27     $employee->update_attributes(
28       name      => $user_config->{name},
29       workphone => $user_config->{tel},
30       deleted   => 0,
31     );
32   }
33 }
34
35 1;
36 __END__
37
38 =pod
39
40 =encoding utf8
41
42 =head1 NAME
43
44 SL::DB::Manager::Employee - RDBO manager for the C<employee> table
45
46 =head1 SYNOPSIS
47
48   my $logged_in_employee = SL::DB::Manager::Employee->current;
49
50 =head1 FUNCTIONS
51
52 =over 4
53
54 =item C<current>
55
56 Returns an RDBO instance corresponding to the currently logged-in user.
57
58 =item C<update_entries_for_authorized_users>
59
60 For each user created by the administrator in the admin section an
61 entry only exists in the authentication table, but not in the employee
62 table. This is where this function comes in: It iterates over all
63 authentication users that have access to the current client and ensure
64 than an entry for them exists in the table C<employee>. The matching
65 is done via the login name which must be the same in both tables.
66
67 The only other properties that will be copied from the authentication
68 table into the C<employee> row are C<name> and C<workphone>. In
69 addition C<deleted> is always set to 0.
70
71 The intention is that this function is called automatically during the
72 login process.
73
74 =back
75
76 =head1 BUGS
77
78 Nothing here yet.
79
80 =head1 AUTHOR
81
82 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
83
84 =cut