2 # @description: WebDAV-Migration für Mandanten
 
   5 package SL::DBUpgrade2::clients_webdav;
 
  10 use parent qw(SL::DBUpgrade2::Base);
 
  13 use File::Path qw(make_path);
 
  15 use List::MoreUtils qw(any all);
 
  16 use List::Util qw(first);
 
  21 use SL::Helper::Flash;
 
  23 use Rose::Object::MakeMethods::Generic (
 
  24   'scalar --get_set_init' => [ qw(clients old_folders) ],
 
  29   return [ selectall_hashref_query($::form, $self->dbh, qq|SELECT * FROM auth.clients ORDER BY lower(name)|) ];
 
  32 sub init_old_folders {
 
  33   tie my %dir, 'IO::Dir', 'webdav';
 
  34   return [ sort grep { -d } keys %dir ];
 
  37 sub _unlink_old_folders {
 
  38   my ($self, %params) = @_;
 
  40   rmdir $_ for @{ $self->old_folders };
 
  45 sub _ensure_one_client_exists {
 
  46   my ($self, %params) = @_;
 
  48   return if 0 != scalar @{ $self->clients };
 
  51     INSERT INTO auth.clients (name, dbhost, dbport, dbname, dbuser, dbpasswd, is_default)
 
  52     VALUES                   (?,    ?,      5432,   ?,      ?,      ?,        true)
 
  55   $self->dbh->do($sql, undef, $::locale->text('Default Client (unconfigured)'), ($::locale->text('unconfigured')) x 4);
 
  57   undef $self->{clients};
 
  60 sub _move_files_into {
 
  61   my ($self, $client) = @_;
 
  63   tie my %dir, 'IO::Dir', 'webdav';
 
  64   my @entries = grep { !m/^\.\.?$/ } keys %dir;
 
  66   make_path('webdav/' . $client->{id});
 
  67   rename "webdav/$_", "webdav/" . $client->{id} . "/$_" for @entries;
 
  71   my ($self, $client) = @_;
 
  72   make_path('webdav/' . $client->{id} . "/$_") for qw(angebote bestellungen anfragen lieferantenbestellungen verkaufslieferscheine einkaufslieferscheine gutschriften rechnungen einkaufsrechnungen);
 
  76   my ($self, $client) = @_;
 
  78   my $name =  $client->{name} // '';
 
  81   make_path('webdav/links');
 
  82   symlink '../' . $client->{id}, "webdav/links/${name}";
 
  85 sub _webdav_folders_used {
 
  86   my ($self, %params) = @_;
 
  88   my $contains_files  = 0;
 
  90     $contains_files   = 1 if -f && !m{/(?:\.gitignore|.dummy|webdav-user)$};
 
  93   File::Find::find({ wanted => $wanted, no_chdir => 1 }, 'webdav');
 
  95   return $contains_files;
 
 101   # WebDAV not used? Remove old folders, and we're done.
 
 102   return $self->_unlink_old_folders if !$self->_webdav_folders_used;
 
 104   # Ensure at least one client exists.
 
 105   $self->_ensure_one_client_exists;
 
 108   if (1 == scalar @{ $self->clients }) {
 
 109     # Exactly one client? Great, use that one without bothering the
 
 111     $client_to_use = $self->clients->[0];
 
 114     # If there's more than one client then let the user select which
 
 115     # client to move the old files into. Maybe she already did?
 
 116     $client_to_use = first { $_->{id} == $::form->{client_id} } @{ $self->clients } if $::form->{client_id};
 
 118     if (!$client_to_use) {
 
 119       # Nope, let's select it.
 
 120       print $::form->parse_html_template('dbupgrade/auth/clients_webdav', { SELF => $self, default_client => (first { $_->{is_default} } @{ $self->clients }) });
 
 125   # Move files for the selected client.
 
 126   $self->_move_files_into($client_to_use);
 
 128   # Create the directory structures for all (even the selected client
 
 129   # -- folders might be missing).
 
 130   for (@{ $self->clients }) {
 
 131     $self->_create_folders($_);
 
 132     $self->_create_symlink($_);