Skip to content

added option for account deletion requests #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions resources/lib/UnityGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public function requestGroup($send_mail_to_admins, $send_mail = true)
return;
}

// check if account deletion request already exists
if ($this->SQL->accDeletionRequestExists($this->getOwner()->getUID())) {
return;
}

$this->SQL->addRequest($this->getOwner()->getUID());

if ($send_mail) {
Expand Down Expand Up @@ -352,6 +357,11 @@ public function newUserRequest($new_user, $send_mail = true)
return;
}

// check if account deletion request already exists
if ($this->SQL->accDeletionRequestExists($new_user->getUID())) {
return;
}

$this->addRequest($new_user->getUID());

if ($send_mail) {
Expand Down
23 changes: 23 additions & 0 deletions resources/lib/UnitySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class UnitySQL
private const TABLE_PAGES = "pages";
private const TABLE_EVENTS = "events";
private const TABLE_AUDIT_LOG = "audit_log";
private const TABLE_ACCOUNT_DELETION_REQUESTS = "account_deletion_requests";

private const REQUEST_ADMIN = "admin";

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

$stmt->execute();
}

public function addAccountDeletionRequest($uid)
{
$stmt = $this->conn->prepare(
"INSERT INTO " . self::TABLE_ACCOUNT_DELETION_REQUESTS . " (uid) VALUE (:uid)"
);
$stmt->bindParam(":uid", $uid);

$stmt->execute();
}

public function accDeletionRequestExists($uid)
{
$stmt = $this->conn->prepare(
"SELECT * FROM " . self::TABLE_ACCOUNT_DELETION_REQUESTS . " WHERE uid=:uid"
);
$stmt->bindParam(":uid", $uid);

$stmt->execute();

return count($stmt->fetchAll()) > 0;
}
}
27 changes: 27 additions & 0 deletions resources/lib/UnityUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -644,4 +644,31 @@ public function getGroups($ignorecache = false)

return $out;
}

/**
* Sends an email to admins about account deletion request and also adds it to a table in the database
*/
public function requestAccountDeletion()
{
$this->SQL->addAccountDeletionRequest($this->getUID());
$this->MAILER->sendMail(
"admin",
"account_deletion_request_admin",
array(
"user" => $this->getUID(),
"name" => $this->getFullname(),
"email" => $this->getMail()
)
);
}

/**
* Checks if the user has requested account deletion
*
* @return boolean true if account deletion has been requested, false if not
*/
public function hasRequestedAccountDeletion()
{
return $this->SQL->accDeletionRequestExists($this->getUID());
}
}
17 changes: 17 additions & 0 deletions resources/mail/account_deletion_request_admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

// This template is sent to admins when a new group is requested
$this->Subject = "Account Deletion Request";
?>

<p>Hello,</p>

<p>A user has requested deletion of their account. User details are below:</p>

<p>
<strong>Username</strong> <?php echo $data["user"]; ?>
<br>
<strong>Name</strong> <?php echo $data["name"]; ?>
<br>
<strong>Email</strong> <?php echo $data["email"]; ?>
</p>
28 changes: 28 additions & 0 deletions tools/docker-dev/sql/bootstrap.sql
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ CREATE TABLE `audit_log` (

-- --------------------------------------------------------

-- --------------------------------------------------------

--
-- Table structure for table `account_deletion_requests`
--

CREATE TABLE `account_deletion_requests` (
`id` int(11) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT current_timestamp(),
`uid` varchar(1000) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- --------------------------------------------------------

--
-- Indexes for dumped tables
--
Expand Down Expand Up @@ -148,6 +162,12 @@ ALTER TABLE `sso_log`
ALTER TABLE `audit_log`
ADD PRIMARY KEY (`id`);

--
-- Indexes for table `audit_log`
--
ALTER TABLE `account_deletion_requests`
ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--
Expand Down Expand Up @@ -189,6 +209,14 @@ ALTER TABLE `audit_log`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
COMMIT;

--
--
-- AUTO_INCREMENT for table `account_deletion_requests`
--
ALTER TABLE `account_deletion_requests`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
6 changes: 5 additions & 1 deletion webroot/admin/user-mgmt.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@
});

foreach ($users as $user) {
echo "<tr>";
if ($user->hasRequestedAccountDeletion()) {
echo "<tr style='color:grey; font-style: italic'>";
} else {
echo "<tr>";
}
echo "<td>" . $user->getFirstname() . " " . $user->getLastname() . "</td>";
echo "<td>" . $user->getUID() . "</td>";
echo "<td>" . $user->getOrg() . "</td>";
Expand Down
66 changes: 58 additions & 8 deletions webroot/panel/account.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@
}
}
break;
case "account_deletion_request":
$hasGroups = count($USER->getGroups()) > 0;
if ($hasGroups) {
die();
break;
}
if (!$SQL->accDeletionRequestExists($USER->getUID())) {
$USER->requestAccountDeletion();
}
Comment on lines +79 to +81
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should still check if the user is a member of any PI group before doing this. If they are, you can just die() the page, since it's not a state that should be reachable. Frontend verification is ideal, but the backend needs to verify as well since the user can change the frontend and submit the form in a way that you weren't expecting.

break;
}
}
?>
Expand Down Expand Up @@ -107,17 +117,30 @@
}

if (!$isPI) {
echo
"<form action='' method='POST' id='piReq'
onsubmit='return confirm(\"Are you sure you want to request a PI account?\")'>
<input type='hidden' name='form_type' value='pi_request'>";
if ($SQL->requestExists($USER->getUID())) {
if ($SQL->accDeletionRequestExists($USER->getUID())) {
echo
"<form action='' method='POST' id='piReq'
onsubmit='return confirm(\"Are you sure you want to request a PI account?\")'>
<input type='hidden' name='form_type' value='pi_request'>";
echo "<input type='submit' value='Request PI Account' disabled>";
echo "<label style='margin-left: 10px'>Your request has been submitted and is currently pending</label>";
echo
"<label style='margin-left: 10px'>
You cannot request PI Account while you have requested account deletion.
</label>";
echo "</form>";
} else {
echo "<input type='submit' value='Request PI Account'>";
echo
"<form action='' method='POST' id='piReq'
onsubmit='return confirm(\"Are you sure you want to request a PI account?\")'>
<input type='hidden' name='form_type' value='pi_request'>";
if ($SQL->requestExists($USER->getUID())) {
echo "<input type='submit' value='Request PI Account' disabled>";
echo "<label style='margin-left: 10px'>Your request has been submitted and is currently pending</label>";
} else {
echo "<input type='submit' value='Request PI Account'>";
}
echo "</form>";
}
echo "</form>";
}
?>

Expand Down Expand Up @@ -194,6 +217,33 @@

</form>

<hr>

<h5>Account Deletion</h5>
<?php
$hasGroups = count($USER->getGroups()) > 0;

if ($hasGroups) {
echo "<p>You cannot request to delete your account while you are in a PI group.</p>";
} else {
echo
"<form action='' method='POST' id='accDel'
onsubmit='return confirm(\"Are you sure you want to request an account deletion?\")'>
<input type='hidden' name='form_type' value='account_deletion_request'>";
if ($SQL->accDeletionRequestExists($USER->getUID())) {
echo "<input type='submit' value='Request Account Deletion' disabled>";
echo "<label style='margin-left: 10px'>Your request has been submitted and is currently pending</label>";
} else {
echo "<input type='submit' value='Request Account Deletion'>";
}
echo "</form>";
}

?>

<hr>


<script>
$("button.btnAddKey").click(function() {
openModal("Add New Key", "<?php echo $CONFIG["site"]["prefix"]; ?>/panel/modal/new_key.php");
Expand Down
9 changes: 8 additions & 1 deletion webroot/panel/groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,14 @@
echo "</table>";
?>

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

<style>
div.modalContent {
Expand Down