* The new endpoint will only accept POSTs * The request body should have two parameters called 'type', 'username' and 'domain' * 'type' will accept either 'administrator', in which case only 'username' is needed, or 'subscriber', in which case 'username' and 'domain' will be needed * The regular password reset email will be sent to either the admin or the subscriber Change-Id: If1457c8c625a95295e5e93b6637927e3905698d9mr9.0
parent
444a0d35b3
commit
be14a9242e
@ -0,0 +1,116 @@
|
||||
package NGCP::Panel::Controller::API::PasswordReset;
|
||||
use NGCP::Panel::Utils::Generic qw(:all);
|
||||
|
||||
use Sipwise::Base;
|
||||
|
||||
use boolean qw(true);
|
||||
use Data::HAL qw();
|
||||
use Data::HAL::Link qw();
|
||||
use HTTP::Headers qw();
|
||||
use HTTP::Status qw(:constants);
|
||||
|
||||
|
||||
sub allowed_methods{
|
||||
return [qw/POST OPTIONS/];
|
||||
}
|
||||
|
||||
use parent qw/NGCP::Panel::Role::Entities NGCP::Panel::Role::API::PasswordReset/;
|
||||
|
||||
sub api_description {
|
||||
return 'Request a password reset using administrator email or subscriber SIP URI (username@domain).';
|
||||
}
|
||||
|
||||
sub query_params {
|
||||
return [
|
||||
];
|
||||
}
|
||||
|
||||
sub resource_name{
|
||||
return 'passwordreset';
|
||||
}
|
||||
|
||||
sub dispatch_path{
|
||||
return '/api/passwordreset/';
|
||||
}
|
||||
|
||||
sub relation{
|
||||
return 'http://purl.org/sipwise/ngcp-api/#rel-passwordreset';
|
||||
}
|
||||
|
||||
__PACKAGE__->set_config({
|
||||
action => {
|
||||
map { $_ => {
|
||||
Args => 0,
|
||||
Does => [qw(CheckTrailingSlash RequireSSL)],
|
||||
Method => $_,
|
||||
Path => __PACKAGE__->dispatch_path,
|
||||
} } @{ __PACKAGE__->allowed_methods },
|
||||
},
|
||||
});
|
||||
|
||||
sub POST :Allow {
|
||||
my ($self, $c) = @_;
|
||||
|
||||
my $res;
|
||||
|
||||
my $guard = $c->model('DB')->txn_scope_guard;
|
||||
{
|
||||
my $resource = $self->get_valid_post_data(
|
||||
c => $c,
|
||||
media_type => 'application/json',
|
||||
);
|
||||
last unless $resource;
|
||||
|
||||
my $form = $self->get_form($c);
|
||||
last unless $self->validate_form(
|
||||
c => $c,
|
||||
resource => $resource,
|
||||
form => $form,
|
||||
);
|
||||
|
||||
if ($resource->{type} eq 'administrator') {
|
||||
my $admin = $c->model('DB')->resultset('admins')->search({
|
||||
'me.login' => $resource->{username}
|
||||
})->first;
|
||||
if($admin && $admin->email && $admin->can_reset_password) {
|
||||
NGCP::Panel::Utils::Auth::initiate_password_reset($c, $admin);
|
||||
}
|
||||
}
|
||||
elsif($resource->{type} eq 'subscriber') {
|
||||
my ($user, $domain) = ($resource->{username}, $resource->{domain});
|
||||
my $subscriber = $c->model('DB')->resultset('voip_subscribers')->find({
|
||||
username => $user,
|
||||
'domain.domain' => $domain,
|
||||
},{
|
||||
join => 'domain',
|
||||
});
|
||||
|
||||
if($subscriber) {
|
||||
# don't clear web password, a user might just have guessed it and
|
||||
# could then block the legit user out
|
||||
my ($uuid_bin, $uuid_string);
|
||||
UUID::generate($uuid_bin);
|
||||
UUID::unparse($uuid_bin, $uuid_string);
|
||||
$subscriber->password_resets->delete; # clear any old entries of this subscriber
|
||||
$subscriber->password_resets->create({
|
||||
uuid => $uuid_string,
|
||||
timestamp => NGCP::Panel::Utils::DateTime::current_local->epoch + 300, #expire in 5 minutes
|
||||
});
|
||||
my $url = $c->uri_for_action('/subscriber/recover_webpassword')->as_string . '?uuid=' . $uuid_string;
|
||||
NGCP::Panel::Utils::Email::password_reset($c, $subscriber, $url);
|
||||
}
|
||||
}
|
||||
|
||||
$guard->commit;
|
||||
|
||||
$res = { success => 1, message => 'Please check your email for password reset instructions.' };
|
||||
|
||||
$c->response->status(HTTP_OK);
|
||||
$c->response->body(JSON::to_json($res));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
# vim: set tabstop=4 expandtab:
|
@ -0,0 +1,68 @@
|
||||
package NGCP::Panel::Form::PasswordResetAPI;
|
||||
|
||||
use HTML::FormHandler::Moose;
|
||||
use Email::Valid;
|
||||
extends 'HTML::FormHandler';
|
||||
|
||||
has_field 'username' => (
|
||||
type => 'Text',
|
||||
required => 1,
|
||||
label => 'Username',
|
||||
);
|
||||
|
||||
has_field 'domain' => (
|
||||
type => 'Text',
|
||||
required => 0,
|
||||
label => 'Domain',
|
||||
);
|
||||
|
||||
has_field 'type' => (
|
||||
type => 'Select',
|
||||
options => [
|
||||
{ value => 'administrator', label => 'Administrator' },
|
||||
{ value => 'subscriber', label => 'Subscriber' },
|
||||
],
|
||||
required => 1,
|
||||
label => 'Type',
|
||||
);
|
||||
|
||||
has_block 'fields' => (
|
||||
tag => 'div',
|
||||
class => [qw/modal-body/],
|
||||
render_list => [qw/username/],
|
||||
);
|
||||
|
||||
sub validate {
|
||||
my ($self) = @_;
|
||||
my $c = $self->ctx;
|
||||
return unless $c;
|
||||
|
||||
my $resource = Storable::dclone($self->values);
|
||||
if ($resource->{type} eq 'administrator') {
|
||||
my $address = $resource->{username}.'@ngcp.local';
|
||||
unless (Email::Valid->address($address)) {
|
||||
my $err = "'username' is not valid.";
|
||||
$c->log->error($err);
|
||||
$self->field('username')->add_error($err);
|
||||
}
|
||||
}
|
||||
elsif ($resource->{type} eq 'subscriber') {
|
||||
my $err;
|
||||
if (!$resource->{domain}) {
|
||||
$err = "'domain' field is required when requesting a password reset for a subscriber";
|
||||
}
|
||||
else {
|
||||
my $address = $resource->{username}.'@'.$resource->{domain};
|
||||
unless (Email::Valid->address($address)) {
|
||||
$err = "username and domain combination is not valid.";
|
||||
}
|
||||
}
|
||||
if ($err) {
|
||||
$c->log->error($err);
|
||||
$self->field('username')->add_error($err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
1;
|
||||
# vim: set tabstop=4 expandtab:
|
@ -0,0 +1,24 @@
|
||||
package NGCP::Panel::Role::API::PasswordReset;
|
||||
use NGCP::Panel::Utils::Generic qw(:all);
|
||||
|
||||
use Sipwise::Base;
|
||||
|
||||
use parent 'NGCP::Panel::Role::API';
|
||||
|
||||
|
||||
use boolean qw(true);
|
||||
use Data::HAL qw();
|
||||
use Data::HAL::Link qw();
|
||||
use HTTP::Status qw(:constants);
|
||||
|
||||
|
||||
sub _item_rs {
|
||||
}
|
||||
|
||||
sub get_form {
|
||||
my ($self, $c) = @_;
|
||||
return NGCP::Panel::Form::get("NGCP::Panel::Form::PasswordResetAPI", $c);
|
||||
}
|
||||
|
||||
1;
|
||||
# vim: set tabstop=4 expandtab:
|
Loading…
Reference in new issue