8#include <Cutelyst/Controller>
9#include <Cutelyst/Dispatcher>
10#include <Cutelyst/Plugins/Authentication/authentication.h>
12using namespace Qt::StringLiterals;
131 return AroundExecute;
137 Q_UNUSED(application)
139 const auto attributes = args.value(QLatin1String(
"attributes")).value<
ParamsMultiMap>();
140 d->actionReverse = args.value(QLatin1String(
"reverse")).toString();
142 if (!attributes.contains(QLatin1String(
"RequiresRole")) &&
143 !attributes.contains(QLatin1String(
"AllowedRole"))) {
144 qFatal(
"RoleACL: Action %s requires at least one RequiresRole or AllowedRole attribute",
145 qPrintable(d->actionReverse));
147 const QStringList required = attributes.values(QLatin1String(
"RequiresRole"));
148 for (
const QString &role : required) {
149 d->requiresRole.append(role);
152 const QStringList allowed = attributes.values(QLatin1String(
"AllowedRole"));
153 for (
const QString &role : allowed) {
154 d->allowedRole.append(role);
158 auto it = attributes.constFind(QLatin1String(
"ACLDetachTo"));
159 if (it == attributes.constEnd() || it.value().isEmpty()) {
160 qFatal(
"RoleACL: Action %s requires the ACLDetachTo(<action>) attribute",
161 qPrintable(d->actionReverse));
163 d->aclDetachTo = it.value();
187 const QStringList required = d->requiresRole;
188 const QStringList allowed = d->allowedRole;
190 if (!required.isEmpty() && !allowed.isEmpty()) {
191 for (
const QString &role : required) {
192 if (!user_has.contains(role)) {
197 for (
const QString &role : allowed) {
198 if (user_has.contains(role)) {
202 }
else if (!required.isEmpty()) {
203 return std::ranges::all_of(
204 required, [user_has](
const QString &role) {
return user_has.contains(role); });
205 }
else if (!allowed.isEmpty()) {
206 for (
const QString &role : allowed) {
207 if (user_has.contains(role)) {
221 d->detachTo = controller->
actionFor(d->aclDetachTo);
226 "RoleACL: Action '%s' requires a valid action set on the ACLDetachTo(%s) attribute",
227 qPrintable(d->actionReverse),
228 qPrintable(d->aclDetachTo));
235#include "moc_roleacl.cpp"
The Cutelyst application.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
static AuthenticationUser user(Context *c)
The Cutelyst Component base class.
virtual bool aroundExecute(Context *c, QStack< Component * > stack)
void detach(Action *action=nullptr)
Cutelyst Controller base class.
Action * actionFor(QStringView name) const
Action * getActionByPath(QStringView path) const
User role-based authorization action role.
bool canVisit(Context *c) const
bool init(Application *application, const QVariantHash &args) override
RoleACL(QObject *parent=nullptr)
bool aroundExecute(Context *c, QStack< Component * > stack) override
Modifiers modifiers() const override
bool dispatcherReady(const Dispatcher *dispatcher, Controller *controller) override
QMultiMap< QString, QString > ParamsMultiMap
The Cutelyst namespace holds all public Cutelyst API.