Skip to content

Commit

Permalink
User human readable quota sizes for domain creation and editing (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
buschmann23 committed Jun 29, 2017
1 parent b91746c commit c17066a
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 47 deletions.
40 changes: 32 additions & 8 deletions src/domaineditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,19 @@ void DomainEditor::edit(Context *c)
new ValidatorInteger(QStringLiteral("domainQuota")),
new ValidatorMin(QStringLiteral("domainQuota"), QMetaType::UInt, 0),
new ValidatorBoolean(QStringLiteral("freeNames")),
new ValidatorBoolean(QStringLiteral("freeAddress"))
new ValidatorBoolean(QStringLiteral("freeAddress")),
new ValidatorRegularExpression(QStringLiteral("humanQuota"), QRegularExpression(QStringLiteral("^\\d+[,.٫]?\\d*\\s*[KMGT]?i?B?"), QRegularExpression::CaseInsensitiveOption)),
new ValidatorRegularExpression(QStringLiteral("humanDomainQuota"), QRegularExpression(QStringLiteral("^\\d+[,.٫]?\\d*\\s*[KMGT]?i?B?"), QRegularExpression::CaseInsensitiveOption))
});

vr = v.validate(c, Validator::FillStashOnError);

} else {

static Validator v({
new ValidatorMin(QStringLiteral("quota"), QMetaType::UInt, 0)
new ValidatorInteger(QStringLiteral("quota")),
new ValidatorMin(QStringLiteral("quota"), QMetaType::UInt, 0),
new ValidatorRegularExpression(QStringLiteral("humanQuota"), QRegularExpression(QStringLiteral("^\\d+[,.٫]?\\d*\\s*[KMGT]?i?B?"), QRegularExpression::CaseInsensitiveOption))
});

vr = v.validate(c, Validator::FillStashOnError);
Expand All @@ -133,16 +137,26 @@ void DomainEditor::edit(Context *c)
help.insert(QStringLiteral("prefix"), HelpEntry(c->translate("DomainEditor", "Prefix"), c->translate("DomainEditor", "The prefix might be used for automatically generated user names, especially if free names are not allowed for this domain.")));
help.insert(QStringLiteral("created"), HelpEntry(c->translate("DomainEditor", "Created"), c->translate("DomainEditor", "Date and time this domain has been created in Skaffari.")));
help.insert(QStringLiteral("updated"), HelpEntry(c->translate("DomainEditor", "Updated"), c->translate("DomainEditor", "Date and time this domain has been updated in Skafari.")));

const QString domainQuotaTitle = c->translate("DomainEditor", "Domain quota");
if (c->stash(QStringLiteral("userType")).value<qint16>() == 0) {
// current user is a super administrator
help.insert(QStringLiteral("maxAccounts"), HelpEntry(c->translate("DomainEditor", "Maximum accounts"), c->translate("DomainEditor", "The maximum accounts value limits the amount of accounts that can be created for this domain. Set it to 0 to disable the limit.")));
help.insert(QStringLiteral("domainQuota"), HelpEntry(c->translate("DomainEditor", "Domain quota"), c->translate("DomainEditor", "Overall quota limit for all accounts that belong to this domain. If the domain quota is set, every account must have a quota defined. Set it to 0 to disable the domain quota.")));
help.insert(QStringLiteral("domainQuota"), HelpEntry(domainQuotaTitle, c->translate("DomainEditor", "Overall quota limit for all accounts that belong to this domain. If the domain quota is set, every account must have a quota defined. Set it to 0 to disable the domain quota.")));
help.insert(QStringLiteral("humanDomainQuota"), HelpEntry(domainQuotaTitle, c->translate("DomainEditor", "Overall quota limit for all accounts that belong to this domain. If the domain quota is set, every account must have a quota defined. Set it to 0 to disable the domain quota. You can use the multipliers M, MiB, G, GiB, T and TiB. Without a multiplier, KiB is the default.")));
} else {
// current user is a domain administrator
const QString domainQuotaText = c->translate("DomainEditor", "Overall quota limit for all accounts that belong to this domain. If the domain quota is set (not unlimited), every account must have a quota defined.");
help.insert(QStringLiteral("maxAccounts"), HelpEntry(c->translate("DomainEditor", "Accounts"), c->translate("DomainEditor", "Shows the current amount of accounts created in this domain and the maximum number of accounts that can be created for this domain.")));
help.insert(QStringLiteral("domainQuota"), HelpEntry(c->translate("DomainEditor", "Domain quota"), c->translate("DomainEditor", "Overall quota limit for all accounts that belong to this domain. If the domain quota is set (not unlimited), every account must have a quota defined.")));
help.insert(QStringLiteral("domainQuota"), HelpEntry(domainQuotaTitle, domainQuotaText));
help.insert(QStringLiteral("humanDomainQuota"), HelpEntry(domainQuotaTitle, domainQuotaText));
}
help.insert(QStringLiteral("quota"), HelpEntry(c->translate("DomainEditor", "Default quota"), c->translate("DomainEditor", "Default quota for new accounts for this domain. This value can be changed individually for every account.")));

const QString quotaTitle = c->translate("DomainEditor", "Default quota");
help.insert(QStringLiteral("quota"), HelpEntry(quotaTitle, c->translate("DomainEditor", "Default quota for new accounts for this domain. This value can be changed individually for every account.")));
help.insert(QStringLiteral("humanQuota"), HelpEntry(quotaTitle, c->translate("DomainEditor", "Default quota for new accounts for this domain. This value can be changed individually for every account. You can use the multipliers M, MiB, G, GiB, T and TiB. Without a multiplier, KiB is the default.")));


help.insert(QStringLiteral("folders"), HelpEntry(c->translate("DomainEditor", "Standard folders"), c->translate("DomainEditor", "Comma separated list of folders that will be automatically created when creating a new account for this domain. You can safely insert localized folder names in UTF-8 encoding. They will be internally converted into UTF-7-IMAP encoding.")));
help.insert(QStringLiteral("transport"), HelpEntry(c->translate("DomainEditor", "Transport"), c->translate("DomainEditor", "The transport mechanism for received emails for this domain. Defaults to Cyrus.")));
help.insert(QStringLiteral("freeNames"), HelpEntry(c->translate("DomainEditor", "Allow free names"), c->translate("DomainEditor", "If enabled, account user names for this domain can be freely selected (if not in use already).")));
Expand Down Expand Up @@ -215,7 +229,9 @@ void DomainEditor::create(Context* c)
new ValidatorInteger(QStringLiteral("domainQuota")),
new ValidatorMin(QStringLiteral("domainQuota"), QMetaType::UInt, 0),
new ValidatorBoolean(QStringLiteral("freeNames")),
new ValidatorBoolean(QStringLiteral("freeAddress"))
new ValidatorBoolean(QStringLiteral("freeAddress")),
new ValidatorRegularExpression(QStringLiteral("humanQuota"), QRegularExpression(QStringLiteral("^\\d+[,.٫]?\\d*\\s*[KMGT]?i?B?"), QRegularExpression::CaseInsensitiveOption)),
new ValidatorRegularExpression(QStringLiteral("humanDomainQuota"), QRegularExpression(QStringLiteral("^\\d+[,.٫]?\\d*\\s*[KMGT]?i?B?"), QRegularExpression::CaseInsensitiveOption))
});

const ValidatorResult vr = v.validate(c, Validator::FillStashOnError);
Expand All @@ -234,16 +250,24 @@ void DomainEditor::create(Context* c)
help.insert(QStringLiteral("domainName"), HelpEntry(c->translate("DomainEditor", "Domain name"), c->translate("DomainEditor", "The name of the domain you want to manage emails for, like example.com. You can safely insert international domain names in UTF-8 encoding, it will be converted internally into ASCII compatible encoding.")));
help.insert(QStringLiteral("prefix"), HelpEntry(c->translate("DomainEditor", "Prefix"), c->translate("DomainEditor", "The prefix might be used for automatically generated user names, especially if free names are not allowed for this domain.")));
help.insert(QStringLiteral("maxAccounts"), HelpEntry(c->translate("DomainEditor", "Maximum accounts"), c->translate("DomainEditor", "The maximum accounts value limits the amount of accounts that can be created for this domain. Set it to 0 to disable the limit.")));
help.insert(QStringLiteral("domainQuota"), HelpEntry(c->translate("DomainEditor", "Domain quota"), c->translate("DomainEditor", "Overall quota limit for all accounts that belong to this domain. If the domain quota is set, every account must have a quota defined. Set it to 0 to disable the domain quota.")));
help.insert(QStringLiteral("quota"), HelpEntry(c->translate("DomainEditor", "Default quota"), c->translate("DomainEditor", "Default quota for new accounts for this domain. This value can be changed individually for every account.")));

const QString domainQuotaTitle = c->translate("DomainEditor", "Domain quota");
const QString quotaTitle = c->translate("DomainEditor", "Default quota");
help.insert(QStringLiteral("domainQuota"), HelpEntry(domainQuotaTitle, c->translate("DomainEditor", "Overall quota limit for all accounts that belong to this domain. If the domain quota is set, every account must have a quota defined. Set it to 0 to disable the domain quota. ")));
help.insert(QStringLiteral("humanDomainQuota"), HelpEntry(domainQuotaTitle, c->translate("DomainEditor", "Overall quota limit for all accounts that belong to this domain. If the domain quota is set, every account must have a quota defined. Set it to 0 to disable the domain quota. You can use the multipliers M, MiB, G, GiB, T and TiB. Without a multiplier, KiB is the default.")));
help.insert(QStringLiteral("quota"), HelpEntry(quotaTitle, c->translate("DomainEditor", "Default quota for new accounts for this domain. This value can be changed individually for every account.")));
help.insert(QStringLiteral("humanQuota"), HelpEntry(quotaTitle, c->translate("DomainEditor", "Default quota for new accounts for this domain. This value can be changed individually for every account. You can use the multipliers M, MiB, G, GiB, T and TiB. Without a multiplier, KiB is the default.")));

help.insert(QStringLiteral("folders"), HelpEntry(c->translate("DomainEditor", "Standard folders"), c->translate("DomainEditor", "Comma separated list of folders that will be automatically created when creating a new account for this domain. You can safely insert localized folder names in UTF-8 encoding. They will be internally converted into UTF-7-IMAP encoding.")));
help.insert(QStringLiteral("transport"), HelpEntry(c->translate("DomainEditor", "Transport"), c->translate("DomainEditor", "The transport mechanism for received emails for this domain. Defaults to Cyrus.")));
help.insert(QStringLiteral("freeNames"), HelpEntry(c->translate("DomainEditor", "Allow free names"), c->translate("DomainEditor", "If enabled, account user names for this domain can be freely selected (if not in use already).")));
help.insert(QStringLiteral("freeAddress"), HelpEntry(c->translate("DomainEditor", "Allow free addresses"), c->translate("DomainEditor", "If enabled, user accounts in this domain can have email addresses for all domains managed by Skaffari. If disabled, only email addresses for this domain can be added to user accounts in this domain.")));

c->stash({
{QStringLiteral("defQuota"), SkaffariConfig::defQuota()},
{QStringLiteral("defHumanQuota"), Utils::humanBinarySize(c, static_cast<quint64>(SkaffariConfig::defQuota()) * Q_UINT64_C(1024))},
{QStringLiteral("defDomainQuota"), SkaffariConfig::defDomainquota()},
{QStringLiteral("defHumanDomainQuota"), Utils::humanBinarySize(c, static_cast<quint64>(SkaffariConfig::defDomainquota()) * Q_UINT64_C(1024))},
{QStringLiteral("defMaxAccounts"), SkaffariConfig::defMaxaccounts()},
{QStringLiteral("template"), QStringLiteral("domain/create.html")},
{QStringLiteral("domainAsPrefix"), SkaffariConfig::imapDomainasprefix()},
Expand Down
80 changes: 76 additions & 4 deletions src/objects/domain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,43 @@ Domain Domain::create(Cutelyst::Context *c, const Cutelyst::ParamsMultiMap &para
qCWarning(SK_DOMAIN, "Failed to create domain %s: name is already in use by domain ID %u", domainName.toUtf8().constData(), q.value(0).value<quint32>());
}

quint32 quota = 0;
bool quotaOk = true;
if (params.contains(QStringLiteral("humanQuota"))) {
quota = Utils::humanToIntSize(c, params.value(QStringLiteral("humanQuota")), &quotaOk);
if (!quotaOk) {
errorData->setErrorType(SkaffariError::InputError);
errorData->setErrorText(c->translate("Domain", "Failed to convert human readable quota size string into valid integer value."));
return dom;
}
} else {
quota = params.value(QStringLiteral("quota")).toULong(&quotaOk);
if (!quotaOk) {
errorData->setErrorType(SkaffariError::InputError);
errorData->setErrorText(c->translate("Domain", "Failed to parse quota string into integer value."));
return dom;
}
}

quint32 domainQuota = 0;
bool domainQuotaOk = true;
if (params.contains(QStringLiteral("humanDomainQuota"))) {
domainQuota = Utils::humanToIntSize(c, params.value(QStringLiteral("humanDomainQuota")), &domainQuotaOk);
if (!domainQuotaOk) {
errorData->setErrorType(SkaffariError::InputError);
errorData->setErrorText(c->translate("Domain", "Failed to convert human readable quota size string into valid integer value."));
return dom;
}
} else {
domainQuota = params.value(QStringLiteral("domainQuota")).toULong(&domainQuotaOk);
if (!domainQuotaOk) {
errorData->setErrorType(SkaffariError::InputError);
errorData->setErrorText(c->translate("Domain", "Failed to parse quota string into integer value."));
return dom;
}
}

const quint32 maxAccounts = params.value(QStringLiteral("maxAccounts")).toULong();
const quint32 quota = params.value(QStringLiteral("quota")).toULong();
const quint32 domainQuota = params.value(QStringLiteral("domainQuota")).toULong();
const bool freeNames = params.contains(QStringLiteral("freeNames"));
const bool freeAddress = params.contains(QStringLiteral("freeAddress"));
const QStringList folders = Domain::trimStringList(params.value(QStringLiteral("folders")).split(QLatin1Char(','), QString::SkipEmptyParts));
Expand Down Expand Up @@ -663,14 +697,49 @@ bool Domain::update(Cutelyst::Context *c, const Cutelyst::ParamsMultiMap &p, Ska

QSqlQuery q;

const quint32 quota = p.value(QStringLiteral("quota"), QString::number(d->getQuota())).toULong();
const QStringList folders = Domain::trimStringList(p.value(QStringLiteral("folders")).split(QLatin1Char(','), QString::SkipEmptyParts));
const QDateTime currentTimeUtc = QDateTime::currentDateTimeUtc();


quint32 quota = 0;
bool quotaOk = true;
if (p.contains(QStringLiteral("humanQuota"))) {
quota = Utils::humanToIntSize(c, p.value(QStringLiteral("humanQuota")), &quotaOk);
if (!quotaOk) {
e->setErrorType(SkaffariError::InputError);
e->setErrorText(c->translate("Domain", "Failed to convert human readable quota size string into valid integer value."));
return ret;
}
} else {
quota = p.value(QStringLiteral("quota"), QString::number(d->getQuota())).toULong(&quotaOk);
if (!quotaOk) {
e->setErrorType(SkaffariError::InputError);
e->setErrorText(c->translate("Domain", "Failed to parse quota string into integer value."));
return ret;
}
}

if (u.value(QStringLiteral("type")).value<qint16>() == 0) {

quint32 domainQuota = 0;
bool domainQuotaOk = true;
if (p.contains(QStringLiteral("humanDomainQuota"))) {
domainQuota = Utils::humanToIntSize(c, p.value(QStringLiteral("humanDomainQuota")), &domainQuotaOk);
if (!domainQuotaOk) {
e->setErrorType(SkaffariError::InputError);
e->setErrorText(c->translate("Domain", "Failed to convert human readable quota size string into valid integer value."));
return ret;
}
} else {
domainQuota = p.value(QStringLiteral("domainQuota"), QString::number(d->getDomainQuota())).toULong(&domainQuotaOk);
if (!domainQuotaOk) {
e->setErrorType(SkaffariError::InputError);
e->setErrorText(c->translate("Domain", "Failed to parse quota string into integer value."));
return ret;
}
}

const quint32 maxAccounts = p.value(QStringLiteral("maxAccounts"), QString::number(d->getMaxAccounts())).toULong();
const quint32 domainQuota = p.value(QStringLiteral("domainQuota"), QString::number(d->getDomainQuota())).toULong();
const bool freeNames = p.contains(QStringLiteral("freeNames"));
const bool freeAddress = p.contains(QStringLiteral("freeAddress"));
const QString transport = p.value(QStringLiteral("transport"), d->getTransport());
Expand All @@ -692,8 +761,10 @@ bool Domain::update(Cutelyst::Context *c, const Cutelyst::ParamsMultiMap &p, Ska
}

d->setQuota(quota);
d->setHumanQuota(Utils::humanBinarySize(c, static_cast<quint64>(quota) * Q_UINT64_C(1024)));
d->setMaxAccounts(maxAccounts);
d->setDomainQuota(domainQuota);
d->setHumanDomainQuota(Utils::humanBinarySize(c, static_cast<quint64>(domainQuota) * Q_UINT64_C(1024)));
d->setFreeNamesEnabled(freeNames);
d->setFreeAddressEnabled(freeAddress);
d->setTransport(transport);
Expand All @@ -715,6 +786,7 @@ bool Domain::update(Cutelyst::Context *c, const Cutelyst::ParamsMultiMap &p, Ska
}

d->setQuota(quota);
d->setHumanQuota(Utils::humanBinarySize(c, static_cast<quint64>(quota) * Q_UINT64_C(1024)));
d->setUpdated(Utils::toUserTZ(c, currentTimeUtc));

ret = true;
Expand Down
Loading

0 comments on commit c17066a

Please sign in to comment.