✨ New events UserLogged(In|Out) & modified ClientOpened
On the push socket only available for admin users. ClientOpened now also get the remoteAddress stripped from the payload for guests on the push socket. Implements #783
This commit is contained in:
1 parent
311fdd5aca
commit
1146a1bb6f
5 files changed
+53
-3
No files matched your search
+15
-1
@@ -104,7 +104,8 @@ ClientOpened
|
||||
|
||||
Payload:
|
||||
|
||||
* ``remoteAddress``: the remote address (IP) of the client that connected
|
||||
* ``remoteAddress``: the remote address (IP) of the client that connected. On the push socket only available with
|
||||
a valid login session.
|
||||
|
||||
**Note:** Name changed in version 1.1.0
|
||||
|
||||
@@ -115,6 +116,19 @@ ClientClosed
|
||||
|
||||
* ``remoteAddress``: the remote address (IP) of the client that disconnected
|
||||
|
||||
UserLoggedIn
|
||||
A user logged in. On the push socket only available with a valid login session with admin rights.
|
||||
|
||||
Payload:
|
||||
|
||||
* ``username``: the name of the user who logged in
|
||||
|
||||
UserLoggedOut
|
||||
A user logged out. On the push socket only available with a valid login session with admin rights.
|
||||
|
||||
Payload:
|
||||
* ``username``: the name of the user who logged out
|
||||
|
||||
ConnectivityChanged
|
||||
The server's internet connectivity changed
|
||||
|
||||
|
||||
@@ -47,6 +47,10 @@ class Events(object):
|
||||
CLIENT_OPENED = "ClientOpened"
|
||||
CLIENT_CLOSED = "ClientClosed"
|
||||
|
||||
# user login/logout
|
||||
USER_LOGGED_IN = "UserLoggedIn"
|
||||
USER_LOGGED_OUT = "UserLoggedOut"
|
||||
|
||||
# File management
|
||||
UPLOAD = "Upload"
|
||||
FILE_SELECTED = "FileSelected"
|
||||
|
||||
@@ -21,6 +21,7 @@ from octoprint.settings import settings as s, valid_boolean_trues
|
||||
from octoprint.server.util import noCachingExceptGetResponseHandler, loginFromApiKeyRequestHandler, loginFromAuthorizationHeaderRequestHandler, corsRequestHandler, corsResponseHandler
|
||||
from octoprint.server.util.flask import no_firstrun_access, get_json_command_from_request, passive_login, get_remote_address
|
||||
from octoprint.access.permissions import Permissions
|
||||
from octoprint.events import eventManager, Events
|
||||
|
||||
|
||||
#~~ init api blueprint, including sub modules
|
||||
@@ -242,6 +243,9 @@ def login():
|
||||
|
||||
r = make_response(jsonify(response))
|
||||
r.delete_cookie("active_logout")
|
||||
|
||||
eventManager().fire(Events.USER_LOGGED_IN, payload=dict(username=user.get_id()))
|
||||
|
||||
return r
|
||||
|
||||
return make_response(("User unknown or password incorrect", 401, []))
|
||||
@@ -254,6 +258,10 @@ def login():
|
||||
|
||||
@api.route("/logout", methods=["POST"])
|
||||
def logout():
|
||||
username = None
|
||||
if current_user:
|
||||
username = current_user.get_id()
|
||||
|
||||
# logout from user manager...
|
||||
_logout(current_user)
|
||||
|
||||
@@ -264,6 +272,9 @@ def logout():
|
||||
r = make_response(jsonify(octoprint.server.userManager.anonymous_user_factory()))
|
||||
r.set_cookie("active_logout", "true")
|
||||
|
||||
if username:
|
||||
eventManager().fire(Events.USER_LOGGED_OUT, payload=dict(username=username))
|
||||
|
||||
return r
|
||||
|
||||
|
||||
|
||||
@@ -35,6 +35,8 @@ from octoprint.util import DefaultOrderedDict
|
||||
from octoprint.util.json import JsonEncoding
|
||||
from octoprint.util.net import is_lan_address
|
||||
|
||||
from octoprint.events import eventManager, Events
|
||||
|
||||
from werkzeug.local import LocalProxy
|
||||
from cachelib import BaseCache
|
||||
|
||||
@@ -583,6 +585,9 @@ def passive_login():
|
||||
if hasattr(u, "session"):
|
||||
flask.session["usersession.id"] = u.session
|
||||
flask.g.user = u
|
||||
|
||||
eventManager().fire(Events.USER_LOGGED_IN, payload=dict(username=u.get_id()))
|
||||
|
||||
return u
|
||||
|
||||
if user is not None and user.is_active:
|
||||
|
||||
@@ -73,6 +73,13 @@ class PrinterStateConnection(octoprint.vendor.sockjs.tornado.SockJSConnection,
|
||||
LoginStatusListener,
|
||||
GroupChangeListener):
|
||||
|
||||
_event_permissions = {Events.USER_LOGGED_IN: [Permissions.ADMIN],
|
||||
Events.USER_LOGGED_OUT: [Permissions.ADMIN],
|
||||
"*": []}
|
||||
|
||||
_event_payload_processors = {Events.CLIENT_OPENED: [lambda user, payload: payload if user.has_permission(Permissions.ADMIN) else dict()],
|
||||
"*": []}
|
||||
|
||||
_emit_permissions = {"connected": [],
|
||||
"reauthRequired": [],
|
||||
"*": [Permissions.STATUS]}
|
||||
@@ -255,6 +262,15 @@ class PrinterStateConnection(octoprint.vendor.sockjs.tornado.SockJSConnection,
|
||||
self._emit("history", payload=data_to_send)
|
||||
|
||||
def sendEvent(self, type, payload=None):
|
||||
permissions = self._event_permissions.get(type, self._event_permissions["*"])
|
||||
permissions = [x(self._user) if callable(x) else x for x in permissions]
|
||||
if not self._user or not all(map(lambda p: self._user.has_permission(p), permissions)):
|
||||
return
|
||||
|
||||
processors = self._event_payload_processors.get(type, self._event_payload_processors["*"])
|
||||
for processor in processors:
|
||||
payload = processor(self._user, payload)
|
||||
|
||||
self._emit("event", payload=dict(type=type, payload=payload))
|
||||
|
||||
def sendTimelapseConfig(self, timelapseConfig):
|
||||
@@ -358,8 +374,7 @@ class PrinterStateConnection(octoprint.vendor.sockjs.tornado.SockJSConnection,
|
||||
# For now this is the easiest way though to at least inform the user that a timelapse is still ongoing.
|
||||
#
|
||||
# TODO remove when central job management becomes available and takes care of this for us
|
||||
self._emit("event", payload=dict(type=Events.MOVIE_RENDERING,
|
||||
payload=octoprint.timelapse.current_render_job))
|
||||
self.sendEvent(Events.MOVIE_RENDERING, payload=octoprint.timelapse.current_render_job)
|
||||
self._registered = True
|
||||
|
||||
def _unregister(self):
|
||||
@@ -393,6 +408,7 @@ class PrinterStateConnection(octoprint.vendor.sockjs.tornado.SockJSConnection,
|
||||
|
||||
if permissions is None:
|
||||
permissions = self._emit_permissions.get(type, self._emit_permissions["*"])
|
||||
permissions = [x() if callable(x) else x for x in permissions]
|
||||
|
||||
if not self._user or not all(map(lambda p: self._user.has_permission(p), permissions)):
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user