Skip to content
This repository has been archived by the owner on Mar 18, 2022. It is now read-only.

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
Add access permissions
  • Loading branch information
Zinc-OS authored Dec 24, 2021
2 parents f2bd9dd + 01d7870 commit 4c80e4d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
71 changes: 63 additions & 8 deletions continuousprint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import flask, json
from octoprint.server.util.flask import restricted_access
from octoprint.events import eventManager, Events
from octoprint.access.permissions import Permissions,ADMIN_GROUP,USER_GROUP

from .print_queue import PrintQueue, QueueItem
from .driver import ContinuousPrintDriver
Expand Down Expand Up @@ -67,6 +68,7 @@ def get_settings_defaults(self):
d[RESTART_MAX_TIME_KEY] = 60*60
return d


##~~ StartupPlugin
def on_after_startup(self):
self._settings.save()
Expand All @@ -85,26 +87,29 @@ def on_after_startup(self):
def on_event(self, event, payload):
if not hasattr(self, "d"): # Sometimes message arrive pre-init
return

if event == Events.PRINT_DONE:
self.d.on_print_success()
self.paused = False
self._msg(type="reload") # reload UI
elif event == Events.PRINT_FAILED and payload["reason"] != "cancelled":
self.d.on_print_failed()
self.paused = False
self._msg(type="reload") # reload UI
elif event == Events.PRINT_CANCELLED:
self.d.on_print_cancelled()
self.paused = False
self._msg(type="reload") # reload UI
elif event == Events.PRINT_PAUSED:
self.d.on_print_paused()
self.paused = True
self._msg(type="reload") # reload UI
elif event == Events.PRINTER_STATE_CHANGED and self._printer.get_state_id() == "OPERATIONAL":
self._msg(type="reload") # reload UI
elif event == Events.UPDATED_FILES:
self._msg(type="updatefiles")
elif event == Events.SETTINGS_UPDATED:
self._update_driver_settings()

# Play out actions until printer no longer in a state where we can run commands
while self._printer.get_state_id() in ["OPERATIONAL", "PAUSED"] and self.d.pending_actions() > 0:
self.d.on_printer_ready()
Expand Down Expand Up @@ -142,7 +147,8 @@ def state_json(self, changed=None):
if changed is not None:
q = json.loads(q)
for i in changed:
q[i]["changed"] = True
if i<len(q):# no deletion of last item
q[i]["changed"] = True
q = json.dumps(q)

resp = ('{"active": %s, "status": "%s", "queue": %s}' % (
Expand All @@ -152,7 +158,13 @@ def state_json(self, changed=None):
))
return resp


# Listen for resume from printer ("M118 //action:queuego"), only act if actually paused. #from @grtrenchman
def resume_action_handler(self, comm, line, action, *args, **kwargs):
if not action == "queuego":
return
if self.paused:
self.d.set_active()

##~~ APIs
@octoprint.plugin.BlueprintPlugin.route("/state", methods=["GET"])
@restricted_access
Expand All @@ -162,6 +174,9 @@ def state(self):
@octoprint.plugin.BlueprintPlugin.route("/move", methods=["POST"])
@restricted_access
def move(self):
if not Permissions.PLUGIN_CONTINUOUSPRINT_CHQUEUE.can():
return flask.make_response("Insufficient Rights", 403)
self._logger.info("attempt failed due to insufficient permissions.")
idx = int(flask.request.form["idx"])
count = int(flask.request.form["count"])
offs = int(flask.request.form["offs"])
Expand All @@ -171,6 +186,9 @@ def move(self):
@octoprint.plugin.BlueprintPlugin.route("/add", methods=["POST"])
@restricted_access
def add(self):
if not Permissions.PLUGIN_CONTINUOUSPRINT_ADDQUEUE.can():
return flask.make_response("Insufficient Rights", 403)
self._logger.info("attempt failed due to insufficient permissions.")
idx = flask.request.form.get("idx")
if idx is None:
idx = len(self.q)
Expand All @@ -187,15 +205,20 @@ def add(self):
@octoprint.plugin.BlueprintPlugin.route("/remove", methods=["POST"])
@restricted_access
def remove(self):
if not Permissions.PLUGIN_CONTINUOUSPRINT_RMQUEUE.can():
return flask.make_response("Insufficient Rights", 403)
self._logger.info("attempt failed due to insufficient permissions.")
idx = int(flask.request.form["idx"])
count = int(flask.request.form["count"])
self.q.remove(idx, count)
return self.state_json(changed=[idx])



@octoprint.plugin.BlueprintPlugin.route("/set_active", methods=["POST"])
@restricted_access
def set_active(self):
if not Permissions.PLUGIN_CONTINUOUSPRINT_STARTQUEUE.can():
return flask.make_response("Insufficient Rights", 403)
self._logger.info(f"attempt failed due to insufficient permissions.")
self.d.set_active(flask.request.form["active"] == "true", printer_ready=(self._printer.get_state_id() == "OPERATIONAL"))
return self.state_json()

Expand Down Expand Up @@ -287,7 +310,37 @@ def get_update_information(self):
pip="https://github.com/Zinc-OS/continuousprint/archive/{target_version}.zip",
)
)

def add_permissions(*args, **kwargs):
return [
dict(key="STARTQUEUE",
name="Start Queue",
description="Allows for starting queue",
roles=["admin","continuousprint-start"],
dangerous=True,
default_groups=[ADMIN_GROUP]
),
dict(key="ADDQUEUE",
name="Add to Queue",
description="Allows for adding prints to the queue",
roles=["admin","continuousprint-add"],
dangerous=True,
default_groups=[ADMIN_GROUP]
),
dict(key="RMQUEUE",
name="Remove Print from Queue ",
description="Allows for removing prints from the queue",
roles=["admin","continuousprint-remove"],
dangerous=True,
default_groups=[ADMIN_GROUP]
),
dict(key="CHQUEUE",
name="Move items in Queue ",
description="Allows for moving items in the queue",
roles=["admin","continuousprint-move"],
dangerous=True,
default_groups=[ADMIN_GROUP]
),
]

__plugin_name__ = "Continuous Print"
__plugin_pythoncompat__ = ">=3.6,<4"
Expand All @@ -299,5 +352,7 @@ def __plugin_load__():

global __plugin_hooks__
__plugin_hooks__ = {
"octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information
"octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information,
"octoprint.access.permissions": __plugin_implementation__.add_permissions,
"octoprint.comm.protocol.action": __plugin_implementation__.resume_action_handler # register to listen for "M118 //action:" commands
}
2 changes: 1 addition & 1 deletion continuousprint/static/js/continuousprint_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CPrintAPI {
dataType: "json",
data: data,
headers: {"X-Api-Key":UI_API_KEY},
success: cb,
success:cb
});
}

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
plugin_name = "continuousprint"

# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
plugin_version = "1.3.3a"
plugin_version = "1.3.0"

# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
# module
Expand Down

0 comments on commit 4c80e4d

Please sign in to comment.