Upgrade-Script für WebDAV-Migration zu Mandanten
[kivitendo-erp.git] / sql / Pg-upgrade2-auth / clients_webdav.pl
1 # @tag: clients_webdav
2 # @description: WebDAV-Migration für Mandanten
3 # @depends: clients
4 # @ignore: 0
5 package SL::DBUpgrade2::clients_webdav;
6
7 use strict;
8 use utf8;
9
10 use parent qw(SL::DBUpgrade2::Base);
11
12 use File::Path qw(make_path);
13 use IO::Dir;
14 use List::MoreUtils qw(any all);
15 use List::Util qw(first);
16
17 use SL::DBConnect;
18 use SL::DBUtils;
19 use SL::Template;
20 use SL::Helper::Flash;
21
22 use Rose::Object::MakeMethods::Generic (
23   'scalar --get_set_init' => [ qw(clients old_folders) ],
24 );
25
26 sub init_clients {
27   my ($self) = @_;
28   return [ selectall_hashref_query($::form, $self->dbh, qq|SELECT * FROM auth.clients ORDER BY lower(name)|) ];
29 }
30
31 sub init_old_folders {
32   tie my %dir, 'IO::Dir', 'webdav';
33   return [ sort grep { -d } keys %dir ];
34 }
35
36 sub _unlink_old_folders {
37   my ($self, %params) = @_;
38
39   rmdir $_ for @{ $self->old_folders };
40
41   return 1;
42 }
43
44 sub _ensure_one_client_exists {
45   my ($self, %params) = @_;
46
47   return if 0 != scalar @{ $self->clients };
48
49   my $sql = <<SQL;
50     INSERT INTO auth.clients (name, dbhost, dbport, dbname, dbuser, dbpasswd, is_default)
51     VALUES                   (?,    ?,      5432,   ?,      ?,      ?,        true)
52 SQL
53
54   $self->dbh->do($sql, undef, $::locale->text('Default Client (unconfigured)'), ($::locale->text('unconfigured')) x 4);
55
56   undef $self->{clients};
57 }
58
59 sub _move_files_into {
60   my ($self, $client) = @_;
61
62   tie my %dir, 'IO::Dir', 'webdav';
63   my @entries = grep { !m/^\.\.?$/ } keys %dir;
64
65   make_path('webdav/' . $client->{id});
66   rename "webdav/$_", "webdav/" . $client->{id} . "/$_" for @entries;
67 }
68
69 sub _create_folders {
70   my ($self, $client) = @_;
71   make_path('webdav/' . $client->{id} . "/$_") for qw(angebote bestellungen anfragen lieferantenbestellungen verkaufslieferscheine einkaufslieferscheine gutschriften rechnungen einkaufsrechnungen);
72 }
73
74 sub _create_symlink {
75   my ($self, $client) = @_;
76
77   my $name =  $client->{name} // '';
78   $name    =~ s:/+:_:g;
79
80   make_path('webdav/links');
81   symlink '../' . $client->{id}, "webdav/links/${name}";
82 }
83
84 sub run {
85   my ($self) = @_;
86
87   # WebDAV not activated? Remove old folders, and we're done.
88   return $self->_unlink_old_folders if !$::lx_office_conf{features}->{webdav};
89
90   # Ensure at least one client exists.
91   $self->_ensure_one_client_exists;
92
93   my $client_to_use;
94   if (1 == scalar @{ $self->clients }) {
95     # Exactly one client? Great, use that one without bothering the
96     # user.
97     $client_to_use = $self->clients->[0];
98
99   } else {
100     # If there's more than one client then let the user select which
101     # client to move the old files into. Maybe she already did?
102     $client_to_use = first { $_->{id} == $::form->{client_id} } @{ $self->clients } if $::form->{client_id};
103
104     if (!$client_to_use) {
105       # Nope, let's select it.
106       print $::form->parse_html_template('dbupgrade/auth/clients_webdav', { SELF => $self, default_client => (first { $_->{is_default} } @{ $self->clients }) });
107       return 2;
108     }
109   }
110
111   # Move files for the selected client.
112   $self->_move_files_into($client_to_use);
113
114   # Create the directory structures for all (even the selected client
115   # -- folders might be missing).
116   for (@{ $self->clients }) {
117     $self->_create_folders($_);
118     $self->_create_symlink($_);
119   }
120
121   return 1;
122 }
123
124 1;