aboutsummaryrefslogtreecommitdiffstats
path: root/lib/CatDap/Controller/register.pm
blob: d8fc7c176e4a69a57aa12ce7e38efc658c6b3fa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package CatDap::Controller::register;
use Moose;
use namespace::autoclean;
use Data::Dumper;
use Email::Valid;
use Data::UUID;

BEGIN {extends 'Catalyst::Controller'; }

=head1 NAME

CatDap::Controller::register - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 index

=cut

sub index :Path :Args(0) : Form {
    my ( $self, $c ) = @_;

    #my $form = Catalyst::Controller::HTML::FormFu->form();
    #$c->response->body('Matched CatDap::Controller::register in register.');
}

sub check : Local {
    my ( $self, $c ) = @_;

    my %details = %{$c->request->params};
    my $username = $c->request->params->{uid};
    $username =~ s/[A-Z]/[a-z]/g;
    my @errors;
    $c->stash(errors => []);
    # Check username, start with letter, followed by letters or numbers
    if ($username !~ /^[a-z][a-z0-9_\-]*$/) {
	    push @errors, $c->loc('Invalid username');
    }
    my $email = $c->request->params->{mail1};
    if (! Email::Valid->address($email)) {
	    push @errors, $c->loc('Invalid email address');
    }
    if ($email ne $c->request->params->{mail2}) {
	    push @errors, $c->loc('Addresses do not match');
    }
    if (! $c->validate_captcha($c->req->param('validate'))){
        push @errors, $c->loc('Incorrect validation text, please try again');
    }
    if ($c->request->params->{gn} !~ /^\p{IsAlnum}*\z$/) {
        push @errors, $c->loc('The first name supplied contains illegal characters');
    }
    if ($c->request->params->{sn} !~ /^\p{IsAlnum}*\z$/) {
	    #push @errors, $c->loc('The') . ' ' $c->loc('surname') . ' ' . $c->loc('supplied contains unprintable characters');
        push @errors, $c->loc('The surname supplied contains illegal characters');
    }

    if (@errors gt 0) {
	    $c->stash(errors => \@errors);
	    $c->stash(template => 'register/index.tt');
    } else {
        # check in LDAP now that we have validated username and email
        my $mesg = $c->model('Proxy')->search("(mail=$email)");
	if ($mesg->entries ne 0) {
		push @errors,$c->loc('An account already exists with this email address');
	}
	$mesg = $c->model('Proxy')->search("(uid=$username)");
	if ($mesg->entries ne 0) {
		my $foo = Dumper(${$c->config}{'Model::Proxy'}{'base'});
		push @errors,$c->loc('An account already exists with this username');
		#push @errors,"under base" . __PACKAGE__->config{Model::Proxy}{base};
		push @errors,$foo;
	}
	if (@errors gt 0) {
		$c->stash(errors => \@errors);
		$c->stash(template => 'register/index.tt');
	} else {
		my $dn = "uid=$username,${$c->config}{'Model::Proxy'}{'base'}";
		my $ug =  Data::UUID->new;
		my $password = $ug->create_str();
		my $cn = $c->request->params->{gn} . " " .  $c->request->params->{sn};
		$mesg = $c->model('Proxy')->add($dn,
			attr => [
				objectclass => [ 'inetOrgPerson' ],
				sn => $c->request->params->{sn},
				gn => $c->request->params->{gn},
				cn => $cn,
				mail => $email,
				pwdReset => 'TRUE',
				userPassword => $password,
				]
			);
		if ($mesg) {
			push @errors,$mesg->error;
			$c->stash(errors => \@errors);
			#$c->stash(template => 'register/index.tt');
		}
		#} else {
			my $body;
			$body .= $c->loc('Dear') . " $c->request->params->{gn},\n";
			$body .= $c->loc("Your Mageia indentity has been successfully created, but requires activation.\n");
		        $body .= $c->loc("To activate your account, please follow the link below.\n");
			$body .= $c->uri_for('/user/firstlogin') . "?username=$username&key=$password";
			$c->stash->{email} = {
				to      => $email,
				from    => 'no-reply@mageia.org',
				subject => $c->loc('Mageia Identity Activation'),
				body    => $body,
			};

			$c->forward( $c->view('Email') );
			if ( scalar( @{ $c->error } ) ) {
				my $errors = join "\n",@{ $c->error };
				$c->response->body($c->loc('An error occured sending the email, but your account was created. Please try the password recovery process f you entered the correct email address: [_1]', $errors));
				$c->error(0); # Reset the error condition if you need to
			}
			$c->stash(template => 'register/complete.tt');
    			$c->stash(message => 'Check your email');
		#}
	}
    }
}

sub captcha : Local {
	my ($self, $c) = @_;
	return $c->create_captcha();
}



=head1 AUTHOR

Buchan Milne

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

__PACKAGE__->meta->make_immutable;

1;