Welcome to mirror list, hosted at ThFree Co, Russian Federation.

4.11.0-rc2.php « Updates « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 745abe583ab9ac26742d1ba0faf9ad305ffd328f (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
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */

namespace Piwik\Updates;

use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\Db;
use Piwik\Piwik;
use Piwik\Plugins\UsersManager\Emails\UserInviteEmail;
use Piwik\Plugins\UsersManager\Model;
use Piwik\Site;
use Piwik\Updater;
use Piwik\Updater\Migration;
use Piwik\Updater\Migration\Factory as MigrationFactory;
use Piwik\Updates as PiwikUpdates;

/**
 * Update for version 4.11.0-rc2
 */
class Updates_4_11_0_rc2 extends PiwikUpdates
{
    /**
     * @var MigrationFactory
     */
    private $migration;

    private $pendingUsers;

    private $userTable;

    public function __construct(MigrationFactory $factory)
    {
        $this->migration = $factory;
        $this->userTable = Common::prefixTable('user');
    }

    /**
     * @param Updater $updater
     *
     * @return Migration[]
     */
    public function getMigrations(Updater $updater)
    {
        try {
            $this->pendingUsers = Db::fetchAll(
                "SELECT * FROM $this->userTable WHERE invite_status = ? ",
                ['pending']
            );
        } catch (\Exception $e) {
            // ignore any errors. The column might not exist when updating from an older version,
            // so there wouldn't be anything to update anyway
        }
        return [
          $this->migration->db->dropColumn('user', 'invite_status'),
          $this->migration->db->addColumns('user', ['invite_token' => 'VARCHAR(191) DEFAULT null']),
          $this->migration->db->addColumns('user', ['invited_by' => 'VARCHAR(100) DEFAULT null']),
          $this->migration->db->addColumns('user', ['invite_expired_at' => 'TIMESTAMP null DEFAULT null']),
          $this->migration->db->addColumns('user', ['invite_accept_at' => 'TIMESTAMP null DEFAULT null']),
        ];
    }

    public function doUpdate(Updater $updater)
    {
        $updater->executeMigrations(__FILE__, $this->getMigrations($updater));

        $model = new Model();
        if (!empty($this->pendingUsers)) {
            foreach ($this->pendingUsers as $user) {
                $model->deleteAllTokensForUser($user['login']);

                $site = $model->getSitesAccessFromUser($user['login']);
                if (isset($site[0])) {
                    $site = new Site($site[0]['site']);
                    $siteName = $site->getName();
                } else {
                    $siteName = "Default Site";
                }
                //generate Token
                $generatedToken = $model->generateRandomTokenAuth();

                //attach token to user
                $model->attachInviteToken($user['login'], $generatedToken, 7);

                // send email
                $email = StaticContainer::getContainer()->make(UserInviteEmail::class, [
                  'currentUser'  => Piwik::getCurrentUserLogin(),
                  'invitedUser'  => $user,
                  'siteName'     => $siteName,
                  'token'        => $generatedToken,
                  'expiryInDays' => 7
                ]);
                $email->safeSend();
            }
        }
    }
}