You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.0 KiB
75 lines
2.0 KiB
package LDV::Actions; |
|
|
|
use strict; |
|
use warnings; |
|
use utf8; |
|
|
|
use Mojo::Base 'Mojolicious::Controller'; |
|
use Net::LDAP; |
|
use Net::LDAP::Util qw(ldap_error_name); |
|
use Crypt::SaltedHash; |
|
|
|
sub create |
|
{ |
|
my ($self) = @_; |
|
my ($result); |
|
|
|
$result = "Created"; |
|
eval { |
|
my ($ldap, $mesg); |
|
$ldap = Net::LDAP->new($self->app->config->{server}) |
|
or die("$@"); |
|
$mesg = $ldap->bind($self->app->config->{binddn}, |
|
password => $self->app->config->{bindpass}); |
|
if ($mesg->code) { |
|
$self->app->log->error($mesg->error); |
|
die("Can't connect to server\n"); |
|
} |
|
|
|
my $base = $self->app->config->{userbase}; |
|
my $login = $self->req->param('login'); |
|
die ("Empty username\n") |
|
unless ($login); |
|
die ("Forbidden characters in username\n") |
|
unless ($login =~ m|^[a-z]{2,36}$|oi); |
|
$mesg = $ldap->search(base => $base, scope => 'one', deref => 'never', |
|
filter => '(&(uid=$login)(class=InetOrgPerson))'); |
|
die("This user already exists\n") |
|
if ($mesg->count); |
|
|
|
my $attrs = {}; |
|
$attrs->{objectclass} = [ "top", @{$self->app->config->{defclasses}} ]; |
|
$attrs->{mail} = $self->req->param('mail'); |
|
$attrs->{displayname} = $self->req->param('displayname') || ''; |
|
if ($attrs->{displayname} =~ m|^(\S+)\s+(?:.*\s+)?(\S+)$|oi) { |
|
$attrs->{cn} = $1; |
|
$attrs->{sn} = $2; |
|
} else { |
|
$attrs->{cn} = '!not set!'; |
|
$attrs->{sn} = '!not set!'; |
|
} |
|
|
|
$attrs->{uid} = $login; |
|
my $csh = Crypt::SaltedHash->new(algorithm => 'SHA-1'); |
|
$csh->add($self->req->param('pass')); |
|
$attrs->{userpassword} = $csh->generate(); |
|
|
|
$mesg = $ldap->add("uid=$login,$base", attrs => [ %$attrs ]); |
|
if ($mesg->code) { |
|
$self->app->log->error($mesg->error); |
|
die("Can't add user\n"); |
|
} |
|
|
|
$ldap->unbind(); 1; |
|
} or do { |
|
$self->app->log->error($@); |
|
$result = "Error: $@"; |
|
}; |
|
|
|
$self->flash({result => $result}); |
|
$self->redirect_to('/user/create'); |
|
$self->rendered(); |
|
return 1; |
|
} |
|
|
|
1;
|
|
|