@@ -23,6 +23,16 @@ class Base(DeclarativeBase):
2323
2424# ==================== ENUMS ====================
2525
26+ # Enum to indicate permission target type (user, department, or all)
27+ class PermissionReceiverType (enum .Enum ):
28+ USER = "user"
29+ DEPARTMENT = "department"
30+ ALL = "all"
31+
32+ class PermissionType (enum .Enum ):
33+ VIEW = "view"
34+ EDIT = "edit"
35+
2636class UserStatus (enum .Enum ):
2737 """User account status"""
2838 ACTIVE = "ACTIVE"
@@ -42,19 +52,8 @@ class PermissionAction(enum.Enum):
4252 """Available permission actions"""
4353 VIEW = "VIEW"
4454 EDIT = "EDIT"
45- DELETE = "DELETE"
46- SHARE = "SHARE"
4755 MANAGE = "MANAGE" # Includes permission management
4856
49-
50- class FileVisibility (enum .Enum ):
51- """File visibility levels"""
52- PRIVATE = "PRIVATE" # Only owner
53- SHARED = "SHARED" # Explicitly shared users/departments
54- DEPARTMENT = "DEPARTMENT" # All department members
55- PUBLIC = "PUBLIC" # Everyone (including guests)
56-
57-
5857class AuditAction (enum .Enum ):
5958 """Audit log action types"""
6059 FILE_UPLOAD = "FILE_UPLOAD"
@@ -103,7 +102,8 @@ class Department(Base):
103102 back_populates = "parent_department" , cascade = "all, delete-orphan"
104103 )
105104 members : Mapped [List ["User" ]] = relationship (back_populates = "department" )
106- file_permissions : Mapped [List ["FileDepartmentPermission" ]] = relationship (
105+ # Files shared with this department, could be either view or edit permission
106+ file_permissions : Mapped [List ["FilePermission" ]] = relationship (
107107 back_populates = "department"
108108 )
109109
@@ -145,7 +145,8 @@ class User(Base):
145145 role : Mapped [Optional ["Role" ]] = relationship (back_populates = "users" )
146146 files : Mapped [List ["FileMetadata" ]] = relationship (back_populates = "owner" )
147147 chat_sessions : Mapped [List ["ChatSession" ]] = relationship (back_populates = "user" )
148- file_permissions : Mapped [List ["FileUserPermission" ]] = relationship (
148+ # Files shared with this user, could be either view or edit permission
149+ file_permissions : Mapped [List ["FilePermission" ]] = relationship (
149150 back_populates = "user"
150151 )
151152 audit_logs : Mapped [List ["AuditLog" ]] = relationship (back_populates = "user" )
@@ -270,11 +271,6 @@ class FileMetadata(Base):
270271 blob_key : Mapped [str ] = mapped_column (String (500 ), nullable = False )
271272 filename : Mapped [str ] = mapped_column (String (255 ), nullable = False )
272273
273- # Visibility and access control
274- visibility : Mapped [FileVisibility ] = mapped_column (
275- SQLEnum (FileVisibility ), default = FileVisibility .PRIVATE , nullable = False
276- )
277-
278274 # Processing status
279275 status : Mapped [FileStatus ] = mapped_column (SQLEnum (FileStatus ), nullable = False )
280276
@@ -291,86 +287,56 @@ class FileMetadata(Base):
291287 parsed_contents : Mapped [List ["ParsedContentMetadata" ]] = relationship (
292288 back_populates = "source_file" , cascade = "all, delete-orphan"
293289 )
294- user_permissions : Mapped [List ["FileUserPermission" ]] = relationship (
295- back_populates = "file" , cascade = "all, delete-orphan"
296- )
297- department_permissions : Mapped [List ["FileDepartmentPermission" ]] = relationship (
290+ permissions : Mapped [List ["FilePermission" ]] = relationship (
298291 back_populates = "file" , cascade = "all, delete-orphan"
299292 )
300293 audit_logs : Mapped [List ["AuditLog" ]] = relationship (back_populates = "file" )
301294
302295
303- class FileUserPermission (Base ):
296+ class FilePermission (Base ):
304297 """
305- Explicit file permissions for individual users.
306- Used when files are shared with specific users.
298+ Explicit file permissions for users/departments/all .
299+ Used when files are shared with specific users/departments/all .
307300 """
308- __tablename__ = 'file_user_permission '
301+ __tablename__ = 'file_permission '
309302
310303 id : Mapped [uuid .UUID ] = mapped_column (
311304 UUID (as_uuid = True ), primary_key = True , default = uuid .uuid4
312305 )
313306 file_id : Mapped [str ] = mapped_column (
314307 String (255 ), ForeignKey ("file_metadata.file_id" ), nullable = False , index = True
315308 )
316- user_id : Mapped [uuid .UUID ] = mapped_column (
317- UUID (as_uuid = True ), ForeignKey ("user.id" ), nullable = False , index = True
318- )
319-
320- # Permissions
321- can_view : Mapped [bool ] = mapped_column (Boolean , default = True , nullable = False )
322- can_edit : Mapped [bool ] = mapped_column (Boolean , default = False , nullable = False )
323- can_delete : Mapped [bool ] = mapped_column (Boolean , default = False , nullable = False )
324- can_share : Mapped [bool ] = mapped_column (Boolean , default = False , nullable = False )
325-
326- # Grant information
327- granted_by : Mapped [uuid .UUID ] = mapped_column (
328- UUID (as_uuid = True ), ForeignKey ("user.id" ), nullable = False
329- )
330- granted_at : Mapped [datetime ] = mapped_column (DateTime , default = datetime .now , nullable = False )
331- expires_at : Mapped [Optional [datetime ]] = mapped_column (DateTime )
332309
333- # Relationships
334- file : Mapped ["FileMetadata" ] = relationship (back_populates = "user_permissions" )
335- user : Mapped ["User" ] = relationship (
336- foreign_keys = [user_id ], back_populates = "file_permissions"
310+ permission_receiver_type : Mapped ["PermissionReceiverType" ] = mapped_column (
311+ SQLEnum (PermissionReceiverType ), nullable = False , default = PermissionReceiverType .USER
337312 )
338313
339-
340- class FileDepartmentPermission (Base ):
341- """
342- File permissions for entire departments.
343- Enables department-wide sharing.
344- """
345- __tablename__ = 'file_department_permission'
346-
347- id : Mapped [uuid .UUID ] = mapped_column (
348- UUID (as_uuid = True ), primary_key = True , default = uuid .uuid4
349- )
350- file_id : Mapped [str ] = mapped_column (
351- String (255 ), ForeignKey ("file_metadata.file_id" ), nullable = False , index = True
314+ user_id : Mapped [uuid .UUID ] = mapped_column (
315+ UUID (as_uuid = True ), ForeignKey ("user.id" ), nullable = False , index = True
352316 )
353317 department_id : Mapped [uuid .UUID ] = mapped_column (
354318 UUID (as_uuid = True ), ForeignKey ("department.id" ), nullable = False , index = True
355319 )
356320
357- # Permissions
358- can_view : Mapped [bool ] = mapped_column (Boolean , default = True , nullable = False )
359- can_edit : Mapped [bool ] = mapped_column (Boolean , default = False , nullable = False )
360- can_delete : Mapped [bool ] = mapped_column (Boolean , default = False , nullable = False )
361- can_share : Mapped [bool ] = mapped_column (Boolean , default = False , nullable = False )
321+ # Permission type (view, edit)
322+ permission_type : Mapped ["PermissionType" ] = mapped_column (
323+ SQLEnum (PermissionType ), nullable = False , default = PermissionType .VIEW
324+ )
362325
363326 # Grant information
364327 granted_by : Mapped [uuid .UUID ] = mapped_column (
365328 UUID (as_uuid = True ), ForeignKey ("user.id" ), nullable = False
366329 )
367330 granted_at : Mapped [datetime ] = mapped_column (DateTime , default = datetime .now , nullable = False )
331+ expires_at : Mapped [Optional [datetime ]] = mapped_column (DateTime )
368332
369333 # Relationships
370- file : Mapped ["FileMetadata" ] = relationship (back_populates = "department_permissions" )
334+ file : Mapped ["FileMetadata" ] = relationship (back_populates = "permissions" )
335+ user : Mapped ["User" ] = relationship (
336+ foreign_keys = [user_id ], back_populates = "file_permissions"
337+ )
371338 department : Mapped ["Department" ] = relationship (back_populates = "file_permissions" )
372339
373-
374340# ==================== AUDIT LOG ====================
375341
376342class AuditLog (Base ):
0 commit comments