Get user group ID by login user id in joomla? - joomla

How to get user group ID by login user id in Joomla?
I try this
$user = JFactory::getUser();
$user_groups = $user->groups;
this return an array.
I want only current group ID.
And is this possible to disable admin menu in Joomla and redirect to any component.

Try this code -
$user = JFactory::getUser();
$groups = $user->get('groups');
foreach($groups as $group) {
echo "<p>You're group ID is:" . $group . "</p>";
}

Related

Joomla 3 - How to find if logged in user is registered?

I have the following code
<?php if ($user->id >= 1): // logged in ?>
This checks to see if the user is logged in but not which group the user belongs to. I'm trying to show content only for registered users.
//Get the logged user
$user = JFactory::getUser($user->id >= 1);
//Set the group id to check against
$group_id = 9;
//see if the user is in bo
if( in_array($group_id, $user->groups ) ){
echo "User is in group"
}else{
echo "user not in group";
}
//Side note: You can also get user groups with the following method
$user->getAuthorisedGroups();

Joomla 3 get profile

I use function onUserAfterSave and $isnew, but I can't get phone and address of user registered in Joomla 3.X. I only can get name, username, email, but no data from profile.
To access the user profile data you need to use JUserHelper class
jimport( 'joomla.user.helper' );
$user = JFactory::getUser();
$userProfile = JUserHelper::getProfile( $user->id ); //Get userprofile data
//You can get all the user input of profile data. If you want to get country of user use
echo "Country :" . $userProfile->profile['country'];
Amit Ray,
I use in my plugin:
public function onUserAfterSave($user, $isnew, $success, $msg)
{
//die(print_r($user));
if ($isnew){
.............
$pluginParams = new JRegistry($plugin->params);
$name = $user['name'];
$name_usuario = $user['username'];
$email_user = $user['email'];
$id_usuario = $user['id'];
jimport( 'joomla.user.helper' );
$user = JFactory::getUser();
$userProfile = JUserHelper::getProfile( $id_usuario );
//You can get all the user input of profile data. If you want to get country of user use
$user_phone = $userProfile->profile['phone'];
$user_address = $userProfile->profile['address1'];
But $user_phone and $user_address no have data... whats is error? Thanks

How to get Store Name by using Multiple Store Id in Magento?

I want to get store name by using multiple store ids in magento.
like:
<?php $store_ids= array(1,2,3);
Now how to get store names of these store ids.
// Gets the current store's details
$store = Mage::app()->getStore();
// Gets the current store's id
$storeId = Mage::app()->getStore()->getStoreId();
// Gets the current store's code $storeCode =
Mage::app()->getStore()->getCode();
// Gets the current website's id
$websiteId = Mage::app()->getStore()->getWebsiteId();
// Gets the current store's group id
$storeGroupId = Mage::app()->getStore()->getGroupId();
// Gets the current store's name
$storeName = Mage::app()->getStore()->getName();
// Gets the current store's sort order
$storeSortOrder = Mage::app()->getStore()->getSortOrder();
// Gets the current store's status
$storeIsActive = Mage::app()->getStore()->getIsActive();
// Gets the current store's locale
$storeLocaleCode = Mage::app()->getStore()->getLocaleCode();
// Gets the current store's home url
$storeHomeUrl = Mage::app()->getStore()->getHomeUrl();
echo "Website ID: " . Mage::app()->getWebsite()->getId() . "<br/>";
echo "Website Name: " . Mage::app()->getWebsite()->getName() . "<br/>";
echo "Store ID: " . Mage::app()->getStore()->getId() . "<br/>";
echo "Store Name: ".Mage::app()->getStore()->getName(). "<br/>";
echo "Store code: ". Mage::app()->getStore()->getCode()."<br/>";
The below code snippet will print all the store IDs and store names in Magento.
foreach (Mage::app()->getWebsites() as $website) {
foreach ($website->getGroups() as $group) {
$stores = $group->getStores();
foreach ($stores as $store) {
echo $store->getId() ." ".$store->getName()."<br/>";
}
} }
I got the answer:
We can get storename through multiple store ids like this:
$store_ids= array(1,2,3);
foreach($store_ids as $storeId){
$store = Mage::getModel('core/store')->load($storeId);
$name = $store->getName();
}

Joomla 2.5 - How to correctly check if user is guest or member

What is the best (fastest/safest/better for the long run) way to check if a member is signed in Joomla.
1# way: $user =& JFactory::getUser();
if($user->id!=0){
2# way: $user =JFactory::getUser()->guest;
or another way?
These are snippets out of a custom script to check if a user is a guest to tell him to signup and if he signed in to welcome him.
I am using Joomla 2.5
I have read on Joomla 2.5 check user logged in that some codes are different depending on the php version I am using? I am puzzled.
Thanks
You can use either assuming you're running PHP 5.3, however, if you're running anything below that, then use:
$user = JFactory::getUser();
if($user->id!=0){
//your code goes here
}
Personally, I would go for the second method as it goes through the Joomla API and is a tad more thorough.
On a side note, if you do use the first method, you can remove the &.
/* some information about the current logged in user is displayed, but only when the user is actually logged in. */
$user =& JFactory::getUser();
if (!$user->guest) {
echo 'You are logged in as:<br />';
echo 'User name: ' . $user->username . '<br />';
echo 'Real name: ' . $user->name . '<br />';
echo 'User ID : ' . $user->id . '<br />';
}
For Joomla there are two ways you can check if user is loged in
$user = JFactory::getUser();
if($user->guest = 1){
//User in not logged in
}
else
{
//User is logged in
}
Or second one is
$user = JFactory::getUser();
if($user->id != 0){
//User is logged in
$id = $user->id; // You can use this ID for further Use
}
else
{
//User is no logged in
}
I personally Prefer Second one as it fulfill two usability at same time.

How to Get User Group Names in Joomla 2.5

I'm writing a Joomla 2.5 component that I had been developing in Joomla 1.7. I have been using code like this:
$user = JFactory::getUser();
$groups = $user->get('groups');
The $groups array would contain a list of ids with the group name as the index. Joomla 2.5 seems to have scrapped this functionality. I have been unable to find out how to get the group names without directly querying the database. Is there any method for getting a list of the groups a user is a member of without having to resort to querying the database?
The code I generated below gets the names of all the groups the user is a part of and stores them in the variable $groupNames separated by line breaks:
foreach ($user->groups as $groupId => $value){
$db = JFactory::getDbo();
$db->setQuery(
'SELECT `title`' .
' FROM `#__usergroups`' .
' WHERE `id` = '. (int) $groupId
);
$groupNames .= $db->loadResult();
$groupNames .= '<br/>';
}
print $groupNames;
It technically queries the database but is done via the Joomla API. This is working well for me on Joomla 2.5.
Yes, this changed.
But what you should be using instead is:
JFactory::getUser()->getAuthorisedGroups();
or just getUserGroups
Real snippet:
$user = JFactory::getUser();
$db = JFactory::getDBO();
$db->setQuery($db->getQuery(true)
->select('*')
->from("#__usergroups")
);
$groups=$db->loadRowList();
$userGroups = $user->groups;
$return=array();
foreach ($groups as $key=>$g){
if (array_key_exists($g[0],$userGroups)) array_push($return,$g[4]);
}
$groups=$return;
/////printing groupnames for current user/////////////
print_r($groups);
Here it is:
<?php
$user =& JFactory::getUser();
foreach ($user->groups as $key => $value){
echo $key.'<br>';
}
?>
This will print all the user group names to the screen. The user group names are the "keys" of the array $user->groups.

Resources