JUser

From Joomla! Documentation
(Difference between revisions)
Jump to: navigation, search
m (removed bracket which was causing syntax error: ata['activation'] =JUtility::getHash( JUserHelper::genRandomPassword() );)
m (bad link repair)
 
(4 intermediate revisions by one user not shown)
Line 1: Line 1:
'''JUser''' is an abstract class which provides a number of methods to handle Users.
+
This class is available in the following Joomla versions:-
 
+
<splist showpath=notparent />
===Availability===
+
<noinclude>[[Category:Platform JClasses]][[Category:JUser]]</noinclude>
{{JVer|1.5}} {{JVer|1.6}}
+
 
+
===Defined in===
+
/libraries/joomla/user/user.php
+
 
+
===Extends===
+
* [[JObject]]
+
 
+
===Get and Set Methods===
+
Properties which do not have specific get or set methods listed here can be retrieved or set using the inherited [[JObject/get|JObject->get]] method.
+
{| class="wikitable"
+
|-
+
!Get method
+
!Set method
+
!Description
+
!Property
+
|-
+
|
+
|[[JUser/setLastVisit|setLastVisit]]
+
|Sets the last visit date
+
|
+
|-
+
|[[JUser/getParam|getParam]]
+
|[[JDocument/setParam|setParam]]
+
|User Param
+
|
+
|-
+
|[[JUser/getParameters|getParameters]]
+
|[[JUser/setParameters|setParameters]]
+
|User Params
+
|
+
|-
+
|[[JUser/getTable|getTable]]
+
|
+
|Gets the user table object
+
|
+
|}
+
 
+
===Other Methods===
+
{| class="wikitable"
+
|-
+
!Method name
+
!Description
+
|-
+
|[[JUser/defParam|defParam]]
+
|Sets a default Param, if it does not exists. See separate reference page for further information.
+
|-
+
|[[JUser/authorize|authorize]]
+
|Checks JUser object authorization against an access control object and optionally an access extension object. See separate reference page for further information.
+
|-
+
|[[JUser/bind|bind]]
+
|Binds an associative array of data to the user object.  See separate reference page for further information.
+
|-
+
|[[JUser/save|save]]
+
|Saves the JUser object to the database. See separate reference page for further information.
+
|-
+
|[[JUser/getInstance|getInstance]]
+
|Returns a reference to the global JUser object of a given type, only creating it if it doesn't already exist.  In most cases you should use [[JFactory/getUser|JFactory::getUser]] instead.
+
|-
+
|[[JUser/delete|delete]]
+
|Deletes the JUser object from the database.
+
|}
+
===Importing===
+
<source lang="php">jimport( 'joomla.user.user' );</source>
+
 
+
== Examples ==
+
=== Create a fresh user ===
+
{|style="border:1px #F28500 solid;width:90%;font-weight:bold;font-size:15px;text-align:center;" align="center"
+
|-
+
|Should perhaps be moved to [[JFactory/getUser|JFactory::getUser]], cause using "new JUser" isn't recommended; so no example is necessary
+
|}
+
<source lang="php">
+
/*
+
 
+
I handle this code as if it is a snippet of a method or function!!
+
 
+
First set up some variables/objects
+
*/
+
// get the ACL
+
$acl =& JFactory::getACL();
+
 
+
/* get the com_user params */
+
 
+
jimport('joomla.application.component.helper'); // include libraries/application/component/helper.php
+
$usersParams = &JComponentHelper::getParams( 'com_users' ); // load the Params
+
 
+
// "generate" a new JUser Object
+
$user = JFactory::getUser(0); // it's important to set the "0" otherwise your admin user information will be loaded
+
 
+
$data = array(); // array for all user settings
+
 
+
// get the default usertype
+
$usertype = $usersParams->get( 'new_usertype' );
+
if (!$usertype) {
+
    $usertype = 'Registered';
+
}
+
 
+
// set up the "main" user information
+
 
+
$data['name'] = $firstname.' '.$lastname; // add first- and lastname
+
$data['username'] = $username; // add username
+
$data['email'] = $email; // add email
+
$data['gid'] = $acl->get_group_id( '', $usertype, 'ARO' );  // generate the gid from the usertype
+
 
+
/* no need to add the usertype, it will be generated automaticaly from the gid */
+
 
+
$data['password'] = $password; // set the password
+
$data['password2'] = $password; // confirm the password
+
$data['sendEmail'] = 1; // should the user receive system mails?
+
 
+
/* Now we can decide, if the user will need an activation */
+
 
+
$useractivation = $usersParams->get( 'useractivation' ); // in this example, we load the config-setting
+
if ($useractivation == 1) { // yeah we want an activation
+
 
+
    jimport('joomla.user.helper'); // include libraries/user/helper.php
+
    $data['block'] = 1; // block the User
+
    $data['activation'] =JUtility::getHash( JUserHelper::genRandomPassword() ); // set activation hash (don't forget to send an activation email)
+
 
+
}
+
else { // no we need no activation
+
 
+
    $data['block'] = 0; // don't block the user
+
 
+
}
+
 
+
if (!$user->bind($data)) { // now bind the data to the JUser Object, if it not works....
+
 
+
    JError::raiseWarning('', JText::_( $user->getError())); // ...raise an Warning
+
    return false; // if you're in a method/function return false
+
 
+
}
+
 
+
if (!$user->save()) { // if the user is NOT saved...
+
 
+
    JError::raiseWarning('', JText::_( $user->getError())); // ...raise an Warning
+
    return false; // if you're in a method/function return false
+
 
+
}
+
 
+
return $user; // else return the new JUser object
+
</source>
+
 
+
==== Used Classes/Methods ====
+
 
+
# [[JFactory]]
+
#* [[JFactory/getACL|getACL]]
+
#* [[JFactory/getUser|getUser]]
+
# [[JUser]]
+
#* [[JUser/bind|bind]]
+
#* [[JUser/save|save]]
+
# [[JUserHelper]]
+
#* [[JUserHelper/genRandomPassword|genRandomPassword]]
+
# [[JComponentHelper]]
+
#* [[JComponentHelper/getParams|getParams]]
+
# [[JUtility]]
+
#* [[JUtility/getHash|getHash]]
+
# [[JError]]
+
#* [[JError/raiseWarning|raiseWarning]]
+
# [[JText]]
+
#* [[JText/_|_]]
+
# [[jimport]]
+
 
+
==See also==
+
* [http://api.joomla.org/Joomla-Framework/User/JUser.html JUser on api.joomla.org]
+
* [[JFactory/getUser|JFactory::getUser]]
+
<noinclude>[[Category:Development]][[Category:Framework]][[Category:JUser]]</noinclude>
+

Latest revision as of 14:06, 29 August 2012

This class is available in the following Joomla versions:-

Personal tools
Namespaces

Variants
Actions
Navigation
Joomla! Sites
Toolbox