Authorization system implemented
Check if user is authorized to perform an action and return an authorization denied message if not.
This commit is contained in:
parent
80c746df20
commit
97a29406ca
@ -32,6 +32,7 @@ class Bot(TelegramBot):
|
|||||||
final_state = 0
|
final_state = 0
|
||||||
_maintenance_message = ("I am currently under maintenance!\n"
|
_maintenance_message = ("I am currently under maintenance!\n"
|
||||||
"Please retry later...")
|
"Please retry later...")
|
||||||
|
_authorization_denied_message = None
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, token, hostname='', certificate=None, max_connections=40,
|
self, token, hostname='', certificate=None, max_connections=40,
|
||||||
@ -110,6 +111,10 @@ class Bot(TelegramBot):
|
|||||||
self._under_maintenance = False
|
self._under_maintenance = False
|
||||||
self._allowed_during_maintenance = []
|
self._allowed_during_maintenance = []
|
||||||
self._maintenance_message = None
|
self._maintenance_message = None
|
||||||
|
# Message to be returned if user is not allowed to call method
|
||||||
|
self._authorization_denied_message = None
|
||||||
|
# Default authorization function (always return True)
|
||||||
|
self.authorization_function = lambda update, authorization_level: True
|
||||||
return
|
return
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -221,6 +226,16 @@ class Bot(TelegramBot):
|
|||||||
return ("I am currently under maintenance!\n"
|
return ("I am currently under maintenance!\n"
|
||||||
"Please retry later...")
|
"Please retry later...")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def authorization_denied_message(self):
|
||||||
|
"""Return this text if user is unauthorized to make a request.
|
||||||
|
|
||||||
|
If instance message is not set, class message is returned.
|
||||||
|
"""
|
||||||
|
if self._authorization_denied_message:
|
||||||
|
return self._authorization_denied_message
|
||||||
|
return self.__class__._authorization_denied_message
|
||||||
|
|
||||||
async def message_router(self, update):
|
async def message_router(self, update):
|
||||||
"""Route Telegram `message` update to appropriate message handler."""
|
"""Route Telegram `message` update to appropriate message handler."""
|
||||||
for key, value in update.items():
|
for key, value in update.items():
|
||||||
@ -623,6 +638,31 @@ class Bot(TelegramBot):
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def set_class_authorization_denied_message(csl, message):
|
||||||
|
"""Set class authorization denied message.
|
||||||
|
|
||||||
|
It will be returned if user is unauthorized to make a request.
|
||||||
|
"""
|
||||||
|
csl._authorization_denied_message = message
|
||||||
|
|
||||||
|
def set_authorization_denied_message(self, message):
|
||||||
|
"""Set instance authorization denied message.
|
||||||
|
|
||||||
|
If instance message is None, default class message is used.
|
||||||
|
"""
|
||||||
|
self._authorization_denied_message = message
|
||||||
|
|
||||||
|
def set_authorization_function(self, authorization_function):
|
||||||
|
"""Set a custom authorization_function.
|
||||||
|
|
||||||
|
It should evaluate True if user is authorized to perform a specific
|
||||||
|
action and False otherwise.
|
||||||
|
It should take update and role and return a Boolean.
|
||||||
|
Default authorization_function always evaluates True.
|
||||||
|
"""
|
||||||
|
self.authorization_function = authorization_function
|
||||||
|
|
||||||
async def webhook_feeder(self, request):
|
async def webhook_feeder(self, request):
|
||||||
"""Handle incoming HTTP `request`s.
|
"""Handle incoming HTTP `request`s.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user