Adding Multiple Users Adding Multiple Users
 

News:

cpg1.5.48 Security release - upgrade mandatory!
The Coppermine development team is releasing a security update for Coppermine in order to counter a recently discovered vulnerability. It is important that all users who run version cpg1.5.46 or older update to this latest version as soon as possible.
[more]

Main Menu

Adding Multiple Users

Started by Sam Rajan, June 21, 2012, 11:36:27 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Sam Rajan

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?

Αndré

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.

Sam Rajan

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?

Αndré

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?

cottage

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!


Αndré

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).

cottage

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?

Αndré

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:
<?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.

cottage

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).

Αndré

In your example "124" is the actual data for storing multiple groups? If so, which is the primary group? I guess the first digit?

cottage

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.

Αndré

Quote from: cottage on March 05, 2013, 08:43:37 PM
I thought that 124 would be a user assigned to the group IDs 1,2 and 4
No. Sorry for the confusion.


Quote from: cottage on March 05, 2013, 08:43:37 PM
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):
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:
<?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";
?>

cottage

Absolutely brilliant!!!!

Thanks for taking the time to do this!

Αndré