From 6b4ced06bb20d0193781e73b4b5bad0826cae0c8 Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Sun, 11 May 2014 22:29:40 +0200 Subject: [PATCH] Implement join password setting But it's not used yet :-) --- public/js/vroom.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ public/vroom.pl | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/public/js/vroom.js b/public/js/vroom.js index eab0806..60b0350 100644 --- a/public/js/vroom.js +++ b/public/js/vroom.js @@ -616,6 +616,55 @@ function initVroom(room) { } }); + $('#joinPass').on('input', function() { + if ($('#joinPass').val() == ''){ + $('#setJoinPassButton').addClass('disabled'); + } + else{ + $('#setJoinPassButton').removeClass('disabled'); + } + }); + + // Join password protection + $('#setJoinPassButton').click(function(event) { + var pass = $('#joinPass').val(); + $('#joinPass').val(''); + $('#setJoinPassButton').addClass('disabled'); + $.ajax({ + data: { + action: 'setJoinPassword', + password: pass, + room: roomName + }, + error: function(data) { + var msg = (data && data.msg) ? data.msg : locale.ERROR_OCCURED; + $.notify(msg, 'error'); + }, + success: function(data) { + $.notify(data.msg, 'success'); + $('#joinPass').val(''); + } + }); + }); + + // Remove password protection + $('#removeJoinPassButton').click(function(event) { + $.ajax({ + data: { + action: 'setJoinPassword', + room: roomName + }, + error: function(data) { + var msg = (data && data.msg) ? data.msg : locale.ERROR_OCCURED; + $.notify(msg, 'error'); + }, + success: function(data) { + $.notify(data.msg, 'success'); + $('#joinPass').val(''); + } + }); + }); + // Choose another color. Useful if two peers have the same $('#changeColorButton').click(function(){ peers.local.color = chooseColor(); diff --git a/public/vroom.pl b/public/vroom.pl index 8cefd3d..3dca185 100755 --- a/public/vroom.pl +++ b/public/vroom.pl @@ -237,6 +237,22 @@ helper get_mtime => sub { return stat($file)->mtime; }; +# password protect a room +helper set_join_pass => sub { + my $self = shift; + my ($room,$pass) = @_; + return undef unless ( %{ $self->get_room($room) }); + my $sth = eval { $self->db->prepare("UPDATE rooms SET join_password=? where name=?;") } || return undef; + $sth->execute($pass,$room) || return undef; + if ($pass){ + $self->app->log->debug($self->session('name') . " has set a password on room $room"); + } + else{ + $self->app->log->debug($self->session('name') . " has removed password on room $room"); + } + return 1; +}; + any '/' => 'index'; get '/about' => sub { @@ -338,7 +354,6 @@ get '/(*room)' => sub { room => $room ); } - $self->cookie(vroomsession => encode_base64($self->session('name') . ':' . $data->{name} . ':' . $data->{token}, ''), {expires => time + 60}); my @participants = $self->get_participants($room); if ($data->{'locked'}){ unless (($self->session('name') eq $data->{'owner'}) || (grep { $_ eq $self->session('name') } @participants )){ @@ -349,6 +364,8 @@ get '/(*room)' => sub { ); } } + # TODO: check join password here + $self->cookie(vroomsession => encode_base64($self->session('name') . ':' . $data->{name} . ':' . $data->{token}, ''), {expires => time + 60}); # Add this user to the participants table unless($self->add_participant($room,$self->session('name'))){ return $self->render('error', @@ -455,6 +472,26 @@ post '/action' => sub { ); } } + elsif ($action eq 'setJoinPassword'){ + my $pass = $self->param('password'); + $pass = undef if ($pass eq ''); + my $res = $self->set_join_pass($room,$pass); + if (!$res){ + return $self->render( + json => { + msg => $self->l('ERROR_OCCURED'), + }, + status => '500' + ); + } + else{ + return $self->render( + json => { + msg => ($pass) ? 'JOIN_PASSWORD_SET':'JOIN_PASSWORD_REMOVED', + } + ); + } + } }; # Not found (404)