user_add and fun with newest_user_colour

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
User avatar
barrycarlyon
Registered User
Posts: 851
Joined: Wed Mar 29, 2006 3:46 pm
Location: Manchester
Name: Barry Carlyon
Contact:

user_add and fun with newest_user_colour

Post by barrycarlyon » Sun Apr 15, 2018 12:34 pm

This block of code in functions_user.php

Code: Select all

	if ($user_row['user_type'] == USER_NORMAL || $user_row['user_type'] == USER_FOUNDER)
	{
		$config->set('newest_user_id', $user_id, false);
		$config->set('newest_username', $user_row['username'], false);
		$config->increment('num_users', 1, false);

		$sql = 'SELECT group_colour
			FROM ' . GROUPS_TABLE . '
			WHERE group_id = ' . (int) $user_row['group_id'];
		$result = $db->sql_query_limit($sql, 1);
		$row = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);

		$config->set('newest_user_colour', $row['group_colour'], false);
	}
Combined with a user_add along the lines of

Code: Select all

                    $user_row = array(
                        'username'              => SOMEUSERNAME,
                        'user_password'         => phpbb_hash(time()),
                        'user_email'            => SOMEEMAIL,
                        'group_id'              => 2,// registered to start with
                        'user_timezone'         => 'UTC',
                        'user_lang'             => 'en',
                        'user_type'             => USER_NORMAL,
                        'user_ip'               => $this->user->ip,
                        'user_regdate'          => time(),
                    }

                    $user_id = user_add($user_row);
Someusername/someemail passed in from third party authentication loop.

Worked fine under development, but when we launched a new forum using this, a number of people, appeared to register at the same time and get a sql_error along the lines of "duplicate entry for newest_user_colour" in the config table. Seems the $config->set in the posted group colour code is throwing a insert instead of an update.

Adding a colour to the $user_row and/or to the group(s) has not helped.

Currently I'm operating with the $config->set commented out and seems to have solved the issue. But if anyone has any thoughts, or even if this is a bug in phpBB at all. I'd entertain some responses!
Barry Carlyon

User avatar
posey
Registered User
Posts: 706
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: user_add and fun with newest_user_colour

Post by posey » Sun Apr 15, 2018 2:43 pm

I might be missing the point here, but it looks like you just want to grab the latest registered user?
Cause the way the code is currently coded, the newest_user_colour is always set to the group colour of the group with the identifier 2.

To easily display the latest registered user you can do:

Code: Select all

$sql = 'SELECT user_id, username, user_colour 
	FROM ' . USERS_TABLE . ' 
	WHERE group_id = 2
	ORDER BY user_regdate DESC';
$this->db->sql_query_limit($sql, 1);
$latest_user_row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
And if you want to only get users registered through the custom form you're talking about, it might be easier to add an additional column to the users table that is a bool, whether the custom form was used and use that column in the where clause.
''I'm pretty sure there's a lot more to life than being really, really, ridiculously good looking. And I plan on finding out what that is.''

User avatar
barrycarlyon
Registered User
Posts: 851
Joined: Wed Mar 29, 2006 3:46 pm
Location: Manchester
Name: Barry Carlyon
Contact:

Re: user_add and fun with newest_user_colour

Post by barrycarlyon » Sun Apr 15, 2018 2:50 pm

I might be missing the point here, but it looks like you just want to grab the latest registered user?
Yes you have.

I am attempting to register new users.

The result is often "SQL_ERROR Duplicate Key newest_user_colour"
Barry Carlyon

User avatar
3Di
Registered User
Posts: 12887
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: user_add and fun with newest_user_colour

Post by 3Di » Sun Apr 15, 2018 9:56 pm

barrycarlyon wrote:
Sun Apr 15, 2018 2:50 pm
I am attempting to register new users.
There is already the extension Add User which does that, if you want to lurk at the code.. anyway:

The function user_add automatically assigns the related color of the group of choise, ie.:
'group_id' => 2,// registered to start with

Code: Select all

                    $user_row = array(
                        'username'              => SOMEUSERNAME,
                        'user_password'         => phpbb_hash(time()),
                        'user_email'            => SOMEEMAIL,
                        'group_id'              => 2,// registered to start with
                        'user_timezone'         => 'UTC',
                        'user_lang'             => 'en',
                        'user_type'             => USER_NORMAL,
                        'user_ip'               => $this->user->ip,
                        'user_regdate'          => time(),
                    }
with the native code

Code: Select all

	// Place into appropriate group...
	$sql = 'INSERT INTO ' . USER_GROUP_TABLE . ' ' . $db->sql_build_array('INSERT', array(
		'user_id'		=> (int) $user_id,
		'group_id'		=> (int) $user_row['group_id'],
		'user_pending'	=> 0)
	);
	$db->sql_query($sql);
then does some other stuffs and finally:

Code: Select all

	// set the newest user and adjust the user count if the user is a normal user and no activation mail is sent
	if ($user_row['user_type'] == USER_NORMAL || $user_row['user_type'] == USER_FOUNDER)
	{
		$config->set('newest_user_id', $user_id, false);
		$config->set('newest_username', $user_row['username'], false);
		$config->increment('num_users', 1, false);

		$sql = 'SELECT group_colour
			FROM ' . GROUPS_TABLE . '
			WHERE group_id = ' . (int) $user_row['group_id'];
		$result = $db->sql_query_limit($sql, 1);
		$row = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);

		$config->set('newest_user_colour', $row['group_colour'], false);
	}
Want to compensate me for my interest? Donate
Please PM me only to request paid works. Thx.
Extensions, Scripts, MOD porting, Update/Upgrades
My development's activity º PhpStorm's proud user

User avatar
barrycarlyon
Registered User
Posts: 851
Joined: Wed Mar 29, 2006 3:46 pm
Location: Manchester
Name: Barry Carlyon
Contact:

Re: user_add and fun with newest_user_colour

Post by barrycarlyon » Mon Apr 16, 2018 3:36 pm

Yes. My code does basically what the add user ext does

The problem is that when two or more people are user_add'ed at the same time the result is:

Code: Select all

 "duplicate entry for newest_user_colour"
Again user_add is working fine with the assigned groups untile the lines of

Code: Select all

 // set the newest user and adjust the user count if the user is a normal user and no activation mail is sent
 ~snip~
 $config->set('newest_user_colour', $row['group_colour'], false);
Which results in

Code: Select all

duplicate entry for newest_user_colour
And crashes out with a SQL error.

I'm really not sure how I can make the problem more clear…

When a user_add occurs via my extension the result is

Code: Select all

duplicate entry for newest_user_colour
due to

Code: Select all

 $config->set('newest_user_colour', $row['group_colour'], false);
Performing an insert query instead of a update query.
Barry Carlyon

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3095
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: user_add and fun with newest_user_colour

Post by kasimi » Mon Apr 16, 2018 8:23 pm

What seems most likely to me is that your $config object isn't initialized properly, either because of wrong code or a stale cache. Are you accessing $config via dependency injection? Have you tried clearing your board cache?

Instead of editing core code, add this line before your call to user_add() as a temporary fix in your extension code:

Code: Select all

$this->config['newest_user_colour'] = '';

User avatar
barrycarlyon
Registered User
Posts: 851
Joined: Wed Mar 29, 2006 3:46 pm
Location: Manchester
Name: Barry Carlyon
Contact:

Re: user_add and fun with newest_user_colour

Post by barrycarlyon » Mon Apr 16, 2018 8:36 pm

Hmm…

That could be it.

My function in ext/me/module/service/thing.php doesn't grab $config at all.

It literally:

Code: Select all

        include_once('includes/functions_user.php');

/* misc bits and pieces/some cURL calls */

                    $user_row = array(
                        'username'              => SOMEUSERNAME,
                        'user_password'         => phpbb_hash(time()),
                        'user_email'            => SOMEEMAIL,
                        'group_id'              => 2,// registered to start with
                        'user_timezone'         => 'UTC',
                        'user_lang'             => 'en',
                        'user_type'             => USER_NORMAL,
                        'user_ip'               => $this->user->ip,
                        'user_regdate'          => time(),
                    }

                    $user_id = user_add($user_row);

                    if ($user_id === false) {
                        // should not happen but just in case
                        trigger_error('NO_USER', E_USER_ERROR);
                    } else {
                        // user group initial
                        foreach ($group_ids as $group_id) {
                            group_user_add($group_id, array($user_id));
                        }

                        $this->_group_update($user_id, $group_ids);
                        $this->_update_profile_fields($user_id, $r);

                        // user created
                        // perform login
                        $this->user->session_begin();
                        $result = $this->user->session_create($user_id, false, true, true);

                        global $phpbb_root_path, $phpEx;
                        $redirect = request_var('redirect', "{$phpbb_root_path}index.$phpEx");;
                        redirect($redirect);

                        exit;
                    }
                    
So I need to add $config to my service via dep. injection (even though I'm not calling if, the user_add function is)?
Barry Carlyon

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3095
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: user_add and fun with newest_user_colour

Post by kasimi » Tue Apr 17, 2018 7:59 pm

You don't need to inject $config if you're not using it yourself.

It sounds like you're not actually writing an extension, or you're calling your thing.php script directly, which is not how extension scripts are supposed to be used. You should use a controller and a route. https://area51.phpbb.com/docs/dev/3.2.x ... llers.html

User avatar
3Di
Registered User
Posts: 12887
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: user_add and fun with newest_user_colour

Post by 3Di » Tue Apr 17, 2018 10:00 pm

barrycarlyon wrote:
Mon Apr 16, 2018 8:36 pm
My function in ext/me/module/service/thing.php doesn't grab $config at all.
Look, the simplest way to get further help is to put all of your extension folder on GitHub and link us to it.

Instead if you are bound to hard-code the core code then the right place would be the Custom Coding Forum for that.
barrycarlyon wrote:
Mon Apr 16, 2018 3:36 pm
And crashes out with a SQL error.
Well, the grab of that whole error screen will help somehow, a simple statement does not.
duplicate entry for newest_user_colour
Are you using an array of users there or single user?
Want to compensate me for my interest? Donate
Please PM me only to request paid works. Thx.
Extensions, Scripts, MOD porting, Update/Upgrades
My development's activity º PhpStorm's proud user

User avatar
barrycarlyon
Registered User
Posts: 851
Joined: Wed Mar 29, 2006 3:46 pm
Location: Manchester
Name: Barry Carlyon
Contact:

Re: user_add and fun with newest_user_colour

Post by barrycarlyon » Tue Apr 17, 2018 10:10 pm

The controller that calls a service

Controller: https://gist.github.com/BarryCarlyon/78 ... 288e9ec660
Service: https://gist.github.com/BarryCarlyon/d3 ... ae7dbc285f

Had to remove the cURL calls and some other NDA/secrets

I can prepare the rest of it into a whole zip but would take a bit to clean out the secrets/NDA stuff I can't share

The response users are presented with is the standard phpBB SQL error page with the message "duplicate key 'newest_user_color'" they get a "An error occured during Registration." on the second attempt as the username is taken at some of the logic breaks due to the SQL error. (when the SQL error occurs it blocks my code finishing doing profile fields, and as you can see I'm attempting to find a user by profile field first (admin only field))

Basically it's an oAuth loop and process the response and create a user based on the response. Just getting a weird trip over the newest_user_color config set call, which didn't surface till going live with the forum, and I'm not even calling that call myself directly.
Barry Carlyon

User avatar
3Di
Registered User
Posts: 12887
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: user_add and fun with newest_user_colour

Post by 3Di » Tue Apr 17, 2018 10:24 pm

I don't think a zipped package is a good idea, nobody's going to download and unzip etc.., instead a good code reading is enough for most of us.
Want to compensate me for my interest? Donate
Please PM me only to request paid works. Thx.
Extensions, Scripts, MOD porting, Update/Upgrades
My development's activity º PhpStorm's proud user

User avatar
barrycarlyon
Registered User
Posts: 851
Joined: Wed Mar 29, 2006 3:46 pm
Location: Manchester
Name: Barry Carlyon
Contact:

Re: user_add and fun with newest_user_colour

Post by barrycarlyon » Tue Apr 17, 2018 10:26 pm

Derp I misread, been a long day, dunno where I got zip from…

I have linked my service and controller. That should be sufficient?

I don't have the stack trace as I can't replicate it in development, it only seems to do it in production where, of course, stack traces are disabled.
Barry Carlyon

User avatar
3Di
Registered User
Posts: 12887
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: user_add and fun with newest_user_colour

Post by 3Di » Tue Apr 17, 2018 10:31 pm

I will take a look there nextly, no promises though.
I am sure somebody else will do the same.

Edit: BTW, I meant the whole extension, not "Acme" gists. :)
Want to compensate me for my interest? Donate
Please PM me only to request paid works. Thx.
Extensions, Scripts, MOD porting, Update/Upgrades
My development's activity º PhpStorm's proud user

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: No registered users and 7 guests