Advertisement

Add User Manually via php

For Joomla! 1.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Locked
User avatar
nkatt
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Sat Jul 26, 2008 4:45 am
Location: Vancouver Island, Canada

Add User Manually via php

Post by nkatt » Wed Sep 17, 2008 6:14 pm

Hi there,

I am an experienced php coder, but new to Joomla. I'm considering using Joomla for an upcoming project in which an organization needs a membership database, but also wants to have a login for each member so that they can login and view private pages. My hope is to extend the JUser object to add a bunch of custom fields (using something like the User Meta).

I was then thinking that I would create a custom component with a nice user interface for the organizations administrator to update/add members to the membership database. When they add a new member a user account would automatically be created for that member.

Sorry if this is a simple question, but I basically just want to confirm that this type of approach is possible before I go ahead and accept the project. If I create a component for the membership administration, can I manually create new Joomla users via php?

Thanks in advance,

nkatt

Advertisement
prateekshaweb
Joomla! Explorer
Joomla! Explorer
Posts: 331
Joined: Mon Aug 04, 2008 8:00 am
Location: India
Contact:

Re: Add User Manually via php

Post by prateekshaweb » Fri Sep 19, 2008 4:05 am

dear nkatt

1. yes.. you can... a lot of components extend the user... namely virutemart for one...

2. check out this book... which has a chapter on extending the user
Learning Joomla! 1.5 Extension Development: Creating Modules, Components, and Plugins with PHP (Paperback) Packt Pub

3. however you should avoid making changes to the user table.. it will cause problems... instead create a new table jos_user_info

4. instead of rewriting the entire component you can use an existing component and hack into it... the component is called com_user in the component directory.

sumeet
Sumeet Shroff
PSD to Joomla, Joomla Custom Template Design and Virtuemart Shopping Cart
http://www.prateeksha.com http://www.joomlawebsitedesigners.com

prateekshaweb
Joomla! Explorer
Joomla! Explorer
Posts: 331
Joined: Mon Aug 04, 2008 8:00 am
Location: India
Contact:

Re: Add User Manually via php

Post by prateekshaweb » Fri Sep 19, 2008 4:08 am

the book is
Mastering Joomla! 1.5 Extension and Framework Development by James Kennard - Packt Publisher

sumeet

Mod note: Please add signature to your profile and not to each post.
Sumeet Shroff
PSD to Joomla, Joomla Custom Template Design and Virtuemart Shopping Cart
http://www.prateeksha.com http://www.joomlawebsitedesigners.com

User avatar
nkatt
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Sat Jul 26, 2008 4:45 am
Location: Vancouver Island, Canada

Re: Add User Manually via php

Post by nkatt » Wed Sep 24, 2008 12:12 am

Great, thanks for the info, I think I'm starting to get a handle on this :)

k98514700
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Fri Oct 26, 2007 2:11 pm

Re: Add User Manually via php

Post by k98514700 » Fri Jan 02, 2009 1:29 am

prateekshaweb wrote:however you should avoid making changes to the user table.. it will cause problems
As I often need to bulk add users,
I add users by manipulating these 3 tables :

users
core_acl_aro
core_acl_groups_aro_map

It seems to be working fine. However as you mentioned, there might be problems.
What are the potential problems please?

Thanks! :p

prateekshaweb
Joomla! Explorer
Joomla! Explorer
Posts: 331
Joined: Mon Aug 04, 2008 8:00 am
Location: India
Contact:

Re: Add User Manually via php

Post by prateekshaweb » Fri Jan 02, 2009 2:54 am

well....

the potential problems i see is that

1. u will have to change the table structure wherever user table is used. frontend and administration...

2. when u upgrade Joomla..... your hacked files will be overwritten.

sumeet
Sumeet Shroff
PSD to Joomla, Joomla Custom Template Design and Virtuemart Shopping Cart
http://www.prateeksha.com http://www.joomlawebsitedesigners.com

User avatar
cbridges
Joomla! Intern
Joomla! Intern
Posts: 74
Joined: Tue Jun 10, 2008 10:57 am
Location: Dallas, TX (since Feb 09) - Prev London, UK

Re: Add User Manually via php

Post by cbridges » Mon Apr 19, 2010 11:12 am

I have a php file that receives user data via a URL string and I insert name, email, password, username, usertype, gid and registerDate to jos_users. This works perfectly and I can see the inserted data when I use phpadmin.

However, and as you probably know, I cannot login using the inserted details and the user does not appear in the user manager in the Joomla backend.

So, my question is, what do I need to add to 'core_acl_aro' & 'core_acl_groups_aro_map' so that the new user is successfully generated and can login?

Cheers

Code: Select all

$insertFields = "INSERT INTO jos_users(name, email, password, username, usertype, gid, registerDate) VALUES('$name','$email', '$password2', '$email', '$usertype', '$gid', '$registerDate')"
;
k98514700 wrote:
prateekshaweb wrote:however you should avoid making changes to the user table.. it will cause problems
As I often need to bulk add users,
I add users by manipulating these 3 tables :

users
core_acl_aro
core_acl_groups_aro_map

It seems to be working fine. However as you mentioned, there might be problems.
What are the potential problems please?

Thanks! :p
Colin Bridges
"Iced Tea with a bucket full of Sugar Please!"

Gowzancha
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Tue Feb 23, 2010 9:37 pm

Re: Add User Manually via php

Post by Gowzancha » Wed May 26, 2010 10:11 am

cbridges! i hv exactlly the same prb! did u solve it??

steve7642
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Mon Jul 20, 2009 10:05 pm

Re: Add User Manually via php

Post by steve7642 » Wed Aug 04, 2010 9:08 am

thanks, enough info to solve this problem, to be more explicit..
1. Create the user record transforming the password with php's md5 function.
2. Create the core_acl_aro record using the new user id
3. Create the core_acl_groups_aro_map record using the new id in cor_acl_aro and the value of group_id depending what joomla role is desired ( ie Super admin, registered, etc) .

PenguinMan98
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Fri Aug 13, 2010 11:19 pm

Re: Add User Manually via php

Post by PenguinMan98 » Fri Aug 13, 2010 11:22 pm

use PHP's md5() function to encrypt the password

Joomla Users Table:

1. jos_users
2. jos_core_acl_aro
3. jos_core_acl_groups_aro_map

Adding new registered user:

1. Insert into jos_users
- fields are : id( = autoincrement field), username, name, email,
password, gid (18)
- these are compulsory fields to fillup.
2. Insert into jos_core_acl_aro
-fields are : aro_id ( = autoincrement field),
section_value( ”users” ), value(=jos_users.id), name=(=jos_users.name)
3. Insert into jos_core_acl_groups_aro_map
-fields are : group_id ( 18 ), aro_id( =jos_core_acl_aro.aro_id)

I hope that's clear enough. '18' is the registered user.

roberto_sm822
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Mon Apr 30, 2012 6:55 pm

Re: Add User Manually via php

Post by roberto_sm822 » Mon Apr 30, 2012 7:01 pm

Inserindo um novo usuário no Joomla 2.5:

1º - Inserir dados de usuários na tabela #__users:

INSERT INTO `#__users` (
`id` ,
`name` ,
`username` ,
`email` ,
`password` ,
`usertype` ,
`block` ,
`sendEmail` ,
`registerDate` ,
`lastvisitDate` ,
`activation` ,
`params`
)
VALUES (
NULL , 'TesteInsercao', 'testando', 'testando@teste.com.br', 'a49366b0d33a0ead9e7b9066bce2a72a:5H2CzNy3mxx5qTPIikVibZlbjeggSEsr', 'deprecated', '0', '1', '2012-04-30 15:12:00', '0000-00-00 00:00:00', '0', '{"editor":"jce","timezone":"","language":"pt-BR","admin_style":"","admin_language":"pt-BR","helpsite":""}'
);


2º - Varrer base para capturar o ID deste usuário recem cadastrado:

"SELECT id, username FROM #__users WHERE email = 'testando@teste.com.br'";
(Neste exemplo retornou "44")

3º - Inserir na tabela #__user_usergroup_map

INSERT INTO `#__user_usergroup_map` (`user_id`, `group_id`) VALUES ('45', '19');
//Observando que o parametro de grupo de usuário registrado agora é "1" (antes no 1.5 era 19)
//basta consultar tabela de grupos #__usergroups

Um abraço :)

sovainfo
Joomla! Exemplar
Joomla! Exemplar
Posts: 8808
Joined: Sat Oct 01, 2011 7:06 pm

Re: Add User Manually via php

Post by sovainfo » Tue May 01, 2012 9:59 am

The code above will not work because of different user_id's (44 & 45). Combining 2 and 3 will resolve problem with different id's (44 & 45).

Code: Select all

INSERT INTO `#__user_usergroup_map` (`user_id`, `group_id`)
VALUES ((SELECT id FROM #__users WHERE email = 'testando@teste.com.br'), (SELECT id FROM #__usergroups WHERE title = 'Registered'));
This will insert the user as Registered and not as Super user(19) as done above.
Issue with migrating? Include logs/joomla_update.php in your report!
Blank screen? Verify pagesource for HTML code (javascript error)
Installation failing on populating database? Install with set_time_limit(0)
Document your customizations!

fade2clear
Joomla! Apprentice
Joomla! Apprentice
Posts: 11
Joined: Mon Mar 09, 2009 11:43 pm

Add User Manually via php VIA API THE RIGHT WAY

Post by fade2clear » Fri Jul 06, 2012 6:34 pm

If you care about using the native api when creating a user then you can use this script.

I took it directly from com_users. For some reason the password comparison (does pass = passver) was in the com_users controller but the rest of the save function in the model. Here is what I put together...

call it like this in your view:

Code: Select all

$model = &$this->getModel();
$status = $model->makeAccount($data);
$data is a variable that contains all the info:

Code: Select all

$data = array('name' => 'ety' ,'username' => 'testing' ,'password' => '','password2' => '','email' => 'rty@dtydfg.gyy' ,'registerDate' => '','lastvisitDate' => '','lastResetTime' => '','resetCount' => 0 ,'sendEmail' => 0 ,'block' => 1 ,'id' => 0 ,'groups' => array( 0 => 2 ) ,'params' => array( 'admin_style' => '','admin_language' => '','language' => '','editor' => '','helpsite' => '','timezone' => '') ) ;
The function goes in your model:

Code: Select all

public function makeAccount($data)
	{
		if (isset($data['password']) && isset($data['password2']))
		{
			// Check the passwords match.
			if ($data['password'] != $data['password2'])
			{
				$this->setError(JText::_('JLIB_USER_ERROR_PASSWORD_NOT_MATCH'));
				return false;
			}

			unset($data['password2']);
		}
		
		// Initialise variables;
		$pk			= (!empty($data['id'])) ? $data['id'] : (int) $this->getState('user.id');
		$user		= JUser::getInstance($pk);

		$my = JFactory::getUser();

		if ($data['block'] && $pk == $my->id && !$my->block)
		{
			$this->setError(JText::_('COM_USERS_USERS_ERROR_CANNOT_BLOCK_SELF'));
			return false;
		}

		// Make sure that we are not removing ourself from Super Admin group
		$iAmSuperAdmin = $my->authorise('core.admin');
		if ($iAmSuperAdmin && $my->get('id') == $pk)
		{
			// Check that at least one of our new groups is Super Admin
			$stillSuperAdmin = false;
			$myNewGroups = $data['groups'];
			foreach ($myNewGroups as $group)
			{
				$stillSuperAdmin = ($stillSuperAdmin) ? ($stillSuperAdmin) : JAccess::checkGroup($group, 'core.admin');
			}
			if (!$stillSuperAdmin)
			{
				$this->setError(JText::_('COM_USERS_USERS_ERROR_CANNOT_DEMOTE_SELF'));
				return false;
			}
		}

		// Bind the data.
		if (!$user->bind($data))
		{
			$this->setError($user->getError());
			return false;
		}

		// Store the data.
		if (!$user->save())
		{
			$this->setError($user->getError());
			return false;
		}

		$this->setState('user.id', $user->id);

		return true;
	}

Advertisement

Locked

Return to “Joomla! 1.5 Coding”