Add configurable note quota to limit the number of notes per user.

This commit is contained in:
Skylar Ittner 2019-01-15 13:25:01 -07:00
parent a87ce1ee73
commit dbd5776b8f
7 changed files with 104 additions and 1 deletions

View File

@ -12,11 +12,21 @@
$note = new Note("", "", getRequestUser()->getUID(), null); $note = new Note("", "", getRequestUser()->getUID(), null);
$newnote = true;
if (!empty($VARS['id'])) { if (!empty($VARS['id'])) {
try { try {
$note = Note::loadNote($VARS['id']); $note = Note::loadNote($VARS['id']);
$newnote = false;
} catch (Exception $ex) { } catch (Exception $ex) {
// It's a new note I guess // It's a new note I guess
$newnote = true;
}
}
if ($newnote && $SETTINGS['note_limit'] !== false && !getRequestUser()->hasPermission($SETTINGS['unlimited_permission'])) {
$notecount = $database->count("notes", ['ownerid' => $_SESSION['uid']]);
if ($notecount >= $SETTINGS['note_limit']) {
sendJsonResp($Strings->get("You've reached your quota limit and can't make new notes.", false), "ERROR");
} }
} }

8
langs/en/quota.json Normal file
View File

@ -0,0 +1,8 @@
{
"You've reached your quota limit and can't make new notes.": "You've reached your quota limit and can't make new notes.",
"Note quota reached": "Note quota reached",
"Note quota": "Note quota",
"Note quota: {x} of {y} used.": "Note quota: {x} of {y} used.",
"Note quota: Unlimited": "Note quota: Unlimited",
"unlimited": "unlimited"
}

View File

@ -20,5 +20,9 @@ define("MESSAGES", [
"note_deleted" => [ "note_deleted" => [
"string" => "Note deleted", "string" => "Note deleted",
"type" => "success" "type" => "success"
],
"quota_reached" => [
"string" => "You've reached your quota limit and can't make new notes.",
"type" => "danger"
] ]
]); ]);

View File

@ -5,13 +5,16 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/ */
$newnote = true;
$note = new Note("", "", $_SESSION['uid'], null); $note = new Note("", "", $_SESSION['uid'], null);
if (!empty($VARS['note'])) { if (!empty($VARS['note'])) {
try { try {
$note = Note::loadNote($VARS['note']); $note = Note::loadNote($VARS['note']);
$newnote = false;
} catch (Exception $ex) { } catch (Exception $ex) {
// It's a new note I guess // It's a new note I guess
$newnote = true;
} }
} }
@ -21,6 +24,13 @@ if ($note->getOwnerID() != $_SESSION['uid']) {
die(); die();
} }
if ($newnote && $SETTINGS['note_limit'] !== false && !(new User($_SESSION['uid']))->hasPermission($SETTINGS['unlimited_permission'])) {
$notecount = $database->count("notes", ['ownerid' => $_SESSION['uid']]);
if ($notecount >= $SETTINGS['note_limit']) {
header("Location: app.php?msg=quota_reached");
}
}
$note->saveNote(); $note->saveNote();
?> ?>

View File

@ -10,6 +10,23 @@ $notes = [];
foreach ($noteids as $n) { foreach ($noteids as $n) {
$notes[] = Note::loadNote($n); $notes[] = Note::loadNote($n);
} }
// Check note quota
$notequota = $SETTINGS['note_limit'];
$notequotaset = $SETTINGS['note_limit'] !== false;
$notequotareached = false;
$usernotecount = 0;
$userunlimited = false;
if ($notequotaset) {
$usernotecount = $database->count("notes", ['ownerid' => $_SESSION['uid']]);
$usernotelimit = $SETTINGS['note_limit'];
if ((new User($_SESSION['uid']))->hasPermission($SETTINGS['unlimited_permission'])) {
$userunlimited = true;
}
if (!$userunlimited && $usernotecount >= $usernotelimit) {
$notequotareached = true;
}
}
?> ?>
<style nonce="<?php echo $SECURE_NONCE; ?>"> <style nonce="<?php echo $SECURE_NONCE; ?>">
@ -70,7 +87,13 @@ foreach ($colors as $c) {
</style> </style>
<div class="btn-group mb-4"> <div class="btn-group mb-4">
<a href="app.php?page=newnote" class="btn btn-success"><i class="fas fa-plus"></i> <?php $Strings->get("New note"); ?></a> <?php if ($notequotareached) { ?>
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#quotamodal">
<i class="fas fa-plus"></i> <?php $Strings->get("New note"); ?>
</button>
<?php } else { ?>
<a href="app.php?page=newnote" class="btn btn-success"><i class="fas fa-plus"></i> <?php $Strings->get("New note"); ?></a>
<?php } ?>
<a href="app.php?page=home" class="btn btn-light"><i class="fas fa-sync-alt"></i> <?php $Strings->get("Refresh"); ?></a> <a href="app.php?page=home" class="btn btn-light"><i class="fas fa-sync-alt"></i> <?php $Strings->get("Refresh"); ?></a>
</div> </div>
@ -185,6 +208,40 @@ foreach ($colors as $c) {
</div> </div>
<?php if ($notequotaset) { ?>
<div class="quotamsg" data-toggle="modal" data-target="#quotamodal">
<?php
if ($userunlimited) {
$Strings->get("Note quota: Unlimited");
} else {
$Strings->build("Note quota: {x} of {y} used.", ["x" => $usernotecount, "y" => $notequota]);
}
?>
</div>
<div class="modal fade" id="quotamodal" tabindex="-1" role="dialog" aria-labelledby="quotamodallabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="quotamodallabel"><i class="fas fa-tachometer-alt"></i> <?php $Strings->get("Note quota"); ?></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<?php
$quotamsg = str_replace(["{{notecount}}", "{{notelimit}}"], [$usernotecount, ($userunlimited ? $Strings->get("unlimited", false) : $notequota)], $SETTINGS['quota_msg_content']);
echo $quotamsg;
?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal"><?php $Strings->get("Close"); ?></button>
</div>
</div>
</div>
</div>
<?php } ?>
<div class="modal fade" tabindex="-1" role="dialog" id="colormodal"> <div class="modal fade" tabindex="-1" role="dialog" id="colormodal">
<div class="modal-dialog" role="document"> <div class="modal-dialog" role="document">
<form class="modal-content" action="action.php" method="POST"> <form class="modal-content" action="action.php" method="POST">

View File

@ -42,6 +42,13 @@ $SETTINGS = [
// List of required user permissions to access this app. // List of required user permissions to access this app.
"permissions" => [ "permissions" => [
], ],
// Set to a number to limit the number of notes each user can create.
"note_limit" => false,
// User permission required to bypass the note limit.
"unlimited_permission" => "",
// HTML to show when the quota is reached. "{{notecount}}" and "{{notelimit}}"
// will be replaced with the number of notes the user has and the note_limit.
"quota_msg_content" => "",
// For supported values, see http://php.net/manual/en/timezones.php // For supported values, see http://php.net/manual/en/timezones.php
"timezone" => "America/Denver", "timezone" => "America/Denver",
// Language to use for localization. See langs folder to add a language. // Language to use for localization. See langs folder to add a language.

View File

@ -59,4 +59,11 @@ li:not(.parsedown-task-list-close):not(.parsedown-task-list-open) {
.parsedown-task-list-open { .parsedown-task-list-open {
list-style-type: none; list-style-type: none;
}
.quotamsg {
text-align: center;
cursor: pointer;
margin-top: 100px;
margin-bottom: -100px;
} }