Skip to content

Commit 525ed17

Browse files
committed
✨ Add dedicated file check permission
1 parent 71dc42d commit 525ed17

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

octoprint_file_check/__init__.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import octoprint.plugin
1616
import sarge
1717
from flask_babel import gettext
18+
from octoprint.access import ADMIN_GROUP, USER_GROUP
1819
from octoprint.filemanager import get_file_type
1920

2021
CHECKS = {
@@ -107,7 +108,7 @@ def on_event(self, event, payload):
107108
##~~ SimpleApiPlugin API
108109

109110
def on_api_get(self, request):
110-
if not octoprint.access.permissions.Permissions.FILES_DOWNLOAD.can():
111+
if not octoprint.access.permissions.Permissions.PLUGIN_FILE_CHECK_RUN.can():
111112
return flask.make_response("Insufficient rights", 403)
112113

113114
response = {
@@ -121,7 +122,7 @@ def get_api_commands(self):
121122

122123
def on_api_command(self, command, data):
123124
if command == "check_all":
124-
if not octoprint.access.permissions.Permissions.FILES_DOWNLOAD.can():
125+
if not octoprint.access.permissions.Permissions.PLUGIN_FILE_CHECK_RUN.can():
125126
return flask.make_response("Insufficient rights", 403)
126127

127128
self._start_full_check()
@@ -130,6 +131,19 @@ def on_api_command(self, command, data):
130131
headers={"Location": flask.url_for("index") + "api/plugin/file_check"},
131132
)
132133

134+
##~~ Additional permissions hook
135+
136+
def get_additional_permissions(self):
137+
return [
138+
{
139+
"key": "RUN",
140+
"name": "Run File Check",
141+
"description": gettext("Allows to run File Check and view the results."),
142+
"default_groups": [USER_GROUP, ADMIN_GROUP],
143+
"roles": ["run"],
144+
}
145+
]
146+
133147
##~~ SoftwareUpdate hook
134148

135149
def get_update_information(self):
@@ -322,5 +336,6 @@ def _trigger_check_update(self):
322336

323337
__plugin_implementation__ = FileCheckPlugin()
324338
__plugin_hooks__ = {
325-
"octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information
339+
"octoprint.access.permissions": __plugin_implementation__.get_additional_permissions,
340+
"octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information,
326341
}

octoprint_file_check/static/js/file_check.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ $(function () {
3737
};
3838

3939
self.requestData = function () {
40-
if (!self.loginState.hasPermission(self.access.permissions.FILES_DOWNLOAD))
40+
if (
41+
!self.loginState.hasPermission(
42+
self.access.permissions.PLUGIN_FILE_CHECK_RUN
43+
)
44+
)
4145
return $.Deferred().reject();
4246
return OctoPrint.plugins.file_check.get().done(self.fromResponse);
4347
};
@@ -58,7 +62,11 @@ $(function () {
5862
};
5963

6064
self.checkAll = function () {
61-
if (!self.loginState.hasPermission(self.access.permissions.FILES_DOWNLOAD))
65+
if (
66+
!self.loginState.hasPermission(
67+
self.access.permissions.PLUGIN_FILE_CHECK_RUN
68+
)
69+
)
6270
return;
6371

6472
showConfirmationDialog({
@@ -90,12 +98,18 @@ $(function () {
9098
return (
9199
self.checkResult() &&
92100
self.checkResult()[key] &&
93-
self.loginState.hasPermission(self.access.permissions.FILES_DOWNLOAD)
101+
self.loginState.hasPermission(
102+
self.access.permissions.PLUGIN_FILE_CHECK_RUN
103+
)
94104
);
95105
};
96106

97107
self.showCheckResult = function (storage, file) {
98-
if (!self.loginState.hasPermission(self.access.permissions.FILES_DOWNLOAD))
108+
if (
109+
!self.loginState.hasPermission(
110+
self.access.permissions.PLUGIN_FILE_CHECK_RUN
111+
)
112+
)
99113
return;
100114

101115
if (_.isPlainObject(storage)) {
@@ -150,7 +164,11 @@ $(function () {
150164
self.onDataUpdaterPluginMessage = function (plugin, data) {
151165
if (plugin !== "file_check") return;
152166

153-
if (!self.loginState.hasPermission(self.access.permissions.FILES_DOWNLOAD))
167+
if (
168+
!self.loginState.hasPermission(
169+
self.access.permissions.PLUGIN_FILE_CHECK_RUN
170+
)
171+
)
154172
return;
155173

156174
switch (data["action"]) {
@@ -231,7 +249,7 @@ $(function () {
231249
class: "filecheck-trigger accordion-heading-button btn-group",
232250
attr: {
233251
"data-bind":
234-
"visible: $root.fileCheckViewModel.fullCheckAvailable() && $root.loginState.hasPermission($root.access.permissions.FILES_DOWNLOAD)"
252+
"visible: $root.fileCheckViewModel.fullCheckAvailable() && $root.loginState.hasPermission($root.access.permissions.PLUGIN_FILE_CHECK_RUN)"
235253
}
236254
}).append(
237255
$("<a/>", {
@@ -256,7 +274,7 @@ $(function () {
256274

257275
const regex = /<div class="uploaded">/;
258276
const template =
259-
"<div class='filecheck_result' data-bind='visible: $root.fileCheckViewModel.hasCheckResult($data) && $root.loginState.hasPermission($root.access.permissions.FILES_DOWNLOAD)'><small>" +
277+
"<div class='filecheck_result' data-bind='visible: $root.fileCheckViewModel.hasCheckResult($data) && $root.loginState.hasPermission($root.access.permissions.PLUGIN_FILE_CHECK_RUN)'><small>" +
260278
"<a href='javascript:void(0)' class='text-error' style='text-decoration: underline' data-bind='click: function() { $root.fileCheckViewModel.showCheckResult($data) }'>" +
261279
"<i class='fas fa-exclamation-circle'></i> " +
262280
gettext("File Check detected issues with this file!") +
@@ -272,7 +290,7 @@ $(function () {
272290
$("<small/>", {
273291
attr: {
274292
"data-bind":
275-
"visible: $root.fileCheckViewModel.hasCheckResult($root.filepath()) && $root.loginState.hasPermission($root.access.permissions.FILES_DOWNLOAD)"
293+
"visible: $root.fileCheckViewModel.hasCheckResult($root.filepath()) && $root.loginState.hasPermission($root.access.permissions.PLUGIN_FILE_CHECK_RUN)"
276294
}
277295
})
278296
.append(

0 commit comments

Comments
 (0)