refac
This commit is contained in:
1 parent
3d99de6771
commit
176f9a7816
7 files changed
+125
-261
No files matched your search
@@ -29,8 +29,8 @@ from open_webui.storage.provider import Storage
|
|||||||
|
|
||||||
from open_webui.constants import ERROR_MESSAGES
|
from open_webui.constants import ERROR_MESSAGES
|
||||||
from open_webui.utils.auth import get_verified_user, get_admin_user
|
from open_webui.utils.auth import get_verified_user, get_admin_user
|
||||||
from open_webui.utils.access_control import has_permission
|
from open_webui.utils.access_control import has_permission, filter_allowed_access_grants
|
||||||
from open_webui.models.access_grants import AccessGrants, has_public_read_access_grant, has_user_access_grant, strip_user_access_grants
|
from open_webui.models.access_grants import AccessGrants
|
||||||
|
|
||||||
|
|
||||||
from open_webui.config import BYPASS_ADMIN_ACCESS_CONTROL
|
from open_webui.config import BYPASS_ADMIN_ACCESS_CONTROL
|
||||||
@@ -251,7 +251,7 @@ async def create_new_knowledge(
|
|||||||
user=Depends(get_verified_user),
|
user=Depends(get_verified_user),
|
||||||
):
|
):
|
||||||
# NOTE: We intentionally do NOT use Depends(get_session) here.
|
# NOTE: We intentionally do NOT use Depends(get_session) here.
|
||||||
# Database operations (has_permission, insert_new_knowledge) manage their own sessions.
|
# Database operations (has_permission, filter_allowed_access_grants, insert_new_knowledge) manage their own sessions.
|
||||||
# This prevents holding a connection during embed_knowledge_base_metadata()
|
# This prevents holding a connection during embed_knowledge_base_metadata()
|
||||||
# which makes external embedding API calls (1-5+ seconds).
|
# which makes external embedding API calls (1-5+ seconds).
|
||||||
if user.role != "admin" and not has_permission(
|
if user.role != "admin" and not has_permission(
|
||||||
@@ -262,29 +262,13 @@ async def create_new_knowledge(
|
|||||||
detail=ERROR_MESSAGES.UNAUTHORIZED,
|
detail=ERROR_MESSAGES.UNAUTHORIZED,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Check if user can share publicly
|
form_data.access_grants = filter_allowed_access_grants(
|
||||||
if (
|
request.app.state.config.USER_PERMISSIONS,
|
||||||
user.role != "admin"
|
user.id,
|
||||||
and has_public_read_access_grant(form_data.access_grants)
|
user.role,
|
||||||
and not has_permission(
|
form_data.access_grants,
|
||||||
user.id,
|
"sharing.public_knowledge",
|
||||||
"sharing.public_knowledge",
|
)
|
||||||
request.app.state.config.USER_PERMISSIONS,
|
|
||||||
)
|
|
||||||
):
|
|
||||||
form_data.access_grants = []
|
|
||||||
|
|
||||||
# Strip individual user sharing if user lacks permission
|
|
||||||
if (
|
|
||||||
user.role != "admin"
|
|
||||||
and has_user_access_grant(form_data.access_grants)
|
|
||||||
and not has_permission(
|
|
||||||
user.id,
|
|
||||||
"access_grants.allow_users",
|
|
||||||
request.app.state.config.USER_PERMISSIONS,
|
|
||||||
)
|
|
||||||
):
|
|
||||||
form_data.access_grants = strip_user_access_grants(form_data.access_grants)
|
|
||||||
|
|
||||||
knowledge = Knowledges.insert_new_knowledge(user.id, form_data)
|
knowledge = Knowledges.insert_new_knowledge(user.id, form_data)
|
||||||
|
|
||||||
@@ -494,29 +478,13 @@ async def update_knowledge_by_id(
|
|||||||
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Check if user can share publicly
|
form_data.access_grants = filter_allowed_access_grants(
|
||||||
if (
|
request.app.state.config.USER_PERMISSIONS,
|
||||||
user.role != "admin"
|
user.id,
|
||||||
and has_public_read_access_grant(form_data.access_grants)
|
user.role,
|
||||||
and not has_permission(
|
form_data.access_grants,
|
||||||
user.id,
|
"sharing.public_knowledge",
|
||||||
"sharing.public_knowledge",
|
)
|
||||||
request.app.state.config.USER_PERMISSIONS,
|
|
||||||
)
|
|
||||||
):
|
|
||||||
form_data.access_grants = []
|
|
||||||
|
|
||||||
# Strip individual user sharing if user lacks permission
|
|
||||||
if (
|
|
||||||
user.role != "admin"
|
|
||||||
and has_user_access_grant(form_data.access_grants)
|
|
||||||
and not has_permission(
|
|
||||||
user.id,
|
|
||||||
"access_grants.allow_users",
|
|
||||||
request.app.state.config.USER_PERMISSIONS,
|
|
||||||
)
|
|
||||||
):
|
|
||||||
form_data.access_grants = strip_user_access_grants(form_data.access_grants)
|
|
||||||
|
|
||||||
knowledge = Knowledges.update_knowledge_by_id(id=id, form_data=form_data)
|
knowledge = Knowledges.update_knowledge_by_id(id=id, form_data=form_data)
|
||||||
if knowledge:
|
if knowledge:
|
||||||
@@ -578,36 +546,13 @@ async def update_knowledge_access_by_id(
|
|||||||
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Strip public sharing if user lacks permission
|
form_data.access_grants = filter_allowed_access_grants(
|
||||||
if (
|
request.app.state.config.USER_PERMISSIONS,
|
||||||
user.role != "admin"
|
user.id,
|
||||||
and has_public_read_access_grant(form_data.access_grants)
|
user.role,
|
||||||
and not has_permission(
|
form_data.access_grants,
|
||||||
user.id,
|
"sharing.public_knowledge"
|
||||||
"sharing.public_knowledge",
|
)
|
||||||
request.app.state.config.USER_PERMISSIONS,
|
|
||||||
)
|
|
||||||
):
|
|
||||||
form_data.access_grants = [
|
|
||||||
grant
|
|
||||||
for grant in form_data.access_grants
|
|
||||||
if not (
|
|
||||||
grant.get("principal_type") == "user"
|
|
||||||
and grant.get("principal_id") == "*"
|
|
||||||
)
|
|
||||||
]
|
|
||||||
|
|
||||||
# Strip individual user sharing if user lacks permission
|
|
||||||
if (
|
|
||||||
user.role != "admin"
|
|
||||||
and has_user_access_grant(form_data.access_grants)
|
|
||||||
and not has_permission(
|
|
||||||
user.id,
|
|
||||||
"access_grants.allow_users",
|
|
||||||
request.app.state.config.USER_PERMISSIONS,
|
|
||||||
)
|
|
||||||
):
|
|
||||||
form_data.access_grants = strip_user_access_grants(form_data.access_grants)
|
|
||||||
|
|
||||||
AccessGrants.set_access_grants("knowledge", id, form_data.access_grants, db=db)
|
AccessGrants.set_access_grants("knowledge", id, form_data.access_grants, db=db)
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ from open_webui.models.models import (
|
|||||||
ModelAccessResponse,
|
ModelAccessResponse,
|
||||||
Models,
|
Models,
|
||||||
)
|
)
|
||||||
from open_webui.models.access_grants import AccessGrants, has_public_read_access_grant, has_user_access_grant, strip_user_access_grants
|
from open_webui.models.access_grants import AccessGrants
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from open_webui.constants import ERROR_MESSAGES
|
from open_webui.constants import ERROR_MESSAGES
|
||||||
@@ -33,7 +33,7 @@ from fastapi.responses import FileResponse, StreamingResponse
|
|||||||
|
|
||||||
|
|
||||||
from open_webui.utils.auth import get_admin_user, get_verified_user
|
from open_webui.utils.auth import get_admin_user, get_verified_user
|
||||||
from open_webui.utils.access_control import has_permission
|
from open_webui.utils.access_control import has_permission, filter_allowed_access_grants
|
||||||
from open_webui.config import BYPASS_ADMIN_ACCESS_CONTROL, STATIC_DIR
|
from open_webui.config import BYPASS_ADMIN_ACCESS_CONTROL, STATIC_DIR
|
||||||
from open_webui.internal.db import get_session
|
from open_webui.internal.db import get_session
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
@@ -565,36 +565,13 @@ async def update_model_access_by_id(
|
|||||||
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Strip public sharing if user lacks permission
|
form_data.access_grants = filter_allowed_access_grants(
|
||||||
if (
|
request.app.state.config.USER_PERMISSIONS,
|
||||||
user.role != "admin"
|
user.id,
|
||||||
and has_public_read_access_grant(form_data.access_grants)
|
user.role,
|
||||||
and not has_permission(
|
form_data.access_grants,
|
||||||
user.id,
|
"sharing.public_models"
|
||||||
"sharing.public_models",
|
)
|
||||||
request.app.state.config.USER_PERMISSIONS,
|
|
||||||
)
|
|
||||||
):
|
|
||||||
form_data.access_grants = [
|
|
||||||
grant
|
|
||||||
for grant in form_data.access_grants
|
|
||||||
if not (
|
|
||||||
grant.get("principal_type") == "user"
|
|
||||||
and grant.get("principal_id") == "*"
|
|
||||||
)
|
|
||||||
]
|
|
||||||
|
|
||||||
# Strip individual user sharing if user lacks permission
|
|
||||||
if (
|
|
||||||
user.role != "admin"
|
|
||||||
and has_user_access_grant(form_data.access_grants)
|
|
||||||
and not has_permission(
|
|
||||||
user.id,
|
|
||||||
"access_grants.allow_users",
|
|
||||||
request.app.state.config.USER_PERMISSIONS,
|
|
||||||
)
|
|
||||||
):
|
|
||||||
form_data.access_grants = strip_user_access_grants(form_data.access_grants)
|
|
||||||
|
|
||||||
AccessGrants.set_access_grants(
|
AccessGrants.set_access_grants(
|
||||||
"model", form_data.id, form_data.access_grants, db=db
|
"model", form_data.id, form_data.access_grants, db=db
|
||||||
|
|||||||
@@ -27,8 +27,8 @@ from open_webui.constants import ERROR_MESSAGES
|
|||||||
|
|
||||||
|
|
||||||
from open_webui.utils.auth import get_admin_user, get_verified_user
|
from open_webui.utils.auth import get_admin_user, get_verified_user
|
||||||
from open_webui.utils.access_control import has_permission
|
from open_webui.utils.access_control import has_permission, filter_allowed_access_grants
|
||||||
from open_webui.models.access_grants import AccessGrants, has_public_read_access_grant, has_user_access_grant, strip_user_access_grants
|
from open_webui.models.access_grants import AccessGrants
|
||||||
from open_webui.internal.db import get_session
|
from open_webui.internal.db import get_session
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
@@ -283,30 +283,14 @@ async def update_note_by_id(
|
|||||||
status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
|
status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
|
||||||
)
|
)
|
||||||
|
|
||||||
# Check if user can share publicly
|
form_data.access_grants = filter_allowed_access_grants(
|
||||||
if (
|
request.app.state.config.USER_PERMISSIONS,
|
||||||
user.role != "admin"
|
user.id,
|
||||||
and has_public_read_access_grant(form_data.access_grants)
|
user.role,
|
||||||
and not has_permission(
|
form_data.access_grants,
|
||||||
user.id,
|
"sharing.public_notes",
|
||||||
"sharing.public_notes",
|
db=db,
|
||||||
request.app.state.config.USER_PERMISSIONS,
|
)
|
||||||
db=db,
|
|
||||||
)
|
|
||||||
):
|
|
||||||
form_data.access_grants = []
|
|
||||||
|
|
||||||
# Strip individual user sharing if user lacks permission
|
|
||||||
if (
|
|
||||||
user.role != "admin"
|
|
||||||
and has_user_access_grant(form_data.access_grants)
|
|
||||||
and not has_permission(
|
|
||||||
user.id,
|
|
||||||
"access_grants.allow_users",
|
|
||||||
request.app.state.config.USER_PERMISSIONS,
|
|
||||||
)
|
|
||||||
):
|
|
||||||
form_data.access_grants = strip_user_access_grants(form_data.access_grants)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
note = Notes.update_note_by_id(id, form_data, db=db)
|
note = Notes.update_note_by_id(id, form_data, db=db)
|
||||||
@@ -369,36 +353,13 @@ async def update_note_access_by_id(
|
|||||||
status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
|
status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
|
||||||
)
|
)
|
||||||
|
|
||||||
# Strip public sharing if user lacks permission
|
form_data.access_grants = filter_allowed_access_grants(
|
||||||
if (
|
request.app.state.config.USER_PERMISSIONS,
|
||||||
user.role != "admin"
|
user.id,
|
||||||
and has_public_read_access_grant(form_data.access_grants)
|
user.role,
|
||||||
and not has_permission(
|
form_data.access_grants,
|
||||||
user.id,
|
"sharing.public_notes"
|
||||||
"sharing.public_notes",
|
)
|
||||||
request.app.state.config.USER_PERMISSIONS,
|
|
||||||
)
|
|
||||||
):
|
|
||||||
form_data.access_grants = [
|
|
||||||
grant
|
|
||||||
for grant in form_data.access_grants
|
|
||||||
if not (
|
|
||||||
grant.get("principal_type") == "user"
|
|
||||||
and grant.get("principal_id") == "*"
|
|
||||||
)
|
|
||||||
]
|
|
||||||
|
|
||||||
# Strip individual user sharing if user lacks permission
|
|
||||||
if (
|
|
||||||
user.role != "admin"
|
|
||||||
and has_user_access_grant(form_data.access_grants)
|
|
||||||
and not has_permission(
|
|
||||||
user.id,
|
|
||||||
"access_grants.allow_users",
|
|
||||||
request.app.state.config.USER_PERMISSIONS,
|
|
||||||
)
|
|
||||||
):
|
|
||||||
form_data.access_grants = strip_user_access_grants(form_data.access_grants)
|
|
||||||
|
|
||||||
AccessGrants.set_access_grants("note", id, form_data.access_grants, db=db)
|
AccessGrants.set_access_grants("note", id, form_data.access_grants, db=db)
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ from open_webui.models.prompts import (
|
|||||||
PromptModel,
|
PromptModel,
|
||||||
Prompts,
|
Prompts,
|
||||||
)
|
)
|
||||||
from open_webui.models.access_grants import AccessGrants, has_public_read_access_grant, has_user_access_grant, strip_user_access_grants
|
from open_webui.models.access_grants import AccessGrants
|
||||||
from open_webui.models.groups import Groups
|
from open_webui.models.groups import Groups
|
||||||
from open_webui.models.prompt_history import (
|
from open_webui.models.prompt_history import (
|
||||||
PromptHistories,
|
PromptHistories,
|
||||||
@@ -18,7 +18,7 @@ from open_webui.models.prompt_history import (
|
|||||||
)
|
)
|
||||||
from open_webui.constants import ERROR_MESSAGES
|
from open_webui.constants import ERROR_MESSAGES
|
||||||
from open_webui.utils.auth import get_admin_user, get_verified_user
|
from open_webui.utils.auth import get_admin_user, get_verified_user
|
||||||
from open_webui.utils.access_control import has_permission
|
from open_webui.utils.access_control import has_permission, filter_allowed_access_grants
|
||||||
from open_webui.config import BYPASS_ADMIN_ACCESS_CONTROL
|
from open_webui.config import BYPASS_ADMIN_ACCESS_CONTROL
|
||||||
from open_webui.internal.db import get_session
|
from open_webui.internal.db import get_session
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
@@ -473,36 +473,13 @@ async def update_prompt_access_by_id(
|
|||||||
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Strip public sharing if user lacks permission
|
form_data.access_grants = filter_allowed_access_grants(
|
||||||
if (
|
request.app.state.config.USER_PERMISSIONS,
|
||||||
user.role != "admin"
|
user.id,
|
||||||
and has_public_read_access_grant(form_data.access_grants)
|
user.role,
|
||||||
and not has_permission(
|
form_data.access_grants,
|
||||||
user.id,
|
"sharing.public_prompts"
|
||||||
"sharing.public_prompts",
|
)
|
||||||
request.app.state.config.USER_PERMISSIONS,
|
|
||||||
)
|
|
||||||
):
|
|
||||||
form_data.access_grants = [
|
|
||||||
grant
|
|
||||||
for grant in form_data.access_grants
|
|
||||||
if not (
|
|
||||||
grant.get("principal_type") == "user"
|
|
||||||
and grant.get("principal_id") == "*"
|
|
||||||
)
|
|
||||||
]
|
|
||||||
|
|
||||||
# Strip individual user sharing if user lacks permission
|
|
||||||
if (
|
|
||||||
user.role != "admin"
|
|
||||||
and has_user_access_grant(form_data.access_grants)
|
|
||||||
and not has_permission(
|
|
||||||
user.id,
|
|
||||||
"access_grants.allow_users",
|
|
||||||
request.app.state.config.USER_PERMISSIONS,
|
|
||||||
)
|
|
||||||
):
|
|
||||||
form_data.access_grants = strip_user_access_grants(form_data.access_grants)
|
|
||||||
|
|
||||||
AccessGrants.set_access_grants("prompt", prompt_id, form_data.access_grants, db=db)
|
AccessGrants.set_access_grants("prompt", prompt_id, form_data.access_grants, db=db)
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ from open_webui.models.skills import (
|
|||||||
SkillAccessListResponse,
|
SkillAccessListResponse,
|
||||||
Skills,
|
Skills,
|
||||||
)
|
)
|
||||||
from open_webui.models.access_grants import AccessGrants, has_public_read_access_grant, has_user_access_grant, strip_user_access_grants
|
from open_webui.models.access_grants import AccessGrants
|
||||||
from open_webui.utils.auth import get_admin_user, get_verified_user
|
from open_webui.utils.auth import get_admin_user, get_verified_user
|
||||||
from open_webui.utils.access_control import has_access, has_permission
|
from open_webui.utils.access_control import has_access, has_permission
|
||||||
|
|
||||||
@@ -341,36 +341,13 @@ async def update_skill_access_by_id(
|
|||||||
detail=ERROR_MESSAGES.UNAUTHORIZED,
|
detail=ERROR_MESSAGES.UNAUTHORIZED,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Strip public sharing if user lacks permission
|
form_data.access_grants = filter_allowed_access_grants(
|
||||||
if (
|
request.app.state.config.USER_PERMISSIONS,
|
||||||
user.role != "admin"
|
user.id,
|
||||||
and has_public_read_access_grant(form_data.access_grants)
|
user.role,
|
||||||
and not has_permission(
|
form_data.access_grants,
|
||||||
user.id,
|
"sharing.public_skills"
|
||||||
"sharing.public_skills",
|
)
|
||||||
request.app.state.config.USER_PERMISSIONS,
|
|
||||||
)
|
|
||||||
):
|
|
||||||
form_data.access_grants = [
|
|
||||||
grant
|
|
||||||
for grant in form_data.access_grants
|
|
||||||
if not (
|
|
||||||
grant.get("principal_type") == "user"
|
|
||||||
and grant.get("principal_id") == "*"
|
|
||||||
)
|
|
||||||
]
|
|
||||||
|
|
||||||
# Strip individual user sharing if user lacks permission
|
|
||||||
if (
|
|
||||||
user.role != "admin"
|
|
||||||
and has_user_access_grant(form_data.access_grants)
|
|
||||||
and not has_permission(
|
|
||||||
user.id,
|
|
||||||
"access_grants.allow_users",
|
|
||||||
request.app.state.config.USER_PERMISSIONS,
|
|
||||||
)
|
|
||||||
):
|
|
||||||
form_data.access_grants = strip_user_access_grants(form_data.access_grants)
|
|
||||||
|
|
||||||
AccessGrants.set_access_grants("skill", id, form_data.access_grants, db=db)
|
AccessGrants.set_access_grants("skill", id, form_data.access_grants, db=db)
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ from open_webui.models.tools import (
|
|||||||
ToolAccessResponse,
|
ToolAccessResponse,
|
||||||
Tools,
|
Tools,
|
||||||
)
|
)
|
||||||
from open_webui.models.access_grants import AccessGrants, has_public_read_access_grant, has_user_access_grant, strip_user_access_grants
|
from open_webui.models.access_grants import AccessGrants
|
||||||
from open_webui.utils.plugin import (
|
from open_webui.utils.plugin import (
|
||||||
load_tool_module_by_id,
|
load_tool_module_by_id,
|
||||||
replace_imports,
|
replace_imports,
|
||||||
@@ -576,36 +576,13 @@ async def update_tool_access_by_id(
|
|||||||
detail=ERROR_MESSAGES.UNAUTHORIZED,
|
detail=ERROR_MESSAGES.UNAUTHORIZED,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Strip public sharing if user lacks permission
|
form_data.access_grants = filter_allowed_access_grants(
|
||||||
if (
|
request.app.state.config.USER_PERMISSIONS,
|
||||||
user.role != "admin"
|
user.id,
|
||||||
and has_public_read_access_grant(form_data.access_grants)
|
user.role,
|
||||||
and not has_permission(
|
form_data.access_grants,
|
||||||
user.id,
|
"sharing.public_tools"
|
||||||
"sharing.public_tools",
|
)
|
||||||
request.app.state.config.USER_PERMISSIONS,
|
|
||||||
)
|
|
||||||
):
|
|
||||||
form_data.access_grants = [
|
|
||||||
grant
|
|
||||||
for grant in form_data.access_grants
|
|
||||||
if not (
|
|
||||||
grant.get("principal_type") == "user"
|
|
||||||
and grant.get("principal_id") == "*"
|
|
||||||
)
|
|
||||||
]
|
|
||||||
|
|
||||||
# Strip individual user sharing if user lacks permission
|
|
||||||
if (
|
|
||||||
user.role != "admin"
|
|
||||||
and has_user_access_grant(form_data.access_grants)
|
|
||||||
and not has_permission(
|
|
||||||
user.id,
|
|
||||||
"access_grants.allow_users",
|
|
||||||
request.app.state.config.USER_PERMISSIONS,
|
|
||||||
)
|
|
||||||
):
|
|
||||||
form_data.access_grants = strip_user_access_grants(form_data.access_grants)
|
|
||||||
|
|
||||||
AccessGrants.set_access_grants("tool", id, form_data.access_grants, db=db)
|
AccessGrants.set_access_grants("tool", id, form_data.access_grants, db=db)
|
||||||
|
|
||||||
|
|||||||
@@ -194,3 +194,53 @@ def migrate_access_control(
|
|||||||
|
|
||||||
data[grants_key] = grants
|
data[grants_key] = grants
|
||||||
data.pop(ac_key, None)
|
data.pop(ac_key, None)
|
||||||
|
|
||||||
|
from open_webui.models.access_grants import (
|
||||||
|
has_public_read_access_grant,
|
||||||
|
has_user_access_grant,
|
||||||
|
strip_user_access_grants,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def filter_allowed_access_grants(
|
||||||
|
default_permissions: Dict[str, Any],
|
||||||
|
user_id: str,
|
||||||
|
user_role: str,
|
||||||
|
access_grants: list,
|
||||||
|
public_permission_key: str,
|
||||||
|
db: Optional[Any] = None,
|
||||||
|
) -> list:
|
||||||
|
"""
|
||||||
|
Checks if the user has the required permissions to grant access to a resource.
|
||||||
|
Returns the filtered list of access grants if permissions are missing.
|
||||||
|
"""
|
||||||
|
if user_role == "admin" or not access_grants:
|
||||||
|
return access_grants
|
||||||
|
|
||||||
|
# Check if user can share publicly
|
||||||
|
if has_public_read_access_grant(access_grants) and not has_permission(
|
||||||
|
user_id,
|
||||||
|
public_permission_key,
|
||||||
|
default_permissions,
|
||||||
|
db=db,
|
||||||
|
):
|
||||||
|
access_grants = [
|
||||||
|
grant
|
||||||
|
for grant in access_grants
|
||||||
|
if not (
|
||||||
|
(grant.get("principal_type") if isinstance(grant, dict) else getattr(grant, "principal_type", None)) == "user"
|
||||||
|
and (grant.get("principal_id") if isinstance(grant, dict) else getattr(grant, "principal_id", None)) == "*"
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
|
# Strip individual user sharing if user lacks permission
|
||||||
|
if has_user_access_grant(access_grants) and not has_permission(
|
||||||
|
user_id,
|
||||||
|
"access_grants.allow_users",
|
||||||
|
default_permissions,
|
||||||
|
db=db,
|
||||||
|
):
|
||||||
|
access_grants = strip_user_access_grants(access_grants)
|
||||||
|
|
||||||
|
return access_grants
|
||||||
|
|
||||||
Reference in new issue
Block a user