You are here

function cf_current_user in Common Functionality 7.2

Same name and namespace in other branches
  1. 7 cf.module \cf_current_user()

Provide a safe way to get the current user.

This protects the user global from coding accidents.

Justification: The global $user data may not have contrib modules data included, so accessing $user directly is generally not a good idea. This function seems rather waseteful given its simplicity. It may be better if drupal core would allow $uid to be left empty such that the user_load() function would handle accessing the global $user->id if $uid is empty.

Also, this is too easy to accidentally do: 'if ($user->uid = 1) {'.

See: https://drupal.org/node/57287 See: http://api.drupal.org/api/drupal/developer--globals.php/global/user/7#co...

Parameters

bool $reset: TRUE to reset the internal cache and load from the database. FALSE (default) to load from the internal cache, if set.

Return value

object|FALSE A copy of the global variable $user. Changes to this variable will not be retained.

Related topics

5 calls to cf_current_user()
cf_get_user in modules/cf_1x_compatibility/cf_1x_compatibility.module
This is a compatibility function for cf-1.x.
cf_node_create in modules/cf_node/cf_node.module
Programatically creates a node of the form type and returns any errors.
cf_theme_get_variables in modules/cf_theme/cf_theme.module
Returns an array of variables to be used by a given theme.
cf_user_get_rids in modules/cf_user/cf_user.module
Provide a simple way to get the roles ids for the current or specified user.
cf_user_has_role in modules/cf_user/cf_user.module
Confirm whether a user is in a given role by the role id.

File

./cf.module, line 88
Common Functionality module.

Code

function cf_current_user($reset = FALSE) {
  global $user;
  if (!is_object($user) || !property_exists($user, 'uid')) {
    if (class_exists('cf_error')) {
      cf_error::invalid_variable('user', "Not a valid user object.");
    }
    return FALSE;
  }
  return user_load($user->uid, $reset);
}