เนื่องจากผมไม่เห็น field ที่อยู่ใน table login เลยกำหนดเป็นแบบนี้ละกัน
Code: Select all
CREATE TABLE `login` (
`id` int(11) NOT NULL AUTO_INCREMENT ,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' ,
`username` varchar(150) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' ,
`email` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' ,
`password` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' ,
PRIMARY KEY (`id`),
INDEX `idx_name` USING BTREE (`name`),
INDEX `username` USING BTREE (`username`),
INDEX `email` USING BTREE (`email`)
)
ENGINE=MyISAM
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=75
CHECKSUM=0
ROW_FORMAT=DYNAMIC
DELAY_KEY_WRITE=0
;
หมายเหตุ นี้เป็นการแก้ไข core component ของ joomla ซื่งอาจทำให้ไม่สร้าง update joomla เป็น version ใหม่
หรือ update patch ได้ หรืออาจมีปัญหากับ component อื่น ๆ ได้ เพราะผมยังไม่ได้ทดสอบทางด้านนี้
code ที่เขียน เพิ่มมานี้ได้ผ่านการทดสอบมาเพียงไม่กี่ร้อยครั้ง ซื่งแต่ละครั้งที่ทดสอบก็ผ่านด้วยดี แต่ผมไม่อาจแน่ใจได้ว่า
มันจะมี bug หรือไม่ ในระหว่างทดสอบควรสร้าง web ใหม่มาทดสอบ ถ้าเกิดนำไปใช้งานแล้วมีปัญหากรุณาแจ้งให้ทราบด้วย
สมมุติว่า site เป็น localhost\joomla
ส่วนแรกเป็นการแก้ไข code component users ในส่วนของ front end หน้าสมัครสมาชิก
ให้เข้าไปที่ localhost\joomla\components\com_user\controller.php
เปิดขึ้นมา มองหา function register_save
จาก code นี้
Code: Select all
function register_save()
{
global $mainframe;
// Check for request forgeries
JRequest::checkToken() or jexit( 'Invalid Token' );
// Get required system objects
$user = clone(JFactory::getUser());
$pathway =& $mainframe->getPathway();
$config =& JFactory::getConfig();
$authorize =& JFactory::getACL();
$document =& JFactory::getDocument();
// If user registration is not allowed, show 403 not authorized.
$usersConfig = &JComponentHelper::getParams( 'com_users' );
if ($usersConfig->get('allowUserRegistration') == '0') {
JError::raiseError( 403, JText::_( 'Access Forbidden' ));
return;
}
// Initialize new usertype setting
$newUsertype = $usersConfig->get( 'new_usertype' );
if (!$newUsertype) {
$newUsertype = 'Registered';
}
// Bind the post array to the user object
if (!$user->bind( JRequest::get('post'), 'usertype' )) {
JError::raiseError( 500, $user->getError());
}
// Set some initial user values
$user->set('id', 0);
$user->set('usertype', $newUsertype);
$user->set('gid', $authorize->get_group_id( '', $newUsertype, 'ARO' ));
$date =& JFactory::getDate();
$user->set('registerDate', $date->toMySQL());
// If user activation is turned on, we need to set the activation information
$useractivation = $usersConfig->get( 'useractivation' );
if ($useractivation == '1')
{
jimport('joomla.user.helper');
$user->set('activation', JUtility::getHash( JUserHelper::genRandomPassword()) );
$user->set('block', '1');
}
// If there was an error with registration, set the message and display form
if ( !$user->save() )
{
JError::raiseWarning('', JText::_( $user->getError()));
$this->register();
return false;
}
// Send registration confirmation mail
$password = JRequest::getString('password', '', 'post', JREQUEST_ALLOWRAW);
$password = preg_replace('/[\x00-\x1F\x7F]/', '', $password); //Disallow control chars in the email
UserController::_sendMail($user, $password);
// Everything went fine, set relevant message depending upon user activation state and display message
if ( $useractivation == 1 ) {
$message = JText::_( 'REG_COMPLETE_ACTIVATE' );
} else {
$message = JText::_( 'REG_COMPLETE' );
}
$this->setRedirect('index.php', $message);
}
แก้เป็น
Code: Select all
function register_save()
{
global $mainframe;
// Check for request forgeries
JRequest::checkToken() or jexit( 'Invalid Token' );
// Get required system objects
$user = clone(JFactory::getUser());
$pathway =& $mainframe->getPathway();
$config =& JFactory::getConfig();
$authorize =& JFactory::getACL();
$document =& JFactory::getDocument();
// If user registration is not allowed, show 403 not authorized.
$usersConfig = &JComponentHelper::getParams( 'com_users' );
if ($usersConfig->get('allowUserRegistration') == '0') {
JError::raiseError( 403, JText::_( 'Access Forbidden' ));
return;
}
// Initialize new usertype setting
$newUsertype = $usersConfig->get( 'new_usertype' );
if (!$newUsertype) {
$newUsertype = 'Registered';
}
// Bind the post array to the user object
if (!$user->bind( JRequest::get('post'), 'usertype' )) {
JError::raiseError( 500, $user->getError());
}
// Set some initial user values
$user->set('id', 0);
$user->set('usertype', $newUsertype);
$user->set('gid', $authorize->get_group_id( '', $newUsertype, 'ARO' ));
$date =& JFactory::getDate();
$user->set('registerDate', $date->toMySQL());
// If user activation is turned on, we need to set the activation information
$useractivation = $usersConfig->get( 'useractivation' );
if ($useractivation == '1')
{
jimport('joomla.user.helper');
$user->set('activation', JUtility::getHash( JUserHelper::genRandomPassword()) );
$user->set('block', '1');
}
// If there was an error with registration, set the message and display form
if ( !$user->save() )
{
JError::raiseWarning('', JText::_( $user->getError()));
$this->register();
return false;
}
// Send registration confirmation mail
$password = JRequest::getString('password', '', 'post', JREQUEST_ALLOWRAW);
$password = preg_replace('/[\x00-\x1F\x7F]/', '', $password); //Disallow control chars in the email
UserController::_sendMail($user, $password);
// Everything went fine, set relevant message depending upon user activation state and display message
if ( $useractivation == 1 ) {
$message = JText::_( 'REG_COMPLETE_ACTIVATE' );
} else {
$message = JText::_( 'REG_COMPLETE' );
}
$model=$this->getModel('user');
$post=JRequest::get('post');
$model->register_user_game($post);
$this->setRedirect('index.php', $message);
}
นี้คือ code ที่เพิ่มเข้าไป
Code: Select all
$model=$this->getModel('user');
$post=JRequest::get('post');
$model->register_user_game($post);
ต่อมาให้เข้าไปที่ localhost\joomla\components\com_user\models\user.php
เปิดขึ้นมา แล้วเพิ่ม function นี้ เข้าไป
Code: Select all
function register_user_game($post=''){
if(!isset(self::$option)){
self::$option= array();
self::$option['driver'] = 'mysql'; // Database driver name
self::$option['host'] = 'localhost'; // ชื่อ host
self::$option['user'] = 'root'; // ชื่อผู้ใช้
self::$option['password'] = 'root'; // รหัสผ่าน
self::$option['database'] = 'game'; // ชื่อฐานข้อมูล
self::$option['prefix'] = ''; // คำนำหน้า table ถ้าไม่มีให้ใส่ว่างไว้
}
$db = & JDatabase::getInstance(self::$option );
$insertQuery="INSERT INTO `game`.`login` (`name`, `username`, `password`, `email`)
VALUES ('%s', '%s', '%s', '%s');";
$insertQuery=sprintf($insertQuery,$post['name'],$post['username'],$post['password'],$post['email']);
$db->setQuery($insertQuery);
$db->query();
}
ใส่ข้อมูลจากเชื่อมต่อฐานข้อมูลที่ต้องการให้เรียบร้อย
และ class UserModelUser ให้ใ่ส่ attributes นี้เข้าไป
อธิบายภาพรวม คือหลังจาก เพิ่ม user ให้กับ joomla แล้ว ให้นำข้อมูลของ user
เดียวกันนี้ไปเพิ่มให้กับ ฐานข้อมูล game ใน table login ด้วย
บันทึกทั้งสอง file จากนั้นให้ทดสอบ สมัครสมาชิกใหม่ ทางหน้า web สัก 2 - 3 คน จะเห็นว่า user ที่เข้ามา จะเพิ่มเข้าไปที่ table login ของ ฐานข้อมูล game ด้วย
เมื่อมีเพิ่มก็ต้องมี ลบ แต่ไปจะเป็นการแก้ของ code component users ในส่วนของ back end ในส่วนของการลบ user
ไปที่ \Joomla\administrator\components\com_users\controller.php
เปิดขึ้นมาเพิ่ม function นี้เข้าไป
Code: Select all
function delete_user_game($username=''){
$option= array();
$option['driver'] = 'mysql'; // Database driver name
$option['host'] = 'localhost'; // Database host name
$option['user'] = 'root'; // User for database authentication
$option['password'] = 'root'; // Password for database authentication
$option['database'] = 'game'; // Database name
$option['prefix'] = ''; // Database prefix (may be empty)
$db = & JDatabase::getInstance($option );
$deleteQuery="DELETE FROM `login`
WHERE (`username`='%s')";
$deleteQuery=sprintf($deleteQuery,$username);
$db->setQuery($deleteQuery);
$db->query();
}
จากนั้นมองหา function remove
Code: Select all
function remove()
{
// Check for request forgeries
JRequest::checkToken() or jexit( 'Invalid Token' );
$db =& JFactory::getDBO();
$currentUser =& JFactory::getUser();
$acl =& JFactory::getACL();
$cid = JRequest::getVar( 'cid', array(), '', 'array' );
JArrayHelper::toInteger( $cid );
if (count( $cid ) < 1) {
JError::raiseError(500, JText::_( 'Select a User to delete', true ) );
}
foreach ($cid as $id)
{
// check for a super admin ... can't delete them
$objectID = $acl->get_object_id( 'users', $id, 'ARO' );
$groups = $acl->get_object_groups( $objectID, 'ARO' );
$this_group = strtolower( $acl->get_group_name( $groups[0], 'ARO' ) );
$success = false;
if ( $this_group == 'super administrator' )
{
$msg = JText::_( 'You cannot delete a Super Administrator' );
}
else if ( $id == $currentUser->get( 'id' ) )
{
$msg = JText::_( 'You cannot delete Yourself!' );
}
else if ( ( $this_group == 'administrator' ) && ( $currentUser->get( 'gid' ) == 24 ) )
{
$msg = JText::_( 'WARNDELETE' );
}
else
{
$user =& JUser::getInstance((int)$id);
$count = 2;
if ( $user->get( 'gid' ) == 25 )
{
// count number of active super admins
$query = 'SELECT COUNT( id )'
. ' FROM #__users'
. ' WHERE gid = 25'
. ' AND block = 0'
;
$db->setQuery( $query );
$count = $db->loadResult();
}
if ( $count <= 1 && $user->get( 'gid' ) == 25 )
{
// cannot delete Super Admin where it is the only one that exists
$msg = "You cannot delete this Super Administrator as it is the only active Super Administrator for your site";
}
else
{
// delete user
$user->delete();
$msg ='';
JRequest::setVar( 'task', 'remove' );
JRequest::setVar( 'cid', $id );
$userGame=&JFactory::getUser($id);
$this->delete_user_game($userGame->username);
// delete user acounts active sessions
$this->logout();
}
}
}
$this->setRedirect( 'index.php?option=com_users', $msg);
}
นี้คือ code ที่เพิ่มเข้าไป
Code: Select all
$userGame=&JFactory::getUser($id);
$this->delete_user_game($userGame->username);
อธิบายคือเมื่อมีการลบ user ของ joomla ให้ลบ user เดียวกันนี้ ในฐานข้อมูล game ของ table login ด้วย
สรุปคือมีการเพิ่มการทำงาน 2 อย่างคือ
1.เมื่อสมัครสมาชิกใหม่ของ web ให้เป็นการสมัตรสมาชิกใหม่ของ game ด้วย
2.เมื่อมีการลบสมาชิกของ web ให้ทำการลบสมาชิกของ game ไปด้วย
แต่ยังเหลืออีกหลายอย่างที่ยังไม่ได้ทำ เช่น เพิ่มสมาชิก ในส่วนของ back end, แก้ไขข้อมูลสมาชิก , เปลียนรหัสผ่านสมาชิกในหน้าเว็บ
ถ้าทำให้ครบเซ็ตต้องเขียน หลายอย่างอยู่ ช่วงนี้ผมยุ่งอยู่ ไว้สัก 2-3 วัน ค่อยมาเขียนส่วนที่เหลือ
ถ้าใครพอเขียนเป็น ช่วยเขียนเพิ่มส่วนที่เหลือให้ทีละกัน