Skip to content

Commit

Permalink
Merge pull request #37 from nextcloud/managed-server
Browse files Browse the repository at this point in the history
add registration form for managed server service
  • Loading branch information
sualko authored Jul 26, 2017
2 parents 788efe3 + ad196fb commit 844d03c
Show file tree
Hide file tree
Showing 12 changed files with 640 additions and 21 deletions.
22 changes: 22 additions & 0 deletions appinfo/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OCA\OJSXC\AppInfo;

use OCA\OJSXC\Controller\ManagedServerController;
use OCA\OJSXC\Controller\SettingsController;
use OCA\OJSXC\Controller\ExternalApiController;
use OCA\OJSXC\Middleware\ExternalApiMiddleware;
Expand All @@ -19,6 +20,7 @@
use OCA\OJSXC\StanzaHandlers\Presence;
use OCA\OJSXC\StanzaLogger;
use OCA\OJSXC\RawRequest;
use OCA\OJSXC\DataRetriever;
use OCP\AppFramework\App;
use OCA\OJSXC\ILock;
use OCA\OJSXC\DbLock;
Expand Down Expand Up @@ -89,6 +91,19 @@ public function __construct(array $urlParams=array()){
);
});

$container->registerService('ManagedServerController', function(IContainer $c) {
return new ManagedServerController(
$c->query('AppName'),
$c->query('Request'),
$c->query('URLGenerator'),
\OC::$server->getConfig(),
\OC::$server->getUserSession(),
$c->query('Logger'),
$c->query('DataRetriever'),
'https://xmpp.jsxc.ch/registration'
);
});

/**
* Middleware
*/
Expand Down Expand Up @@ -231,6 +246,13 @@ public function __construct(array $urlParams=array()){
return new RawRequest();
});

/**
* Data retriever
*/
$container->registerService('DataRetriever', function($c) {
return new DataRetriever();
});

}

/**
Expand Down
7 changes: 7 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@

use \OCA\OJSXC\AppInfo\Application;

$this->create('ojsxc_ajax_registerManagedServer', 'ajax/registerManagedServer.php')
->actionInclude('ojsxc/ajax/registerManagedServer.php');

$application = new Application();
$application->registerRoutes($this, array(
'routes' => array(
array('name' => 'http_bind#index', 'url' => '/http-bind', 'verb' => 'POST'),

array('name' => 'settings#index', 'url' => '/settings', 'verb' => 'POST'),
array('name' => 'settings#setAdmin', 'url' => '/settings/admin', 'verb' => 'POST'),
array('name' => 'settings#setUser', 'url' => '/settings/user', 'verb' => 'POST'),
array('name' => 'settings#getIceServers', 'url' => '/settings/iceServers', 'verb' => 'GET'),
array('name' => 'settings#getUsers', 'url' => '/settings/users', 'verb' => 'GET'),

array('name' => 'externalApi#index', 'url' => '/ajax/externalApi.php', 'verb' => 'POST'),
array('name' => 'externalApi#check_password', 'url' => '/api/v2/checkPassword', 'verb' => 'POST'),
array('name' => 'externalApi#is_user', 'url' => '/api/v2/isUser', 'verb' => 'POST'),
array('name' => 'externalApi#shared_roster', 'url' => '/api/v2/sharedRoster', 'verb' => 'POST'),

array('name' => 'managedServer#register', 'url' => '/managedServer/register', 'verb' => 'POST')
)
));
?>
2 changes: 1 addition & 1 deletion js/jsxc
Submodule jsxc updated from da3b09 to fc8e08
94 changes: 80 additions & 14 deletions js/settings/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ $(document).ready(function() {
}
}

if(xhr.status === 0) {
if (xhr.status === 0) {
// cross-side
fail('Cross domain request was not possible. Either your BOSH server does not send any ' +
'Access-Control-Allow-Origin header or the content-security-policy (CSP) blocks your request. ' +
Expand All @@ -93,15 +93,15 @@ $(document).ready(function() {
});
}

$('#ojsxc [name=serverType]').change(function(){
$('#ojsxc .ojsxc-external, #ojsxc .ojsxc-internal').hide();
$('#ojsxc .ojsxc-external, #ojsxc .ojsxc-internal').find('.required').removeAttr('required');
$('#ojsxc [name=serverType]').change(function() {
$('#ojsxc .ojsxc-external, #ojsxc .ojsxc-internal, #ojsxc .ojsxc-managed').hide();
$('#ojsxc .ojsxc-external, #ojsxc .ojsxc-internal, #ojsxc .ojsxc-managed').find('.required').removeAttr('required');
$('#ojsxc .ojsxc-' + $(this).val()).show();
$('#ojsxc .ojsxc-' + $(this).val()).find('.required').attr('required', 'true');
});
$('#ojsxc [name=serverType]:checked').change();

$('#boshUrl, #xmppDomain').on('input', function(){
$('#boshUrl, #xmppDomain').on('input', function() {
var self = $(this);
var timeout = self.data('timeout');

Expand Down Expand Up @@ -151,7 +151,7 @@ $(document).ready(function() {
status.addClass('jsxc_fail').text('Error!');
}

setTimeout(function(){
setTimeout(function() {
status.hide('slow');
}, 3000);
});
Expand Down Expand Up @@ -182,21 +182,21 @@ $(document).ready(function() {

var options = jsxc.options.get('httpUpload') || {};

var services = $('[name="externalServices[]"]').map(function(){
var services = $('[name="externalServices[]"]').map(function() {
var inputField = $(this);

return inputField.val() || null;
});

if (options.server && services.toArray().indexOf(options.server) < 0) {
// insert service
var emptyInputFields = $('[name="externalServices[]"]').filter(function(){
var emptyInputFields = $('[name="externalServices[]"]').filter(function() {
return $(this).val() === '';
});

var targetInputField;

if(emptyInputFields.length === 0) {
if (emptyInputFields.length === 0) {
$(this).parents('.form-group').find('.add-input').click();
targetInputField = $('[name="externalServices[]"]').last();
} else {
Expand All @@ -207,12 +207,78 @@ $(document).ready(function() {
}
});

var apiUrl = window.location.origin + OC.filePath('ojsxc', 'ajax', 'externalApi.php');
$('#jsxc-api-url').val(apiUrl);

$('#ojsxc input[readonly]').focus(function(){
if(typeof this.select === 'function') {
$('#ojsxc input[readonly]').focus(function() {
if (typeof this.select === 'function') {
this.select();
}
});

$('#ojsxc-register').click(function() {
var el = $(this);
var msgEl = el.parents('.ojsxc-managed').find('.msg');
var promotionCode = $('#ojsxc-managed-promotion-code').val();

if (promotionCode.length > 0 && !/^[0-9a-z]+$/i.test(promotionCode)) {
msgEl.addClass('jsxc_fail');
msgEl.text('Your promotion code is invalid.');

$('#ojsxc-managed-promotion-code').one('input', function() {
msgEl.removeClass('jsxc_fail');
msgEl.text('');
});

return;
}

el.prop('disabled', 'disabled');
el.val(el.attr('data-toggle-value'));
el.addClass('jsxc-loading');

$.ajax({
method: 'POST',
url: OC.generateUrl('apps/ojsxc/managedServer/register'),
data: {
promotionCode: promotionCode
}
}).always(function(responseJSON) {
el.removeClass('jsxc-loading');

if (responseJSON && responseJSON.result === 'success') {
$('.ojsxc-managed-registration').hide();

msgEl.addClass('jsxc_success');
msgEl.text('Congratulations! You got your own XMPP server. Please log out and in again.');

var submitEl = $('#ojsxc input[type="submit"]');
submitEl.prop('disabled', 'disabled');
submitEl.val('Please reload this page to continue');

return;
}

if (responseJSON.responseJSON) {
responseJSON = responseJSON.responseJSON;
}

var errorMsg = (responseJSON && responseJSON.data) ? responseJSON.data.msg : 'unknown error';

msgEl.addClass('jsxc_fail');
msgEl.append($('<span>').text('Sorry we couldn\'t complete your registration.'));
msgEl.append($('<br>'));
msgEl.append($('<span>').text(errorMsg));

el.val('Registration failed');
});
});

$('.ojsxc-refresh-registration').click(function(ev){
ev.preventDefault();

var msgEl = $(this).parents('.msg');

msgEl.removeClass('jsxc_success');
msgEl.empty();

$('.ojsxc-managed-registration').show();
});
});
22 changes: 19 additions & 3 deletions lib/Controller/ExternalApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,28 @@ public function __construct(
$this->logger = $logger;
}

/**
* @PublicPage
* @NoCSRFRequired
*/
public function index($operation)
{
switch ($operation) {
case 'auth':
return checkPassword(
return $this->checkPassword(
$this->request->getParam('username'),
$this->request->getParam('password'),
$this->request->getParam('domain')
);
break;
case 'isuser':
return isUser(
return $this->isUser(
$this->request->getParam('username'),
$this->request->getParam('domain')
);
break;
case 'sharedroster':
return sharedRoster(
return $this->sharedRoster(
$this->request->getParam('username'),
$this->request->getParam('domain')
);
Expand All @@ -65,6 +69,10 @@ public function index($operation)
}
}

/**
* @PublicPage
* @NoCSRFRequired
*/
public function checkPassword($username = '', $password = '', $domain = '')
{
$currentUser = null;
Expand Down Expand Up @@ -100,6 +108,10 @@ public function checkPassword($username = '', $password = '', $domain = '')
];
}

/**
* @PublicPage
* @NoCSRFRequired
*/
public function isUser($username = '', $domain = '')
{
$this->logger->info('ExAPI: Check if "'.$username.'@'.$domain.'" exists');
Expand All @@ -123,6 +135,10 @@ public function isUser($username = '', $domain = '')
];
}

/**
* @PublicPage
* @NoCSRFRequired
*/
public function sharedRoster($username = '', $domain = '')
{
if (!empty($username)) {
Expand Down
Loading

0 comments on commit 844d03c

Please sign in to comment.