def check_logged_in(cls) -> int: """Check if logged in""" if "sessionid" in cherrypy.request.cookie.keys(): sessionid = cherrypy.request.cookie["sessionid"].value if sessionid in cls.__temp_sessions: user: User = User.get_by_id(cls.__temp_sessions[sessionid]) is_admin = user.is_admin if is_admin: return cls.ADMIN return cls.USER return cls.GUEST
def change_password(cls, password: str, new_password: str) -> bool: """change the logged in users password""" if not password or not new_password: return False if "sessionid" not in cherrypy.request.cookie.keys(): return False session_id = cherrypy.request.cookie["sessionid"].value user: User = User.get_by_id(cls.__temp_sessions[session_id]) if user.password != cls.__password_encryption(password): return False user.password = cls.__password_encryption(new_password) user.save() return True
def update_user( cls, user_id: int, username: Union[str, None] = None, password: Union[str, None] = None, is_admin: bool = None, ) -> bool: """update the user info""" if cls.check_logged_in() != cls.ADMIN and cls.get_logged_in_userid( ) != user_id: return False user = User.get_by_id(user_id) if isinstance(username, str) and len(username) > 0: user.username = username if isinstance(password, str) and len(password) > 0: user.password = cls.__password_encryption(password) if isinstance(is_admin, bool): user.is_admin = is_admin return user.save() == 1