Programmatically working with the modUser Object
A variety of ways to directly access the modUser object: PHP array, JSON, profile, extended fields, user specific settings, resource groups, and contexts.
Posted on
March 10, 2012 at 16:40:00
Level:
Expert
Dependencies: Access to modUser
<?php
/**
* File thisUser.php (requires MODx Revolution 2.1+)
* Created on: 7/6/11 at 12:07 PM
* Project shawn_wilkerson
* @package
* @version 1.0
* @category navigation
* @author W. Shawn Wilkerson
* @link http://www.shawnWilkerson.com
* @copyright Copyright (c) 2010, W. Shawn Wilkerson. All rights reserved.
* @license GPL
*
*
* This is the main file to access the breadcrumb class.
*
*/
$user = (!empty($userId)) ? $modx->getObject('modUser', $userId) : $modx->user;
if (is_object($user)) {
/*
* Access the modUser object in a JSON array
*/
$modx->toPlaceHolder('user.toJSON', $user->toJSON());
/*
* Access the modUser object in a PHP array
*/
$arry = print_r($user->toArray(), true);
$modx->toPlaceHolder('user.toArray', $arry);
/*
* Directly access any given modUser data base field
*/
$modx->toPlaceHolder('user.field', $user->get('hash_class'));
/*
* Directly access an array of modUser object fields
*/
$fieldNamesArray = array('hash_class', 'salt');
$arry = print_r($user->get($fieldNamesArray), true);
$modx->toPlaceHolder('user.fieldarray', $arry);
/*
* Directly access a modUser Profile
*/
$profile = $user->getOne('Profile');
if (is_object($profile)) {
/*
* Convert a modUser Profile to JSON
*/
$modx->toPlaceHolder('user.profileJSON', $profile->toJSON());
/*
* Convert a modUser Profile to a PHP array
*/
$arry = print_r($profile->toArray(), true);
$modx->toPlaceHolder('user.profileArray', $arry);
/*
* Access a modUser Extended fields
*/
$arry = print_r($profile->get('extended'), true);
$modx->toPlaceHolder('user.extendedFields', $arry);
}
/*
* modUser specific settngs
*/
$userSettings = $user->getSettings();
$arry = print_r($userSettings, true);
$modx->toPlaceHolder('user.settings', $arry);
/*
* modUser Contexts
*/
$arry = print_r($user->getSessionContexts(), true);
$modx->toPlaceHolder('user.contexts', $arry);
/*
* modUser Groups
*/
$arry = print_r($user->getUserGroups(), true);
$modx->toPlaceHolder('user.groups', $arry);
/*
* modUser Group Names
*/
$arry = print_r($user->getUserGroupNames(), true);
$modx->toPlaceHolder('user.groupnames', $arry);
/*
* Dump the current modUser $_SESSION - example to protect the specified user
*/
if ($user->get('id') == $modx->user->get('id')) {
// $arry = print_r($_SESSION, true);
// $modx->toPlaceHolder('user.session', $arry);
}
}
modUser->toJSON()
{"id":2,"username":"wshawn","password":"PgAr9EpFA4x++BbMgOtBtFowRWtq6c9XNzQhYuDfEqB5","cachepwd":"","class_key":"modUser","active":true,"remote_key":null,"remote_data":null,"hash_class":"hashing.modPBKDF2","salt":"9c553a1a86200d8d9c85d068ff5131c7","primary_group":1,"session_stale":["mgr","web"],"sudo":true}
modUser->toArray()
Array
(
[id] => 2
[username] => wshawn
[password] => PgAr9EpFA4x++BbMgOtBtFowRWtq6c9XNzQhYuDfEqB5
[cachepwd] =>
[class_key] => modUser
[active] => 1
[remote_key] =>
[remote_data] =>
[hash_class] => hashing.modPBKDF2
[salt] => 9c553a1a86200d8d9c85d068ff5131c7
[primary_group] => 1
[session_stale] => Array
(
[0] => mgr
[1] => web
)
[sudo] => 1
)
modUser->get('val')
hashing.modPBKDF2
modUser->get($array)
Array
(
[hash_class] => hashing.modPBKDF2
[salt] => 200d8d9c85d068ff9c553a1a865131c7
)
modUser->getSettings()
Array
(
[site_start] => 8
)
modUser->getSessionContexts()
Array
(
[mgr] => 2
)
modUser->getUserGroups()
Array
(
[0] => 1
)
modUser->getUserGroupNames()
Array
(
[0] => Administrator
)
modUser->set('val', $value)
$user->set('gender', 1);
modUser->getOne('Profile')
$profile = $user->getOne('Profile');
$profile->toJSON()
{"id":2,"internalKey":2,"fullname":"W. Shawn Wilkerson","email":"shawn@shawnwilkerson.com","phone":"3863227999","mobilephone":"","blocked":false,"blockeduntil":0,"blockedafter":0,"logincount":15,"lastlogin":1331349509,"thislogin":1331379488,"failedlogincount":0,"sessionid":"005e44b34ef7e46a50ceda8ff88be2f8","dob":-123456789,"gender":1,"address":"1234 main Street","country":"United States","city":"Daytona Beach","state":"FL","zip":"32117","fax":"","photo":"","comment":"","website":"\/\/www.shawnwilkerson.com","extended":{"extendedVal":"This could be something important"}}
$profile->toArray()
Array
(
[id] => 2
[internalKey] => 2
[fullname] => W. Shawn Wilkerson
[email] => shawn@shawnwilkerson.com
[phone] => 3863227999
[mobilephone] =>
[blocked] =>
[blockeduntil] => 0
[blockedafter] => 0
[logincount] => 15
[lastlogin] => 1331349509
[thislogin] => 1331379488
[failedlogincount] => 0
[sessionid] => 005e44b34ef7e46a50ceda8ff88be2f8
[dob] => -123456789
[gender] => 1
[address] => 1234 Main Street
[country] => United States
[city] => Daytona Beach
[state] => FL
[zip] => 32117
[fax] =>
[photo] =>
[comment] =>
[website] => //www.shawnwilkerson.com
[extended] => Array
(
[College] => Array
(
[Degree] => BAS Supervision And Management
[Name] => Daytona State College
[SecondaryDegrees] => Array
(
[secDegree1] => AS Computer Programming and Analysis - Software Engineer
[secDegree2] => AS Internet Services Technology
[secDegree3] => AS Network Administrator
)
)
)
)
$profile->get('extended')
Array
(
[College] => Array
(
[Degree] => BAS Supervision And Management
[Name] => Daytona State College
[SecondaryDegrees] => Array
(
[secDegree1] => AS Computer Programming and Analysis - Software Engineer
[secDegree2] => AS Internet Services Technology
[secDegree3] => AS Network Administrator
)
)
)
PHP $_SESSION
View an example user $_SESSION here