I just want to set a user to a specific group. I have tried many things but not helped. One solutiuon is worked for me, but I think it is deprecated because in the Joomla's admin page I see other groups.
My code:
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] );
require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
$user =JFactory::getUser(joe); //joe is the username
$GroupJoomla = array('14'=>14); //I want to change it's group to 14
$user->set('groups',$GroupJoomla); //Method one, which I have found on documentations
print_r ( $user->get('groups')); //Succesfully sets, BUT if I check on the Joomla's admin page, the user is still on other group. Maybe this is a deprecated function and the Joomla didnt use it now.
$user->groups = $GroupJoomla;
$user->save(); //Method two. Unfortunatelly this is always returns to false for me, dont know why. There is no error message. (Apache error log also empty)
print_r ($user->getAuthorisedGroups()); //This is the good to get the user's groups. This is correct.
if ($user->save() === false)
{
throw new Exception($user->getError(), 500); // This gives me this error: Error displaying the error page: Application Instantiation Error: Application Instantiation Error
}
What am I doing wrong? :/
I havent got Joomfish, so it isnt why the $user->save() returns false.
Update:
This code is also returns false on $user->save().
$uid = 1748;
$user =JFactory::getUser($uid); //joe is the username
$GroupJoomla = array(14); //I want to change it's group to 14
$user->set('groups',$GroupJoomla);
$user->groups = $GroupJoomla;
$user->save();
jimport( 'joomla.access.access' );//Call the Access Class
/*If the below code is not there it wont save.*/
if ($user->save() === false){ //This is gives me false somehow
throw new Exception($user->getError(), 500);
}
$groups = JAccess::getGroupsByUser($uid, false);
print_r($groups); exit; //Receives Group id 15 (Not 14)
With both code samples I have got the good user object. $user->get('username'); This gives me the good username. (Joomla 3.6.5)
As #itoctopus said you need to enter id instead of username but also there are some other mistakes that needs to be taken care of like initiating the group variable. It should be
$GroupJoomla = array(14);
if more than one group
$GroupJoomla = array(13,14);
then to get the groups you can use
$groups = JAccess::getGroupsByUser($id, false);
So finally your code will be
<?php
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', 'C:\xampp\htdocs\joomla' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
$uid = 404;
$user =JFactory::getUser($uid); //joe is the username
$GroupJoomla = array(4,6); //I want to change it's group to 14
$user->set('groups',$GroupJoomla);
$user->groups = $GroupJoomla;
$user->save();
jimport( 'joomla.access.access' );//Call the Access Class
/*If the below code is not there it wont save.*/
if ($user->save() === false){
throw new Exception($user->getError(), 500);
}
$groups = JAccess::getGroupsByUser($uid, false);
print_r($groups); exit;
A quick glimpse at your code, and I think this line:
$user =JFactory::getUser(joe);
Should be:
$user =JFactory::getUser(5);
Where 5 is the id of the user with username "joe". You cannot get a user by username by using the function JFactory::getUser.
Alternatively, if you really want to get a user by username, then you should use:
$userId = JUserHelper::getUserId('joe'); //note that there are quotes around joe (in your code you omitted the quote)
$user =JFactory::getUser($userId);
Hope this helps.
Related
I'm well familiar with coding in PHP and other languages, but I'm totally unfamiliar with Joomla.
What I want to accomplish is to know when a user has logged in on the Joomla site (under /web) and differentiate this script's behaviour accordingly.
I have a Joomla 2.5 site under /web/ and I have a script under /web/test/index.php, where the following code lies (collected from other replies in here and elsewhere):
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
echo dirname(dirname(__FILE__)) . "\n";
if (file_exists(dirname(dirname(__FILE__)) . '/defines.php'))
include_once dirname(dirname(__FILE__)) . '/defines.php';
if (!defined('_JDEFINES')) {
define('JPATH_BASE', dirname(dirname(__FILE__)));
require_once JPATH_BASE.'/includes/defines.php';
}
require_once JPATH_BASE.'/includes/framework.php';
$app = JFactory::getApplication('site');
$app->initialise();
print_r(JFactory::getUser());
What I get back is
/web
JUser Object
(
[isRoot:protected] =>
[id] => 0
[name] =>
...
Thank you,
Use this code it's working:
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/' ));
require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
$mainframe = JFactory::getApplication('site');
var_dump(JFactory::getUser());
This used to work in 1.5... I could set up the following code:
function startJoomla() {
define('_JEXEC', true);
define( 'DS', DIRECTORY_SEPARATOR );
$dir= dirname(__FILE__);
define('JPATH_BASE', $dir);
// load joomla libraries
require_once JPATH_BASE . DS . 'includes' . DS . 'defines.php';
require_once JPATH_LIBRARIES . DS . 'loader.php';
jimport('joomla.base.object');
jimport('joomla.factory');
jimport('joomla.filter.filterinput');
jimport('joomla.error.error');
jimport('joomla.event.dispatcher');
jimport('joomla.event.plugin');
jimport('joomla.plugin.helper');
jimport( 'joomla.utilities.utility' );
jimport('joomla.utilities.arrayhelper');
jimport('joomla.environment.uri');
jimport('joomla.environment.request');
jimport('joomla.user.user');
// JText cannot be loaded with jimport since it's not in a file called text.php but in methods
JLoader::register('JText', JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'methods.php');
JLoader::register('JRoute', JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'methods.php');
$mainframe = & JFactory::getApplication('site');
$GLOBALS['mainframe'] = & $mainframe;
return $mainframe;
}
$mainframe = startJoomla();
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');
And then, when I wanted to call something up from Joomla, I could do the following:
$custom = new CustomController(); // assuming there's a com_custom somewhere
This worked for 1.5. But in 2.5, the above doesn't work at all. I found a revised version of the load script:
function startJoomla() {
define('_JEXEC', 1);
define( 'DS', DIRECTORY_SEPARATOR );
$dir = dirname(__FILE__);
define('JPATH_BASE', $dir);
// load joomla libraries
require_once JPATH_BASE.'/includes/defines.php';
require_once JPATH_BASE.'/includes/framework.php';
require_once JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php';
// Instantiate the application.
$app = JFactory::getApplication('site');
// Initialise the application.
return $app->initialise();
}
$mainframe = startJoomla();
This enables things like 'JFactory::getUser();' but will no longer allow me to utilize commands like 'new MycustomcomponentController();' Gives me 'core' Joomla functionality, but skips everything I installed. Is there an appropriate new revised compilation of jimport instructions or perhaps a different function of JComponent that allows me to call up my custom functions along with other components and models for use in external php files? Assume this will be run from a CRON or similar.
I am doing coding in Joomla and now i want to retrieve the User id that were Log in. When the user got logged in then the it works fine but when it clicks on my page then it shows an error. I think their may be caching or session problem. is it?
Here is my Joomla page code:
<?php
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] );
defined('_JEXEC') or die;
require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
// Initialise variables.
$app = JFactory::getApplication();
$user = JFactory::getUser();
$userId = $user->get('id');
$dispatcher = JDispatcher::getInstance();
$userId = $user->get('id');
echo $userId;
?>
Now in this code it retrieves 00 as a user id. But the user is logged in. So what is the problem?
use this code for detail users logged
$user = JFactory::getUser(); $userid = $user->id; echo $userid;
This line of code will get the current logged in user's ID.
$user_id = JFactory::getUser()->id;
I'm trying to retrieve the "current" article id from an external script in Joomla! 2.5
Firstly, I included Joomla core files:
define( '_JEXEC', 1 );
defined('_JEXEC') or die('Restricted access');
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/..' ));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
Then, initialized the session:
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
Now, I know that for Joomla 2.5 I have to use Jinput instead of JRequest (deprecated), so:
$jAp = JFactory::getApplication();
$jInput = $jAp->input;
As already told, I'm trying to retrieve the article id.
I tried many instances but nothing seems to work.
$id = $jInput->get('id', 0); // doesn't work
$idInt = $jInput->getInt('id',0); //doesn't work
Also with JRequest... I can't get current article id.
This works only if I would request data for logged in users, example:
$user = JFactory::getUser();
echo $user->username; // this works...
What I am missing?
Where is my fault?
Thanks a lot to all!
Debugging AJAX can be complex. I often find adding some logging in my handler helps a lot, for example adding the following to the start of your code...
# logging of all hits
$log_file = realpath(dirname(__FILE__)) . '/ajax_debug.log';
$fh = fopen($log_file, 'a') or die();
$log_string = "Backend Hit \n" . date("Y-m-d H:i:s") . "\n";
$log_string .= "POST: " . print_r($_POST, true) . "\n";
$log_string .= "GET: " . print_r($_GET, true) . "\n";
$log_string .= "Hit by: " . $_SERVER['REMOTE_ADDR'] . "\n";
$log_string .= "\n\n\n";
fwrite($fh, $log_string);
fclose($fh);
You didn't mention how/where your AJAX code was implemented. It sounds like it might be an implementation outwith Joomla. I would tend to put this in a plug-in if it is quite stand-alone, though module or component AJAX handlers also work well in Joomla. There are some good docs and code at https://docs.joomla.org/Using_Joomla_Ajax_Interface
I'm a newbie to Joomla and want to understand how Joomla! API works.
I have this piece of code in an external file that connects to another joomla site call APITEST
Basically, I have already created a user that has username: demo and password demo12. I want to use the login() method from JApplication class.
<?php
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', dirname(__FILE__).DS."../apitest/");
require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
$user = JFactory::getApplication('site');
$credential = array('demo', 'demo12');
$remember = array(true);
print_r($user->login($credential, $remember));
?>
However it gives me this error message:
Notice: Undefined index: username in C:\xampp\htdocs\apitest\libraries\joomla\user\authentication.php on line 321
Notice: Undefined index: username in C:\xampp\htdocs\apitest\libraries\joomla\user\authentication.php on line 326
Notice: Undefined index: password in C:\xampp\htdocs\apitest\libraries\joomla\user\authentication.php on line 331
I expect the result to return True. How can I do so? Thanks.
Joomla is looking for the username and password in an associative array, not a numerical keyed array, which is what you have.
Change to:
$credential = array('username' => 'demo', 'password' => 'demo12');