4
4
* @subpackage plg_api_users
5
5
*
6
6
* @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.
8
8
* @license GNU GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
9
9
*/
10
10
11
11
// No direct access.
12
12
defined ('_JEXEC ' ) or die ('Restricted access ' );
13
13
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 ;
15
23
24
+ require_once JPATH_SITE . '/components/com_api/vendors/php-jwt/src/JWT.php ' ;
16
25
use Firebase \JWT \JWT ;
17
26
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 ' );
25
28
26
- JModelLegacy::addIncludePath (JPATH_SITE . 'components/com_api/models ' );
27
29
require_once JPATH_SITE . '/components/com_api/libraries/authentication/user.php ' ;
28
30
require_once JPATH_SITE . '/components/com_api/libraries/authentication/login.php ' ;
29
31
require_once JPATH_ADMINISTRATOR . '/components/com_api/models/key.php ' ;
@@ -44,7 +46,7 @@ class UsersApiResourceLogin extends ApiResource
44
46
*/
45
47
public function get ()
46
48
{
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 ' ));
48
50
}
49
51
50
52
/**
@@ -65,33 +67,43 @@ public function post()
65
67
public function keygen ()
66
68
{
67
69
// 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 ();
73
72
$ username = $ app ->input ->get ('username ' , 0 , 'STRING ' );
74
73
75
- $ user = JFactory ::getUser ();
76
- $ id = JUserHelper ::getUserId ($ username );
74
+ $ user = Factory ::getUser ();
75
+ $ id = UserHelper ::getUserId ($ username );
77
76
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
79
91
{
80
- $ model = FD ::model ('Users ' );
81
- $ id = $ model ->getUserId ('email ' , $ username );
92
+ $ userId = $ user ->id ;
82
93
}
83
94
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 ;
87
99
88
100
// Get login user hash
89
- // $kmodel ->setState('user_id', $user->id);
101
+ // $keyModel ->setState('user_id', $user->id);
90
102
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 ();
95
107
96
108
$ log_hash = (!empty ($ log_hash )) ? $ log_hash [count ($ log_hash ) - count ($ log_hash )] : $ log_hash ;
97
109
@@ -103,18 +115,18 @@ public function keygen()
103
115
{
104
116
// Create new key for user
105
117
$ data = array (
106
- 'userid ' => $ user -> id ,
118
+ 'userid ' => $ userId ,
107
119
'domain ' => '' ,
108
120
'state ' => 1 ,
109
121
'id ' => '' ,
110
122
'task ' => 'save ' ,
111
123
'c ' => 'key ' ,
112
124
'ret ' => 'index.php?option=com_api&view=keys ' ,
113
125
'option ' => 'com_api ' ,
114
- JSession ::getFormToken () => 1
126
+ Session ::getFormToken () => 1
115
127
);
116
128
117
- $ result = $ kmodel ->save ($ data );
129
+ $ result = $ keyModel ->save ($ data );
118
130
119
131
// $key = $result->hash;
120
132
@@ -124,15 +136,16 @@ public function keygen()
124
136
}
125
137
126
138
// 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 ' );
129
141
$ table ->load (array ('userid ' => $ user ->id ));
142
+
130
143
$ key = $ table ->hash ;
131
144
132
145
// Add new key in easysocial table
133
146
$ easyblog = JPATH_ROOT . '/administrator/components/com_easyblog/easyblog.php ' ;
134
147
135
- if (JFile ::exists ($ easyblog ) && JComponentHelper ::isEnabled ('com_easysocial ' , true ))
148
+ if (File ::exists ($ easyblog ) && ComponentHelper ::isEnabled ('com_easysocial ' , true ))
136
149
{
137
150
$ this ->updateEauth ($ user , $ key );
138
151
}
@@ -143,13 +156,22 @@ public function keygen()
143
156
$ obj ->auth = $ key ;
144
157
$ obj ->code = '200 ' ;
145
158
159
+ // For backward compatability - TODO
160
+ $ obj ->token = $ key ;
161
+
146
162
// $obj->id = $user->id;
163
+ // $obj->id = $id;
147
164
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 ;
149
170
150
171
// Generate claim for jwt
151
172
$ data = [
152
- "id " => trim ($ id ),
173
+ "id " => trim ($ userId ),
174
+
153
175
/*"iat" => '',
154
176
"exp" => '',
155
177
"aud" => '',
@@ -171,7 +193,7 @@ public function keygen()
171
193
else
172
194
{
173
195
$ obj ->code = 403 ;
174
- $ obj ->message = JText ::_ ('PLG_API_USERS_BAD_REQUEST_MESSAGE ' );
196
+ $ obj ->message = Text ::_ ('PLG_API_USERS_BAD_REQUEST_MESSAGE ' );
175
197
}
176
198
177
199
return ($ obj );
@@ -191,9 +213,10 @@ public function updateEauth ($user = null, $key = null)
191
213
{
192
214
require_once JPATH_ADMINISTRATOR . '/components/com_easysocial/includes/foundry.php ' ;
193
215
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
+
197
220
$ user ->alias = $ user ->username ;
198
221
$ user ->auth = $ key ;
199
222
$ user ->store ();
0 commit comments