Skip to content

Instantly share code, notes, and snippets.

@larscwallin
Created January 23, 2012 12:09
Show Gist options
  • Save larscwallin/1662775 to your computer and use it in GitHub Desktop.
Save larscwallin/1662775 to your computer and use it in GitHub Desktop.
SIMPLX UserManager
SIMPLX UserManager version 0.0.0.0.0.0.0.1
Helps you to get JSON encoded information from the MODx User related tables.
I use it to populate a TV dropdown with MODx users.
UPDATE 120123 13:17
Parameters:
&a - Action, possible values: getuser, getusers, getusergroup, getusergroups
&oid - Object id, valid for getuser and getusergroup
&query - XPDO query, json array representing an XPDO query. Works for for getusers and getusergroups.
&tvformat - Format for TV input, returns a string formated to populate TVs (key==value||key2==value2).
Examples:
[[!simplx.usermanager?&a=`getuser`&oid=`14`]]
[[!simplx.usermanager?&a=`getusers`&query=`{"username:LIKE":"bert"}`]]
Output:
[
{
"id":1,
"username":"lawa",
"password":"2VhePNILp6Zf5ROJPniwYLd4qS5uLmF+REhFUNXkupc=",
"cachepwd":"",
"class_key":"modUser",
"active":true,
"remote_key":null,
"remote_data":null,
"hash_class":"hashing.modPBKDF2",
"salt":"e912a8c59dd2db6f11ecef09d9362a88",
"primary_group":1,
"profile":{
"id":1,
"internalKey":1,
"fullname":"Default Admin User",
"email":"[email protected]",
"phone":"",
"mobilephone":"",
"blocked":false,
"blockeduntil":0,
"blockedafter":0,
"logincount":3,
"lastlogin":1326794370,
"thislogin":1327317021,
"failedlogincount":0,
"sessionid":"5nsjqtbc65t2hlq25j2p7l3387",
"dob":0,
"gender":0,
"address":"",
"country":"",
"city":"",
"state":"",
"zip":"",
"fax":"",
"photo":"",
"comment":"",
"website":"",
"extended":{
"test":"value",
"Company":""
}
}
}
]
Style with the Widgeteer or whatever :)
Have fun!
<?php
class Simplx_UserManager{
public static $jsonEncode = false;
public static function getUsers($query=array()){
global $modx;
$result = null;
$resultList = array();
$profile = array();
$tmpUser = array();
$result = $modx->getCollection('modUser',$query);
if($result){
foreach($result as $user){
unset($profile);
unset($tmpUser);
$profile = $user->getOne('Profile');
$tmpUser = $user->toArray();
$tmpUser['profile'] = $profile->toArray();
$resultList[] = $tmpUser;
}
if(!self::$jsonEncode){
return $resultList;
}else{
return json_encode($resultList);
}
}else{
$modx->log(modX::LOG_LEVEL_ERROR, 'Simplx_UserManager, getUsers(): $modx->getCollection("modUser") failed.');
// Exception
}
}
public static function getUserGroups($query=array()){
global $modx;
$result = null;
$result = $modx->getCollection('modUserGroup',$query);
if($result){
if(!self::$jsonEncode){
return $result;
}else{
return json_encode($result);
}
}else{
$modx->log(modX::LOG_LEVEL_ERROR, 'Simplx_UserManager, getUsers(): $modx->getMany("modUserGroup","'.json_encode($query).'") failed.');
// Exception
}
}
public static function getUser($id=null){
global $modx;
if($id){
$result = null;
$user = &$modx->getObject('modUser',$id);
if($user){
$result = $user->toArray();
$profile = $user->getOne('Profile');
$result['profile'] = $profile->toArray();
if(!self::$jsonEncode){
return $result;
}else{
return $result->toJSON();
}
}else{
$modx->log(modX::LOG_LEVEL_ERROR, 'Simplx_UserManager, getUsers(): $modx->getObject("modUser",'.$id.') failed.');
// Exception
}
}
}
public static function getUserGroup($id){
global $modx;
$result = null;
$result = $modx->getObject('modUserGroup',$id);
if($result){
if(!self::$jsonEncode){
return $result;
}else{
return json_encode($result);
}
}else{
$modx->log(modX::LOG_LEVEL_ERROR, 'Simplx_UserManager, getUsers(): $modx->getMany("modUserGroup",'.$id.') failed.');
// Exception
}
}
}
class Simplx_UserGroup{
/**
* Represents a group of users with common attributes.
*
* @property string $name The name of the User Group
* @property string $description A user-specified description of the User Group
* @property int $parent The parent group for this User Group. If none, will be 0
* @property int $rank The rank of this group, used when sorting the groups
*
* @see modUser
* @see modUserGroupRole
* @see modUserGroupMember
* @package modx
*/
/**
* Represents a user membership in a user group.
*
* @property int $user_group The ID of the related User Group
* @property int $member The ID of the related User
* @property int $role The ID of the Role the User has for this User Group
* @property int $rank Used when sorting memberships within a User Group
*
* @property modUser $User
* @property modUserGroup $UserGroup
* @property modUserGroupRole $UserGroupRole
*
* @see modUser
* @see modUserGroup
* @see modUserGroupRole
* @package modx
*/
/**
* Represents a Role that a User can have within a specific User Group. Roles are sorted into authority levels, where
* lower authority numbers will automatically inherit Permissions owned by higher authority numbers.
*
* For example, an Administrator with authority of 1 will automatically inherit any Permissions assigned to a Member
* role with authority 9999, since 1 is less than 9999. However, the reverse will not be true.
*
* @property string $name The name of the Role
* @property string $description A user-provided description of this Role
* @property int $authority The authority of the role. Lower authority numbers have more power than higher ones, and
* lower numbers will inherit the Permissions of higher numbers.
*
* @see modUser
* @see modUserGroup
* @package modx
*/
public $members = array(); // Array of modUserGroupMember
public $roles = array(); // Array of modUserGroupRole
public function __construct($id=null,$state=null){
if($id){
// Get
}else{
// New
}
}
public function getMemeber($id=null,$query=array()){
}
public function addMember($id){
}
public function removeUser($id){
}
public function save(){
}
}
class Simplx_User{
/**
* The core MODX user class.
*
* @property string $username The username for this User
* @property string $password The encrypted password for this User
* @property string $cachepwd A cached, encrypted password used when resetting the User's password or for confirmation
* @property string $class_key The class key of the user. Used for extending the modUser class.
* @property boolean $active Whether or not this user is active, and thereby able to log in
* @property string $remote_key Used for storing a remote reference key for authentication for a User
* @property json $remote_data Used for storing remote data for authentication for a User
* @property string $hash_class The hashing class used to create this User's password
* @property string $salt A salt that might have been used to create this User's password
*
* @property modUserProfile $Profile
* @property modUserGroup $PrimaryGroup
* @property array $CreatedResources
* @property array $EditedResources
* @property array $DeletedResources
* @property array $PublishedResources
* @property array $SentMessages
* @property array $ReceivedMessages
* @property array $UserSettings
* @property array $UserGroupMembers
*
* @see modUserGroupMember
* @see modUserGroupRole
* @see modUserMessage
* @see modUserProfile
* @package modx
*/
public static function get($id){
}
public static function query($query=array()){
}
}
class Simplx_Organisation{
public $properties = array();
public function __construct($id=null,$state=null){
if($id){
// Get
}else{
// New
}
}
public function save(){
}
}
/*
Snippet starts here
*/
$a = isset($a) ? $a : 'getuser';
$oid = isset($oid) ? $oid : null;
$query = isset($query) ? $query : array();
$jsonencode = isset($jsonencode) ? $jsonencode : false;
$tvformat = isset($tvformat) ? $tvformat : false;
$result;
$temp;
Simplx_UserManager::$jsonEncode = $jsonencode;
// We need an array to pass to the modx api so lets make sure we decode any json
if(!is_array($query) && $query !== ''){
$temp = json_decode($query,true);
if(is_array($temp)){
$query = $temp;
}else{
$modx->log(modX::LOG_LEVEL_ERROR, 'Snippet simplx.usermanager: Unable to decode query="'.$query.'".');
$query = array();
}
}
switch($a){
case 'getuser':
if($oid){
$result = Simplx_UserManager::getUser($oid);
if($result){
return json_encode($result);
}else{
// Exception
$modx->log(modX::LOG_LEVEL_ERROR, 'Snippet simplx.usermanager: Simplx_UserManager::getUser() returned false.');
}
}
break;
case 'getusers':
$result = Simplx_UserManager::getUsers($query);
$objectlist = array();
if($result){
if($tvformat){
foreach($result as &$obj){
$objectlist[] = ($obj['username'].'=='.$obj['id']);
}
$objectlist = implode('||',$objectlist);
return $objectlist;
}else{
return json_encode($result);
}
}else{
// Exception
$modx->log(modX::LOG_LEVEL_ERROR, 'Snippet simplx.usermanager: Simplx_UserManager::getUsers() returned false.');
}
break;
case 'getusergroup':
if($oid){
$result = Simplx_UserManager::getUserGroup($oid);
if($result){
foreach($result as &$obj){
$objectlist[] = $obj->toArray();
}
return json_encode($objectlist);
}else{
// Exception
}
}
break;
case 'getusergroups':
$result = Simplx_UserManager::getUserGroups($query);
$objectlist = array();
if($result){
if($tvformat){
foreach($result as &$obj){
$objectlist[] = ($obj->get('name').'=='.$obj->get('id'));
}
return implode('||',$objectlist);
}else{
foreach($result as &$obj){
$objectlist[] = $obj->toArray();
}
return json_encode($objectlist);
}
}else{
// Exception
}
break;
default:
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment