Permissions redesign #2149

Merged
Tobotimus merged 23 commits from V3/redesign_permissions_model into V3/develop 2018-10-01 05:19:26 +02:00
Tobotimus commented 2018-09-25 15:47:14 +02:00 (Migrated from github.com)

This PR proposes a complete redesign of how the permissions cog works, how our default check decorators work, amongst some other things.

One major difference is how default permissions are described. An instance of the new Requires class in the redbot.core.commands.requires will be attached to every commands.Command object and every commands.Cog object at the requires attribute. This object describes the bot's and user's required permissions for the command, the privilege level required, the overrides for the command/cog, and any checks which should be checked after overrides. In my opinion this improves the current design of how there are managed since it is a clear representation of everything related to the command's required permissions, and provides a simple and efficient API for modifying these requirements during runtime.

How permissions rules are applied to Requires objects is another question. One of the main motivations of this redesign was to resolve #1931 in a way which is sane, reliable, and doesn't impact performance. The main focus was trying to delegate complex tasks to the application and/or removal of rules, rather than having them be resolved every time a command is called. This is why now, when a rule is explicitly allowed or denied, rules for supercommands will have rules applied as well. Overrides are now resolved much like a state machine; see the requires module for more info on how this works.

This does mean rules stored in the config of the Permissions cog need to be loaded into these objects either when the permissions cog is loaded, or the receiving command/cog is loaded (whichever comes first). Events, setup and teardown tasks have been added to facilitate this accordingly.

API changes:

  • Cogs must now inherit from commands.Cog (see #2151 for discussion and more details)
  • All functions which are not decorators in the redbot.core.checks module are now deprecated in favour of their counterparts in redbot.core.utils.mod. This is to make this module more consistent and end the confusing naming convention.
  • redbot.core.checks.check_overrides function is now gone, overrideable checks can now be created with the @commands.permissions_check decorator
  • Command, Group, Cog and Context have some new attributes and methods, but they are for internal use so shouldn't concern cog creators (unless they're making a permissions cog!).
  • __permissions_check_before and __permissions_check_after have been replaced: A cog method named __permissions_hook will be evaluated as permissions hooks in the same way __permissions_check_before previously was. Permissions hooks can also be added/removed/verified through the new *_permissions_hook() methods on the bot object, and they will be verified even when permissions is unloaded.
  • New utility method redbot.core.utils.chat_formatting.humanize_list
  • New dependency schema

User-facing changes:

  • When a @bot_has_permissions check fails, the bot will respond saying what permissions were actually missing.
  • All YAML-related [p]permissions subcommands now reside under the [p]permissions acl sub-group (tbh I still think the whole cog has too many top-level commands)
  • The YAML schema for these commands has been changed
  • A rule cannot be set as allow and deny at the same time (previously this would just default to allow)

Documentation:

  • New documentation for redbot.core.commands.requires and redbot.core.checks modules
  • Renewed documentation for the permissions cog
  • sphinx.ext.doctest is now enabled

Note: standard discord.py checks will still behave exactly the same way, in fact they are checked before Requires is looked at, so they are not overrideable.

This PR proposes a complete redesign of how the permissions cog works, how our default check decorators work, amongst some other things. One major difference is how default permissions are described. An instance of the new `Requires` class in the [`redbot.core.commands.requires`](https://github.com/Tobotimus/Red-DiscordBot/blob/V3/redesign_permissions_model/redbot/core/commands/requires.py) will be attached to every `commands.Command` object and every `commands.Cog` object at the `requires` attribute. This object describes the bot's and user's required permissions for the command, the privilege level required, the overrides for the command/cog, and any checks which should be checked *after* overrides. In my opinion this improves the current design of how there are managed since it is a clear representation of everything related to the command's required permissions, and provides a simple and efficient API for modifying these requirements during runtime. How permissions rules are applied to `Requires` objects is another question. One of the main motivations of this redesign was to resolve #1931 in a way which is sane, reliable, and doesn't impact performance. The main focus was trying to delegate complex tasks to the application and/or removal of rules, rather than having them be resolved every time a command is called. This is why now, when a rule is explicitly allowed or denied, rules for supercommands will have rules applied as well. Overrides are now resolved much like a state machine; see the `requires` module for more info on how this works. This does mean rules stored in the config of the Permissions cog need to be loaded into these objects either when the permissions cog is loaded, or the receiving command/cog is loaded (whichever comes first). Events, setup and teardown tasks have been added to facilitate this accordingly. API changes: - Cogs must now inherit from `commands.Cog` (see #2151 for discussion and more details) - All functions which are not decorators in the `redbot.core.checks` module are now deprecated in favour of their counterparts in `redbot.core.utils.mod`. This is to make this module more consistent and end the confusing naming convention. - `redbot.core.checks.check_overrides` function is now gone, overrideable checks can now be created with the `@commands.permissions_check` decorator - Command, Group, Cog and Context have some new attributes and methods, but they are for internal use so shouldn't concern cog creators (unless they're making a permissions cog!). - `__permissions_check_before` and `__permissions_check_after` have been replaced: A cog method named `__permissions_hook` will be evaluated as permissions hooks in the same way `__permissions_check_before` previously was. Permissions hooks can also be added/removed/verified through the new `*_permissions_hook()` methods on the bot object, and they will be verified even when permissions is unloaded. - New utility method `redbot.core.utils.chat_formatting.humanize_list` - New dependency [`schema`](https://github.com/keleshev/schema) User-facing changes: - When a `@bot_has_permissions` check fails, the bot will respond saying what permissions were actually missing. - All YAML-related `[p]permissions` subcommands now reside under the `[p]permissions acl` sub-group (tbh I still think the whole cog has too many top-level commands) - The YAML schema for these commands has been changed - A rule cannot be set as allow and deny at the same time (previously this would just default to allow) Documentation: - New documentation for `redbot.core.commands.requires` and `redbot.core.checks` modules - Renewed documentation for the permissions cog - `sphinx.ext.doctest` is now enabled Note: standard discord.py checks will still behave exactly the same way, in fact they are checked before `Requires` is looked at, so they are not overrideable.
palmtree5 (Migrated from github.com) reviewed 2018-09-25 15:47:14 +02:00
tekulvw (Migrated from github.com) reviewed 2018-09-25 15:47:14 +02:00
Twentysix26 (Migrated from github.com) reviewed 2018-09-25 15:47:14 +02:00
mikeshardmind (Migrated from github.com) reviewed 2018-09-26 18:47:19 +02:00
@ -317,3 +320,33 @@ def escape(text: str, *, mass_mentions: bool = False, formatting: bool = False)
if formatting:
mikeshardmind (Migrated from github.com) commented 2018-09-26 18:47:19 +02:00

This doctest looks like it isn't right....

humanize != humanized

This doctest looks like it isn't right.... `humanize` != `humanized`
Tobotimus commented 2018-09-27 08:01:35 +02:00 (Migrated from github.com)

This PR should now be ready for the first round of reviews! I've updated the header with the details. Sorry if I've missed some stuff.

This PR should now be ready for the first round of reviews! I've updated the header with the details. Sorry if I've missed some stuff.
mikeshardmind commented 2018-09-27 12:22:02 +02:00 (Migrated from github.com)

I'll note on the removal of __permissions_check_before and __permissions_check_after, that I don't think getting rid of them in entirety is a good thing. Getting rid of the after should be mostly fine, but the before did serve an important purpose.

Neither the previous iteration or permissions, nor this one can properly support a negative condition. (i.e. deny to anyone not is a specific group, without modifying the rule for that specific group to do it ). An example of where this can be problematic actually shows up in how the docs are being modified to hedge the actual intent behind locking audio, and how using this method will actually prevent a discord server owner (not bot owner) from further restricting those commands. These extra hooks allowed for quick extra rules to be added for people with such cases, without needing to design an entire permission model to make that possible.

I'll note on the removal of `__permissions_check_before` and `__permissions_check_after`, that I don't think getting rid of them in entirety is a good thing. Getting rid of the after should be mostly fine, but the before did serve an important purpose. Neither the previous iteration or permissions, nor this one can properly support a negative condition. (i.e. deny to anyone _not_ is a specific group, without modifying the rule for that specific group to do it ). An example of where this can be problematic actually shows up in how the docs are being modified to hedge the actual intent behind locking audio, and how using this method will actually prevent a discord server owner (not bot owner) from further restricting those commands. These extra hooks allowed for quick extra rules to be added for people with such cases, without needing to design an entire permission model to make that possible.
mikeshardmind (Migrated from github.com) requested changes 2018-09-30 10:01:19 +02:00
mikeshardmind (Migrated from github.com) left a comment

Setting via ACL appears to be working as intended. I've not found any major issues. I have found an inability to set rules via non-acl methods, (cause: converter, see comment).

This is a much more sustainable solution than before and seems to be performing well. With a fix for the converters that are problematic, this should be ready to go.

Setting via ACL appears to be working as intended. I've not found any major issues. I have found an inability to set rules via non-acl methods, (cause: converter, see comment). This is a much more sustainable solution than before and seems to be performing well. With a fix for the converters that are problematic, this should be ready to go.
@ -0,0 +54,4 @@
]
_T = TypeVar("_T")
GlobalPermissionModel = Union[
mikeshardmind (Migrated from github.com) commented 2018-09-30 09:50:05 +02:00

This is being used as a union converter in a couple places, which isn't safe. There's no such converter for guild, breaking every attempt to add a rule not via acl, and also the error from this is not user facing (traceback in console, but silent bot response).

I'd suggest adding a guild converter in our subclassed commands to accommodate this.

This is being used as a union converter in a couple places, which isn't safe. There's no such converter for guild, breaking every attempt to add a rule not via acl, and also the error from this is not user facing (traceback in console, but silent bot response). I'd suggest adding a guild converter in our subclassed commands to accommodate this.
mikeshardmind (Migrated from github.com) approved these changes 2018-10-01 01:49:11 +02:00
mikeshardmind (Migrated from github.com) left a comment

All changes needed have been made.

All changes needed have been made.
Sign in to join this conversation.
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Mrcoolgamer11/Red-DiscordBot!2149
No description provided.