project-fsck-status.sh: --no-full mode can generate warnings
[girocco.git] / cgi / deluser.cgi
blobbeaa4525816631a07e6b9921ddf582bde8145c5a
1 #!/usr/bin/perl
3 # deluser.cgi -- support for user deletion via web
4 # Copyright (c) 2013 Kyle J. McKay. All rights reserved.
5 # Portions (c) Petr Baudis <pasky@suse.cz> and (c) Jan Krueger <jk@jk.gs>
6 # License GPLv2+: GNU GPL version 2 or later.
7 # www.gnu.org/licenses/gpl-2.0.html
8 # This is free software: you are free to change and redistribute it.
9 # There is NO WARRANTY, to the extent permitted by law.
11 use strict;
12 use warnings;
14 use lib "__BASEDIR__";
15 use Girocco::CGI;
16 use Girocco::Config;
17 use Girocco::User;
18 use Girocco::Util;
19 binmode STDOUT, ':utf8';
21 my $gcgi = Girocco::CGI->new('User Removal');
22 my $cgi = $gcgi->cgi;
24 unless ($Girocco::Config::manage_users) {
25 print "<p>I don't manage users.</p>";
26 exit;
29 if ($cgi->param('mail')) {
30 print "<p>Go away, bot.</p>";
31 exit;
34 sub _auth_form {
35 my ($name, $submit) = @_;
36 print <<EOT;
37 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/deluser.cgi">
38 <input type="hidden" name="name" value="$name" />
39 <p>Authorization code: <input name="auth" size="50" /></p>
40 <p><input type="submit" name="y0" value="$submit" /></p>
41 </form>
42 EOT
45 my $y0 = $cgi->param('y0') || '';
46 if ($cgi->param('name') && $y0 && $cgi->request_method eq 'POST') {
47 # submitted, let's see
48 # FIXME: racy, do a lock
49 my $name = $gcgi->wparam('name');
50 Girocco::User::does_exist($name, 1)
51 or $gcgi->err("Username is not registered.");
53 $gcgi->err_check and exit;
55 my $user;
56 ($user = Girocco::User->load($name)) && valid_email($user->{email})
57 or $gcgi->err("Username may not be removed.");
59 $gcgi->err_check and exit;
61 if (!$cgi->param('auth')) {
62 if ($y0 ne 'Send authorization code') {
63 print "<p>Invalid data. Go away, sorcerer.</p>\n";
64 exit;
67 valid_email($user->{email}) or die "Sorry, this user cannot be removed.";
69 my $auth = $user->gen_auth('DEL');
71 # Send auth mail
72 defined(my $MAIL = mailer_pipe '-s', "[$Girocco::Config::name] Account removal authorization", $user->{email}) or
73 die "Sorry, could not send authorization code: $!";
74 print $MAIL <<EOT;
75 Hello,
77 You have requested an authorization code be sent to you for removing
78 your account. If you don't want to actually remove your account, just
79 ignore this e-mail. Otherwise, use this code within 24 hours:
81 $auth
83 Should you run into any problems, please let us know.
85 Have fun!
86 EOT
87 close $MAIL;
89 print "<p>You should shortly receive an e-mail containing an authorization code.
90 Please enter this code below to remove your account.
91 The code will expire in 24 hours or after you have used it.</p>";
92 _auth_form($name, "'Login'");
93 exit;
94 } else {
95 if ($y0 ne "'Login'" && $y0 ne "Remove user account") {
96 print "<p>Invalid data. Go away, sorcerer.</p>\n";
97 exit;
100 $user->{auth} && $user->{authtype} eq 'DEL' or do {
101 print "<p>There currently isn't any authorization code filed under your account. ".
102 "Please <a href=\"@{[url_path($Girocco::Config::webadmurl)]}/deluser.cgi\">generate one</a>.</p>";
103 exit;
106 my $auth = $gcgi->wparam('auth');
107 if ($auth ne $user->{auth}) {
108 print "<p>Invalid authorization code, please re-enter or ".
109 "<a href=\"@{[url_path($Girocco::Config::webadmurl)]}/deluser.cgi\">generate a new one</a>.</p>";
110 _auth_form($name, "'Login'");
111 exit;
114 my $conf = $gcgi->wparam('confirm') || '';
115 if ($y0 ne 'Remove user account' || $conf ne $user->{name}) {
116 my $blurb1 = '.';
117 my $projectsinfo = '';
118 my @projects = $user->get_projects;
119 if (@projects) {
120 $projectsinfo = projects_html_list({target=>"_blank", sizecol=>1, typecol=>1, changed=>1}, @projects);
121 $blurb1 = ' and from the following projects:' if $projectsinfo;
123 my $ownedinfo = '';
124 my @ownedprojects = filedb_grep($Girocco::Config::projlist_cache_dir.'/gitproj.list',
125 sub {
126 chomp;
127 my ($proj, $hash, $owner) = split(/ /, $_, 3);
128 if ($owner eq $user->{email}) {
129 $proj;
133 if (@ownedprojects) {
134 $ownedinfo = <<EOT;
135 <p>The following project(s) are owned by the same email address as user account '$user->{name}'
136 and <b>will NOT be removed</b>. If desired, they can be removed from their project admin
137 page(s) (the "edit" link on the project page).</p>
139 $ownedinfo .= projects_html_list(
140 {target=>"_blank", sizecol=>1, typecol=>1, changed=>1}, @ownedprojects);
142 print <<EOT;
143 <p>Please confirm that you are going to remove user account '$user->{name}'
144 from the site$blurb1</p>$projectsinfo$ownedinfo
145 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/deluser.cgi">
146 <input type="hidden" name="name" value="$name" />
147 <input type="hidden" name="auth" value="$auth" />
148 <input type="hidden" name="confirm" value="$name" />
149 <p><input type="submit" name="y0" value="Remove user account" /></p>
150 </form>
152 exit;
155 $user->remove;
156 print "<p>User account successfully removed. Have a nice day.</p>\n";
157 exit;
161 print <<EOT;
162 <p>Here you can request an authorization code to remove your user account.</p>
164 <p>Please enter your username below;
165 we will send you an email with an authorization code
166 and further instructions.</p>
168 <form method="post" action="@{[url_path($Girocco::Config::webadmurl)]}/deluser.cgi">
169 <table class="form">
170 <tr><td class="formlabel">Login:</td><td><input type="text" name="name" /></td></tr>
171 <tr style="display:none"><td class="formlabel">Anti-captcha (leave empty!):</td><td><input type="text" name="mail" /></td></tr>
172 <tr><td class="formlabel"></td><td><input type="submit" name="y0" value="Send authorization code" /></td></tr>
173 </table>
174 </form>