Skip to content

Roles and Permissions - #1018

Merged
SchrodingersGat merged 16 commits into
inventree:masterfrom
SchrodingersGat:group-roles
Oct 5, 2020
Merged

SchrodingersGat merged 16 commits into
inventree:masterfrom
SchrodingersGat:group-roles

Conversation

@SchrodingersGat

@SchrodingersGat SchrodingersGat commented Oct 3, 2020 •

Copy link
Copy Markdown
Member

Closes #1018 (comment)

This PR augments the great work done by @eeintech in #1016

The default django permission management system is pretty complex to use, and while the fine-grained permissions control can be useful, we really want a "role" or "ruleset" approach.

This PR adds the RuleSet concept which maps to a particular group. The following table is provided in the admin page for each group:

image

Each row covers a particular set of database models, and each column determines the permission type for that ruleset.

Future work will be required to actually make use of the per-model permissions

  • Add particular permission requirements to the DRF API
  • Add particular permission requirements to various web pages
  • Add particular permission requirements to the modal forms

(All this is pretty straight forward really)

- ALL models must be covered by rulesets
- Added a RULESET_IGNORE list for models we do not want permissions for
- Only runs when the group is changed
- Does not add permissions if they already exist
- Does not remove permissions if they do not exist
- Does not actually *do* anything any more as the RuleSet approach overrides it anyway
@SchrodingersGat SchrodingersGat added the enhancement This is an suggested enhancement or new feature label Oct 4, 2020
@SchrodingersGat SchrodingersGat changed the title DO NOT MERGE Roles and Permissions Oct 4, 2020
@SchrodingersGat

Copy link
Copy Markdown
Member Author

@eeintech please let me know what you think of this implementation. I think it's pretty clean, but I might have missed something! 👍

@eeintech eeintech left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far what I see looks nice, however I don't see the option to dive down to the Model level for managing permissions inside the Admin interface, would be handy to still have the choice to do so, no?

Comment thread InvenTree/users/models.py Outdated
],
'purchase_order': [
'company_company',
'part_supplierpart',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SupplierPart and SupplierPartBreak are models of the Company app, not the Part app.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They were originally in the Part app, so the database tables retain the old names:

image

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the unit testing has been setup to detect any of the following cases

  • A ruleset references a permission that does not exist
  • An existing permission is not referenced by any ruleset

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SchrodingersGat I was not able to create a new Group after pulling your branch, I hit the ContentType.DoesNotExist error...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, it does not actually use the table name, it does use the app name.

So company_supplierpart and company_supplierpricebreak are correct here.

I'm just writing some better unit testing to check for this. I'll push it momentarily

Comment thread InvenTree/users/models.py Outdated

(permission_name, model) = perm.split('_')

content_type = ContentType.objects.get(app_label=app, model=model)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid Django to error for any mislabeled app and model (human mistakes can happen), I suggest replacing with:

        try:
            content_type = ContentType.objects.get(app_label=app, model=model)
            permission = Permission.objects.get(content_type=content_type, codename=perm)
        except ContentType.DoesNotExist:
            permission = None

group.permissions.add(permission) does not error out if None object is passed.

Better yet would be to report this to the user in a "nice" way.

@SchrodingersGat

Copy link
Copy Markdown
Member Author

So far what I see looks nice, however I don't see the option to dive down to the Model level for managing permissions inside the Admin interface, would be handy to still have the choice to do so, no?

Originally I had thought this was a nice idea, but there is an issue. The new RuleSet approach is pretty "heavy handed" with the permission control - every time you save a group, the permisions all get updated on the group. I couldn't think of a clean way to still allow fine-grained control of the permissions (and this is the antithesis of what we are trying to achieve by simplifying the permissions in the first place).

I think that the new ruleset approach is sufficient. Permissions can also be set individually for users if need be.

What do you think?

@eeintech

eeintech commented Oct 4, 2020

Copy link
Copy Markdown
Contributor

Originally I had thought this was a nice idea, but there is an issue. The new RuleSet approach is pretty "heavy handed" with the permission control - every time you save a group, the permisions all get updated on the group. I couldn't think of a clean way to still allow fine-grained control of the permissions (and this is the antithesis of what we are trying to achieve by simplifying the permissions in the first place).

I think that the new ruleset approach is sufficient. Permissions can also be set individually for users if need be.

What do you think?

Umm I see, this would require a way to log individual users overwrites on each permission and add extra logic and complexity... I guess you are right, better abstract this layer completely and move forward with the rulesets.

- Uses the app_model name, *NOT* the name of the database table
- Adds extra tests to ensure that permissions get assigned and removed correctly
@SchrodingersGat

Copy link
Copy Markdown
Member Author

Umm I see, this would require a way to log individual users overwrites on each permission and add extra logic and complexity... I guess you are right, better abstract this layer completely and move forward with the rulesets.

It's a good start - if in the future we find this approach is not sufficient for some nominal use case, we can always re-address it then...

@SchrodingersGat

Copy link
Copy Markdown
Member Author

@eeintech please pull down the latest commit and see if it works for you now

Comment thread InvenTree/users/models.py
model=model
)

def __str__(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace with something like?

    def __str__(self):
        """ Ruleset string representation """

        return f'{self.name} | v: {self.can_view} | a: {self.can_add} | ' \
               f'c: {self.can_change} | d: {self.can_delete}'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gasp, this actually is captured in the admin form... Can put this on backburner / for debug only

@eeintech

eeintech commented Oct 4, 2020 •

Copy link
Copy Markdown
Contributor

@SchrodingersGat I noticed that update_group_roles is called 3 times when RoleGroupAdmin is saved... Not quite sure why 🤔

It probably was the case with #1016 too...

I was testing your latest changes (with part_partsupplierbreak instead of company) and this is the output when printing the RuleSet (custom) string value and added/deleted permissions:

admin | v: False | a: False | c: False | d: False
part | v: False | a: False | c: False | d: False
stock | v: False | a: False | c: False | d: False
build | v: False | a: False | c: False | d: False
purchase_order | v: False | a: True | c: False | d: False
sales_order | v: False | a: False | c: False | d: False
Adding permission part.add_supplierpricebreak
Error: Could not find permission matching 'part.add_supplierpricebreak'
Removing permission company.add_company
admin | v: False | a: False | c: False | d: False
part | v: False | a: False | c: False | d: False
stock | v: False | a: False | c: False | d: False
build | v: False | a: False | c: False | d: False
purchase_order | v: False | a: True | c: False | d: False
sales_order | v: False | a: False | c: False | d: False
Adding permission company.add_company
Adding permission part.add_supplierpricebreak
Error: Could not find permission matching 'part.add_supplierpricebreak'
admin | v: False | a: False | c: False | d: False
part | v: False | a: False | c: False | d: False
stock | v: False | a: False | c: False | d: False
build | v: False | a: False | c: False | d: False
purchase_order | v: False | a: True | c: False | d: False
sales_order | v: False | a: False | c: False | d: False
Adding permission part.add_supplierpricebreak
Error: Could not find permission matching 'part.add_supplierpricebreak'
Removing permission company.add_company

@SchrodingersGat

Copy link
Copy Markdown
Member Author

Does it work better now that part_supplierpricebreak has been changed to company_supplierpricebreak?

@eeintech

eeintech commented Oct 4, 2020

Copy link
Copy Markdown
Contributor

Does it work better now that part_supplierpricebreak has been changed to company_supplierpricebreak?

Yes it does 👍

@SchrodingersGat
SchrodingersGat merged commit dc41231 into inventree:master Oct 5, 2020
@SchrodingersGat

Copy link
Copy Markdown
Member Author

Great! Now we'll need to add some documentation about this great new feature :)

This was referenced Oct 5, 2020
@SchrodingersGat
SchrodingersGat deleted the group-roles branch February 11, 2021 01:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement This is an suggested enhancement or new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants