diff --git a/include/ajax.users.php b/include/ajax.users.php index 19db8c3ded7cff8af6b4e576cb8b07ac8cde14d0..5da92b78cbd305675ddf095d6ff84c01a0106db7 100644 --- a/include/ajax.users.php +++ b/include/ajax.users.php @@ -33,7 +33,7 @@ class UsersAjaxAPI extends AjaxController { $emails=array(); if (!$type || !strcasecmp($type, 'remote')) { - foreach (StaffAuthenticationBackend::searchUsers($_REQUEST['q']) as $u) { + foreach (AuthenticationBackend::searchUsers($_REQUEST['q']) as $u) { $name = "{$u['first']} {$u['last']}"; $users[] = array('email' => $u['email'], 'name'=>$name, 'info' => "{$u['email']} - $name (remote)", @@ -213,16 +213,17 @@ class UsersAjaxAPI extends AjaxController { $info = array(); - if ($_POST) { + $info['title'] = 'Add New User'; + + if (!AuthenticationBackend::getSearchDirectories()) + $info['lookup'] = 'local'; + if ($_POST) { $form = UserForm::getUserForm()->getForm($_POST); if (($user = User::fromForm($form))) Http::response(201, $user->to_json()); - $info = array('error' =>'Error adding user - try again!'); - } else { - $info['lookuptype'] = remote; - $info['title'] = 'Add New User'; + $info['error'] = 'Error adding user - try again!'; } return self::_lookupform($form, $info); @@ -235,10 +236,10 @@ class UsersAjaxAPI extends AjaxController { Http::response(403, 'Login Required'); elseif (!$bk || !$id) Http::response(422, 'Backend and user id required'); - elseif (!($backend = StaffAuthenticationBackend::getBackend($bk))) + elseif (!($backend = AuthenticationBackend::getSearchDirectoryBackend($bk)) + || !($user_info = $backend->lookup($id))) Http::response(404, 'User not found'); - $user_info = $backend->lookup($id); $form = UserForm::getUserForm()->getForm($user_info); $info = array('title' => 'Import Remote User'); if (!$user_info) @@ -290,13 +291,11 @@ class UsersAjaxAPI extends AjaxController { Http::response(400, 'Query argument is required'); $users = array(); - foreach (StaffAuthenticationBackend::allRegistered() as $ab) { - if (!$ab instanceof AuthDirectorySearch) - continue; - + foreach (AuthenticationBackend::searchDirectories() as $ab) { foreach ($ab->search($_REQUEST['q']) as $u) $users[] = $u; } + return $this->json_encode($users); } diff --git a/include/class.auth.php b/include/class.auth.php index 74e7b9c787635fbf5cf3a30b6100164f794a8886..487dbe3c6b984530ba750e59f91c60f9efb43540 100644 --- a/include/class.auth.php +++ b/include/class.auth.php @@ -138,6 +138,14 @@ abstract class AuthenticationBackend { return $backends[$id]; } + static function getSearchDirectoryBackend($id) { + + if ($id + && ($backends = static::getSearchDirectories()) + && isset($backends[$id])) + return $backends[$id]; + } + /* * Allow the backend to do login audit depending on the result * This is mainly used to track failed login attempts @@ -229,13 +237,24 @@ abstract class AuthenticationBackend { self::authAudit($result); } + static function getSearchDirectories() { + $backends = array(); + foreach (StaffAuthenticationBackend::allRegistered() as $bk) + if ($bk instanceof AuthDirectorySearch) + $backends[$bk::$id] = $bk; + + foreach (UserAuthenticationBackend::allRegistered() as $bk) + if ($bk instanceof AuthDirectorySearch) + $backends[$bk::$id] = $bk; + + return array_unique($backends); + } + static function searchUsers($query) { $users = array(); - foreach (static::allRegistered() as $bk) { - if ($bk instanceof AuthDirectorySearch) { - $users += $bk->search($query); - } - } + foreach (static::getSearchDirectories() as $bk) + $users += $bk->search($query); + return $users; } diff --git a/include/staff/templates/user-lookup.tmpl.php b/include/staff/templates/user-lookup.tmpl.php index 39a617e01d4672c661cd65a2699d559c202dcb68..5978242d2effdbce631f8330c97c396f47b7b800 100644 --- a/include/staff/templates/user-lookup.tmpl.php +++ b/include/staff/templates/user-lookup.tmpl.php @@ -2,6 +2,8 @@ <h3><?php echo $info['title']; ?></h3> <b><a class="close" href="#"><i class="icon-remove-circle"></i></a></b> <hr/> +<?php +if (!isset($info['lookup']) || $info['lookup'] !== false) { ?> <div><p id="msg_info"><i class="icon-info-sign"></i> Search existing users or add a new user.</p></div> <div style="margin-bottom:10px;"> <input type="text" class="search-input" style="width:100%;" @@ -9,6 +11,8 @@ autocorrect="off" autocomplete="off"/> </div> <?php +} + if ($info['error']) { echo sprintf('<p id="msg_error">%s</p>', $info['error']); } elseif ($info['msg']) { @@ -78,7 +82,7 @@ $(function() { if (last_req) last_req.abort(); last_req = $.ajax({ url: "ajax.php/users<?php - echo $info['lookuptype'] ? "/{$info['lookuptype']}" : '' ?>?q="+query, + echo $info['lookup'] ? "/{$info['lookup']}" : '' ?>?q="+query, dataType: 'json', success: function (data) { typeahead.process(data);