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 && $::lx_office_conf{features}->{webdav};
 
  52 sub _after_delete_delete_webdav_symlink {
 
  55   return 1 if !$::lx_office_conf{features}->{webdav};
 
  56   my $name = $self->webdav_symlink_basename;
 
  57   unlink "webdav/links/${name}";
 
  65   push @errors, $::locale->text('The name is missing.')                                           if !$self->name;
 
  66   push @errors, $::locale->text('The database name is missing.')                                  if !$self->dbname;
 
  67   push @errors, $::locale->text('The database host is missing.')                                  if !$self->dbhost;
 
  68   push @errors, $::locale->text('The database port is missing.')                                  if !$self->dbport;
 
  69   push @errors, $::locale->text('The database user is missing.')                                  if !$self->dbuser;
 
  70   push @errors, $::locale->text('The name is not unique.')                                        if !SL::DB::Helper::Util::is_unique($self, 'name');
 
  71   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');
 
  76 sub webdav_symlink_basename {
 
  77   my ($self, $name) =  @_;
 
  79   $name             =  $name || $self->name || '';
 
  85 sub ensure_webdav_symlink_correctness {
 
  86   my ($self, $old_name) = @_;
 
  88   return unless $::lx_office_conf{features}->{webdav};
 
  90   croak "Need object ID" unless $self->id;
 
  92   my $new_symlink = $self->webdav_symlink_basename;
 
  94   croak "Need name" unless $new_symlink;
 
  96   my $base_path = 'webdav/links';
 
  99     my $old_symlink = $self->webdav_symlink_basename($old_name);
 
 100     return if $old_symlink eq $new_symlink;
 
 102     if (-l "${base_path}/${old_symlink}") {
 
 103       rename "${base_path}/${old_symlink}", "${base_path}/${new_symlink}";
 
 108   File::Path::make_path('webdav/' . $self->id);
 
 109   symlink '../' . $self->id, "${base_path}/${new_symlink}";
 
 112 sub get_dbconnect_args {
 
 113   my ($self, %params) = @_;
 
 116     'dbi:Pg:dbname=' . $self->dbname . ';host=' . ($self->dbhost || 'localhost') . ';port=' . ($self->dbport || 5432),
 
 119     SL::DBConnect->get_options(%params),
 
 124   my ($self, %params) = @_;
 
 125   return SL::DBConnect->connect($self->get_dbconnect_args(%params));
 
 137 SL::DB::AuthClient - RDBO model for the auth.clients table
 
 143 =item C<dbconnect [%params]>
 
 145 Establishes a new database connection to the database configured for
 
 146 C<$self>. Returns a database handle as returned by
 
 147 L<SL::DBConnect/connect> (which is either a normal L<DBI> handle or
 
 148 one handled by L<DBIx::Log4perl>).
 
 150 C<%params> are optional parameters passed as the fourth argument to
 
 151 L<SL::DBConnect/connect>. They're first filtered through
 
 152 L<SL::DBConnect/get_options> so the UTF-8 flag will be set properly.
 
 154 =item C<ensure_webdav_symlink_correctness>
 
 156 Handles the symlink creation/deletion for the WebDAV folder. Does
 
 157 nothing if WebDAV is not enabled in the configuration.
 
 159 For each existing client a symbolic link should exist in the directory
 
 160 C<webdav/links> pointing to the actual WebDAV directory which is the
 
 161 client's database ID.
 
 163 The symbolic link's name is the client's name sanitized a bit. It's
 
 164 calculated by L</webdav_symlink_basename>.
 
 166 =item C<get_dbconnect_args [%params]>
 
 168 Returns an array of database connection parameters suitable for
 
 169 passing to L<SL::DBConnect/connect>.
 
 171 C<%params> are optional parameters passed as the fourth argument to
 
 172 L<SL::DBConnect/connect>. They're first filtered through
 
 173 L<SL::DBConnect/get_options> so the UTF-8 flag will be set properly.
 
 177 Returns an array of human-readable error message if the object must
 
 178 not be saved and an empty list if nothing's wrong.
 
 180 =item C<webdav_symlink_basename>
 
 182 Returns the base name of the symbolic link for the WebDAV C<links>
 
 193 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>