From b746d00261803eb0e9e000225ca89a41fa0a8079 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Wed, 15 Jan 2014 21:03:01 +0200 Subject: [PATCH] Add register and passwd --- lib/Gruntmaster/Handlers.pm | 41 +++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/Gruntmaster/Handlers.pm b/lib/Gruntmaster/Handlers.pm index 9e92dc2..c9a6c70 100644 --- a/lib/Gruntmaster/Handlers.pm +++ b/lib/Gruntmaster/Handlers.pm @@ -6,6 +6,7 @@ use warnings; our $VERSION = '0.001'; use Apache2::Access; +use Apache2::Authen::Passphrase qw/pwcheck pwset USER_REGEX/; use Apache2::AuthzCaps qw/hascaps/; use Apache2::RequestRec; use Apache2::RequestIO; @@ -18,12 +19,19 @@ use Cwd qw/cwd/; use File::Basename qw/fileparse/; use File::Temp qw/tempdir/; use File::Copy qw/move/; -use Gruntmaster::Data qw/contest_start contest_end push_job set_job_inmeta PUBLISH/; +use Gruntmaster::Data qw/contest_start contest_end push_job set_job_inmeta insert_user PUBLISH/; use Gruntmaster::Page::Log; +sub aputs{ + my ($r, $str) = @_; + $r->set_content_length(length $str); + $r->puts($str); + $r->content_type('text/plain'); + OK +} + sub submit{ my $r = shift; - chdir $r->dir_config('root'); my $req = Apache2::Request->new($r); my ($problem, $format, $contest, $private) = map {scalar $req->param($_)} 'problem', 'prog_format', 'contest', 'private'; my $prog; @@ -56,6 +64,35 @@ sub submit{ OK } +sub register{ + my $r = shift; + my $req = Apache2::Request->new($r); + my ($username, $password, $confirm_password, $name, $email, $town, $university) = map {scalar $req->param($_)} 'username', 'password', 'confirm_password', 'name', 'email', 'town', 'university'; + + local $Apache2::Authen::Passphrase::rootdir = $r->dir_config('AuthenPassphraseRootdir'); + return aputs $r, 'Bad username. Allowed characters are letters, digits and underscores, and the username must be between 2 and 20 characters long.' unless $username =~ USER_REGEX; + return aputs $r, 'Username already in use' if -e "$Apache2::Authen::Passphrase::rootdir/$username.yml"; + return aputs $r, 'The two passwords do not match' unless $password eq $confirm_password; + pwset $username, $password; + + insert_user $username, name => $name, email => $email, town => $town, university => $university; + + aputs $r, 'Registered successfully'; +} + +sub passwd{ + my $r = shift; + my $req = Apache2::Request->new($r); + my ($oldpass, $newpass, $confirm) = map {scalar $req->param($_)} 'password', 'new_password', 'confirm_new_password'; + + local $Apache2::Authen::Passphrase::rootdir = $r->dir_config('AuthenPassphraseRootdir'); + return aputs $r, 'Incorrect password' unless eval { pwcheck $r->user, $oldpass; 1 }; + return aputs $r, 'The two passwords do not match' unless $newpass eq $confirm; + + pwset $r->user, $newpass; + aputs $r, 'Password changed successfully'; +} + =begin comment sub private{ -- 2.30.2