Alex 'AdUser' Z
10 years ago
2 changed files with 84 additions and 79 deletions
@ -1,75 +0,0 @@
|
||||
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; |
Loading…
Reference in new issue