diff options
-rw-r--r-- | autotests/kconfigtest.cpp | 13 | ||||
-rw-r--r-- | autotests/kconfigtest.h | 1 | ||||
-rw-r--r-- | src/core/kauthorized.cpp | 18 | ||||
-rw-r--r-- | src/core/kauthorized.h | 54 |
4 files changed, 86 insertions, 0 deletions
diff --git a/autotests/kconfigtest.cpp b/autotests/kconfigtest.cpp index 00e7bbf0..9f88e67b 100644 --- a/autotests/kconfigtest.cpp +++ b/autotests/kconfigtest.cpp @@ -16,6 +16,7 @@ #include <kdesktopfile.h> #include <qtemporarydir.h> +#include <kauthorized.h> #include <kconfiggroup.h> #include <kconfigwatcher.h> #include <ksharedconfig.h> @@ -2071,6 +2072,18 @@ void KConfigTest::testNotify() QCOMPARE(otherWatcherSpy[0][1].value<QByteArrayList>(), QByteArrayList({"someGlobalEntry"})); } +void KConfigTest::testKAuthorizeEnums() +{ + KSharedConfig::Ptr config = KSharedConfig::openConfig(); + KConfigGroup actionRestrictions = config->group("KDE Action Restrictions"); + actionRestrictions.writeEntry("shell_access", false); + actionRestrictions.writeEntry("action/open_with", false); + + QVERIFY(!KAuthorized::authorize(KAuthorized::SHELL_ACCESS)); + QVERIFY(!KAuthorized::authorizeAction(KAuthorized::OPEN_WITH)); + actionRestrictions.deleteGroup(); +} + void KConfigTest::testKdeglobalsVsDefault() { // Add testRestore key with global value in kdeglobals diff --git a/autotests/kconfigtest.h b/autotests/kconfigtest.h index f716bd70..cdc94366 100644 --- a/autotests/kconfigtest.h +++ b/autotests/kconfigtest.h @@ -82,6 +82,7 @@ private Q_SLOTS: void testNewlines(); void testXdgListEntry(); void testNotify(); + void testKAuthorizeEnums(); void testThreads(); diff --git a/src/core/kauthorized.cpp b/src/core/kauthorized.cpp index 3c0941d5..0c8beef9 100644 --- a/src/core/kauthorized.cpp +++ b/src/core/kauthorized.cpp @@ -218,6 +218,15 @@ bool KAuthorized::authorize(const QString &genericAction) return cg.readEntry(genericAction, true); } +bool KAuthorized::authorize(KAuthorized::GenericRestriction action) +{ + const QMetaEnum metaEnum = QMetaEnum::fromType<KAuthorized::GenericRestriction>(); + if (metaEnum.isValid()) { + return KAuthorized::authorize(QString::fromLatin1(metaEnum.valueToKey(action)).toLower()); + } + return false; +} + bool KAuthorized::authorizeAction(const QString &action) { MY_D if (d->blockEverything) @@ -231,6 +240,15 @@ bool KAuthorized::authorizeAction(const QString &action) return authorize(QLatin1String("action/") + action); } +bool KAuthorized::authorizeAction(KAuthorized::GenericAction action) +{ + const QMetaEnum metaEnum = QMetaEnum::fromType<KAuthorized::GenericAction>(); + if (metaEnum.isValid()) { + return KAuthorized::authorizeAction(QString::fromLatin1(metaEnum.valueToKey(action)).toLower()); + } + return false; +} + #if KCONFIGCORE_BUILD_DEPRECATED_SINCE(5, 24) bool KAuthorized::authorizeKAction(const QString &action) { diff --git a/src/core/kauthorized.h b/src/core/kauthorized.h index a61e0927..b7fa4c97 100644 --- a/src/core/kauthorized.h +++ b/src/core/kauthorized.h @@ -10,7 +10,10 @@ #include <kconfigcore_export.h> +#include <QMetaEnum> +#include <QObject> #include <QStringList> +#include <QVariant> class QUrl; class QString; @@ -25,6 +28,39 @@ class QString; */ namespace KAuthorized { +Q_NAMESPACE_EXPORT(KCONFIGCORE_EXPORT); + +/** + * The enum values lower cased represent the action that is authorized + * For example the SHELL_ACCESS value is converted to the "shell_access" string. + * + * @since 5.88 + */ +enum GenericRestriction { + SHELL_ACCESS, // if the user is authorized to open a shell or execute shell commands + GHNS, /// if the collaborative data sharing framework KNewStuff is authorized + // GUI behavior + LINEEDIT_REVEAL_PASSWORD, /// if typed characters in password fields can be made visible + LINEEDIT_TEXT_COMPLETION, /// if line edits should be allowed to display completions + MOVABLE_TOOLBARS, /// if toolbars of of apps should be movable + RUN_DESKTOP_FILES, /// if .desktop files should be run as executables when clicked +}; +Q_ENUM_NS(GenericRestriction) + +/** + * + * @since 5.88 + */ +enum GenericAction { + OPEN_WITH, /// if the open-with menu should be shown for files etc. + EDITFILETYPE, /// if mime-type accociations are allowed to be configured + + OPTIONS_SHOW_TOOLBAR, /// if the toolbar should be displayed in apps + SWITCH_APPLICATION_LANGUAGE, /// if an action to switch the app language should be shown + BOOKMARKS, /// saving bookmarks is allowed +}; +Q_ENUM_NS(GenericAction) + /** * Returns whether the user is permitted to perform a certain action. * @@ -58,6 +94,16 @@ namespace KAuthorized KCONFIGCORE_EXPORT bool authorize(const QString &action); /** + * Returns whether the user is permitted to perform a common action. + * The enum values lower cased represent the action that is + * passed in to @p authorize(QString) + * + * @overload + * @since 5.88 + */ +KCONFIGCORE_EXPORT bool authorize(GenericRestriction action); + +/** * Returns whether the user is permitted to perform a certain action. * * This behaves like authorize(), except that "action/" is prepended to @@ -84,6 +130,14 @@ KCONFIGCORE_EXPORT bool authorize(const QString &action); */ KCONFIGCORE_EXPORT bool authorizeAction(const QString &action); +/** + * Overload to authorize common actions. + * + * @overload + * @since 5.88 + */ +KCONFIGCORE_EXPORT bool authorizeAction(GenericAction action); + #if KCONFIGCORE_ENABLE_DEPRECATED_SINCE(5, 24) /** * Returns whether the user is permitted to perform a certain action. |