|
1 | 1 | <?php |
2 | 2 | /** |
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> |
7 | 9 | */ |
8 | 10 |
|
9 | | -defined('_JEXEC') or die( 'Restricted access' ); |
| 11 | +// No direct access. |
| 12 | +defined('_JEXEC') or die('Restricted access'); |
10 | 13 |
|
11 | 14 | require_once JPATH_SITE . '/components/com_api/vendors/php-jwt/src/JWT.php'; |
12 | 15 |
|
|
23 | 26 | JModelLegacy::addIncludePath(JPATH_SITE . 'components/com_api/models'); |
24 | 27 | require_once JPATH_SITE . '/components/com_api/libraries/authentication/user.php'; |
25 | 28 | 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'; |
28 | 31 |
|
| 32 | +/** |
| 33 | + * Login API resource class |
| 34 | + * |
| 35 | + * @package API |
| 36 | + * @since 1.6.0 |
| 37 | + */ |
29 | 38 | class UsersApiResourceLogin extends ApiResource |
30 | 39 | { |
| 40 | + /** |
| 41 | + * Get method |
| 42 | + * |
| 43 | + * @return object |
| 44 | + */ |
31 | 45 | public function get() |
32 | 46 | { |
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')); |
34 | 48 | } |
35 | 49 |
|
| 50 | + /** |
| 51 | + * Post method |
| 52 | + * |
| 53 | + * @return object |
| 54 | + */ |
36 | 55 | public function post() |
37 | 56 | { |
38 | 57 | $this->plugin->setResponse($this->keygen()); |
39 | 58 | } |
40 | 59 |
|
| 60 | + /** |
| 61 | + * Generate key method |
| 62 | + * |
| 63 | + * @return object|boolean |
| 64 | + */ |
41 | 65 | public function keygen() |
42 | 66 | { |
43 | | - //init variable |
44 | | - $obj = new stdclass; |
| 67 | + // Init variable |
| 68 | + $obj = new stdclass; |
45 | 69 | $umodel = new JUser; |
46 | | - $user = $umodel->getInstance(); |
| 70 | + $user = $umodel->getInstance(); |
47 | 71 |
|
48 | | - $app = JFactory::getApplication(); |
| 72 | + $app = JFactory::getApplication(); |
49 | 73 | $username = $app->input->get('username', 0, 'STRING'); |
50 | 74 |
|
51 | 75 | $user = JFactory::getUser(); |
52 | | - $id = JUserHelper::getUserId($username); |
| 76 | + $id = JUserHelper::getUserId($username); |
53 | 77 |
|
54 | | - if($id == null) |
| 78 | + if ($id == null) |
55 | 79 | { |
56 | 80 | $model = FD::model('Users'); |
57 | | - $id = $model->getUserId('email', $username); |
| 81 | + $id = $model->getUserId('email', $username); |
58 | 82 | } |
59 | 83 |
|
60 | 84 | $kmodel = new ApiModelKey; |
61 | | - $model = new ApiModelKeys; |
62 | | - $key = null; |
| 85 | + $model = new ApiModelKeys; |
| 86 | + $key = null; |
| 87 | + |
63 | 88 | // 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; |
68 | 97 |
|
69 | | - if( !empty($log_hash) ) |
| 98 | + if (!empty($log_hash)) |
70 | 99 | { |
71 | 100 | $key = $log_hash->hash; |
72 | 101 | } |
73 | | - elseif( $key == null || empty($key) ) |
| 102 | + elseif ($key == null || empty($key)) |
74 | 103 | { |
75 | | - // Create new key for user |
76 | | - $data = array( |
| 104 | + // Create new key for user |
| 105 | + $data = array ( |
77 | 106 | 'userid' => $user->id, |
78 | 107 | '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', |
84 | 113 | 'option' => 'com_api', |
85 | 114 | JSession::getFormToken() => 1 |
86 | | - ); |
| 115 | + ); |
87 | 116 |
|
88 | | - $result = $kmodel->save($data); |
89 | | - $key = $result->hash; |
| 117 | + $result = $kmodel->save($data); |
90 | 118 |
|
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 | + } |
97 | 139 | } |
98 | 140 |
|
99 | | - if( !empty($key) ) |
| 141 | + if (!empty($key)) |
100 | 142 | { |
101 | 143 | $obj->auth = $key; |
102 | 144 | $obj->code = '200'; |
103 | | - //$obj->id = $user->id; |
| 145 | + |
| 146 | + // $obj->id = $user->id; |
| 147 | + |
104 | 148 | $obj->id = $id; |
105 | 149 |
|
106 | 150 | // Generate claim for jwt |
@@ -129,21 +173,29 @@ public function keygen() |
129 | 173 | $obj->code = 403; |
130 | 174 | $obj->message = JText::_('PLG_API_USERS_BAD_REQUEST_MESSAGE'); |
131 | 175 | } |
132 | | - return( $obj ); |
133 | 176 |
|
| 177 | + return ($obj); |
134 | 178 | } |
135 | 179 |
|
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 |
138 | 189 | */ |
139 | | - public function updateEauth($user=null,$key=null) |
| 190 | + public function updateEauth ($user = null, $key = null) |
140 | 191 | { |
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); |
145 | 197 | $user->alias = $user->username; |
146 | | - $user->auth = $key; |
| 198 | + $user->auth = $key; |
147 | 199 | $user->store(); |
148 | 200 |
|
149 | 201 | return $id; |
|
0 commit comments