Commit 2e5a2d96 authored by Deimos's avatar Deimos

Switch user permissions to use an enum

Previously, there wasn't any defined list of which permissions were
valid or not. You basically had to look through each model's __acl__
method to see what the possibilities were.

Using an enum will be less convenient when adding new permissions or
changing existing ones (since it will require a database migration), but
it makes it much easier to see what the valid options are, and will
prevent invalid permissions from being set up in the database.
parent a4628343
"""Use enum for user permissions
Revision ID: 28d7ce2c4825
Revises: 82e9801eb2d6
Create Date: 2020-08-05 20:32:51.047215
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "28d7ce2c4825"
down_revision = "82e9801eb2d6"
branch_labels = None
depends_on = None
def upgrade():
create type userpermission as enum(
alter table user_permissions
alter column permission type userpermission using permission::userpermission
def downgrade():
op.execute("alter table user_permissions alter column permission type text")
op.execute("drop type userpermission")
......@@ -290,6 +290,28 @@ class HTMLSanitizationContext(enum.Enum):
# Enum for the possible user permissions
# (Using functional API for this one because the values aren't valid Python names)
UserPermission = enum.Enum(
class UserPermissionType(enum.Enum):
"""Enum for the types of user permissions."""
......@@ -3,11 +3,11 @@
"""Contains the UserPermissions class."""
from sqlalchemy import Column, ForeignKey, Integer, Text
from sqlalchemy import Column, ForeignKey, Integer
from sqlalchemy.dialects.postgresql import ENUM
from sqlalchemy.orm import relationship
from tildes.enums import UserPermissionType
from tildes.enums import UserPermission, UserPermissionType
from tildes.models import DatabaseModel
from import Group
from tildes.models.user import User
......@@ -21,7 +21,7 @@ class UserPermissions(DatabaseModel):
permission_id: int = Column(Integer, primary_key=True)
user_id: int = Column(Integer, ForeignKey("users.user_id"), nullable=False)
group_id: int = Column(Integer, ForeignKey("groups.group_id"), nullable=True)
permission: str = Column(Text, nullable=False)
permission: UserPermission = Column(ENUM(UserPermission), nullable=False)
permission_type: UserPermissionType = Column(
ENUM(UserPermissionType), nullable=False, server_default="ALLOW"
......@@ -50,6 +50,6 @@ class UserPermissions(DatabaseModel):
if self.permission_type == UserPermissionType.DENY:
principal += "!"
principal += self.permission
principal += str(
return principal
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment