<![CDATA[ Modifying usergroups: Call to a member function getObject() on a non-object - My Forums]]> https://forums.modx.com/thread/?thread=91606 <![CDATA[Re: Modifying usergroups: Call to a member function getObject() on a non-object]]> https://forums.modx.com/thread/91606/modifying-usergroups-call-to-a-member-function-getobject-on-a-non-object?page=2#dis-post-500968 Quote from: BobRay at Jun 18, 2014, 09:23 PM
I'm glad you got it sorted. Thanks for reporting back. smiley

All of a sudden, as soon as I transferred over from .sandbox.paypal, it no longer works! (And I have no idea why!)

I've setup some echos, and it's running in the part where the user should be added to the usergroup, so I'm not quite sure what's wrong! sad

<?php
// Copyright Pixel Crescent 2014 All Rights Reserved
// This initialises all MODx Commands and Permissions! :D
error_reporting(E_ALL);
require_once '../config.core.php';
require_once MODX_CORE_PATH.'model/modx/modx.class.php';
$modx = new modX();
$modx->initialize('web');
$modx->getService('error','error.modError', '', '');
function userPurchasedItem($item_id, $payment_amount, $payer_email, $username) {
	global $modx;
	//mail('[email protected]', 'Just received a purchase!', 'The user: '.$username.' at '.$payer_email.' has just purchased an item for $'.$payment_amount.' ID: '.$item_id);
	$myUser = $modx->getObject('modUser', array('username'=>$username));
	 if ($myUser) {
		$myUser->save();
		$myUser->joinGroup($item_id);
		echo 'It is set!';
		$cm = $modx->getCacheManager();
		$cm->refresh();
    } else {
		 mail('[email protected]', 'Error Assigning group', 'Something went wrong when assigning the user '.$username.' who purchased the item '.$item_id.' for'.$payment_amount);
        die('No User');
    }
}
?>


My test code that is running is:

<?php
include('userPurchase.php');
userPurchasedItem('4', '5', '[email protected]', 'muffinjello');
?>


And there is the user 'muffinjello' in my DB... never even touched that. Any ideas?]]>
muffinjello Jun 19, 2014, 10:41 PM https://forums.modx.com/thread/91606/modifying-usergroups-call-to-a-member-function-getobject-on-a-non-object?page=2#dis-post-500968
<![CDATA[Re: Modifying usergroups: Call to a member function getObject() on a non-object]]> https://forums.modx.com/thread/91606/modifying-usergroups-call-to-a-member-function-getobject-on-a-non-object#dis-post-500816
]]>
BobRay Jun 18, 2014, 04:23 PM https://forums.modx.com/thread/91606/modifying-usergroups-call-to-a-member-function-getobject-on-a-non-object#dis-post-500816
<![CDATA[Re: Modifying usergroups: Call to a member function getObject() on a non-object]]> https://forums.modx.com/thread/91606/modifying-usergroups-call-to-a-member-function-getobject-on-a-non-object#dis-post-500806 Quote from: BobRay at Jun 18, 2014, 05:44 PM
They might, though I think they would have it the next time they log in. You can clear the cache in the code with:

$cm = $modx->getCacheManager();
$cm->refresh();


That clears the entire site cache, though. It's possible to clear just the relevant parts of the cache, but it's fairly complex and difficult to set up.

Everything works! Woohooo!!! I've successfully integrated paypal with my system!!!!!!!!! laugh
Yes, I'm just clearing the entire cache at this point.]]>
muffinjello Jun 18, 2014, 01:26 PM https://forums.modx.com/thread/91606/modifying-usergroups-call-to-a-member-function-getobject-on-a-non-object#dis-post-500806
<![CDATA[Re: Modifying usergroups: Call to a member function getObject() on a non-object]]> https://forums.modx.com/thread/91606/modifying-usergroups-call-to-a-member-function-getobject-on-a-non-object#dis-post-500802
$cm = $modx->getCacheManager();
$cm->refresh();


That clears the entire site cache, though. It's possible to clear just the relevant parts of the cache, but it's fairly complex and difficult to set up.



]]>
BobRay Jun 18, 2014, 12:44 PM https://forums.modx.com/thread/91606/modifying-usergroups-call-to-a-member-function-getobject-on-a-non-object#dis-post-500802
<![CDATA[Re: Modifying usergroups: Call to a member function getObject() on a non-object]]> https://forums.modx.com/thread/91606/modifying-usergroups-call-to-a-member-function-getobject-on-a-non-object#dis-post-500800 sottwell and BobRay, without their assistance, my shiny new MODx setup wouldn't be fully working!!! Thank you so much to these two!!!

And yeah, BobRay, removing the '' around the user group ID and clearing the cache did the trick. I was just wondering - will my members have to have the cache cleared before they show up as having the required permission on the front end (let's say I made an if statement which checked their usergroup) to view restricted information to that group? If not, it'll be unfortunate that clients won't be able to download their files directly after purchase, but rather wait for me to manually clear the cache - how do I circumvent this problem?

Thanks again for all of your help!!!]]>
muffinjello Jun 18, 2014, 12:41 PM https://forums.modx.com/thread/91606/modifying-usergroups-call-to-a-member-function-getobject-on-a-non-object#dis-post-500800
<![CDATA[Re: Modifying usergroups: Call to a member function getObject() on a non-object]]> https://forums.modx.com/thread/91606/modifying-usergroups-call-to-a-member-function-getobject-on-a-non-object#dis-post-500726
You may also have to clear the cache before the user shows up in the group.

You do have a potential security issue if you don't use the authentification system PayPal provides to make sure the request isn't coming from somewhere else, though all a hacker could do is add users to the user group (assuming that they know or can guess the username of a real user). You could also check the email against the user's email address (by getting the User Profile), though you'd have to be sure the user used the same email address at PayPal that they did on your system.

]]>
BobRay Jun 18, 2014, 12:48 AM https://forums.modx.com/thread/91606/modifying-usergroups-call-to-a-member-function-getobject-on-a-non-object#dis-post-500726
<![CDATA[Re: Modifying usergroups: Call to a member function getObject() on a non-object]]> https://forums.modx.com/thread/91606/modifying-usergroups-call-to-a-member-function-getobject-on-a-non-object#dis-post-500722 Quote from: BobRay at Jun 18, 2014, 05:17 AM
You have a function that adds a user to a User Group, but you're never calling that function in your code (at least not in the code you've posted).

This line at the end would add me to group 4 (assuming that I was in your database):

userPurchasedItem(12, 24.95, [email protected], 'BobRay');


FYI, you can send the ID of a role as the second argument to joinGroup() if you want to assign the user a role in the group.


My apologies, I left out the test.php code that I was navigating to, and calling my function through.
<?php
include('userPurchase.php');
userPurchasedItem('001', '25', '[email protected]', 'muffinjello');
?>
]]>
muffinjello Jun 18, 2014, 12:32 AM https://forums.modx.com/thread/91606/modifying-usergroups-call-to-a-member-function-getobject-on-a-non-object#dis-post-500722
<![CDATA[Re: Modifying usergroups: Call to a member function getObject() on a non-object]]> https://forums.modx.com/thread/91606/modifying-usergroups-call-to-a-member-function-getobject-on-a-non-object#dis-post-500718
This line at the end would add me to group 4 (assuming that I was in your database):

userPurchasedItem(12, 24.95, [email protected], 'BobRay');


FYI, you can send the ID of a role as the second argument to joinGroup() if you want to assign the user a role in the group.

]]>
BobRay Jun 18, 2014, 12:17 AM https://forums.modx.com/thread/91606/modifying-usergroups-call-to-a-member-function-getobject-on-a-non-object#dis-post-500718
<![CDATA[Re: Modifying usergroups: Call to a member function getObject() on a non-object]]> https://forums.modx.com/thread/91606/modifying-usergroups-call-to-a-member-function-getobject-on-a-non-object#dis-post-500714 Quote from: sottwell at Jun 18, 2014, 04:16 AM
It's in a function. You have to declare the $modx variable global.
function userPurchasedItem($item_id, $payment_amount, $payer_email, $username) {
    global $modx;


BobRay explains here http://forums.modx.com/thread/15948/when-use-global-modx#dis-post-86192

Awesome! Thank you... however, that still leaves one thing to be desired... it's echoing success, yet the user is not being moved to the user group, and I'm not really sure why (no errors or anything.)

Can someone please expand on this? I also tried it with
$modx->initialize('web');
instead of mgr, but that didn't work.

Update:

I'm thinking this has something to do with the fact that I haven't actually looked at any way of authenticating/confirming that my script is not some random hackers' script, trying to attack the webserver. Any help

Update:

The current code is:
<?php
// Copyright Pixel Crescent 2014 All Rights Reserved
// This initialises all MODx Commands and Permissions! :D
require_once '../config.core.php';
require_once MODX_CORE_PATH.'model/modx/modx.class.php';
$modx = new modX();
$modx->initialize('web');
$modx->getService('error','error.modError', '', '');
function userPurchasedItem($item_id, $payment_amount, $payer_email, $username) {
	global $modx;
	$myUser = $modx->getObject('modUser', array('username'=>$username));
	 if ($myUser instanceof modUser) {
		$myUser->joinGroup('4');
		echo 'user Success!';
    } else {
        die('No User');
    }
}
?>

I noticed the code in my original post did not have the "joinGroup" php line, and I've added that in. (No success however)]]>
muffinjello Jun 17, 2014, 11:43 PM https://forums.modx.com/thread/91606/modifying-usergroups-call-to-a-member-function-getobject-on-a-non-object#dis-post-500714
<![CDATA[Re: Modifying usergroups: Call to a member function getObject() on a non-object]]> https://forums.modx.com/thread/91606/modifying-usergroups-call-to-a-member-function-getobject-on-a-non-object#dis-post-500710
function userPurchasedItem($item_id, $payment_amount, $payer_email, $username) {
    global $modx;


BobRay explains here http://forums.modx.com/thread/15948/when-use-global-modx#dis-post-86192]]>
sottwell Jun 17, 2014, 11:16 PM https://forums.modx.com/thread/91606/modifying-usergroups-call-to-a-member-function-getobject-on-a-non-object#dis-post-500710