2024-11-15 02:35:14 +00:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import time
|
|
|
|
from typing import Optional
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
from open_webui.apps.webui.internal.db import Base, get_db
|
|
|
|
from open_webui.env import SRC_LOG_LEVELS
|
|
|
|
|
|
|
|
from open_webui.apps.webui.models.files import FileMetadataResponse
|
|
|
|
|
|
|
|
|
|
|
|
from pydantic import BaseModel, ConfigDict
|
2024-11-17 19:53:51 +00:00
|
|
|
from sqlalchemy import BigInteger, Column, String, Text, JSON, func
|
2024-11-15 02:35:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
log.setLevel(SRC_LOG_LEVELS["MODELS"])
|
|
|
|
|
|
|
|
####################
|
|
|
|
# UserGroup DB Schema
|
|
|
|
####################
|
|
|
|
|
|
|
|
|
|
|
|
class Group(Base):
|
|
|
|
__tablename__ = "group"
|
|
|
|
|
|
|
|
id = Column(Text, unique=True, primary_key=True)
|
|
|
|
user_id = Column(Text)
|
|
|
|
|
|
|
|
name = Column(Text)
|
|
|
|
description = Column(Text)
|
2024-11-16 22:56:00 +00:00
|
|
|
|
|
|
|
data = Column(JSON, nullable=True)
|
2024-11-15 02:35:14 +00:00
|
|
|
meta = Column(JSON, nullable=True)
|
|
|
|
|
|
|
|
permissions = Column(JSON, nullable=True)
|
|
|
|
user_ids = Column(JSON, nullable=True)
|
|
|
|
|
|
|
|
created_at = Column(BigInteger)
|
|
|
|
updated_at = Column(BigInteger)
|
|
|
|
|
|
|
|
|
|
|
|
class GroupModel(BaseModel):
|
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
id: str
|
|
|
|
user_id: str
|
|
|
|
|
|
|
|
name: str
|
|
|
|
description: str
|
2024-11-16 22:56:00 +00:00
|
|
|
|
|
|
|
data: Optional[dict] = None
|
2024-11-15 02:35:14 +00:00
|
|
|
meta: Optional[dict] = None
|
|
|
|
|
|
|
|
permissions: Optional[dict] = None
|
|
|
|
user_ids: list[str] = []
|
|
|
|
|
|
|
|
created_at: int # timestamp in epoch
|
|
|
|
updated_at: int # timestamp in epoch
|
|
|
|
|
|
|
|
|
|
|
|
####################
|
|
|
|
# Forms
|
|
|
|
####################
|
|
|
|
|
|
|
|
|
|
|
|
class GroupResponse(BaseModel):
|
|
|
|
id: str
|
|
|
|
user_id: str
|
|
|
|
name: str
|
|
|
|
description: str
|
|
|
|
permissions: Optional[dict] = None
|
2024-11-16 22:56:00 +00:00
|
|
|
data: Optional[dict] = None
|
2024-11-15 02:35:14 +00:00
|
|
|
meta: Optional[dict] = None
|
|
|
|
user_ids: list[str] = []
|
|
|
|
created_at: int # timestamp in epoch
|
|
|
|
updated_at: int # timestamp in epoch
|
|
|
|
|
|
|
|
|
|
|
|
class GroupForm(BaseModel):
|
|
|
|
name: str
|
|
|
|
description: str
|
|
|
|
|
|
|
|
|
|
|
|
class GroupUpdateForm(GroupForm):
|
|
|
|
permissions: Optional[dict] = None
|
|
|
|
user_ids: Optional[list[str]] = None
|
|
|
|
admin_ids: Optional[list[str]] = None
|
|
|
|
|
|
|
|
|
|
|
|
class GroupTable:
|
|
|
|
def insert_new_group(
|
|
|
|
self, user_id: str, form_data: GroupForm
|
|
|
|
) -> Optional[GroupModel]:
|
|
|
|
with get_db() as db:
|
|
|
|
group = GroupModel(
|
|
|
|
**{
|
|
|
|
**form_data.model_dump(),
|
|
|
|
"id": str(uuid.uuid4()),
|
|
|
|
"user_id": user_id,
|
|
|
|
"created_at": int(time.time()),
|
|
|
|
"updated_at": int(time.time()),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
2024-11-15 02:37:29 +00:00
|
|
|
result = Group(**group.model_dump())
|
2024-11-15 02:35:14 +00:00
|
|
|
db.add(result)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(result)
|
|
|
|
if result:
|
|
|
|
return GroupModel.model_validate(result)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_groups(self) -> list[GroupModel]:
|
|
|
|
with get_db() as db:
|
|
|
|
return [
|
|
|
|
GroupModel.model_validate(group)
|
2024-11-15 02:37:29 +00:00
|
|
|
for group in db.query(Group).order_by(Group.updated_at.desc()).all()
|
2024-11-15 02:35:14 +00:00
|
|
|
]
|
|
|
|
|
2024-11-15 09:29:07 +00:00
|
|
|
def get_groups_by_member_id(self, user_id: str) -> list[GroupModel]:
|
|
|
|
with get_db() as db:
|
|
|
|
return [
|
|
|
|
GroupModel.model_validate(group)
|
|
|
|
for group in db.query(Group)
|
2024-11-18 14:39:27 +00:00
|
|
|
.filter(
|
|
|
|
func.json_array_length(Group.user_ids) > 0
|
|
|
|
) # Ensure array exists
|
|
|
|
.filter(
|
|
|
|
Group.user_ids.cast(String).like(f'%"{user_id}"%')
|
|
|
|
) # String-based check
|
2024-11-15 09:29:07 +00:00
|
|
|
.order_by(Group.updated_at.desc())
|
|
|
|
.all()
|
|
|
|
]
|
|
|
|
|
2024-11-15 02:35:14 +00:00
|
|
|
def get_group_by_id(self, id: str) -> Optional[GroupModel]:
|
|
|
|
try:
|
|
|
|
with get_db() as db:
|
2024-11-15 02:37:29 +00:00
|
|
|
group = db.query(Group).filter_by(id=id).first()
|
2024-11-15 02:35:14 +00:00
|
|
|
return GroupModel.model_validate(group) if group else None
|
|
|
|
except Exception:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def update_group_by_id(
|
|
|
|
self, id: str, form_data: GroupUpdateForm, overwrite: bool = False
|
|
|
|
) -> Optional[GroupModel]:
|
|
|
|
try:
|
|
|
|
with get_db() as db:
|
2024-11-15 02:37:29 +00:00
|
|
|
db.query(Group).filter_by(id=id).update(
|
2024-11-15 02:35:14 +00:00
|
|
|
{
|
|
|
|
**form_data.model_dump(exclude_none=True),
|
|
|
|
"updated_at": int(time.time()),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
db.commit()
|
|
|
|
return self.get_group_by_id(id=id)
|
|
|
|
except Exception as e:
|
|
|
|
log.exception(e)
|
|
|
|
return None
|
|
|
|
|
|
|
|
def delete_group_by_id(self, id: str) -> bool:
|
|
|
|
try:
|
|
|
|
with get_db() as db:
|
2024-11-15 02:37:29 +00:00
|
|
|
db.query(Group).filter_by(id=id).delete()
|
2024-11-15 02:35:14 +00:00
|
|
|
db.commit()
|
|
|
|
return True
|
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def delete_all_groups(self) -> bool:
|
|
|
|
with get_db() as db:
|
|
|
|
try:
|
2024-11-15 02:37:29 +00:00
|
|
|
db.query(Group).delete()
|
2024-11-15 02:35:14 +00:00
|
|
|
db.commit()
|
|
|
|
|
|
|
|
return True
|
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
Groups = GroupTable()
|