Sometimes $user has yet to be establish in your code; especially if you're using an external code from Community Builder such as a module, but $user is needed for many Community Builder API usages. So you'll need to establish the $user of the logged in user. To do this add the following to your code, which will generate $user as used in Community Builder Plugins.
// Specific User
$user = CBuser::getUserDataInstance( USER_ID_HERE );
// My User
$user = CBuser::getMyUserDataInstance();
The above is great for establishing the $user object if this is the very basic you need. However with just the object you can't perform typical CBUser API such as replacement API to allow substitutions on a text string to be parsed. The below will allow usage of CBUser API as well as establish the $user object.
// Specific User
$cbUser = CBuser::getInstance( USER_ID_HERE, false );
// My User
$cbUser = CBuser::getMyInstance();
To access the user data from an established CBuser object you'd use the below.
$user = $cbUser->getUserData();