forum.coppermine-gallery.net

Support => cpg1.5.x Support => cpg1.5 miscellaneous => Topic started by: Sam Rajan on June 21, 2012, 11:36:27 am

Title: Adding Multiple Users
Post by: Sam Rajan on June 21, 2012, 11:36:27 am
I need to add multiple users (150+) to the gallery, is there a way I can do this using a csv file or is there a plugin that can help me?
Title: Re: Adding Multiple Users
Post by: Αndré on June 21, 2012, 12:48:54 pm
As far as I know such a feature or plugin doesn't exist. But you could use an SQL query or write a simple PHP script that creates them automatically.
Title: Re: Adding Multiple Users
Post by: Sam Rajan on June 21, 2012, 11:16:22 pm
Thanks for your reply.

I am new to this any chance someone out there can help me with how to go about putting a sql query together or have a php script that i could use?
Title: Re: Adding Multiple Users
Post by: Αndré on June 22, 2012, 08:51:20 am
Please post some details about user names, email addresses and passwords and maybe some other data which has to be assigned to each user. I assume you already have a CSV list?
Title: Re: Adding Multiple Users
Post by: cottage on February 26, 2013, 02:38:13 pm
Hi Andre,

I am up against the same challenge as Sam had raised.

Would you be able to assist - I'm not sure where the discussion above ever ended up. I'd be able to rapidly produce a CSV if that facilitates the process, and would want to create the accounts using the bare minimum information (I imagine: First name, surname, email address, username, password?).

Any help would be much appreciated!

Title: Re: Adding Multiple Users
Post by: Αndré on February 26, 2013, 03:03:03 pm
I assume first name and surname will be custom user fields? If so, I which number will be each?

If you expect me to create a script, I need the exact structure of your CSV file (field names and used separator).
Title: Re: Adding Multiple Users
Post by: cottage on March 01, 2013, 06:36:51 pm
Hi Andre,

Apologies for the late response... honeymoons don't allow for much forum participation :-)!

I'd be looking at commas as separators and the following fields (let's drop First Name/Surname): Username,Password,Email,UserIsActive,PrimaryGroup

For more generic use (I imagine there may be other users for this at a later stage and it would be a great download on the plugins page?), I'm not sure whether one could also cater for Secondary Groups assignments. Potentially one could create new fields for each secondary groups and then use a binary indicator (0,1) as values? I'd imagine the same approach of binary indicator could apply to UserIsActive?
Title: Re: Adding Multiple Users
Post by: Αndré on March 05, 2013, 02:40:04 pm
Here's a very basic script that fits your individual needs. It doesn't check or verify anything. Just create a new PHP file at your gallery root and insert the following code:
Code: [Select]
<?php
define
('IN_COPPERMINE'true);
require_once (
'include/init.inc.php');
foreach (
explode("\n"file_get_contents('userlist.csv')) as $line) {
    
$fields explode(","trim($line));
    
cpg_db_query("INSERT IGNORE INTO {$CONFIG['TABLE_USERS']} (user_name, user_password, user_email, user_active, user_group) VALUES ('{$fields[0]}', '".md5($fields[1])."', '{$fields[2]}', '{$fields[3]}', '{$fields[4]}')");
}
echo 
"Done";
?>

The script searches for the file "userlist.csv" in the gallery root, which contains just data rows (no column name rows etc.), e.g.
Code: (userlist.csv) [Select]
foo,bar,foo@bar.com,YES,123
bar,foo,bar@foo.com,NO,1234

Finally, visit the new PHP file with your browser.
Title: Re: Adding Multiple Users
Post by: cottage on March 05, 2013, 06:33:21 pm
Hi Andre, thanks a tonne for that!

I'm having one slight problem, which is that the user group is not register as individual digits, but interpreted as a single number. I.e. if a user has 124 as user groups, the user's group is added as group #124 instead of #1, #2, and #4.

It also means (at least I suspect that this is the reason) that the added users are not shown in the user management interface (although they do appear in the user table).
Title: Re: Adding Multiple Users
Post by: Αndré on March 05, 2013, 07:57:57 pm
In your example "124" is the actual data for storing multiple groups? If so, which is the primary group? I guess the first digit?
Title: Re: Adding Multiple Users
Post by: cottage on March 05, 2013, 08:43:37 pm
So... maybe I interpreted your sample csv data incorrectly. I thought that 124 would be a user assigned to the group IDs 1,2 and 4, with the first digit indeed the primary group. Effectively, this interpretation limits the assignable groups to 9 (all single digit group IDs).

Would the above be doable? It would suit my purposes extremely well.

Otherwise, I'd stick to only assigning a single group to every user.
Title: Re: Adding Multiple Users
Post by: Αndré on March 06, 2013, 09:27:46 am
I thought that 124 would be a user assigned to the group IDs 1,2 and 4
No. Sorry for the confusion.


Would the above be doable? It would suit my purposes extremely well.
Yes, but I suggest to use a slightly different approach. Instead of concatenating the user group IDs without a separator you should divide the primary group and the additional groups into separate fields and use a separator for the additional groups.

Example CSV file (this time with group IDs that hopefully don't cause any confusion):
Code: [Select]
user_without_additional_groups,password1,foo1@bar.com,YES,1
user_with_one_additional_group,password2,foo2@bar.com,NO,2,4
user_with_multiple_additional_groups,password3,foo3@bar.com,YES,3,5/7/9/12

New code for PHP file will be:
Code: [Select]
<?php
define
('IN_COPPERMINE'true);
require_once (
'include/init.inc.php');
foreach (
explode("\n"file_get_contents('userlist.csv')) as $line) {
    
$fields explode(","trim($line));
    
cpg_db_query("INSERT IGNORE INTO {$CONFIG['TABLE_USERS']} (user_name, user_password, user_email, user_active, user_group, user_group_list) VALUES ('{$fields[0]}', '".md5($fields[1])."', '{$fields[2]}', '{$fields[3]}', '{$fields[4]}', '".str_replace('/'','$fields[5])."')");
}
echo 
"Done";
?>
Title: Re: Adding Multiple Users
Post by: cottage on March 06, 2013, 04:28:31 pm
Absolutely brilliant!!!!

Thanks for taking the time to do this!
Title: Re: Adding Multiple Users
Post by: Αndré on March 06, 2013, 05:08:53 pm
Marking as solved.