This commit is contained in:
ziggi
2017-12-11 15:04:31 +00:00
parent fff27e638c
commit 164c83eba8
12 changed files with 424 additions and 0 deletions

View File

@@ -89,6 +89,14 @@ nobase_dist_pkgdata_DATA = \
templs/alias-rename-form.html.ep \
templs/alias-rename-handler.html.ep \
\
templs/forward-list.html.ep \
templs/forward-add-form.html.ep \
templs/forward-add-handler.html.ep \
templs/forward-update-form.html.ep \
templs/forward-update-handler.html.ep \
templs/forward-delete-form.html.ep \
templs/forward-delete-handler.html.ep \
\
templs/exception.development.html.ep \
templs/exception.production.html.ep \
templs/hello.html.ep \

View File

@@ -352,6 +352,14 @@ nobase_dist_pkgdata_DATA = \
templs/alias-rename-form.html.ep \
templs/alias-rename-handler.html.ep \
\
templs/forward-list.html.ep \
templs/forward-add-form.html.ep \
templs/forward-add-handler.html.ep \
templs/forward-update-form.html.ep \
templs/forward-update-handler.html.ep \
templs/forward-delete-form.html.ep \
templs/forward-delete-handler.html.ep \
\
templs/exception.development.html.ep \
templs/exception.production.html.ep \
templs/hello.html.ep \

110
maacom.pl
View File

@@ -328,6 +328,8 @@ sub domain_delete {
$id;
}
# --- ALAIS ---
sub alias_exist {
my ($self, $name, $domain_id) = @_;
return undef unless $name;
@@ -522,6 +524,70 @@ sub user_delete {
$id;
}
# --- FORWARD ---
sub forward_exist {
my ($self, $name) = @_;
return undef unless $name;
my $res = $self->db->exec1("select id from forwards where name = '$name' order by id limit 1");
$res->{id};
}
sub forward_profile {
my ($self, $id) = @_;
return undef unless $id;
my $row = $self->db->exec1("select * from forwards where forwards.id = $id limit 1");
$row;
}
sub forward_nextid {
my $self = shift;
my $res = $self->db->exec1("select id from forwards order by id desc limit 1");
my $id = $res->{id} || 0;
$id += 1;
}
sub forward_add {
my ($self, $name) = @_;
return undef unless $name;
return undef if $self->forward_exist($name);
my $next_id = $self->forward_nextid;
$self->db->do("insert into forwards (id, name) values ($next_id, '$name')");
$self->forward_exist($name);
}
sub forward_list {
my $self = shift;
$self->db->exec("select * from forwards order by id");
}
sub forward_update {
my ($self, $id, %args) = @_;
return undef unless $id;
my $prof = $self->forward_profile($id);
return undef unless $prof;
my $name = $args{name} || $prof->{name};
$self->db->do("update forwards set name = '$name' where id = $id");
my $res = $self->forward_profile($id);
return undef unless $res->{name} eq $name;
$id;
}
sub forward_delete {
my ($self, $id) = @_;
return undef unless $id;
# return $id unless $self->forward_profile($id);
$self->db->do("delete from forwards where id = $id");
return undef if $self->forward_profile($id);
$id;
}
1;
#--------------
@@ -819,6 +885,42 @@ sub mxlog {
$self->render(template => 'mxlog');
}
# --- FORWARD ---
sub forward_list {
my $self = shift;
$self->render(template => 'forward-list');
}
sub forward_add_form {
my $self = shift;
$self->render(template => 'forward-add-form');
}
sub forward_add_handler {
my $self = shift;
$self->render(template => 'forward-add-handler');
}
sub forward_update_form {
my $self = shift;
$self->render(template => 'forward-update-form');
}
sub forward_update_handler {
my $self = shift;
$self->render(template => 'forward-update-handler');
}
sub forward_delete_form {
my $self = shift;
$self->render(template => 'forward-delete-form');
}
sub forward_delete_handler {
my $self = shift;
$self->render(template => 'forward-delete-handler');
}
1;
#-----------
@@ -971,6 +1073,14 @@ $r->any('/alias/rename/handler')->over('auth')->to('controller#alias_rename_hand
$r->any('/mxlog')->over('auth')->to('controller#mxlog' );
$r->any('/forward/list')->over('auth')->to('controller#forward_list' );
$r->any('/forward/add/form')->over('auth')->to('controller#forward_add_form' );
$r->any('/forward/add/handler')->over('auth')->to('controller#forward_add_handler' );
$r->any('/forward/update/form')->over('auth')->to('controller#forward_update_form' );
$r->any('/forward/update/handler')->over('auth')->to('controller#forward_update_handler' );
$r->any('/forward/delete/form')->over('auth')->to('controller#forward_delete_form' );
$r->any('/forward/delete/handler')->over('auth')->to('controller#forward_delete_handler' );
#----------------
#--- LISTENER ---

View File

@@ -20,4 +20,9 @@ CREATE TABLE aliases (
domain_id int,
list text
);
CREATE TABLE forwards (
id int unique NOT NULL PRIMARY KEY,
name text unique
);
COMMIT;

View File

@@ -0,0 +1,30 @@
%#
%# $Id$
%#
% layout 'default';
% title 'Maacom';
% use Mojo::Util qw(dumper);
% my $u = $c->app->user;
<div class="grid-container">
<div class="grid-x grid-padding-x">
<div class="medium-6 medium-centered cell">
<form accept-charset="UTF-8" action="/forward/add/handler" method="get" data-abide novalidate>
<h5 class="text-center">Add forwarded domain</h5>
<label>Domain name
<input type="text" name="forward_name" placeholder="domain.org" required pattern="[_.-zA-Z0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{5,42}"/>
<span class="form-error">Mandatory, 5 or more letter</span>
</label>
<div class="text-center">
<button type="submit" class="button">Accept</button>
<a class="button" href="/forward/list">Escape</a>
</div>
</form>
</div>
</div>
</div>
%#EOF

View File

@@ -0,0 +1,32 @@
%#
%# $Id$
%#
% layout 'default';
% title 'Maacom';
% use Mojo::Util qw(dumper);
% my $u = $c->app->user;
% my $name = $c->req->param('forward_name');
% my $id = $u->forward_exist($name);
% if ($id) {
<div class="callout warning">Domain <%= $name %> already exists</div>
% }
% unless ($id) {
% my $res = $u->forward_add($name);
% if ($res) {
<div class="callout success">Domain <%= $name %> has been added.</div>
% }
% unless ($res) {
<div class="callout alert">Domain <%= $name %> was not added.</div>
% }
% }
%#EOF

View File

@@ -0,0 +1,39 @@
%#
%# $Id$
%#
% layout 'default';
% title 'Maacom';
% use Mojo::Util qw(dumper);
% my $u = $c->app->user;
% my $forward_id = $c->req->param('forward_id') || 0;
% my $profile = $u->forward_profile($forward_id);
% if ($profile) {
% my $forward_name = $profile->{name};
<div class="grid-container">
<div class="grid-x grid-padding-x">
<div class="medium-6 medium-centered cell">
<form accept-charset="UTF-8" action="/forward/delete/handler" method="get" data-abide novalidate>
<h5 class="text-center">Delete domain <%= $forward_name %></h5>
<input type="hidden" name="forward_id" value="<%= $forward_id %>"/>
<div class="text-center">
<button type="submit" class="button">Accept</button>
<a class="button" href="/forward/list">Escape</a>
</div>
</form>
</div>
</div>
</div>
% }
% unless ($profile) {
<div class="callout warning">Domain with id <%= $forward_id %> not exist</div>
% }
%#EOF

View File

@@ -0,0 +1,32 @@
%#
%# $Id$
%#
% layout 'default';
% title 'Maacom';
% use Mojo::Util qw(dumper);
% my $u = $c->app->user;
% my $forward_id = $c->req->param('forward_id') || 0;
% my $profile = $u->forward_profile($forward_id);
% if ($profile) {
% my $name = $profile->{name};
% my $res = $u->forward_delete($forward_id);
% if ($res) {
<div class="callout success">Domain <%= $name %> has been deleted</div>
% }
% unless ($res) {
<div class="callout alert">Domain <%= $name %> was not deleted</div>
% }
% }
% unless ($profile) {
<div class="callout warning">Domain with id <%= $forward_id %> not exist</div>
% }
%#EOF

View File

@@ -0,0 +1,74 @@
%#
%# $Id$
%#
% layout 'default';
% title 'Maacom';
% use Mojo::Util qw(dumper);
% my $u = $c->app->user;
<h5 class="text-center">
Domains <a href="/forward/list"><i class="fi-refresh"></i></a>
<a class="float-right button tiny" href="/forward/add/form">
<i class="fi-plus"></i>
</a>
</h5>
% my $list = $u->forward_list;
% unless ($list) {
<div class="callout alert">
Cannot list domains. May be wrong database?
</div>
% }
<table class="hover" id="table">
<thead>
<tr>
<th>#</th>
<th>domain</th>
<th><i class="fi-pencil"></i></th>
<th><i class="fi-trash"></i></th>
</tr>
</thead>
<tbody>
% my $n = 0;
% if ($list) {
% foreach my $row (@$list) {
% $n += 1;
% my $id = $row->{id};
% my $name = $row->{name};
% my $size = $row->{size};
<tr>
<td><%= $n %></td>
<td><%= $name %></td>
<td><a href="/forward/update/form?forward_id=<%= $id %>"><i class="fi-pencil"></i></a></td>
<td><a href="/forward/delete/form?forward_id=<%= $id %>"><i class="fi-trash"></i></a></td>
</tr>
% }
% }
</tbody>
</table>
<script>
$.extend(true, $.fn.dataTable.defaults, {
"searching": true,
"ordering": true,
"language": {
"search": "",
"lengthMenu": "_MENU_",
"info": "_START_-_END_ of _TOTAL_",
"infoEmpty": "",
},
} );
$(document).ready(function() {
$('#table').DataTable();
});
</script>
%# EOF

View File

@@ -0,0 +1,44 @@
%#
%# $Id$
%#
% layout 'default';
% title 'Maacom';
% use Mojo::Util qw(dumper);
% my $u = $c->app->user;
% my $forward_id = $c->req->param('forward_id') || 0;
% my $profile = $u->forward_profile($forward_id);
% if ($profile) {
% my $forward_name = $profile->{name};
<div class="grid-container">
<div class="grid-x grid-padding-x">
<div class="medium-6 medium-centered cell">
<form accept-charset="UTF-8" action="/forward/update/handler" method="get" data-abide novalidate>
<h5 class="text-center">Edit domain</h5>
<input type="hidden" name="forward_id" value="<%= $forward_id %>"/>
<label>Domain name
<input type="text" value="<%= $forward_name %>" name="forward_name" required pattern="[_.-zA-Z0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{5,42}"/>
<span class="form-error">Mandatory, 5 or more letter</span>
</label>
<div class="text-center">
<button type="submit" class="button">Accept</button>
<a class="button" href="/forward/list">Escape</a>
</div>
</form>
</div>
</div>
</div>
% }
% unless ($profile) {
<div class="callout warning">Domain with id <%= $forward_id %> not exist</div>
% }
%#EOF

View File

@@ -0,0 +1,41 @@
%#
%# $Id$
%#
% layout 'default';
% title 'Maacom';
% use Mojo::Util qw(dumper);
% my $u = $c->app->user;
% my $name = $c->req->param('forward_name');
% my $forward_id = $c->req->param('forward_id') || 0;
% my $profile = $u->forward_profile($forward_id);
% if ($profile) {
% my $cid = $u->forward_exist($name);
% if ($cid) {
<div class="callout warning">Domain <%= $name %> already exists</div>
% }
% unless ($cid) {
% my $res = $u->forward_update($forward_id, name => $name);
% if ($res) {
<div class="callout success">Domain <%= $name %> has been updated.</div>
% }
% unless ($res) {
<div class="callout alert">Domain <%= $name %> was not updated.</div>
% }
% }
% }
% unless ($profile) {
<div class="callout warning">Domain with id <%= $forward_id %> not exist</div>
% }
%#EOF

View File

@@ -31,6 +31,7 @@
<li><a href="/domain/list">Domain</a></li>
<li><a href="/user/list">User</a></li>
<li><a href="/alias/list">Alias</a></li>
<li><a href="/forward/list">Forward</a></li>
<li><a href="/mxlog">Log</a></li>
<li><a href="/logout"><i class="fi-arrow-right"></i></a></li>
</ul>