- Notifications
You must be signed in to change notification settings - Fork5.9k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Roles#1789
Uh oh!
There was an error while loading.Please reload this page.
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 handlersmake roles name a property to ensure its up to dateAdd roles to PrefixHandlerMake Roles inherit from dictAdjust tests for rolesAdjust handlers __new__ to work with conversationhandlerRemove roles from StringCommand/RegexHandler - doesnt make sense thereRemove roles from TypeHandler - doesnt make sense thereadd handler tests
Remove child_roles from role
Rework equality stuff once againImprove equality tests for role
Fixes for chat_ids handlingAdjust CH test for chat_ids
Minor changes
Adjust tests for py3.5 (dicts are not ordered there)Adjust tests for py3.5 V2Just 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 docstringRefine 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 |
Uh oh!
There was an error while loading.Please reload this page.
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, whereAllow updating user_ids/usernames of a running filters.user #1757 comes in. Can be changed to inherinting fromBaseFilterif the reviewer want that)Filters)my_role_1 > my_role_2Roles
A container for multiple roles
dictto allow accessing the roles byroles['role_name']Roles.ADMINSis parent to every roleRoles.CHAT_ADMINS,Roles.CHAT_CREATORas shortcuts for restricting acces in group chatsIntegration with handlers
The base class
Handlernow has a__new__method that makes sure, thatcheck_updateis called only, if the roles checked out. Alternatives approaches would beHandler.check_updatecheck the roles directly and callsuper().check_updaterin all the specific handlers. This means that custom handlers would have to change theircheck_updateimplementation. Then again, they may have to be adjusted to accept therolesargument anywayHandler.check_update_with_rolesthat callesHandler.check_updateafter checking the roles. In the current code base, replace all calls tocheck_updatewithcheck_update_with_role. This would leave current implementation ofcheck_updateuntouched.Integration with dispatcher
Dispatcher.rolesis theRolesinstance managing the rolesDispatcher.rolesis available ascontext.rolesto allow editing roles in callbacksstore_rolesis 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. AsPollupdates have neither user nor chat, we don't need roles withPollHandlerCache 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)willalways 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_roledp.roles.add_role(name='my_group')will lead toValueErroron restart with persistence because after restoring the data, the rolemy_groupwill already be set. Use'my_group' in dp.rolesto check for that before adding the group.Depending on how long it will take to merge this:
allin persistence (seeDon't use builtin function names as variables #1766)tempfilemodule in persistence tests (seeUse tempfile module in tests #1765)