Skip to content

Commit

Permalink
Merge branch 'develop' into 'master'
Browse files Browse the repository at this point in the history
Create way to check for authorization

See merge request namibsun/python/kudubot!14
  • Loading branch information
namboy94 committed Sep 13, 2019
2 parents c87ba0c + f6ca536 commit 42f9fad
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
V 0.31.0:
- Added ways to make commands work only when authorized using decorators
V 0.30.0:
- Less unnecessary logging of exceptions
- Made version strings mandatory for bots
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ you want the bot to react.

Kudubot is meant to react on commands like ```/send 123```. Those are
modelled using CommandParser and Command objects. Those are used to
automatically parse incoming text messages and are sent to ```on_command```.
automatically parse incoming text messages and are sent to ```on_command```,
which delegates them to ```on_<command name>``` methods by default.

If you want complete manual control over what happens with incoming messages,
override the ```on_msg``` method. If you only care about text messages, do the
Expand Down
58 changes: 57 additions & 1 deletion kudubot/Bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import json
import logging
import traceback
from functools import wraps
from threading import Thread
from sentry_sdk import capture_exception
from sqlalchemy import create_engine
Expand All @@ -38,14 +39,42 @@
from kudubot.db.config.impl.SqlteConfig import SqliteConfig
from kudubot.exceptions import ConfigurationError, ParseError
from kudubot.parsing.CommandParser import CommandParser
from typing import Type, Optional, List, Tuple, Dict, Any, cast
from typing import Type, Optional, List, Tuple, Dict, Any, cast, Callable


class Bot:
"""
The Bot class offers an abstraction layer above bokkichat
"""

# noinspection PyMethodParameters
def auth_required(func: Callable) -> Callable:
"""
This is a decorator that makes it possible to restrict a user's access
to certain commands.
To use this, simply decorate an 'on_'-method with this decorator.
The method will then only be called if the is_authorized() method
returns True
:return: None
"""
@wraps(func)
def wrapper(
self: Bot,
sender: Address,
args: Dict[str, Any],
db_session: Session
):
status = self.is_authorized(sender, args, db_session)
if status: # TODO FIX
self.send_txt(
sender,
self.unauthorized_message(),
"Unauthorized"
)
else:
func(self, sender, args, db_session)
return wrapper

def __init__(
self,
connection: Connection,
Expand Down Expand Up @@ -289,14 +318,41 @@ def bg_pause(self) -> int:
"""
return 60

# noinspection PyMethodMayBeStatic,PyUnusedLocal
def is_authorized(
self,
address: Address,
args: Dict[str, Any],
db_session: Session
) -> bool:
"""
Checks if a user is authorized
:param address: The user to check
:param args: possible command arguments
:param db_session: The database session to use
:return: True if authorized, False otherwise
"""
return True

@classmethod
def unauthorized_message(cls) -> str:
"""
:return: A custom message sent to a user if they tried to access
a feature that requires authorization without being
authorized
"""
return "Unauthorized"

def run_in_bg(self):
"""
Method that is started when the bot is started.
This executes the bg_iteration method every bg_pause seconds
:return: None
"""
self.logger.info("Starting background thread")
counter = 0
while True:
self.logger.debug("Starting background iteration " + str(counter))
try:
db_session = self.sessionmaker()
self.bg_iteration(counter, db_session)
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.30.0
0.31.0

0 comments on commit 42f9fad

Please sign in to comment.