Commit b5b899b1 authored by Robert Lyon's avatar Robert Lyon Committed by Gerrit Code Review

Change user searches to autocomplete boxes (Bug #1396845)

Now we pass round the userid rather than the username as well

Change-Id: I633d058e7b0cc6efd9b3fcb626635f380aa55968
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent da2b9fb3
{include file="header.tpl"}
<p>{str tag="usersearchinstructions" section="str.webservice"}</p>
<div id="initials">
<label>{str tag="firstname"}:</label>
<span class="{if !$search->f} selected{/if} all">
<a href="{$WWWROOT}webservice/admin/search.php{if $search->l}?l={$search->l}{/if}">{str tag="All"}</a>
</span>
{foreach from=$alphabet item=a}
<span class="{if $a == $search->f} selected{/if}">
<a href="{$WWWROOT}webservice/admin/search.php?f={$a}{if $search->l}&amp;l={$search->l}{/if}">{$a}</a>
</span>
{/foreach}
<br />
<label>{str tag="lastname"}:</label>
<span class="{if !$search->l} selected{/if} all">
<a href="{$WWWROOT}webservice/admin/search.php{if $search->f}?f={$search->f}{/if}">{str tag="All"}</a>
</span>
{foreach from=$alphabet item=a}
<span class="{if $a == $search->l} selected{/if}">
<a href="{$WWWROOT}webservice/admin/search.php?l={$a}{if $search->f}&amp;f={$search->f}{/if}">{$a}</a>
</span>
{/foreach}
</div>
<form action="{$WWWROOT}webservice/admin/search.php" method="post">
<div class="searchform">
<label>{str tag='Search' section='admin'}:</label>
<input type="text" name="query" id="query"{if $search->query} value="{$search->query}"{/if}>
{if count($institutions) > 1}
<span class="institutions">
<label>{str tag='Institution' section='admin'}:</label>
{if $USER->get('admin')}
<select name="institution" id="institution">
{else}
<select name="institution_requested" id="institution_requested">
{/if}
<option value="all"{if !$.request.institution} selected="selected"{/if}>{str tag=All}</option>
{foreach from=$institutions item=i}
<option value="{$i->name}"{if $i->name == $.request.institution}" selected="selected"{/if}>{$i->displayname}</option>
{/foreach}
</select>
</span>
{/if}
<input type="hidden" name="token" id="token" value="{$token_id}" />
<input type="hidden" name="suid" id="suid" value="{$suid}" />
<input type="hidden" name="ouid" id="ouid" value="{$ouid}" />
<button id="query-button" class="btn-search" type="submit">{str tag="go"}</button>
<input type="submit" class="submitcancel cancel" id="cancel_submit" name="cancel_submit" value="{$cancel}">
</div>
<div id="results" class="section">
<h2 id="resultsheading">{str tag="Results"}</h2>
{if $results}
<table id="searchresults" class="tablerenderer fullwidth listing">
<thead>
<tr>
{foreach from=$columns key=f item=c}
<th class="{if $c.sort}search-results-sort-column{if $f == $sortby} {$sortdir}{/if}{/if}{if $c.class} {$c.class}{/if}">
{if $c.sort}
<a href="{$searchurl}&sortby={$f}&sortdir={if $f == $sortby && $sortdir == 'asc'}desc{else}asc{/if}">
{$c.name}
<span class="accessible-hidden">({str tag=sortby} {if $f == $sortby && $sortdir == 'asc'}{str tag=descending}{else}{str tag=ascending}{/if})</span>
</a>
{else}
{$c.name}
{/if}
{if $c.help}
{$c.helplink|safe}
{/if}
{if $c.headhtml}<div style="font-weight: normal;">{$c.headhtml|safe}</div>{/if}
</th>
{/foreach}
</tr>
</thead>
<tbody>
{$results|safe}
</tbody>
</table>
{$pagination|safe}
{else}
<div>{str tag="noresultsfound"}</div>
{/if}
</div>
</form>
{include file="footer.tpl"}
<a href="{$returnurl}&user={$r.id}"><img src="{profile_icon_url user=$r maxwidth=40 maxheight=40}" alt="{str tag=profileimage}" />
{if $results.data}
<div id="setlimit" class="setlimit fr">
{str tag=resultsperpage}:
{foreach from=$limitoptions item=l}
<a href="?suid={$suid}&token={$token}&limit={$l}"{if $l == $results.limit} class="selected"{/if}>{$l}</a>
{/foreach}
</div>
<h2>{str tag="Results"}</h2>
<table id="searchresults" class="tablerenderer fullwidth listing">
<thead>
<tr>
{foreach from=$columns key=f item=c}
BLAH
<th class="{if $c.sort}search-results-sort-column{if $f == $sortby} {$sortdir}{/if}{/if}{if $c.class} {$c.class}{/if}">
{if $c.sort}
<a href="{$searchurl}&sortby={$f}&sortdir={if $f == $sortby && $sortdir == 'asc'}desc{else}asc{/if}">
{$c.name}
<span class="accessible-hidden">({str tag=sortby} {if $f == $sortby && $sortdir == 'asc'}{str tag=descending}{else}{str tag=ascending}{/if})</span>
</a>
{else}
{$c.name}
{/if}
{if $c.help}
{$c.helplink|safe}
{/if}
{if $c.headhtml}<div style="font-weight: normal;">{$c.headhtml|safe}</div>{/if}
</th>
{/foreach}
</tr>
</thead>
<tbody>
{$results|safe}
</tbody>
</table>
{$pagination|safe}
{else}
<div>{str tag="noresultsfound"}</div>
{/if}
<a href="{$returnurl}&user={$r.id}">{$r.username}</a>
\ No newline at end of file
......@@ -108,8 +108,8 @@ function webservices_token_submit(Pieform $form, $values) {
global $SESSION, $USER;
if ($values['action'] == 'generate') {
if (isset($values['username'])) {
$dbuser = get_record('usr', 'username', $values['username']);
if (isset($values['userid'])) {
$dbuser = get_record('usr', 'id', $values['userid']);
if (!empty($dbuser)) {
$services = get_records_array('external_services', 'restrictedusers', 0);
if (empty($services)) {
......@@ -155,8 +155,8 @@ function webservices_user_submit(Pieform $form, $values) {
global $SESSION, $USER;
if ($values['action'] == 'add') {
if (isset($values['username'])) {
$dbuser = get_record('usr', 'username', $values['username']);
if (isset($values['userid'])) {
$dbuser = get_record('usr', 'id', $values['userid']);
if ($auth_instance = webservice_validate_user($dbuser)) {
// make sure that this account is not already in use
$existing = get_record('external_services_users', 'userid', $dbuser->id);
......@@ -644,20 +644,18 @@ function service_tokens_edit_form() {
'name' => 'webservices_token_generate',
'renderer' => 'oneline',
'successcallback' => 'webservices_token_submit',
'class' => 'oneline inline',
'jsform' => false,
'action' => get_config('wwwroot') . 'webservice/admin/index.php',
'elements' => array(
'username' => array(
'type' => 'text',
'title' => get_string('username'),
'value' => $username,
),
'usersearch' => array(
'type' => 'html',
'value' => '&nbsp;<a href="' . get_config('wwwroot') .'webservice/admin/search.php?token=add"><img src="' . $searchicon . '" id="usersearch"/></a> &nbsp; ',
),
'userid' => array(
'type' => 'autocomplete',
'title' => get_string('username') . ': ',
'ajaxurl' => get_config('wwwroot') . 'webservice/admin/users.json.php',
'multiple' => false,
'allowclear' => true,
'ajaxextraparams' => array(),
'width' => '400px',
),
'action' => array('type' => 'hidden', 'value' => 'generate'),
'submit' => array(
'type' => 'submit',
......@@ -840,20 +838,18 @@ function service_users_edit_form() {
'name' => 'webservices_user_generate',
'renderer' => 'oneline',
'successcallback' => 'webservices_user_submit',
'class' => 'oneline inline',
'jsform' => false,
'action' => get_config('wwwroot') . 'webservice/admin/index.php',
'elements' => array(
'username' => array(
'type' => 'text',
'title' => get_string('username'),
'value' => $username,
),
'usersearch' => array(
'type' => 'html',
'value' => '&nbsp;<a href="' . get_config('wwwroot') . 'webservice/admin/search.php?suid=add"><img src="' . $searchicon . '" id="usersearch"/></a> &nbsp; ',
),
'userid' => array(
'type' => 'autocomplete',
'title' => get_string('username') . ': ',
'ajaxurl' => get_config('wwwroot') . 'webservice/admin/users.json.php',
'multiple' => false,
'allowclear' => true,
'ajaxextraparams' => array(),
'width' => '400px',
),
'action' => array('type' => 'hidden', 'value' => 'add'),
'submit' => array(
'type' => 'submit',
......
<?php
/**
*
* @package mahara
* @subpackage auth-webservice
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*
*/
define('INTERNAL', 1);
define('ADMIN', 1);
define('MENUITEM', 'configextensions/pluginadminwebservices');
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
define('TITLE', get_string('pluginadmin', 'admin'));
$plugintype = 'auth';
$pluginname = 'webservice';
define('SECTION_PLUGINTYPE', $plugintype);
define('SECTION_PLUGINNAME', $pluginname);
define('SECTION_PAGE', 'pluginconfig');
require_once('webservicessearchlib.php');
// validate the incoming token
$token = param_variable('token', '');
$suid = param_variable('suid', '');
$ouid = param_variable('ouid', '');
// did the user cancel
if (param_alpha('cancel_submit', 'empty') != 'empty') {
if ($ouid) {
redirect('/webservice/admin/oauthv1sregister.php?ouid=' . $ouid);
}
else if ($suid) {
redirect('/webservice/admin/userconfig.php?suid=' . $suid);
}
else {
redirect('/webservice/admin/tokenconfig.php?token=' . $token);
}
}
$sortby = param_alpha('sortby', 'firstname');
$sortdir = param_alpha('sortdir', 'asc');
$offset = param_integer('offset', 0);
$limit = param_integer('limit', 10);
$search = (object) array(
'query' => trim(param_variable('query', '')),
'f' => param_alpha('f', null),
'l' => param_alpha('l', null),
'sortby' => $sortby,
'sortdir' => $sortdir,
'loggedin' => 'any',
'loggedindate' => strftime(get_string('strftimedatetimeshort')),
'duplicateemail' => false,
'offset' => $offset,
'limit' => $limit,
);
if ($USER->get('admin')) {
$institutions = get_records_array('institution', '', '', 'displayname');
$search->institution = param_alphanum('institution', 'all');
}
else {
$institutions = get_records_select_array('institution', "name IN ('" . join("','", array_keys($USER->get('admininstitutions'))) . "')", null, 'displayname');
$search->institution_requested = param_alphanum('institution_requested', 'all');
}
$smarty = smarty(array(get_config('wwwroot') . 'webservice/admin/js/usersearch.js'));
$smarty->assign('token_id', $token);
$smarty->assign('token', $token);
$smarty->assign('suid', $suid);
$smarty->assign('ouid', $ouid);
$smarty->assign('search', $search);
$smarty->assign('alphabet', explode(',', get_string('alphabet')));
$smarty->assign('cancel', get_string('cancel'));
$smarty->assign('institutions', $institutions);
list($html, $columns, $searchurl, $pagination) = build_webservice_user_search_results($search, $offset, $limit, $sortby, $sortdir);
$smarty->assign('results', $html);
$smarty->assign('pagination', $pagination['html']);
$smarty->assign('columns', $columns);
$smarty->assign('searchurl', $searchurl['url']);
$smarty->assign('sortby', $searchurl['sortby']);
$smarty->assign('sortdir', $searchurl['sortdir']);
if ($token) {
$heading = get_string('headingusersearchtoken', 'auth.webservice');
}
else {
$heading = get_string('headingusersearchuser', 'auth.webservice');
}
$smarty->assign('PAGEHEADING', $heading);
$smarty->display('auth:webservice:search.tpl');
<?php
/**
*
* @package mahara
* @subpackage webservices
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*
*/
define('INTERNAL', 1);
define('JSON', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require_once(get_config('docroot') . '/lib/searchlib.php');
global $USER;
$request = param_variable('q');
$page = param_integer('page');
if ($page < 1) {
$page = 1;
}
$usersperpage = 5;
$more = true;
$tmpuser = array();
while ($more && count($tmpuser) < $usersperpage) {
$users = search_user($request, $usersperpage, $usersperpage * ($page - 1));
$more = $users['count'] > $usersperpage * $page;
if (!$users['data']) {
$users['data'] = array();
}
foreach ($users['data'] as $user) {
if (count($tmpuser) >= $usersperpage) {
$more = true;
continue;
}
$tmpuser[] = (object) array('id' => $user['id'],
'text' => display_name($user['id']));
}
$page++;
}
echo json_encode(array(
'more' => $more,
'results' => $tmpuser,
));
\ No newline at end of file
<?php
/**
*
* @package mahara
* @subpackage auth-webservice
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*
*/
defined('INTERNAL') || die();
require_once('searchlib.php');
require_once('user.php');
function build_webservice_user_search_results($search, $offset, $limit, $sortby, $sortdir) {
global $USER, $token, $suid, $ouid;
$results = get_admin_user_search_results($search, $offset, $limit, $sortby, $sortdir);
$params = array();
foreach ($search as $k => $v) {
if (!empty($v)) {
$params[] = $k . '=' . $v;
}
}
if ($suid) {
$params[] = 'suid=' . $suid;
}
if ($ouid) {
$params[] = 'ouid=' . $ouid;
}
$searchurl = get_config('wwwroot') . 'webservice/admin/search.php?' . join('&', $params) . '&limit=' . $limit;
$pagination = $results['pagination'] = build_pagination(array(
'id' => 'admin_usersearch_pagination',
'class' => 'center',
'url' => $searchurl,
'count' => $results['count'],
'limit' => $limit,
'setlimit' => true,
'jumplinks' => 8,
'numbersincludeprevnext' => 2,
'offset' => $offset,
'datatable' => 'searchresults',
'jsonscript' => 'webservice/admin/search.json.php',
));
if ($ouid) {
if ($ouid == 'add') {
$url = get_config('wwwroot') . 'webservice/admin/oauthv1sregister.php?';
}
else {
$url = get_config('wwwroot') . 'webservice/admin/oauthv1sregister.php?searchreturn=1&ouid=' . $ouid;
}
}
else if ($suid) {
if ($suid == 'add') {
$url = get_config('wwwroot') . 'webservice/admin/index.php?';
}
else {
$url = get_config('wwwroot') . 'webservice/admin/userconfig.php?searchreturn=1&suid=' . $suid;
}
}
else {
if ($token == 'add') {
$url = get_config('wwwroot') . 'webservice/admin/index.php?';
}
else {
$url = get_config('wwwroot') . 'webservice/admin/tokenconfig.php?searchreturn=1&token=' . $token;
}
}
$cols = array(
'icon' => array('name' => '',
'template' => 'auth:webservice:searchiconcolumn.tpl',
'class' => 'center'),
'firstname' => array('name' => get_string('firstname'), 'sort' => true),
'lastname' => array('name' => get_string('lastname'), 'sort' => true),
'username' => array('name' => get_string('username'), 'sort' => true,
'template' => 'auth:webservice:searchusernamecolumn.tpl'),
'email' => array('name' => get_string('email'), 'sort' => true),
);
$institutions = get_records_assoc('institution', '', '', '', 'name,displayname');
if (count($institutions) > 1) {
$cols['institution'] = array('name' => get_string('institution'),
'template' => 'admin/users/searchinstitutioncolumn.tpl');
}
$smarty = smarty_core();
$smarty->assign_by_ref('results', $results);
$smarty->assign_by_ref('institutions', $institutions);
$smarty->assign('USER', $USER);
$smarty->assign('searchurl', $searchurl);
$smarty->assign('returnurl', $url);
$smarty->assign('sortby', $sortby);
$smarty->assign('sortdir', $sortdir);
$smarty->assign('token', $token);
$smarty->assign('suid', $suid);
$smarty->assign('ouid', $ouid);
$smarty->assign('limitoptions', array(10, 50, 100, 200, 500));
$smarty->assign('pagebaseurl', $searchurl . '&ouid=' . $ouid . '&suid=' . $suid . '&token=' . $token . '&sortby=' . $sortby . '&sortdir=' . $sortdir);
$smarty->assign('cols', $cols);
$smarty->assign('ncols', count($cols));
global $THEME;
$THEME->templatedirs[]= get_config('docroot') . 'auth/webservice/theme/raw/';
return array($smarty->fetch('searchresulttable.tpl'), $cols, array(
'url' => $searchurl . '&ouid=' . $ouid . '&suid=' . $suid . '&token=' . $token . '&sortby=' . $sortby . '&sortdir=' . $sortdir,
'sortby' => $search->sortby,
'sortdir' => $search->sortdir
), $pagination);
}
function build_webservice_log_search_results($search, $offset, $limit, $sortby, $sortdir) {
global $USER;
$results = get_log_search_results($search, $offset, $limit, $sortby, $sortdir);
$params = array();
foreach ($search as $k => $v) {
if (!empty($v)) {
$params[] = $k . '=' . $v;
}
}
$searchurl = get_config('wwwroot') . 'webservice/admin/webservicelogs.php?' . join('&', $params) . '&limit=' . $limit;
$pagination = $results['pagination'] = build_pagination(array(
'id' => 'admin_usersearch_pagination',
'class' => 'center',
'url' => $searchurl,
'count' => $results['count'],
'limit' => $limit,
'setlimit' => true,
'jumplinks' => 8,
'numbersincludeprevnext' => 2,
'offset' => $offset,
'datatable' => 'searchresults',
'jsonscript' => 'webservice/admin/logsearch.json.php',
));
$cols = array(
'username' => array('name' => get_string('userauth', 'auth.webservice'),
'template' => 'auth:webservice:username.tpl',
'class' => 'center',
'sort' => true),
'institution' => array('name' => get_string('institution'), 'sort' => true),
'protocol' => array('name' => get_string('protocol', 'auth.webservice'), 'sort' => true),
'auth' => array('name' => get_string('authtype', 'auth.webservice'), 'sort' => true),
'functionname' => array('name' => get_string('function', 'auth.webservice'), 'sort' => true),
'timetaken' => array('name' => get_string('timetaken', 'auth.webservice'), 'sort' => true),
'timelogged' => array('name' => get_string('timelogged', 'auth.webservice'), 'sort' => true),
'info' => array('name' => get_string('info', 'auth.webservice'), 'class' => 'webservicelogs-info'),
);
$institutions = get_records_assoc('institution', '', '', '', 'name,displayname');
if (count($institutions) > 1) {
$cols['institution'] = array('name' => get_string('institution'),
'template' => 'admin/users/searchinstitutioncolumn.tpl');
}
$smarty = smarty_core();
$smarty->assign_by_ref('results', $results);
$smarty->assign_by_ref('institutions', $institutions);
$smarty->assign('USER', $USER);
$smarty->assign('searchurl', $searchurl);
$smarty->assign('sortby', $sortby);
$smarty->assign('sortdir', $sortdir);
$smarty->assign('limitoptions', array(10, 50, 100, 200, 500));
$smarty->assign('pagebaseurl', $searchurl . '&sortby=' . $sortby . '&sortdir=' . $sortdir);
$smarty->assign('cols', $cols);
$smarty->assign('ncols', count($cols));
global $THEME;
$THEME->templatedirs[]= get_config('docroot') . 'auth/webservice/theme/raw/';
return array($smarty->fetch('searchresulttable.tpl'), $cols, array(
'url' => $searchurl . '&sortby=' . $sortby . '&sortdir=' . $sortdir,
'sortby' => $search->sortby,
'sortdir' => $search->sortdir
), $pagination);
}
/**
* Split a query string into search terms.
*
* Contents of double-quoted strings are counted as a single term,
* '"' can be entered as '\"', '\' as '\\'.
*/
function split_query_string($query) {
$terms = array();
// Split string on unescaped double quotes
$quotesplit = preg_split('/(?<!\\\)(\")/', $query, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$inphrase = false;
foreach ($quotesplit as $q) {
if ($q == '"') {
$inphrase = !$inphrase;
continue;
}
// Remove escaping
$q = preg_replace(array('/\x5C(?!\x5C)/u', '/\x5C\x5C/u'), array('','\\'), $q);
if ($inphrase) {
if ($trimmed = trim($q)) {
$terms[] = $trimmed;
}
}
else {
// Split unquoted sequences on spaces
foreach (preg_split('/\s+/', $q, -1, PREG_SPLIT_NO_EMPTY) as $word) {
if ($word) {
$terms[] = $word;
}
}
}
}
return $terms;
}
function get_log_search_results($search, $offset, $limit) {
$sort = 'TRUE';
if (preg_match('/^[a-zA-Z_0-9"]+$/', $search->sortby)) {
$sort = $search->sortby;
if (strtoupper($search->sortdir) != 'DESC') {
$sort .= ' ASC';
}
else {
$sort .= ' DESC';
}
}
$where = '';
$ilike = db_ilike();
$wheres = array();
if ($search->protocol != 'all') {
$wheres[]= ' el.protocol = \'' . $search->protocol . '\' ';
}
if ($search->authtype != 'all') {