Add plugin permissions and IO API (#5231)

This commit is contained in:
Mm2PL
2024-03-09 20:16:25 +01:00
committed by GitHub
parent 2361d30e4b
commit 658fceddaa
16 changed files with 889 additions and 19 deletions
+66
View File
@@ -9,6 +9,7 @@
# include <QJsonArray>
# include <QJsonObject>
# include <algorithm>
# include <unordered_map>
# include <unordered_set>
@@ -111,6 +112,48 @@ PluginMeta::PluginMeta(const QJsonObject &obj)
QString("version is not a string (its type is %1)").arg(type));
this->version = semver::version(0, 0, 0);
}
auto permsObj = obj.value("permissions");
if (!permsObj.isUndefined())
{
if (!permsObj.isArray())
{
QString type = magic_enum::enum_name(permsObj.type()).data();
this->errors.emplace_back(
QString("permissions is not an array (its type is %1)")
.arg(type));
return;
}
auto permsArr = permsObj.toArray();
for (int i = 0; i < permsArr.size(); i++)
{
const auto &t = permsArr.at(i);
if (!t.isObject())
{
QString type = magic_enum::enum_name(t.type()).data();
this->errors.push_back(QString("permissions element #%1 is not "
"an object (its type is %2)")
.arg(i)
.arg(type));
return;
}
auto parsed = PluginPermission(t.toObject());
if (parsed.isValid())
{
// ensure no invalid permissions slip through this
this->permissions.push_back(parsed);
}
else
{
for (const auto &err : parsed.errors)
{
this->errors.push_back(
QString("permissions element #%1: %2").arg(i).arg(err));
}
}
}
}
auto tagsObj = obj.value("tags");
if (!tagsObj.isUndefined())
{
@@ -201,5 +244,28 @@ void Plugin::removeTimeout(QTimer *timer)
}
}
bool Plugin::hasFSPermissionFor(bool write, const QString &path)
{
auto canon = QUrl(this->dataDirectory().absolutePath() + "/");
if (!canon.isParentOf(path))
{
return false;
}
using PType = PluginPermission::Type;
auto typ = write ? PType::FilesystemWrite : PType::FilesystemRead;
// XXX: Older compilers don't have support for std::ranges
// NOLINTNEXTLINE(readability-use-anyofallof)
for (const auto &p : this->meta.permissions)
{
if (p.type == typ)
{
return true;
}
}
return false;
}
} // namespace chatterino
#endif