Skip to content

Commit e40b710

Browse files
authored
fix: Use backend model to get / generate key/hash (#11)
* Task #139177 fix: Use backend model to get / generate key/hash * Task #139177 fix: Scrutinizer issues
1 parent 221a0a3 commit e40b710

File tree

1 file changed

+102
-50
lines changed

1 file changed

+102
-50
lines changed

src/users/login.php

Lines changed: 102 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
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
3+
* @package API
4+
* @subpackage plg_api_users
5+
*
6+
* @author Techjoomla <extensions@techjoomla.com>
7+
* @copyright Copyright (C) 2009 - 2019 Techjoomla, Tekdi Technologies Pvt. Ltd. All rights reserved.
8+
* @license GNU GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
79
*/
810

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

1114
require_once JPATH_SITE . '/components/com_api/vendors/php-jwt/src/JWT.php';
1215

@@ -23,84 +26,125 @@
2326
JModelLegacy::addIncludePath(JPATH_SITE . 'components/com_api/models');
2427
require_once JPATH_SITE . '/components/com_api/libraries/authentication/user.php';
2528
require_once JPATH_SITE . '/components/com_api/libraries/authentication/login.php';
26-
require_once JPATH_SITE . '/components/com_api/models/key.php';
27-
require_once JPATH_SITE . '/components/com_api/models/keys.php';
29+
require_once JPATH_ADMINISTRATOR . '/components/com_api/models/key.php';
30+
require_once JPATH_ADMINISTRATOR . '/components/com_api/models/keys.php';
2831

32+
/**
33+
* Login API resource class
34+
*
35+
* @package API
36+
* @since 1.6.0
37+
*/
2938
class UsersApiResourceLogin extends ApiResource
3039
{
40+
/**
41+
* Get method
42+
*
43+
* @return object
44+
*/
3145
public function get()
3246
{
33-
$this->plugin->setResponse( JText::_('PLG_API_USERS_GET_METHOD_NOT_ALLOWED_MESSAGE'));
47+
$this->plugin->setResponse(JText::_('PLG_API_USERS_GET_METHOD_NOT_ALLOWED_MESSAGE'));
3448
}
3549

50+
/**
51+
* Post method
52+
*
53+
* @return object
54+
*/
3655
public function post()
3756
{
3857
$this->plugin->setResponse($this->keygen());
3958
}
4059

60+
/**
61+
* Generate key method
62+
*
63+
* @return object|boolean
64+
*/
4165
public function keygen()
4266
{
43-
//init variable
44-
$obj = new stdclass;
67+
// Init variable
68+
$obj = new stdclass;
4569
$umodel = new JUser;
46-
$user = $umodel->getInstance();
70+
$user = $umodel->getInstance();
4771

48-
$app = JFactory::getApplication();
72+
$app = JFactory::getApplication();
4973
$username = $app->input->get('username', 0, 'STRING');
5074

5175
$user = JFactory::getUser();
52-
$id = JUserHelper::getUserId($username);
76+
$id = JUserHelper::getUserId($username);
5377

54-
if($id == null)
78+
if ($id == null)
5579
{
5680
$model = FD::model('Users');
57-
$id = $model->getUserId('email', $username);
81+
$id = $model->getUserId('email', $username);
5882
}
5983

6084
$kmodel = new ApiModelKey;
61-
$model = new ApiModelKeys;
62-
$key = null;
85+
$model = new ApiModelKeys;
86+
$key = null;
87+
6388
// Get login user hash
64-
//$kmodel->setState('user_id', $user->id);
65-
$kmodel->setState('user_id', $id);
66-
$log_hash = $kmodel->getList();
67-
$log_hash = (!empty($log_hash))?$log_hash[count($log_hash) - count($log_hash)]:$log_hash;
89+
// $kmodel->setState('user_id', $user->id);
90+
91+
// $kmodel->setState('user_id', $id);
92+
// $log_hash = $kmodel->getList();
93+
$model->setState('user_id', $id);
94+
$log_hash = $model->getItems();
95+
96+
$log_hash = (!empty($log_hash)) ? $log_hash[count($log_hash) - count($log_hash)] : $log_hash;
6897

69-
if( !empty($log_hash) )
98+
if (!empty($log_hash))
7099
{
71100
$key = $log_hash->hash;
72101
}
73-
elseif( $key == null || empty($key) )
102+
elseif ($key == null || empty($key))
74103
{
75-
// Create new key for user
76-
$data = array(
104+
// Create new key for user
105+
$data = array (
77106
'userid' => $user->id,
78107
'domain' => '' ,
79-
'state' => 1,
80-
'id' => '',
81-
'task' => 'save',
82-
'c' => 'key',
83-
'ret' => 'index.php?option=com_api&view=keys',
108+
'state' => 1,
109+
'id' => '',
110+
'task' => 'save',
111+
'c' => 'key',
112+
'ret' => 'index.php?option=com_api&view=keys',
84113
'option' => 'com_api',
85114
JSession::getFormToken() => 1
86-
);
115+
);
87116

88-
$result = $kmodel->save($data);
89-
$key = $result->hash;
117+
$result = $kmodel->save($data);
90118

91-
//add new key in easysocial table
92-
$easyblog = JPATH_ROOT . '/administrator/components/com_easyblog/easyblog.php';
93-
if (JFile::exists($easyblog) && JComponentHelper::isEnabled('com_easysocial', true))
94-
{
95-
$this->updateEauth( $user , $key );
96-
}
119+
// $key = $result->hash;
120+
121+
if (!$result)
122+
{
123+
return false;
124+
}
125+
126+
// Load api key table
127+
JTable::addIncludePath(JPATH_ROOT . '/administrator/components/com_api/tables');
128+
$table = JTable::getInstance('Key', 'ApiTable');
129+
$table->load(array('userid' => $user->id));
130+
$key = $table->hash;
131+
132+
// Add new key in easysocial table
133+
$easyblog = JPATH_ROOT . '/administrator/components/com_easyblog/easyblog.php';
134+
135+
if (JFile::exists($easyblog) && JComponentHelper::isEnabled('com_easysocial', true))
136+
{
137+
$this->updateEauth($user, $key);
138+
}
97139
}
98140

99-
if( !empty($key) )
141+
if (!empty($key))
100142
{
101143
$obj->auth = $key;
102144
$obj->code = '200';
103-
//$obj->id = $user->id;
145+
146+
// $obj->id = $user->id;
147+
104148
$obj->id = $id;
105149

106150
// Generate claim for jwt
@@ -129,21 +173,29 @@ public function keygen()
129173
$obj->code = 403;
130174
$obj->message = JText::_('PLG_API_USERS_BAD_REQUEST_MESSAGE');
131175
}
132-
return( $obj );
133176

177+
return ($obj);
134178
}
135179

136-
/*
137-
* function to update Easyblog auth keys
180+
/**
181+
* Method to update Easyblog auth keys
182+
*
183+
* @param mixed $user User object
184+
* @param mixed $key Key
185+
*
186+
* @return integer
187+
*
188+
* @since 1.6
138189
*/
139-
public function updateEauth($user=null,$key=null)
190+
public function updateEauth ($user = null, $key = null)
140191
{
141-
require_once JPATH_ADMINISTRATOR.'/components/com_easysocial/includes/foundry.php';
142-
$model = FD::model('Users');
143-
$id = $model->getUserId('username', $user->username);
144-
$user = FD::user($id);
192+
require_once JPATH_ADMINISTRATOR . '/components/com_easysocial/includes/foundry.php';
193+
194+
$model = FD::model('Users');
195+
$id = $model->getUserId('username', $user->username);
196+
$user = FD::user($id);
145197
$user->alias = $user->username;
146-
$user->auth = $key;
198+
$user->auth = $key;
147199
$user->store();
148200

149201
return $id;

0 commit comments

Comments
 (0)