Can you use an enum for permissions?

19 views
Skip to first unread message

Sean Hammond

unread,
Jul 15, 2021, 9:23:14 AM7/15/21
to pylons-...@googlegroups.com
Do permissions in Pyramid always have to be strings, or can you use an enum?

Currently we use this enum-like class to store all our permission strings as namespaced constants:

class Permission:
class User:
READ = "user:read"
CREATE = "user:create"
UPDATE = "user:update"

class Admin:
FOO = "admin:foo"

This is just as an alternative to always duplicating the literal strings everywhere. It works nicely with autocomplete, allows PyLint to catch typos, etc.

It *seems* to work fine if we use enums:

from enum import Enum

class Permission:
class User(Enum):
READ = "user:read"
CREATE = "user:create"
UPDATE = "user:update"

class Admin(Enum):
FOO = "admin:foo"

The reason is just so that we can get some of the functionality of Enum, like being able to iterate over them.

From what I can tell from the Pyramid docs permissions are usually strings, but I don't see any concrete reason why arbitrary values can't be used for permissions. Built-in permissions like security.Authenticated are strings but I don't think that will cause any problems if our own permissions are enums.

This seems to be fine?
Reply all
Reply to author
Forward
0 new messages