1 package SL::DB::AuthClient;
 
   9 use SL::DB::MetaSetup::AuthClient;
 
  10 use SL::DB::Manager::AuthClient;
 
  11 use SL::DB::Helper::Util;
 
  13 __PACKAGE__->meta->add_relationship(
 
  15     type      => 'many to many',
 
  16     map_class => 'SL::DB::AuthClientUser',
 
  21     type      => 'many to many',
 
  22     map_class => 'SL::DB::AuthClientGroup',
 
  28 __PACKAGE__->meta->initialize;
 
  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');
 
  34 sub _before_save_remember_old_name {
 
  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;
 
  45 sub _after_save_ensure_webdav_symlink_correctness {
 
  48   $self->ensure_webdav_symlink_correctness($self->{__before_save_remember_old_name}) if $self->id;
 
  52 sub _after_delete_delete_webdav_symlink {
 
  55   my $name = $self->webdav_symlink_basename;
 
  56   unlink "webdav/links/${name}";
 
  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');
 
  75 sub webdav_symlink_basename {
 
  76   my ($self, $name) =  @_;
 
  78   $name             =  $name || $self->name || '';
 
  84 sub ensure_webdav_symlink_correctness {
 
  85   my ($self, $old_name) = @_;
 
  87   croak "Need object ID" unless $self->id;
 
  89   my $new_symlink = $self->webdav_symlink_basename;
 
  91   croak "Need name" unless $new_symlink;
 
  93   my $base_path = 'webdav/links';
 
  96     my $old_symlink = $self->webdav_symlink_basename($old_name);
 
  97     return if $old_symlink eq $new_symlink;
 
  99     if (-l "${base_path}/${old_symlink}") {
 
 100       rename "${base_path}/${old_symlink}", "${base_path}/${new_symlink}";
 
 105   File::Path::make_path('webdav/' . $self->id);
 
 106   symlink '../' . $self->id, "${base_path}/${new_symlink}";
 
 109 sub get_dbconnect_args {
 
 110   my ($self, %params) = @_;
 
 113     'dbi:Pg:dbname=' . $self->dbname . ';host=' . ($self->dbhost || 'localhost') . ';port=' . ($self->dbport || 5432),
 
 116     SL::DBConnect->get_options(%params),
 
 121   my ($self, %params) = @_;
 
 122   return SL::DBConnect->connect($self->get_dbconnect_args(%params));
 
 134 SL::DB::AuthClient - RDBO model for the auth.clients table
 
 140 =item C<dbconnect [%params]>
 
 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>).
 
 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.
 
 151 =item C<ensure_webdav_symlink_correctness>
 
 153 Handles the symlink creation/deletion for the WebDAV folder. Does
 
 154 nothing if WebDAV is not enabled in the configuration.
 
 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.
 
 160 The symbolic link's name is the client's name sanitized a bit. It's
 
 161 calculated by L</webdav_symlink_basename>.
 
 163 =item C<get_dbconnect_args [%params]>
 
 165 Returns an array of database connection parameters suitable for
 
 166 passing to L<SL::DBConnect/connect>.
 
 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.
 
 174 Returns an array of human-readable error messages if the object must
 
 175 not be saved and an empty list if nothing's wrong.
 
 177 =item C<webdav_symlink_basename>
 
 179 Returns the base name of the symbolic link for the WebDAV C<links>
 
 190 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>