И так допустим есть у нас несколько сайтов, однотипных по внешнему виду, но разных по содержанию(интернет-магазин, блог....). Структура сети сайтов выглядит примерно так:
- Главный сайт компании
- Проект магазина(это просто как раздел тут сайта нет.)
- Сайт магазина
- Сайт блога магазина
- Сайт форума магазина
То есть есть 1 база данных главная и еще 3 подпроектные базы.
Как можно организовать регистрацию и авторизацию пользователей с 3 подпроектных баз в главную базу?Конечно допустим можно сделать костыльным способом и во всех файлах при внимательном рассмотрении добавить что-то вроде известного уже решения:
$params = array(); //prevent problems
$params['driver'] = 'mysqli'; // Имя драйвера удаленной базы данных
$params['host'] = 'localhost'; // Имя сервера удаленной базы данных
$params['user'] = 'root'; // Имя пользователя учетной записи удаленной базы данных
$params['password'] = 'p@svvoRd'; // Пароль доступа учетной записи к удаленной базе данных
$params['database'] = 'ndb'; // Имя удаленной базы данных
$params['prefix'] = 'pref_'; // Префикс таблиц удаленной базы данных (может быть пустым)
$db = JFactory::getDBO ();
$db = & JDatabase::getInstance( $params );
Но это мягко выражаясь не самый лучший способ.
И тут приходит идея создать плагин:
<?php
/**
* @package Joomla.Plugin
* @subpackage Authentication.joomla
*
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
/**
* Joomla Authentication plugin
*
* @since 1.5
*/
class plgAuthenticationWEauth extends JPlugin
{
/**
* This method should handle any authentication and report back to the subject
*
* @param array $credentials Array holding the user credentials
* @param array $options Array of extra options
* @param object &$response Authentication response object
*
* @return boolean
*
* @since 1.5
*/
public function onUserAuthenticate($credentials, $options, &$response)
{
/*
* Here you would do whatever you need for an authentication routine with the credentials
*
* In this example the mixed variable $return would be set to false
* if the authentication routine fails or an integer userid of the authenticated
* user if the routine passes
*/
$params = array(); //prevent problems
$params['driver'] = 'mysqli'; // Имя драйвера удаленной базы данных
$params['host'] = 'localhost'; // Имя сервера удаленной базы данных
$params['user'] = 'root'; // Имя пользователя учетной записи удаленной базы данных
$params['password'] = 'p@svvoRd'; // Пароль доступа учетной записи к удаленной базе данных
$params['database'] = 'ndb'; // Имя удаленной базы данных
$params['prefix'] = 'pref_'; // Префикс таблиц удаленной базы данных (может быть пустым)
$db = JFactory::getDBO ();
$db = & JDatabase::getInstance( $params );
$query = $db->getQuery(true)
->select('id, password')
->from('#__users')
->where('username=' . $db->quote($credentials['username']));
$db->setQuery($query);
$result = $db->loadObject();
if ($result)
{
$match = JUserHelper::verifyPassword($credentials['password'], $result->password, $result->id);
if ($match === true)
{
// Bring this in line with the rest of the system
$user = JUser::getInstance($result->id);
$response->email = $user->email;
$response->fullname = $user->name;
if (JFactory::getApplication()->isAdmin())
{
$response->language = $user->getParam('admin_language');
}
else
{
$response->language = $user->getParam('language');
}
$response->status = JAuthentication::STATUS_SUCCESS;
$response->error_message = '';
}
else
{
// Invalid password
$response->status = JAuthentication::STATUS_FAILURE;
$response->error_message = JText::_('JGLOBAL_AUTH_INVALID_PASS');
}
}
else
{
// Invalid user
$response->status = JAuthentication::STATUS_FAILURE;
$response->error_message = JText::_('JGLOBAL_AUTH_NO_USER');
}
// Check the two factor authentication
if ($response->status == JAuthentication::STATUS_SUCCESS)
{
require_once JPATH_ADMINISTRATOR . '/components/com_users/helpers/users.php';
$methods = UsersHelper::getTwoFactorMethods();
if (count($methods) <= 1)
{
// No two factor authentication method is enabled
return;
}
require_once JPATH_ADMINISTRATOR . '/components/com_users/models/user.php';
$model = new UsersModelUser;
// Load the user's OTP (one time password, a.k.a. two factor auth) configuration
if (!array_key_exists('otp_config', $options))
{
$otpConfig = $model->getOtpConfig($result->id);
$options['otp_config'] = $otpConfig;
}
else
{
$otpConfig = $options['otp_config'];
}
// Check if the user has enabled two factor authentication
if (empty($otpConfig->method) || ($otpConfig->method == 'none'))
{
// Warn the user if he's using a secret code but he has not
// enabed two factor auth in his account.
if (!empty($credentials['secretkey']))
{
try
{
$app = JFactory::getApplication();
$this->loadLanguage();
$app->enqueueMessage(JText::_('PLG_AUTH_JOOMLA_ERR_SECRET_CODE_WITHOUT_TFA'), 'warning');
}
catch (Exception $exc)
{
// This happens when we are in CLI mode. In this case
// no warning is issued
return;
}
}
return;
}
// Load the Joomla! RAD layer
if (!defined('FOF_INCLUDED'))
{
include_once JPATH_LIBRARIES . '/fof/include.php';
}
// Try to validate the OTP
FOFPlatform::getInstance()->importPlugin('twofactorauth');
$otpAuthReplies = FOFPlatform::getInstance()->runPlugins('onUserTwofactorAuthenticate', array($credentials, $options));
$check = false;
/*
* This looks like noob code but DO NOT TOUCH IT and do not convert
* to in_array(). During testing in_array() inexplicably returned
* null when the OTEP begins with a zero! o_O
*/
if (!empty($otpAuthReplies))
{
foreach ($otpAuthReplies as $authReply)
{
$check = $check || $authReply;
}
}
// Fall back to one time emergency passwords
if (!$check)
{
// Did the user use an OTEP instead?
if (empty($otpConfig->otep))
{
if (empty($otpConfig->method) || ($otpConfig->method == 'none'))
{
// Two factor authentication is not enabled on this account.
// Any string is assumed to be a valid OTEP.
return true;
}
else
{
/*
* Two factor authentication enabled and no OTEPs defined. The
* user has used them all up. Therefore anything he enters is
* an invalid OTEP.
*/
return false;
}
}
// Clean up the OTEP (remove dashes, spaces and other funny stuff
// our beloved users may have unwittingly stuffed in it)
$otep = $credentials['secretkey'];
$otep = filter_var($otep, FILTER_SANITIZE_NUMBER_INT);
$otep = str_replace('-', '', $otep);
$check = false;
// Did we find a valid OTEP?
if (in_array($otep, $otpConfig->otep))
{
// Remove the OTEP from the array
$otpConfig->otep = array_diff($otpConfig->otep, array($otep));
$model->setOtpConfig($result->id, $otpConfig);
// Return true; the OTEP was a valid one
$check = true;
}
}
if (!$check)
{
$response->status = JAuthentication::STATUS_FAILURE;
$response->error_message = JText::_('JGLOBAL_AUTH_INVALID_SECRETKEY');
}
}
}
}
Отключая при том другие плагины авторизации я получаю впринципе успешный результат, НО пускает меня на сайт только как администратора, не как пользователя и то с предупреждением:
JUser: :_load: Не удалось загрузить пользователя с ID: 434
Как пользователя в принципе запрос уход и возвращается с ошибкой:
JUser: :_load: Не удалось загрузить пользователя с ID: 435
У вас нет права доступа к закрытой части сайта.
Так я понимаю в плагине я должен был прописать еще и какую то проверку usergroup.
Но поскольку я не совсем понимаю как она должна быть написана и вообще в правильную сторону я мыслю надеюсь на помощь и совет, уважаемого Дмитрия b2z Рекуна, или не менее уважаемых остальных продвинутых пользователей Joomla