@@ -27,6 +27,8 @@ Model *getModel(const std::string &pluginSlug, const std::string &modelSlug); | |||||
std::string normalizeTag(const std::string &tag); | std::string normalizeTag(const std::string &tag); | ||||
/** Checks that the slug contains only alphanumeric characters, "-", and "_" */ | /** Checks that the slug contains only alphanumeric characters, "-", and "_" */ | ||||
bool isSlugValid(const std::string &slug); | bool isSlugValid(const std::string &slug); | ||||
/** Returns a string containing only the valid slug characters. */ | |||||
std::string normalizeSlug(const std::string &slug); | |||||
struct Update { | struct Update { | ||||
@@ -485,8 +485,9 @@ bool isLoggedIn() { | |||||
} | } | ||||
Plugin *getPlugin(const std::string &pluginSlug) { | Plugin *getPlugin(const std::string &pluginSlug) { | ||||
std::string slug = normalizeSlug(pluginSlug); | |||||
for (Plugin *plugin : plugins) { | for (Plugin *plugin : plugins) { | ||||
if (plugin->slug == pluginSlug) { | |||||
if (plugin->slug == slug) { | |||||
return plugin; | return plugin; | ||||
} | } | ||||
} | } | ||||
@@ -610,6 +611,16 @@ bool isSlugValid(const std::string &slug) { | |||||
return true; | return true; | ||||
} | } | ||||
std::string normalizeSlug(const std::string &slug) { | |||||
std::string s; | |||||
for (char c : slug) { | |||||
if (!(std::isalnum(c) || c == '-' || c == '_')) | |||||
continue; | |||||
s += c; | |||||
} | |||||
return s; | |||||
} | |||||
std::vector<Plugin*> plugins; | std::vector<Plugin*> plugins; | ||||
@@ -14,13 +14,19 @@ Plugin::~Plugin() { | |||||
} | } | ||||
void Plugin::addModel(Model *model) { | void Plugin::addModel(Model *model) { | ||||
assert(isSlugValid(model->slug)); | |||||
// Check that the model is not added to a plugin already | |||||
assert(!model->plugin); | assert(!model->plugin); | ||||
// Check model slug | |||||
if (!isSlugValid(model->slug)) { | |||||
WARN("Module slug \"%s\" is invalid", model->slug.c_str()); | |||||
return; | |||||
} | |||||
model->plugin = this; | model->plugin = this; | ||||
models.push_back(model); | models.push_back(model); | ||||
} | } | ||||
Model *Plugin::getModel(std::string slug) { | Model *Plugin::getModel(std::string slug) { | ||||
slug = normalizeSlug(slug); | |||||
for (Model *model : models) { | for (Model *model : models) { | ||||
if (model->slug == slug) { | if (model->slug == slug) { | ||||
return model; | return model; | ||||