benedmunds / CodeIgniter-Ion-Auth

Simple and Lightweight Auth System for CodeIgniter
http://benedmunds.com/ion_auth/
MIT License
2.35k stars 1.14k forks source link

implementation transaction in create user #1538

Closed junglaCODE closed 2 years ago

junglaCODE commented 2 years ago

First of all an apology for not doing it for merge, but I am making several changes and the branches have a lot of conflicts, I hope I have it well with these changes.

Note that when there is a failure when creating a user, it is attached anyway, so I decided to put a transaction to ensure that the insert is correct

/**
     * Register (create) a new user
     *
     * @param string $identity       This must be the value that uniquely identifies the user when he is registered
     * @param string $password       Password
     * @param string $email          Email
     * @param array  $additionalData Multidimensional array
     * @param array  $groups         If not passed the default group name set in the config will be used
     *
     * @return integer|boolean
     * @author Mathew
     * @author JLGC/monolinux
     */
    public function register(string $identity, string $password, string $email, array $additionalData=[], array $groups=[])
    {
        $this->db->transBegin();
        $this->triggerEvents('pre_register');
        $manualActivation = $this->config->manualActivation;

        if ($this->identityCheck($identity))
        {
            $this->setError('IonAuth.account_creation_duplicate_identity');
            return false;
        }
        else if (! $this->config->defaultGroup && empty($groups))
        {
            $this->setError('IonAuth.account_creation_missing_defaultGroup');
            return false;
        }

        // check if the default set in config exists in database
        $query = $this->db->table($this->tables['groups'])->where(['name' => $this->config->defaultGroup], 1)->get()->getRow();
        if (! isset($query->id) && empty($groups))
        {
            $this->setError('IonAuth.account_creation_invalid_defaultGroup');
            return false;
        }

        // capture default group details
        $defaultGroup = $query;

        // IP Address
        $ipAddress = \Config\Services::request()->getIPAddress();

        // Do not pass $identity as user is not known yet so there is no need
        $password = $this->hashPassword($password);

        if ($password === false)
        {
            $this->setError('IonAuth.account_creation_unsuccessful');
            return false;
        }

        // Users table.
        $data = [
            $this->identityColumn => $identity,
            'username'            => $identity,
            'password'            => $password,
            'email'               => $email,
            'ip_address'          => $ipAddress,
            'created_on'          => time(),
            'active'              => ($manualActivation === false ? 1 : 0),
        ];

        // filter out any data passed that doesnt have a matching column in the users table
        // and merge the set user data and the additional data
        $userData = array_merge($this->filterData($this->tables['users'], $additionalData), $data);

        $this->triggerEvents('extra_set');

        $this->db->table($this->tables['users'])->insert($userData);

        $id = $this->db->insertId($this->tables['users'] . '_id_seq');

        // add in groups array if it doesn't exists and stop adding into default group if default group ids are set
        if (isset($defaultGroup->id) && empty($groups))
        {
            $groups[] = $defaultGroup->id;
        }

        if (! empty($groups))
        {
            // add to groups
            foreach ($groups as $group)
            {
                $this->addToGroup($group, $id);
            }
        }
        $this->triggerEvents('post_register');
        if ($this->db->transStatus() === FALSE):
            $this->db->transRollback();
            return false;
        else:
            $this->db->transCommit();
            return $id;
        endif;
    }
benedmunds commented 2 years ago

This makes sense but there does appear to be syntax errors. Closing for now, feel free to open a PR for it sometime.