Skip to content

Commit cb6d232

Browse files
authored
Merge pull request #82 from sheldor1510/account-deletion-request
added option for account deletion requests
2 parents 9e2e00b + f68b85a commit cb6d232

File tree

8 files changed

+176
-10
lines changed

8 files changed

+176
-10
lines changed

resources/lib/UnityGroup.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public function requestGroup($send_mail_to_admins, $send_mail = true)
7878
return;
7979
}
8080

81+
// check if account deletion request already exists
82+
if ($this->SQL->accDeletionRequestExists($this->getOwner()->getUID())) {
83+
return;
84+
}
85+
8186
$this->SQL->addRequest($this->getOwner()->getUID());
8287

8388
if ($send_mail) {
@@ -352,6 +357,11 @@ public function newUserRequest($new_user, $send_mail = true)
352357
return;
353358
}
354359

360+
// check if account deletion request already exists
361+
if ($this->SQL->accDeletionRequestExists($new_user->getUID())) {
362+
return;
363+
}
364+
355365
$this->addRequest($new_user->getUID());
356366

357367
if ($send_mail) {

resources/lib/UnitySQL.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class UnitySQL
1212
private const TABLE_PAGES = "pages";
1313
private const TABLE_EVENTS = "events";
1414
private const TABLE_AUDIT_LOG = "audit_log";
15+
private const TABLE_ACCOUNT_DELETION_REQUESTS = "account_deletion_requests";
1516

1617
private const REQUEST_ADMIN = "admin";
1718

@@ -252,4 +253,26 @@ public function addLog($operator, $operator_ip, $action_type, $recipient)
252253

253254
$stmt->execute();
254255
}
256+
257+
public function addAccountDeletionRequest($uid)
258+
{
259+
$stmt = $this->conn->prepare(
260+
"INSERT INTO " . self::TABLE_ACCOUNT_DELETION_REQUESTS . " (uid) VALUE (:uid)"
261+
);
262+
$stmt->bindParam(":uid", $uid);
263+
264+
$stmt->execute();
265+
}
266+
267+
public function accDeletionRequestExists($uid)
268+
{
269+
$stmt = $this->conn->prepare(
270+
"SELECT * FROM " . self::TABLE_ACCOUNT_DELETION_REQUESTS . " WHERE uid=:uid"
271+
);
272+
$stmt->bindParam(":uid", $uid);
273+
274+
$stmt->execute();
275+
276+
return count($stmt->fetchAll()) > 0;
277+
}
255278
}

resources/lib/UnityUser.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,4 +644,31 @@ public function getGroups($ignorecache = false)
644644

645645
return $out;
646646
}
647+
648+
/**
649+
* Sends an email to admins about account deletion request and also adds it to a table in the database
650+
*/
651+
public function requestAccountDeletion()
652+
{
653+
$this->SQL->addAccountDeletionRequest($this->getUID());
654+
$this->MAILER->sendMail(
655+
"admin",
656+
"account_deletion_request_admin",
657+
array(
658+
"user" => $this->getUID(),
659+
"name" => $this->getFullname(),
660+
"email" => $this->getMail()
661+
)
662+
);
663+
}
664+
665+
/**
666+
* Checks if the user has requested account deletion
667+
*
668+
* @return boolean true if account deletion has been requested, false if not
669+
*/
670+
public function hasRequestedAccountDeletion()
671+
{
672+
return $this->SQL->accDeletionRequestExists($this->getUID());
673+
}
647674
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
// This template is sent to admins when a new group is requested
4+
$this->Subject = "Account Deletion Request";
5+
?>
6+
7+
<p>Hello,</p>
8+
9+
<p>A user has requested deletion of their account. User details are below:</p>
10+
11+
<p>
12+
<strong>Username</strong> <?php echo $data["user"]; ?>
13+
<br>
14+
<strong>Name</strong> <?php echo $data["name"]; ?>
15+
<br>
16+
<strong>Email</strong> <?php echo $data["email"]; ?>
17+
</p>

tools/docker-dev/sql/bootstrap.sql

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ CREATE TABLE `audit_log` (
108108

109109
-- --------------------------------------------------------
110110

111+
-- --------------------------------------------------------
112+
113+
--
114+
-- Table structure for table `account_deletion_requests`
115+
--
116+
117+
CREATE TABLE `account_deletion_requests` (
118+
`id` int(11) NOT NULL,
119+
`timestamp` timestamp NOT NULL DEFAULT current_timestamp(),
120+
`uid` varchar(1000) NOT NULL,
121+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
122+
123+
-- --------------------------------------------------------
124+
111125
--
112126
-- Indexes for dumped tables
113127
--
@@ -148,6 +162,12 @@ ALTER TABLE `sso_log`
148162
ALTER TABLE `audit_log`
149163
ADD PRIMARY KEY (`id`);
150164

165+
--
166+
-- Indexes for table `audit_log`
167+
--
168+
ALTER TABLE `account_deletion_requests`
169+
ADD PRIMARY KEY (`id`);
170+
151171
--
152172
-- AUTO_INCREMENT for dumped tables
153173
--
@@ -189,6 +209,14 @@ ALTER TABLE `audit_log`
189209
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
190210
COMMIT;
191211

212+
--
213+
--
214+
-- AUTO_INCREMENT for table `account_deletion_requests`
215+
--
216+
ALTER TABLE `account_deletion_requests`
217+
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
218+
COMMIT;
219+
192220
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
193221
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
194222
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

webroot/admin/user-mgmt.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@
4343
});
4444

4545
foreach ($users as $user) {
46-
echo "<tr>";
46+
if ($user->hasRequestedAccountDeletion()) {
47+
echo "<tr style='color:grey; font-style: italic'>";
48+
} else {
49+
echo "<tr>";
50+
}
4751
echo "<td>" . $user->getFirstname() . " " . $user->getLastname() . "</td>";
4852
echo "<td>" . $user->getUID() . "</td>";
4953
echo "<td>" . $user->getOrg() . "</td>";

webroot/panel/account.php

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@
7070
}
7171
}
7272
break;
73+
case "account_deletion_request":
74+
$hasGroups = count($USER->getGroups()) > 0;
75+
if ($hasGroups) {
76+
die();
77+
break;
78+
}
79+
if (!$SQL->accDeletionRequestExists($USER->getUID())) {
80+
$USER->requestAccountDeletion();
81+
}
82+
break;
7383
}
7484
}
7585
?>
@@ -107,17 +117,30 @@
107117
}
108118

109119
if (!$isPI) {
110-
echo
111-
"<form action='' method='POST' id='piReq'
112-
onsubmit='return confirm(\"Are you sure you want to request a PI account?\")'>
113-
<input type='hidden' name='form_type' value='pi_request'>";
114-
if ($SQL->requestExists($USER->getUID())) {
120+
if ($SQL->accDeletionRequestExists($USER->getUID())) {
121+
echo
122+
"<form action='' method='POST' id='piReq'
123+
onsubmit='return confirm(\"Are you sure you want to request a PI account?\")'>
124+
<input type='hidden' name='form_type' value='pi_request'>";
115125
echo "<input type='submit' value='Request PI Account' disabled>";
116-
echo "<label style='margin-left: 10px'>Your request has been submitted and is currently pending</label>";
126+
echo
127+
"<label style='margin-left: 10px'>
128+
You cannot request PI Account while you have requested account deletion.
129+
</label>";
130+
echo "</form>";
117131
} else {
118-
echo "<input type='submit' value='Request PI Account'>";
132+
echo
133+
"<form action='' method='POST' id='piReq'
134+
onsubmit='return confirm(\"Are you sure you want to request a PI account?\")'>
135+
<input type='hidden' name='form_type' value='pi_request'>";
136+
if ($SQL->requestExists($USER->getUID())) {
137+
echo "<input type='submit' value='Request PI Account' disabled>";
138+
echo "<label style='margin-left: 10px'>Your request has been submitted and is currently pending</label>";
139+
} else {
140+
echo "<input type='submit' value='Request PI Account'>";
141+
}
142+
echo "</form>";
119143
}
120-
echo "</form>";
121144
}
122145
?>
123146

@@ -194,6 +217,33 @@
194217

195218
</form>
196219

220+
<hr>
221+
222+
<h5>Account Deletion</h5>
223+
<?php
224+
$hasGroups = count($USER->getGroups()) > 0;
225+
226+
if ($hasGroups) {
227+
echo "<p>You cannot request to delete your account while you are in a PI group.</p>";
228+
} else {
229+
echo
230+
"<form action='' method='POST' id='accDel'
231+
onsubmit='return confirm(\"Are you sure you want to request an account deletion?\")'>
232+
<input type='hidden' name='form_type' value='account_deletion_request'>";
233+
if ($SQL->accDeletionRequestExists($USER->getUID())) {
234+
echo "<input type='submit' value='Request Account Deletion' disabled>";
235+
echo "<label style='margin-left: 10px'>Your request has been submitted and is currently pending</label>";
236+
} else {
237+
echo "<input type='submit' value='Request Account Deletion'>";
238+
}
239+
echo "</form>";
240+
}
241+
242+
?>
243+
244+
<hr>
245+
246+
197247
<script>
198248
$("button.btnAddKey").click(function() {
199249
openModal("Add New Key", "<?php echo $CONFIG["site"]["prefix"]; ?>/panel/modal/new_key.php");

webroot/panel/groups.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,14 @@
125125
echo "</table>";
126126
?>
127127

128-
<button type="button" class="plusBtn btnAddPI">&#43;</button>
128+
<?php
129+
if ($SQL->accDeletionRequestExists($USER->getUID())) {
130+
echo "<button type='button' class='plusBtn btnAddPI' disabled>&#43;</button>";
131+
echo "<label>You cannot join a PI while you have requested account deletion.</label>";
132+
} else {
133+
echo "<button type='button' class='plusBtn btnAddPI'>&#43;</button>";
134+
}
135+
?>
129136

130137
<style>
131138
div.modalContent {

0 commit comments

Comments
 (0)