90 lines
2.8 KiB
Perl
90 lines
2.8 KiB
Perl
package My::Mailserver;
|
|
use Mojo::Base 'Mojolicious';
|
|
use My::Mailserver::Schema;
|
|
use Mojo::Loader 'load_class';
|
|
|
|
has db => sub {
|
|
my $c = shift;
|
|
return My::Mailserver::Schema->connect($c->app->config->{db_config});
|
|
};
|
|
|
|
has email_transport => sub {
|
|
my $c = shift;
|
|
my $transport = $c->app->config->{mail_transport};
|
|
my $transport_class = "Email::Sender::Transport::$transport";
|
|
my $e = load_class $transport_class;
|
|
die qq{Loading "$transport_class" failed: $e} if ref $e;
|
|
return $transport_class->new($c->app->config->{mail_transport_config} // {});
|
|
};
|
|
|
|
# This method will run once at server start
|
|
sub startup {
|
|
my $self = shift;
|
|
|
|
push @{ $self->commands->namespaces }, 'My::Mailserver::Command';
|
|
|
|
# Load configuration from hash returned by config file
|
|
my $config = $self->plugin('Config', {
|
|
default => {
|
|
mail_transport => 'Maildir',
|
|
mail_transport_config => {
|
|
dir => $self->home->child('mail'),
|
|
},
|
|
},
|
|
});
|
|
|
|
# Configure the application
|
|
$self->secrets($config->{secrets});
|
|
|
|
$self->plugin('Authentication' => {
|
|
'load_user' => sub {
|
|
my ( $c, $user_id ) = @_;
|
|
return $c->schema->resultset('VirtualUser')->find($user_id);
|
|
},
|
|
'validate_user' => sub {
|
|
my ( $c, $email, $password, $args) = @_;
|
|
my $user = $c->schema->resultset('VirtualUser')->find({email => $email});
|
|
if ( defined $user ) {
|
|
if ( $user->check_password( $password ) ) {
|
|
return $user->id;
|
|
}
|
|
}
|
|
return;
|
|
},
|
|
});
|
|
|
|
$self->helper( schema => sub { $self->db } );
|
|
|
|
$self->helper( email_transport => sub { $self->email_transport });
|
|
|
|
# Router
|
|
my $r = $self->routes;
|
|
|
|
$r->get('/')->to('auth#login_get');
|
|
$r->post('/')->to('auth#login_post');
|
|
$r->post('/recover')->to('auth#recover_post');
|
|
$r->get('/reset/:code')->to('auth#reset_get');
|
|
$r->post('/reset/:code')->to('auth#reset_post');
|
|
$r->any('/logout')->to('auth#logout_any');
|
|
|
|
my $auth_route = $r->under('/dashboard')->to('auth#under');
|
|
|
|
$auth_route->get('/')->to('dashboard#index');
|
|
$auth_route->post('/change_password')->to('dashboard#change_password');
|
|
$auth_route->post('/change_recovery')->to('dashboard#change_recovery');
|
|
|
|
my $manager_route = $auth_route->under('/manager')->to('auth#is_manager');
|
|
|
|
$manager_route->get('/')->to('manager#index');
|
|
$manager_route->get('/domains')->to('manager#domains');
|
|
$manager_route->get('/users')->to('manager#users');
|
|
$manager_route->get('/alias')->to('manager#alias');
|
|
$manager_route->post('/add_domain')->to('manager#add_domain');
|
|
$manager_route->post('/del_domain')->to('manager#del_domain');
|
|
$manager_route->post('/add_user')->to('manager#add_user');
|
|
$manager_route->post('/del_user')->to('manager#del_user');
|
|
$manager_route->post('/add_alias')->to('manager#add_alias');
|
|
$manager_route->post('/del_alias')->to('manager#del_alias');
|
|
}
|
|
|
|
1;
|