kivitendo/SL/DB/AuthClient.pm @ 50b2c44e
3ced230d | Moritz Bunkus | package SL::DB::AuthClient;
|
||
use strict;
|
||||
58af8f75 | Moritz Bunkus | use Carp;
|
||
use File::Path ();
|
||||
a4eedbad | Moritz Bunkus | use SL::DBConnect;
|
||
3ced230d | Moritz Bunkus | use SL::DB::MetaSetup::AuthClient;
|
||
d8ac0828 | Moritz Bunkus | use SL::DB::Manager::AuthClient;
|
||
9636227e | Moritz Bunkus | use SL::DB::Helper::Util;
|
||
3ced230d | Moritz Bunkus | |||
__PACKAGE__->meta->add_relationship(
|
||||
users => {
|
||||
type => 'many to many',
|
||||
3568f2f8 | Moritz Bunkus | map_class => 'SL::DB::AuthClientUser',
|
||
3ced230d | Moritz Bunkus | map_from => 'client',
|
||
map_to => 'user',
|
||||
},
|
||||
groups => {
|
||||
type => 'many to many',
|
||||
3568f2f8 | Moritz Bunkus | map_class => 'SL::DB::AuthClientGroup',
|
||
3ced230d | Moritz Bunkus | map_from => 'client',
|
||
map_to => 'group',
|
||||
},
|
||||
);
|
||||
__PACKAGE__->meta->initialize;
|
||||
58af8f75 | Moritz Bunkus | __PACKAGE__->before_save('_before_save_remember_old_name');
|
||
__PACKAGE__->after_save('_after_save_ensure_webdav_symlink_correctness');
|
||||
__PACKAGE__->after_delete('_after_delete_delete_webdav_symlink');
|
||||
sub _before_save_remember_old_name {
|
||||
my ($self) = @_;
|
||||
delete $self->{__before_save_remember_old_name};
|
||||
if ($self->id && $::lx_office_conf{features}->{webdav}) {
|
||||
$self->{__before_save_remember_old_name} = SL::DB::AuthClient->new(id => $self->id)->load->name;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
sub _after_save_ensure_webdav_symlink_correctness {
|
||||
my ($self) = @_;
|
||||
969916aa | Moritz Bunkus | $self->ensure_webdav_symlink_correctness($self->{__before_save_remember_old_name}) if $self->id;
|
||
58af8f75 | Moritz Bunkus | return 1;
|
||
}
|
||||
sub _after_delete_delete_webdav_symlink {
|
||||
my ($self) = @_;
|
||||
my $name = $self->webdav_symlink_basename;
|
||||
unlink "webdav/links/${name}";
|
||||
return 1;
|
||||
}
|
||||
9636227e | Moritz Bunkus | sub validate {
|
||
my ($self) = @_;
|
||||
my @errors;
|
||||
push @errors, $::locale->text('The name is missing.') if !$self->name;
|
||||
push @errors, $::locale->text('The database name is missing.') if !$self->dbname;
|
||||
push @errors, $::locale->text('The database host is missing.') if !$self->dbhost;
|
||||
push @errors, $::locale->text('The database port is missing.') if !$self->dbport;
|
||||
push @errors, $::locale->text('The database user is missing.') if !$self->dbuser;
|
||||
push @errors, $::locale->text('The name is not unique.') if !SL::DB::Helper::Util::is_unique($self, 'name');
|
||||
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');
|
||||
return @errors;
|
||||
}
|
||||
58af8f75 | Moritz Bunkus | sub webdav_symlink_basename {
|
||
my ($self, $name) = @_;
|
||||
$name = $name || $self->name || '';
|
||||
$name =~ s:/+:_:g;
|
||||
return $name;
|
||||
}
|
||||
sub ensure_webdav_symlink_correctness {
|
||||
my ($self, $old_name) = @_;
|
||||
croak "Need object ID" unless $self->id;
|
||||
my $new_symlink = $self->webdav_symlink_basename;
|
||||
croak "Need name" unless $new_symlink;
|
||||
my $base_path = 'webdav/links';
|
||||
if ($old_name) {
|
||||
my $old_symlink = $self->webdav_symlink_basename($old_name);
|
||||
return if $old_symlink eq $new_symlink;
|
||||
if (-l "${base_path}/${old_symlink}") {
|
||||
rename "${base_path}/${old_symlink}", "${base_path}/${new_symlink}";
|
||||
return;
|
||||
}
|
||||
}
|
||||
File::Path::make_path('webdav/' . $self->id);
|
||||
symlink '../' . $self->id, "${base_path}/${new_symlink}";
|
||||
}
|
||||
a4eedbad | Moritz Bunkus | sub get_dbconnect_args {
|
||
my ($self, %params) = @_;
|
||||
return (
|
||||
'dbi:Pg:dbname=' . $self->dbname . ';host=' . ($self->dbhost || 'localhost') . ';port=' . ($self->dbport || 5432),
|
||||
$self->dbuser,
|
||||
$self->dbpasswd,
|
||||
SL::DBConnect->get_options(%params),
|
||||
);
|
||||
}
|
||||
sub dbconnect {
|
||||
my ($self, %params) = @_;
|
||||
return SL::DBConnect->connect($self->get_dbconnect_args(%params));
|
||||
}
|
||||
3ced230d | Moritz Bunkus | 1;
|
||
a4eedbad | Moritz Bunkus | __END__
|
||
=pod
|
||||
=encoding utf8
|
||||
=head1 NAME
|
||||
SL::DB::AuthClient - RDBO model for the auth.clients table
|
||||
=head1 FUNCTIONS
|
||||
=over 4
|
||||
=item C<dbconnect [%params]>
|
||||
Establishes a new database connection to the database configured for
|
||||
C<$self>. Returns a database handle as returned by
|
||||
L<SL::DBConnect/connect> (which is either a normal L<DBI> handle or
|
||||
one handled by L<DBIx::Log4perl>).
|
||||
C<%params> are optional parameters passed as the fourth argument to
|
||||
L<SL::DBConnect/connect>. They're first filtered through
|
||||
L<SL::DBConnect/get_options> so the UTF-8 flag will be set properly.
|
||||
=item C<ensure_webdav_symlink_correctness>
|
||||
Handles the symlink creation/deletion for the WebDAV folder. Does
|
||||
nothing if WebDAV is not enabled in the configuration.
|
||||
For each existing client a symbolic link should exist in the directory
|
||||
C<webdav/links> pointing to the actual WebDAV directory which is the
|
||||
client's database ID.
|
||||
The symbolic link's name is the client's name sanitized a bit. It's
|
||||
calculated by L</webdav_symlink_basename>.
|
||||
=item C<get_dbconnect_args [%params]>
|
||||
Returns an array of database connection parameters suitable for
|
||||
passing to L<SL::DBConnect/connect>.
|
||||
C<%params> are optional parameters passed as the fourth argument to
|
||||
L<SL::DBConnect/connect>. They're first filtered through
|
||||
L<SL::DBConnect/get_options> so the UTF-8 flag will be set properly.
|
||||
=item C<validate>
|
||||
76cd0405 | Geoffrey Richardson | Returns an array of human-readable error messages if the object must
|
||
a4eedbad | Moritz Bunkus | not be saved and an empty list if nothing's wrong.
|
||
=item C<webdav_symlink_basename>
|
||||
Returns the base name of the symbolic link for the WebDAV C<links>
|
||||
sub-folder.
|
||||
=back
|
||||
=head1 BUGS
|
||||
Nothing here yet.
|
||||
=head1 AUTHOR
|
||||
Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
|
||||
=cut
|