d3acaf998f612919b8bab087f815a2f909553f94
[kivitendo-erp.git] / SL / DB / AuthClient.pm
1 package SL::DB::AuthClient;
2
3 use strict;
4
5 use Carp;
6 use File::Path ();
7
8 use SL::DBConnect;
9 use SL::DB::MetaSetup::AuthClient;
10 use SL::DB::Manager::AuthClient;
11 use SL::DB::Helper::Util;
12
13 __PACKAGE__->meta->add_relationship(
14   users => {
15     type      => 'many to many',
16     map_class => 'SL::DB::AuthClientUser',
17     map_from  => 'client',
18     map_to    => 'user',
19   },
20   groups => {
21     type      => 'many to many',
22     map_class => 'SL::DB::AuthClientGroup',
23     map_from  => 'client',
24     map_to    => 'group',
25   },
26 );
27
28 __PACKAGE__->meta->initialize;
29
30 __PACKAGE__->before_save('_before_save_remember_old_name');
31 __PACKAGE__->after_save('_after_save_ensure_webdav_symlink_correctness');
32 __PACKAGE__->after_delete('_after_delete_delete_webdav_symlink');
33
34 sub _before_save_remember_old_name {
35   my ($self) = @_;
36
37   delete $self->{__before_save_remember_old_name};
38   if ($self->id && $::lx_office_conf{features}->{webdav}) {
39     $self->{__before_save_remember_old_name} = SL::DB::AuthClient->new(id => $self->id)->load->name;
40   }
41
42   return 1;
43 }
44
45 sub _after_save_ensure_webdav_symlink_correctness {
46   my ($self) = @_;
47
48   $self->ensure_webdav_symlink_correctness($self->{__before_save_remember_old_name}) if $self->id;
49   return 1;
50 }
51
52 sub _after_delete_delete_webdav_symlink {
53   my ($self) = @_;
54
55   my $name = $self->webdav_symlink_basename;
56   unlink "webdav/links/${name}";
57   return 1;
58 }
59
60 sub validate {
61   my ($self) = @_;
62
63   my @errors;
64   push @errors, $::locale->text('The name is missing.')                                           if !$self->name;
65   push @errors, $::locale->text('The database name is missing.')                                  if !$self->dbname;
66   push @errors, $::locale->text('The database host is missing.')                                  if !$self->dbhost;
67   push @errors, $::locale->text('The database port is missing.')                                  if !$self->dbport;
68   push @errors, $::locale->text('The database user is missing.')                                  if !$self->dbuser;
69   push @errors, $::locale->text('The name is not unique.')                                        if !SL::DB::Helper::Util::is_unique($self, 'name');
70   push @errors, $::locale->text('The combination of database host, port and name is not unique.') if !SL::DB::Helper::Util::is_unique($self, 'dbhost', 'dbport', 'dbname');
71
72   return @errors;
73 }
74
75 sub webdav_symlink_basename {
76   my ($self, $name) =  @_;
77
78   $name             =  $name || $self->name || '';
79   $name             =~ s:/+:_:g;
80
81   return $name;
82 }
83
84 sub ensure_webdav_symlink_correctness {
85   my ($self, $old_name) = @_;
86
87   croak "Need object ID" unless $self->id;
88
89   my $new_symlink = $self->webdav_symlink_basename;
90
91   croak "Need name" unless $new_symlink;
92
93   my $base_path = 'webdav/links';
94
95   if ($old_name) {
96     my $old_symlink = $self->webdav_symlink_basename($old_name);
97     return if $old_symlink eq $new_symlink;
98
99     if (-l "${base_path}/${old_symlink}") {
100       rename "${base_path}/${old_symlink}", "${base_path}/${new_symlink}";
101       return;
102     }
103   }
104
105   File::Path::make_path('webdav/' . $self->id);
106   symlink '../' . $self->id, "${base_path}/${new_symlink}";
107 }
108
109 sub get_dbconnect_args {
110   my ($self, %params) = @_;
111
112   return (
113     'dbi:Pg:dbname=' . $self->dbname . ';host=' . ($self->dbhost || 'localhost') . ';port=' . ($self->dbport || 5432),
114     $self->dbuser,
115     $self->dbpasswd,
116     SL::DBConnect->get_options(%params),
117   );
118 }
119
120 sub dbconnect {
121   my ($self, %params) = @_;
122   return SL::DBConnect->connect($self->get_dbconnect_args(%params));
123 }
124
125 1;
126 __END__
127
128 =pod
129
130 =encoding utf8
131
132 =head1 NAME
133
134 SL::DB::AuthClient - RDBO model for the auth.clients table
135
136 =head1 FUNCTIONS
137
138 =over 4
139
140 =item C<dbconnect [%params]>
141
142 Establishes a new database connection to the database configured for
143 C<$self>. Returns a database handle as returned by
144 L<SL::DBConnect/connect> (which is either a normal L<DBI> handle or
145 one handled by L<DBIx::Log4perl>).
146
147 C<%params> are optional parameters passed as the fourth argument to
148 L<SL::DBConnect/connect>. They're first filtered through
149 L<SL::DBConnect/get_options> so the UTF-8 flag will be set properly.
150
151 =item C<ensure_webdav_symlink_correctness>
152
153 Handles the symlink creation/deletion for the WebDAV folder. Does
154 nothing if WebDAV is not enabled in the configuration.
155
156 For each existing client a symbolic link should exist in the directory
157 C<webdav/links> pointing to the actual WebDAV directory which is the
158 client's database ID.
159
160 The symbolic link's name is the client's name sanitized a bit. It's
161 calculated by L</webdav_symlink_basename>.
162
163 =item C<get_dbconnect_args [%params]>
164
165 Returns an array of database connection parameters suitable for
166 passing to L<SL::DBConnect/connect>.
167
168 C<%params> are optional parameters passed as the fourth argument to
169 L<SL::DBConnect/connect>. They're first filtered through
170 L<SL::DBConnect/get_options> so the UTF-8 flag will be set properly.
171
172 =item C<validate>
173
174 Returns an array of human-readable error message if the object must
175 not be saved and an empty list if nothing's wrong.
176
177 =item C<webdav_symlink_basename>
178
179 Returns the base name of the symbolic link for the WebDAV C<links>
180 sub-folder.
181
182 =back
183
184 =head1 BUGS
185
186 Nothing here yet.
187
188 =head1 AUTHOR
189
190 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
191
192 =cut