Actions

Difference between revisions of "JFactory/getUser"

From Joomla! Documentation

m (See also: Updated API link)
(Updated the syntax description to remove reference to passing a string into getUser())
 
(2 intermediate revisions by 2 users not shown)
Line 11: Line 11:
 
|-
 
|-
 
|$id
 
|$id
|integer or string
+
|integer
|The user to load.  If a string is passed then it will be interpreted as a user name and converted to an integer id automatically.  If the argument is omitted or is '''null''' then the current user will loaded.
+
|The user to load.  If the argument is omitted or is '''null''' then the current user will loaded.
 
|'''null'''
 
|'''null'''
 
|}
 
|}
Line 18: Line 18:
 
In this example, some information about the current logged in user is displayed, but only when the user is actually logged in.
 
In this example, some information about the current logged in user is displayed, but only when the user is actually logged in.
 
<source lang="php">
 
<source lang="php">
$user =& JFactory::getUser();
+
$user = JFactory::getUser();
  
 
if (!$user->guest) {
 
if (!$user->guest) {
Line 28: Line 28:
 
</source>
 
</source>
 
===Example 2===
 
===Example 2===
In this example, information about a specific user, with username 'joebloggs', is displayed, regardless of the status of the current user.
 
<source lang="php">
 
$user =& JFactory::getUser( 'joebloggs' );
 
 
if ($user->id == 0) {
 
  echo 'There is no user joebloggs registered on this site.<br />';
 
} else {
 
  echo 'User name: ' . $user->username . '<br />';
 
  echo 'Real name: ' . $user->name . '<br />';
 
  echo 'User ID  : ' . $user->id . '<br />';
 
}
 
</source>
 
===Example 3===
 
 
In this example, a check is made to determine if the current user has edit permission on content.  For more information on permissions see [[JAuthorization]].  See also [[JFactory/getACL|JFactory->getACL]].
 
In this example, a check is made to determine if the current user has edit permission on content.  For more information on permissions see [[JAuthorization]].  See also [[JFactory/getACL|JFactory->getACL]].
 
<source lang="php">
 
<source lang="php">
$user =& JFactory::getUser();
+
$user = JFactory::getUser();
  
 
if ($user->authorize( 'com_content', 'edit', 'content', 'all' )) {
 
if ($user->authorize( 'com_content', 'edit', 'content', 'all' )) {

Latest revision as of 06:34, 15 April 2014

Returns a reference to the global user object, only creating it if it doesn't already exist. The object returned will be of type JUser.

Contents

Syntax

object JUser getUser( $id )

where:

Argument Data type Description Default
$id integer The user to load. If the argument is omitted or is null then the current user will loaded. null

Example 1

In this example, some information about the current logged in user is displayed, but only when the user is actually logged in.

$user = JFactory::getUser();
 
if (!$user->guest) {
  echo 'You are logged in as:<br />';
  echo 'User name: ' . $user->username . '<br />';
  echo 'Real name: ' . $user->name . '<br />';
  echo 'User ID  : ' . $user->id . '<br />';
}

Example 2

In this example, a check is made to determine if the current user has edit permission on content. For more information on permissions see JAuthorization. See also JFactory->getACL.

$user = JFactory::getUser();
 
if ($user->authorize( 'com_content', 'edit', 'content', 'all' )) {
  echo 'Editing permitted.';
} else {
  echo 'Editing not permitted.';
}

See also