Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New joomla user search API #66

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions users/users.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,46 @@
<?php
/**
* @package API plugins
* @copyright Copyright (C) 2009 2014 Techjoomla, Tekdi Technologies Pvt. Ltd. All rights reserved.
* @license GNU GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
* @link http://www.techjoomla.com
*/
* @version SVN: <svn_id>
* @package com_api.plugins
* @author Techjoomla <[email protected]>
* @copyright Copyright (c) 2009-2017 TechJoomla. All rights reserved.
* @license GNU General Public License version 2 or later.
*/

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

jimport('joomla.plugin.plugin');

class plgAPIUsers extends ApiPlugin
/**
* Base Class for api plugin
*
* @package com_api.plugins
* @subpackage plugins
* @since 1.0
*/

class PlgAPIUsers extends ApiPlugin
{
/**
* Users api plugin to load com_api classes
*
* @param string &$subject originalamount
* @param array $config coupon_code
*
* @since 1.0
*/
public function __construct(&$subject, $config = array())
{
parent::__construct($subject, $config = array());

ApiResource::addIncludePath(dirname(__FILE__).'/users');
/*load language file for plugin frontend*/
$lang = JFactory::getLanguage();
$lang->load('plg_api_users', JPATH_ADMINISTRATOR,'',true);
ApiResource::addIncludePath(dirname(__FILE__) . '/users');

/*load language file for plugin frontend*/
$lang = JFactory::getLanguage();
$lang->load('plg_api_users', JPATH_ADMINISTRATOR, '', true);

// Set the login resource to be public
$this->setResourceAccess('login', 'public','get');
$this->setResourceAccess('login', 'public', 'get');
$this->setResourceAccess('users', 'public', 'post');
$this->setResourceAccess('config', 'public', 'get');
}
Expand Down
4 changes: 2 additions & 2 deletions users/users/config.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @package API plugins
* @copyright Copyright (C) 2009 2014 Techjoomla, Tekdi Technologies Pvt. Ltd. All rights reserved.
* @package com_api.plugins
* @copyright Copyright (C) 2009 - 2017 Techjoomla, Tekdi Technologies Pvt. Ltd. All rights reserved.
* @license GNU GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
* @link http://www.techjoomla.com
*/
Expand Down
4 changes: 2 additions & 2 deletions users/users/login.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @package API plugins
* @copyright Copyright (C) 2009 2014 Techjoomla, Tekdi Technologies Pvt. Ltd. All rights reserved.
* @package com_api.plugins
* @copyright Copyright (C) 2009 - 2017 Techjoomla, Tekdi Technologies Pvt. Ltd. All rights reserved.
* @license GNU GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
* @link http://www.techjoomla.com
*/
Expand Down
46 changes: 46 additions & 0 deletions users/users/user.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coolbung should resource name be search?

Copy link
Contributor

@thite-amol thite-amol Jan 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think search API is nothing but the list view with the additional filters. Can we merge this code with users post API? cc @coolbung

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Please go ahead as discussed.

Once merged, moved out the plugins in the respective repos. Any plugin that does not have a specific repo can stay here.

/**
* @version SVN: <svn_id>
* @package com_api.plugins
* @author Techjoomla <[email protected]>
* @copyright Copyright (c) 2009-2017 TechJoomla. All rights reserved.
* @license GNU General Public License version 2 or later.
*/
// No direct access.
defined('_JEXEC') or die;

require JPATH_SITE . '/plugins/api/users/users/userService.php';

/**
* User Api.
*
* @package com_api.plugins
* @subpackage plugins
*
* @since 1.0
*/

class UsersApiResourceUser extends ApiResource
{
/**
* Function post for create user record.
*
* @return void
*/
public function post()
{
// Get request body
$requestBody = file_get_contents('php://input');

$app = JFactory::getApplication();
$app->set('reqBody', $requestBody);

$model = JModelLegacy::getInstance('UsersSearch', 'UsersModel');

$data = $model->getItems();

$this->plugin->setResponse($data);

return;
}
}
151 changes: 151 additions & 0 deletions users/users/userService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php
/**
* @version SVN: <svn_id>
* @package com_api.plugins
* @author Techjoomla <[email protected]>
* @copyright Copyright (c) 2009-2017 TechJoomla. All rights reserved.
* @license GNU General Public License version 2 or later.
*/
// No direct access.
defined('_JEXEC') or die;

require_once JPATH_ROOT . '/administrator/components/com_users/models/users.php';

/**
* User Api.
*
* @package com_api.plugins
* @subpackage plugins
*
* @since 1.0
*/
class UsersModelUsersSearch extends UsersModelUsers
{
/**
* Build an SQL query to load the list data.
*
* @return JDatabaseQuery
*
* @since 1.6
*/
public function getListQuery()
{
$app = JFactory::getApplication();
$getReqBody = json_decode($app->get('reqBody'));

$db = JFactory::getDbo();

$query = $db->getQuery(true);

// Select the required fields from the table.
$query->select(
$this->getState(
'list.select',
'a.id, a.name, a.username, a.email, a.registerDate'
)
);

$query->from($db->quoteName('#__users') . ' AS a');

// Check user search filter
$search = isset($getReqBody->request->search) ? $getReqBody->request->search : false;

if (!empty($search))
{
$search = ('%' . str_replace(' ', '%', trim($search) . '%'));

$query->where($db->quoteName('a.name') . ' LIKE ' . $db->quote($search));
}

// Check user block filter
$block = isset($getReqBody->request->filters->block) ? (array) $getReqBody->request->filters->block : array();

if (!empty($block))
{
foreach ($block as &$blockValue)
{
$blockValue = 'a.block = ' . (int) $blockValue;
}

$query->where('(' . implode(' OR ', $block) . ')');
}

// Check user id filter
$userIds = isset($getReqBody->request->filters->id) ? (array) $getReqBody->request->filters->id : array();

if (!empty($userIds))
{
foreach ($userIds as &$userId)
{
$userId = 'a.id = ' . (int) $userId;
}

$query->where('(' . implode(' OR ', $userIds) . ')');
}

// Check user email filter
$userEmails = isset($getReqBody->request->filters->email) ? (array) $getReqBody->request->filters->email : array();

if (!empty($userEmails))
{
foreach ($userEmails as &$userEmail)
{
$userEmail = 'a.email = ' . $db->quote($userEmail);
}

$query->where('(' . implode(' OR ', $userEmails) . ')');
}

// Check user username filter
$usernames = isset($getReqBody->request->filters->username) ? (array) $getReqBody->request->filters->username : array();

if (!empty($usernames))
{
foreach ($usernames as &$username)
{
$username = 'a.username = ' . $db->quote($username);
}

$query->where('(' . implode(' OR ', $usernames) . ')');
}

// Check list ordering filter
if (isset($getReqBody->request->sort_by->name))
{
$orderColumn = 'name';
$ordering = $getReqBody->request->sort_by->name;
}
elseif (isset($getReqBody->request->sort_by->id))
{
$orderColumn = 'id';
$ordering = $getReqBody->request->sort_by->id;
}
elseif (isset($getReqBody->request->sort_by->email))
{
$orderColumn = 'email';
$ordering = $getReqBody->request->sort_by->email;
}
elseif (isset($getReqBody->request->sort_by->username))
{
$orderColumn = 'username';
$ordering = $getReqBody->request->sort_by->username;
}
else
{
$ordering = array();
}

if (!empty($ordering))
{
$query->order('a.' . $orderColumn . ' ' . $ordering);
}

// Check offset & limit filter
$offset = isset($getReqBody->request->offset) ? $getReqBody->request->offset : array();
$limit = isset($getReqBody->request->limit) ? $getReqBody->request->limit : array();

$query->setLimit((int) $limit, (int) $offset);

return $query;
}
}
14 changes: 7 additions & 7 deletions users/users/users.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
/**
* @package Joomla.Administrator
* @subpackage com_trading
*
* @copyright Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
* @version SVN: <svn_id>
* @package com_api.plugins
* @author Techjoomla <[email protected]>
* @copyright Copyright (c) 2009-2017 TechJoomla. All rights reserved.
* @license GNU General Public License version 2 or later.
*/
// No direct access.
defined('_JEXEC') or die;
Expand All @@ -27,8 +27,8 @@
/**
* User Api.
*
* @package Joomla.Administrator
* @subpackage com_api
* @package com_api.plugins
* @subpackage plugins
*
* @since 1.0
*/
Expand Down