@sgoeting opened this Issue on March 6th 2020

Trying to add a new users results in error-message:
Integrity constraint violation: 1062 Duplicate entry '' for key unique_user_id'

This happens after adding a first initial user.

Inspecting the database (users-table) it occurs that the user_id column is not initiated with a value other than an empty string ''.

Inspecting the code in /path/to/matomo/plugins/UsersManager/Model.php function 'addUser' no proper initialization op the user_id property has taken place:

public function addUser($userLogin, $hashedPassword, $email, $alias, $tokenAuth, $dateRegistered)
    {
        $user = array(
            'login'            => $userLogin,
            'password'         => $hashedPassword,
            'alias'            => $alias,
            'email'            => $email,
            'token_auth'       => $tokenAuth,
            'date_registered'  => $dateRegistered,
            'superuser_access' => 0,
            'ts_password_modified' => Date::now()->getDatetime(),
        );

        $db = $this->getDb();
        $db->insert($this->table, $user);
    }

changing this code to:

    // SOURCE :: https://www.php.net/manual/en/function.com-create-guid.php
    private function GUID()
    {
        if (function_exists('com_create_guid') === true)
        {
            return trim(com_create_guid(), '{}');
        }

        return sprintf( '%04X%04X-%04X-%04X-%04X-%04X%04X%04X',
                        mt_rand(0, 65535),
                        mt_rand(0, 65535),
                        mt_rand(0, 65535),
                        mt_rand(16384, 20479),
                        mt_rand(32768, 49151),
                        mt_rand(0, 65535),
                        mt_rand(0, 65535),
                        mt_rand(0, 65535));
    }

    public function addUser($userLogin, $hashedPassword, $email, $alias, $tokenAuth, $dateRegistered)
    {
        $user = array(
            'login'            => $userLogin,
            'password'         => $hashedPassword,
            'alias'            => $alias,
            'email'            => $email,
            'token_auth'       => $tokenAuth,
            'user_id'          => $this->GUID(),
            'date_registered'  => $dateRegistered,
            'superuser_access' => 0,
            'ts_password_modified' => Date::now()->getDatetime(),
        );

        $db = $this->getDb();
        $db->insert($this->table, $user);
    }

results in expected behaviour.

@pebosi commented on March 6th 2020 Contributor

My installation has no column called "user_id".
Please add more information, PHP, MySQL, etc.

@sgoeting commented on March 6th 2020

Using a Matomo fresh install from matomo:latest (ie. 3.13.3) but database is migrated from a PiwikPro database and afterwards upgraded. So maybe that is a issue.
I'am new to both products so I am not familiar with what is legacy and what not and maybe that user_id-col is related to old PiwikPro-stuff :-)

For the record: Host is a redhat 7.3 machine with PHP 7.3.11 installed and using MariaDB 10.2.22 as a backend.

@pebosi commented on March 6th 2020 Contributor

i'm pretty sure the migration was the problem :) try removing the column user_id in the users table. Backup first :)

@mattab commented on March 7th 2020 Member

You may find witch plug-in defines this field by grepping from the string user_id in the plugins/ folder in matomo.

This Issue was closed on March 7th 2020
Powered by GitHub Issue Mirror