mirror of
https://github.com/open-webui/open-webui.git
synced 2025-12-15 19:37:47 +01:00
wip: access control backend
This commit is contained in:
@@ -2,6 +2,8 @@ import time
|
||||
from typing import Optional
|
||||
|
||||
from open_webui.apps.webui.internal.db import Base, get_db
|
||||
from open_webui.apps.webui.models.groups import Groups
|
||||
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
from sqlalchemy import BigInteger, Column, String, Text, JSON
|
||||
|
||||
@@ -100,6 +102,64 @@ class PromptsTable:
|
||||
PromptModel.model_validate(prompt) for prompt in db.query(Prompt).all()
|
||||
]
|
||||
|
||||
def get_prompts_by_user_id(
|
||||
self, user_id: str, permission: str = "write"
|
||||
) -> list[PromptModel]:
|
||||
prompts = self.get_prompts()
|
||||
|
||||
groups = Groups.get_groups_by_member_id(user_id)
|
||||
group_ids = [group.id for group in groups]
|
||||
|
||||
if permission == "write":
|
||||
return [
|
||||
prompt
|
||||
for prompt in prompts
|
||||
if prompt.user_id == user_id
|
||||
or (
|
||||
prompt.access_control
|
||||
and (
|
||||
any(
|
||||
group_id
|
||||
in prompt.access_control.get(permission, {}).get(
|
||||
"group_ids", []
|
||||
)
|
||||
for group_id in group_ids
|
||||
)
|
||||
or (
|
||||
user_id
|
||||
in prompt.access_control.get(permission, {}).get(
|
||||
"user_ids", []
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
]
|
||||
elif permission == "read":
|
||||
return [
|
||||
prompt
|
||||
for prompt in prompts
|
||||
if prompt.user_id == user_id
|
||||
or prompt.access_control is None
|
||||
or (
|
||||
prompt.access_control
|
||||
and (
|
||||
any(
|
||||
prompt.access_control.get(permission, {}).get(
|
||||
"group_ids", []
|
||||
)
|
||||
in group_id
|
||||
for group_id in group_ids
|
||||
)
|
||||
or (
|
||||
user_id
|
||||
in prompt.access_control.get(permission, {}).get(
|
||||
"user_ids", []
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
]
|
||||
|
||||
def update_prompt_by_command(
|
||||
self, command: str, form_data: PromptForm
|
||||
) -> Optional[PromptModel]:
|
||||
|
||||
Reference in New Issue
Block a user