JUser
in JPlugin::onUserLogin(...)
.JUser::setParam($key, $value)
with key/value pair.JUser::save(false)
to supposedly commit changes to the database.params
column.The params
column of the user table in the database should reflect the updated parameter.
The params
column of the user table in the database remains the same.
1.9.15-0ubuntu1
)7.1.7.retag-1+ubuntu16.04.1+deb.sury.org+1
)3.7.3
)If the user doesn't exist yet, then JUser::setParam($key, $value)
and JUser::save(false)
work as expected. I feel like JUser::save(false)
is committing an old, possibly cached instance of the user because JUser::getParam($key) === $value
after calling JUser::setParam($key, $value)
.
<?php
/**
* @copyright Copyright (C) Clay Freeman. All rights reserved.
* @license GNU General Public License version 3 or later.
*/
// No direct access
defined('_JEXEC') or die;
class PlgUserExample extends JPlugin {
/**
* This method intercepts the login process during the
* post-authentication phase.
*
* @param array $user Holds the user data.
* @param array $options Extra options.
*
* @return boolean True on success
*/
public function onUserLogin($user, $options) {
// Fetch the JUser from the database with the resulting ID
$instance = $this->_getUser($user);
// Other functionality here...
return true;
}
/**
* Attempt to fetch a user by username or create a user if it doesn't exist.
*
* @param JAuthenticationResponse $user User account details.
*
* @return JUser The resulting user.
*/
protected function _getUser($user) {
// Attempt to fetch the user by username
$instance = JUser::getInstance();
$id = (int) JUserHelper::getUserId($user['username']);
// If the user already exists, load and return it
if ($id > 0) $instance->load($id);
else {
// Fetch the default user creation group
$config = JComponentHelper::getParams('com_users');
$defaultGroup = $config->get('new_usertype', 2);
// Fill in the values for the new user
$instance->set('id', 0);
$instance->set('name', $user['fullname']);
$instance->set('username', $user['username']);
$instance->set('groups', array($defaultGroup));
// etc...
}
// Forcibly set the user's preferred editor
$instance->setParam('editor', 'codemirror');
// Attempt to create or update the user
if (!$instance->save(false)) JLog::add('Error in login for user '.
$user['username'].'.', JLog::WARNING, 'error');
// Return the resulting JUser instance
return $instance;
}
}
Labels |
Added:
?
|
Category | ⇒ | com_users |
Title |
|
@matrikular Yes, this plugin is modifying a Super User. Should I be using a different facility for this?
I wouldn't recommend to bypass any ACL checks and / or changing Super User params on the fly but here you go:
use Joomla\Registry\Registry;
$userToBeEdited = JFactory::getUser(578);
$registry = new Registry($userToBeEdited->params);
$registry->set('this', 'foo');
$shenanigan = new stdClass;
$shenanigan->id = $userToBeEdited->id;
$shenanigan->params = $registry->toString();
JFactory::getDbo()->updateObject('#__users', $shenanigan, 'id')
@matrikular If I were to add an onUserAfterLogin(...)
hook, do you think I would succeed in editing the parameters using the correct API? The user being edited is always themselves.
Yes, you would.
Awesome! Thanks!
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2017-07-18 00:42:10 |
Closed_By | ⇒ | clayfreeman |
It seems you have some issues with the ACL. Are you creating / editing a Super User maybe? I've tested this with the following result:
https://pastebin.com/QcckQAZc
As you can see, the key / value pair of this & that has been saved.
This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/17080.