Skip to content

Commit 7f3ed9b

Browse files
Merge pull request #41 from ankush-maherwal/joomla-4
Joomla 4
2 parents e136a5f + b330317 commit 7f3ed9b

File tree

7 files changed

+239
-183
lines changed

7 files changed

+239
-183
lines changed

src/language/en-GB/en-GB.plg_api_users.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
; @package API
2+
; @subpackage plg_api_users
3+
;
4+
; @author Techjoomla <extensions@techjoomla.com>
5+
; @copyright Copyright (C) 2009 - 2022 Techjoomla, Tekdi Technologies Pvt. Ltd. All rights reserved.
6+
; @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
7+
; Note All ini files need to be saved as UTF-8
8+
19
PLG_API_USERS="API - Users"
210
PLG_API_USERS_DESCRIPTION="This plugin exposes users to the Joomla! API. Supports creation, listing and login for users."
311
PLG_API_USERS_BAD_REQUEST_MESSAGE="Bad request"

src/users.php

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,43 @@
11
<?php
22
/**
3-
* @package API plugins
4-
* @copyright Copyright (C) 2009 2014 Techjoomla, Tekdi Technologies Pvt. Ltd. All rights reserved.
5-
* @license GNU GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
6-
* @link http://www.techjoomla.com
7-
*/
3+
* @package API
4+
* @subpackage plg_api_users
5+
*
6+
* @author Techjoomla <extensions@techjoomla.com>
7+
* @copyright Copyright (C) 2009 - 2022 Techjoomla, Tekdi Technologies Pvt. Ltd. All rights reserved.
8+
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
9+
*/
810

9-
defined('_JEXEC') or die( 'Restricted access' );
11+
// No direct access.
12+
defined('_JEXEC') or die('Restricted access');
1013

11-
jimport('joomla.plugin.plugin');
14+
use Joomla\CMS\Factory;
1215

13-
class plgAPIUsers extends ApiPlugin
16+
/**
17+
* Users plgAPI class
18+
*
19+
* @since 1.0.0
20+
*/
21+
class PlgAPIUsers extends ApiPlugin
1422
{
23+
/**
24+
* Constructor
25+
*
26+
* @param string &$subject subject
27+
* @param string $config config
28+
*/
1529
public function __construct(&$subject, $config = array())
1630
{
1731
parent::__construct($subject, $config = array());
1832

19-
ApiResource::addIncludePath(dirname(__FILE__).'/users');
33+
ApiResource::addIncludePath(dirname(__FILE__) . '/users');
2034

2135
/*load language file for plugin frontend*/
22-
$lang = JFactory::getLanguage();
23-
$lang->load('plg_api_users', JPATH_ADMINISTRATOR,'',true);
24-
36+
$lang = Factory::getLanguage();
37+
$lang->load('plg_api_users', JPATH_ADMINISTRATOR, '', true);
38+
2539
// Set the login resource to be public
26-
$this->setResourceAccess('login', 'public','get');
40+
$this->setResourceAccess('login', 'public', 'get');
2741
$this->setResourceAccess('users', 'public', 'post');
2842
$this->setResourceAccess('config', 'public', 'get');
2943
$this->setResourceAccess('user', 'public', 'post');

src/users.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<extension version="3.0.0" type="plugin" group="api" method="upgrade">
33
<name>PLG_API_USERS</name>
4-
<version>2.0.0</version>
5-
<creationDate>10th Jan 2019</creationDate>
4+
<version>3.0.0</version>
5+
<creationDate>22nd Sep 2022</creationDate>
66
<author>Techjoomla</author>
77
<authorEmail>extensions@techjoomla.com</authorEmail>
88
<authorUrl>www.techjoomla.com</authorUrl>

src/users/config.php

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
defined('_JEXEC') or die( 'Restricted access' );
1010

11-
jimport('joomla.plugin.plugin');
12-
jimport('joomla.html.html');
13-
jimport('joomla.application.component.controller');
14-
jimport('joomla.application.component.model');
15-
jimport('joomla.user.helper');
16-
jimport('joomla.user.user');
17-
jimport('joomla.application.component.helper');
11+
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
12+
use Joomla\CMS\Filesystem\File;
13+
use Joomla\CMS\Factory;
14+
use Joomla\CMS\Language\LanguageHelper;
15+
use Joomla\CMS\Language\Text;
16+
use Joomla\CMS\Plugin\PluginHelper;
17+
use Joomla\Registry\Registry;
1818

19-
JModelLegacy::addIncludePath(JPATH_SITE.'components/com_api/models');
19+
BaseDatabaseModel::addIncludePath(JPATH_SITE.'components/com_api/models');
2020
require_once JPATH_SITE.'/components/com_api/libraries/authentication/user.php';
2121
require_once JPATH_SITE.'/components/com_api/libraries/authentication/login.php';
2222

@@ -30,22 +30,22 @@ public function get()
3030
$easyblog = JPATH_ADMINISTRATOR .'/components/com_easyblog/easyblog.php';
3131
$easysocial = JPATH_ADMINISTRATOR .'/components/com_easysocial/easysocial.php';
3232
//eb version
33-
if( JFile::exists( $easyblog ) )
33+
if( File::exists( $easyblog ) )
3434
{
3535
$obj->easyblog = $this->getCompParams('com_easyblog','easyblog');
3636
}
3737
//es version
38-
if( JFile::exists( $easysocial ) )
38+
if( File::exists( $easysocial ) )
3939
{
40-
/*$xml = JFactory::getXML(JPATH_ADMINISTRATOR .'/components/com_easysocial/easyblog.xml');
40+
/*$xml = simplexml_load_file(JPATH_ADMINISTRATOR .'/components/com_easysocial/easyblog.xml');
4141
$obj->easysocial_version = (string)$xml->version;*/
4242
$obj->easysocial = $this->getCompParams( 'com_easysocial','easysocial' );
4343
}
4444

4545
$obj->global_config = $this->getJoomlaConfig();
4646
$obj->plugin_config = $this->getpluginConfig();
4747

48-
$installedLanguages = JLanguageHelper::getLanguages();
48+
$installedLanguages = LanguageHelper::getLanguages();
4949
$languages = array();
5050

5151
foreach($installedLanguages as $lang){
@@ -58,24 +58,23 @@ public function get()
5858

5959
public function post()
6060
{
61-
$this->plugin->setResponse( JText::_( 'PLG_API_USERS_UNSUPPORTED_METHOD_POST' ));
61+
$this->plugin->setResponse( Text::_( 'PLG_API_USERS_UNSUPPORTED_METHOD_POST' ));
6262
}
6363

6464

6565
//get component params
6666
public function getCompParams($cname=null,$name=null)
6767
{
68-
jimport('joomla.application.component.helper');
69-
$app = JFactory::getApplication();
68+
$app = Factory::getApplication();
7069
$cdata = array();
7170

72-
$xml = JFactory::getXML(JPATH_ADMINISTRATOR .'/components/'.$cname.'/'.$name.'.xml');
71+
$xml = simplexml_load_file(JPATH_ADMINISTRATOR .'/components/'.$cname.'/'.$name.'.xml');
7372
$cdata['version'] = (string)$xml->version;
74-
$jconfig = JFactory::getConfig();
73+
$jconfig = Factory::getConfig();
7574

7675
if( $cname == 'com_easyblog' )
7776
{
78-
/*$xml = JFactory::getXML(JPATH_ADMINISTRATOR .'/components/com_easyblog/easyblog.xml');
77+
/*$xml = simplexml_load_file(JPATH_ADMINISTRATOR .'/components/com_easyblog/easyblog.xml');
7978
$version = (string)$xml->version;*/
8079

8180
if($cdata['version']<5)
@@ -132,11 +131,11 @@ public function getCompParams($cname=null,$name=null)
132131
public function getpluginConfig()
133132
{
134133
$data = array();
135-
$plugin = JPluginHelper::getPlugin('api', 'users');
136-
$pluginParams = new JRegistry($plugin->params);
134+
$plugin = PluginHelper::getPlugin('api', 'users');
135+
$pluginParams = new Registry($plugin->params);
137136
//code for future use
138-
/*$plugin_es = JPluginHelper::getPlugin('api', 'easysocial');
139-
$pluginParams_es = new JRegistry($plugin_es->params);*/
137+
/*$plugin_es = PluginHelper::getPlugin('api', 'easysocial');
138+
$pluginParams_es = new Registry($plugin_es->params);*/
140139

141140
$data['fb_login'] = $pluginParams->get('fb_login');
142141
$data['fb_app_id'] = $pluginParams->get('fb_app_id');
@@ -149,7 +148,7 @@ public function getpluginConfig()
149148
//get joomla config changes
150149
public function getJoomlaConfig()
151150
{
152-
$jconfig = JFactory::getConfig();
151+
$jconfig = Factory::getConfig();
153152
$jarray = array();
154153
$jarray['global_list_limit'] = $jconfig->get('list_limit');
155154
$jarray['offset'] = $jconfig->get('offset');

src/users/login.php

Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,28 @@
44
* @subpackage plg_api_users
55
*
66
* @author Techjoomla <extensions@techjoomla.com>
7-
* @copyright Copyright (C) 2009 - 2019 Techjoomla, Tekdi Technologies Pvt. Ltd. All rights reserved.
7+
* @copyright Copyright (C) 2009 - 2022 Techjoomla, Tekdi Technologies Pvt. Ltd. All rights reserved.
88
* @license GNU GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
99
*/
1010

1111
// No direct access.
1212
defined('_JEXEC') or die('Restricted access');
1313

14-
require_once JPATH_SITE . '/components/com_api/vendors/php-jwt/src/JWT.php';
14+
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
15+
use Joomla\CMS\Language\Text;
16+
use Joomla\CMS\User\User;
17+
use Joomla\CMS\Factory;
18+
use Joomla\CMS\Session\Session;
19+
use Joomla\CMS\Table\Table;
20+
use Joomla\CMS\Filesystem\File;
21+
use Joomla\CMS\Component\ComponentHelper;
22+
use Joomla\CMS\User\UserHelper;
1523

24+
require_once JPATH_SITE . '/components/com_api/vendors/php-jwt/src/JWT.php';
1625
use Firebase\JWT\JWT;
1726

18-
jimport('joomla.plugin.plugin');
19-
jimport('joomla.html.html');
20-
jimport('joomla.application.component.controller');
21-
jimport('joomla.application.component.model');
22-
jimport('joomla.user.helper');
23-
jimport('joomla.user.user');
24-
jimport('joomla.application.component.helper');
27+
BaseDatabaseModel::addIncludePath(JPATH_SITE . 'components/com_api/models');
2528

26-
JModelLegacy::addIncludePath(JPATH_SITE . 'components/com_api/models');
2729
require_once JPATH_SITE . '/components/com_api/libraries/authentication/user.php';
2830
require_once JPATH_SITE . '/components/com_api/libraries/authentication/login.php';
2931
require_once JPATH_ADMINISTRATOR . '/components/com_api/models/key.php';
@@ -44,7 +46,7 @@ class UsersApiResourceLogin extends ApiResource
4446
*/
4547
public function get()
4648
{
47-
$this->plugin->setResponse(JText::_('PLG_API_USERS_GET_METHOD_NOT_ALLOWED_MESSAGE'));
49+
$this->plugin->setResponse(Text::_('PLG_API_USERS_GET_METHOD_NOT_ALLOWED_MESSAGE'));
4850
}
4951

5052
/**
@@ -65,33 +67,43 @@ public function post()
6567
public function keygen()
6668
{
6769
// Init variable
68-
$obj = new stdclass;
69-
$umodel = new JUser;
70-
$user = $umodel->getInstance();
71-
72-
$app = JFactory::getApplication();
70+
$obj = new stdclass;
71+
$app = Factory::getApplication();
7372
$username = $app->input->get('username', 0, 'STRING');
7473

75-
$user = JFactory::getUser();
76-
$id = JUserHelper::getUserId($username);
74+
$user = Factory::getUser();
75+
$id = UserHelper::getUserId($username);
7776

78-
if ($id == null)
77+
if ($username)
78+
{
79+
$umodel = new User;
80+
$user = $umodel->getInstance();
81+
82+
$userId = UserHelper::getUserId($username);
83+
84+
if ($userId == null)
85+
{
86+
$keysModel = FD::model('Users');
87+
$userId = $keysModel->getUserId('email', $username);
88+
}
89+
}
90+
else
7991
{
80-
$model = FD::model('Users');
81-
$id = $model->getUserId('email', $username);
92+
$userId = $user->id;
8293
}
8394

84-
$kmodel = new ApiModelKey;
85-
$model = new ApiModelKeys;
86-
$key = null;
95+
// Init vars
96+
$keyModel = new ApiModelKey;
97+
$keysModel = new ApiModelKeys;
98+
$key = null;
8799

88100
// Get login user hash
89-
// $kmodel->setState('user_id', $user->id);
101+
// $keyModel->setState('user_id', $user->id);
90102

91-
// $kmodel->setState('user_id', $id);
92-
// $log_hash = $kmodel->getList();
93-
$model->setState('user_id', $id);
94-
$log_hash = $model->getItems();
103+
// $keyModel->setState('user_id', $id);
104+
// $log_hash = $keyModel->getList();
105+
$keysModel->setState('user_id', $userId);
106+
$log_hash = $keysModel->getItems();
95107

96108
$log_hash = (!empty($log_hash)) ? $log_hash[count($log_hash) - count($log_hash)] : $log_hash;
97109

@@ -103,18 +115,18 @@ public function keygen()
103115
{
104116
// Create new key for user
105117
$data = array (
106-
'userid' => $user->id,
118+
'userid' => $userId,
107119
'domain' => '' ,
108120
'state' => 1,
109121
'id' => '',
110122
'task' => 'save',
111123
'c' => 'key',
112124
'ret' => 'index.php?option=com_api&view=keys',
113125
'option' => 'com_api',
114-
JSession::getFormToken() => 1
126+
Session::getFormToken() => 1
115127
);
116128

117-
$result = $kmodel->save($data);
129+
$result = $keyModel->save($data);
118130

119131
// $key = $result->hash;
120132

@@ -124,15 +136,16 @@ public function keygen()
124136
}
125137

126138
// Load api key table
127-
JTable::addIncludePath(JPATH_ROOT . '/administrator/components/com_api/tables');
128-
$table = JTable::getInstance('Key', 'ApiTable');
139+
Table::addIncludePath(JPATH_ROOT . '/administrator/components/com_api/tables');
140+
$table = Table::getInstance('Key', 'ApiTable');
129141
$table->load(array('userid' => $user->id));
142+
130143
$key = $table->hash;
131144

132145
// Add new key in easysocial table
133146
$easyblog = JPATH_ROOT . '/administrator/components/com_easyblog/easyblog.php';
134147

135-
if (JFile::exists($easyblog) && JComponentHelper::isEnabled('com_easysocial', true))
148+
if (File::exists($easyblog) && ComponentHelper::isEnabled('com_easysocial', true))
136149
{
137150
$this->updateEauth($user, $key);
138151
}
@@ -143,13 +156,22 @@ public function keygen()
143156
$obj->auth = $key;
144157
$obj->code = '200';
145158

159+
// For backward compatability - TODO
160+
$obj->token = $key;
161+
146162
// $obj->id = $user->id;
163+
// $obj->id = $id;
147164

148-
$obj->id = $id;
165+
// Set user details for response
166+
$obj->id = $userId;
167+
$obj->name = Factory::getUser($userId)->name;
168+
$obj->username = Factory::getUser($userId)->username;
169+
$obj->email = Factory::getUser($userId)->email;
149170

150171
// Generate claim for jwt
151172
$data = [
152-
"id" => trim($id),
173+
"id" => trim($userId),
174+
153175
/*"iat" => '',
154176
"exp" => '',
155177
"aud" => '',
@@ -171,7 +193,7 @@ public function keygen()
171193
else
172194
{
173195
$obj->code = 403;
174-
$obj->message = JText::_('PLG_API_USERS_BAD_REQUEST_MESSAGE');
196+
$obj->message = Text::_('PLG_API_USERS_BAD_REQUEST_MESSAGE');
175197
}
176198

177199
return ($obj);
@@ -191,9 +213,10 @@ public function updateEauth ($user = null, $key = null)
191213
{
192214
require_once JPATH_ADMINISTRATOR . '/components/com_easysocial/includes/foundry.php';
193215

194-
$model = FD::model('Users');
195-
$id = $model->getUserId('username', $user->username);
196-
$user = FD::user($id);
216+
$keysModel = FD::model('Users');
217+
$id = $keysModel->getUserId('username', $user->username);
218+
$user = FD::user($id);
219+
197220
$user->alias = $user->username;
198221
$user->auth = $key;
199222
$user->store();

0 commit comments

Comments
 (0)