summaryrefslogtreecommitdiffstats
path: root/modules/migrate/application/clicommands/ConfigCommand.php
blob: a5be144a152c6dfe88443321c1033474b5c3cf99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/* Icinga Web 2 | (c) 2017 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Migrate\Clicommands;

use Icinga\Cli\Command;
use Icinga\Module\Migrate\Config\UserDomainMigration;
use Icinga\User;
use Icinga\Util\StringHelper;

class ConfigCommand extends Command
{
    /**
     * Rename users and user configurations according to a given domain
     *
     * The following configurations are taken into account:
     *      - Announcements
     *      - Preferences
     *      - Dashboards
     *      - Custom navigation items
     *      - Role configuration
     *      - Users and group memberships in database backends, if configured
     *
     * USAGE:
     *
     *  icingacli migrate config users [options]
     *
     * OPTIONS:
     *
     *  --to-domain=<to-domain>         The new domain for the users
     *
     *  --from-domain=<from-domain>     Migrate only the users with the given domain.
     *                                  Use this switch in combination with --to-domain.
     *
     *  --user=<user>                   Migrate only the given user in the format <user> or <user@domain>
     *
     *  --map-file=<mapfile>            File to use for renaming users
     *
     *  --separator=<separator>         Separator for the map file
     *
     * EXAMPLES:
     *
     *  icingacli migrate config users ...
     *
     *  Add the domain "icinga.com" to all users:
     *
     *      --to-domain icinga.com
     *
     *  Set the domain "example.com" on all users that have the domain "icinga.com":
     *
     *      --to-domain example.com --from-domain icinga.com
     *
     *  Set the domain "icinga.com" on the user "icingaadmin":
     *
     *      --to-domain icinga.com --user icingaadmin
     *
     *  Set the domain "icinga.com" on the users "icingaadmin@icinga.com"
     *
     *      --to-domain example.com --user icingaadmin@icinga.com
     *
     *  Rename users according to a map file:
     *
     *      --map-file /path/to/mapfile --separator :
     *
     * MAPFILE:
     *
     *  You may rename users according to a given map file. The map file must be separated by newlines. Each line then
     *  is specified in the format <from><separator><to>. The separator is specified with the --separator switch.
     *
     *  Example content:
     *
     *      icingaadmin:icingaadmin@icinga.com
     *      jdoe@example.com:jdoe@icinga.com
     *      rroe@icinga:rroe@icinga.com
     */
    public function usersAction()
    {
        if ($this->params->has('map-file')) {
            $mapFile = $this->params->get('map-file');
            $separator = $this->params->getRequired('separator');

            $source = trim(file_get_contents($mapFile));
            $source = StringHelper::trimSplit($source, "\n");

            $map = array();

            array_walk($source, function ($item) use ($separator, &$map) {
                list($from, $to) = StringHelper::trimSplit($item, $separator, 2);
                $map[$from] = $to;
            });

            $migration = UserDomainMigration::fromMap($map);
        } else {
            $toDomain = $this->params->getRequired('to-domain');
            $fromDomain = $this->params->get('from-domain');
            $user = $this->params->get('user');

            if ($user === null) {
                $migration = UserDomainMigration::fromDomains($toDomain, $fromDomain);
            } else {
                if ($fromDomain !== null) {
                    $this->fail(
                        "Ambiguous arguments: Can't use --user in combination with --from-domain."
                        . " Please use the user@domain syntax for the --user switch instead."
                    );
                }

                $user = new User($user);

                $migrated = clone $user;
                $migrated->setDomain($toDomain);

                $migration = UserDomainMigration::fromMap(array($user->getUsername() => $migrated->getUsername()));
            }
        }

        $migration->migrate();
    }
}