-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Roles #1789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Roles #1789
Conversation
Codacy mainly complains about missing docs for methods which are intended for internal use only. IMHO we can ignore that. |
Add roles to all handlers make roles name a property to ensure its up to date Add roles to PrefixHandler Make Roles inherit from dict Adjust tests for roles Adjust handlers __new__ to work with conversationhandler Remove roles from StringCommand/RegexHandler - doesnt make sense there Remove roles from TypeHandler - doesnt make sense there add handler tests
Remove child_roles from role
Rework equality stuff once again Improve equality tests for role
Fixes for chat_ids handling Adjust CH test for chat_ids
Minor changes
Adjust tests for py3.5 (dicts are not ordered there) Adjust tests for py3.5 V2 Just skip on py3.5 …
Make Codacy a bit happier
Add docs about cache timeout for Roles.CHAT_ADMINS
Use __repr__ instead of name property
Update BasePersistence docstring Refine handler tests
# Conflicts: # telegram/ext/dispatcher.py # tests/test_persistence.py
As suggested by @tsnoam I had a look at third party ACL and RBAC implementations. Though there are some libraries that seem well maintained, they mostly seem
My impression may well be based on lack of expertise, but to me it seems cleaner to have a custom implementation tailored to our needs than to rely on those third party implementations. |
just a thought: classic for a contrib repo, btw ;-) |
decorators? yeah, sounds about right
one would need to decorate every callback … We just made a point not to use decorators on callbacks with deprecating @run_async :D
|
@Bibo-Joshi I like your idea for a HandlerWrapper. That can be contrib as well. Bottom line: contrib. |
Closing to be revisited after #1912 is done. v13 changed so much, I can probably start from scratch anyway :D |
This PR adds the functionality of restricting access to handlers to
Builds upon #1757 (see below)
Closes #1151, #1562
Example usage:
How does it work
Added Classes
Role
Filters.user
(This is, where Allow updating user_ids/usernames of a running filters.user #1757 comes in. Can be changed to inherinting fromBaseFilter
if the reviewer want that)Filters
)my_role_1 > my_role_2
Roles
A container for multiple roles
dict
to allow accessing the roles byroles['role_name']
Roles.ADMINS
is parent to every roleRoles.CHAT_ADMINS
,Roles.CHAT_CREATOR
as shortcuts for restricting acces in group chatsIntegration with handlers
The base class
Handler
now has a__new__
method that makes sure, thatcheck_update
is called only, if the roles checked out. Alternatives approaches would beHandler.check_update
check the roles directly and callsuper().check_updater
in all the specific handlers. This means that custom handlers would have to change theircheck_update
implementation. Then again, they may have to be adjusted to accept theroles
argument anywayHandler.check_update_with_roles
that callesHandler.check_update
after checking the roles. In the current code base, replace all calls tocheck_update
withcheck_update_with_role
. This would leave current implementation ofcheck_update
untouched.Integration with dispatcher
Dispatcher.roles
is theRoles
instance managing the rolesDispatcher.roles
is available ascontext.roles
to allow editing roles in callbacksstore_roles
is added to persistence to allow keeping track of roles over restarts. I know that adding stuff to persistence should be done with care. But if we want dynamic access control (and I do), we need persistence here.ToDo
Add roles to new
PollAnswerHandler
. AsPoll
updates have neither user nor chat, we don't need roles withPollHandler
Cache chat admins and creators (timeout is customizable via
roles.CHAT_ADMINS.timeout = ...
)Handle private chats correctly with
roles.CHAT_ADMINS/CHAT_CREATOR
(this is as easy if checkingchat.id == user.id
)When merged, we should create a wiki page. Important points to mention there:
dp.roles.add_admin(id)
will always add the user to admins on start up with persistence, even if they are removed by dynamic access control. Thus, only the author of the bot should be hard coded as admin.roles.ADMINS
, else they might be excluded byroles=~roles['my_role']
, if they are inmy_role
dp.roles.add_role(name='my_group')
will lead toValueError
on restart with persistence because after restoring the data, the rolemy_group
will already be set. Use'my_group' in dp.roles
to check for that before adding the group.Depending on how long it will take to merge this:
all
in persistence (see Don't use builtin function names as variables #1766)tempfile
module in persistence tests (see Use tempfile module in tests #1765)