a8d8ab5139aa316186b6aab4100f17ca40722b24
[kivitendo-erp.git] / SL / DB / AuthUser.pm
1 package SL::DB::AuthUser;
2
3 use strict;
4
5 use List::Util qw(first);
6
7 use SL::DB::MetaSetup::AuthUser;
8 use SL::DB::Manager::AuthUser;
9 use SL::DB::Helper::Util;
10
11 use constant CONFIG_VARS => qw(copies countrycode dateformat timeformat default_media default_printer_id
12                                email favorites fax hide_cvar_search_options mandatory_departments menustyle name
13                                numberformat show_form_details signature stylesheet taxincluded_checked tel
14                                template_format vclimit focus_position form_cvars_nr_cols item_multiselect);
15
16 __PACKAGE__->meta->add_relationship(
17   groups => {
18     type      => 'many to many',
19     map_class => 'SL::DB::AuthUserGroup',
20     map_from  => 'user',
21     map_to    => 'group',
22   },
23   configs => {
24     type       => 'one to many',
25     class      => 'SL::DB::AuthUserConfig',
26     column_map => { id => 'user_id' },
27   },
28   clients => {
29     type      => 'many to many',
30     map_class => 'SL::DB::AuthClientUser',
31     map_from  => 'user',
32     map_to    => 'client',
33   },
34 );
35
36 __PACKAGE__->meta->initialize;
37
38 sub validate {
39   my ($self) = @_;
40
41   my @errors;
42   push @errors, $::locale->text('The login is missing.')    if !$self->login;
43   push @errors, $::locale->text('The login is not unique.') if !SL::DB::Helper::Util::is_unique($self, 'login');
44
45   return @errors;
46 }
47
48 sub get_config_value {
49   my ($self, $key) = @_;
50
51   my $cfg = first { $_->cfg_key eq $key } @{ $self->configs || [] };
52   return $cfg ? $cfg->cfg_value : undef;
53 }
54
55 sub config_values {
56   my $self = shift;
57
58   if (0 != scalar(@_)) {
59     my %settings = (ref($_[0]) || '') eq 'HASH' ? %{ $_[0] } : @_;
60     $self->configs([ map { SL::DB::AuthUserConfig->new(cfg_key => $_, cfg_value => $settings{$_}) } keys %settings ]);
61   }
62
63   return { map { ($_->cfg_key => $_->cfg_value) } @{ $self->configs || [] } };
64 }
65
66 1;