修复:装填前复核仓库权限
This commit is contained in:
@@ -8,16 +8,22 @@ class AccessDao:
|
||||
"""仓库与客户现场数据权限数据库操作层。"""
|
||||
|
||||
@classmethod
|
||||
async def user_has_warehouse(cls, db: AsyncSession, user_id: int, warehouse_id: int) -> bool:
|
||||
async def user_has_warehouse(
|
||||
cls,
|
||||
db: AsyncSession,
|
||||
user_id: int,
|
||||
warehouse_id: int,
|
||||
*,
|
||||
for_update: bool = False,
|
||||
) -> bool:
|
||||
"""判断用户是否关联指定仓库。"""
|
||||
mapped_user_id = (
|
||||
await db.execute(
|
||||
select(BizUserWarehouse.user_id).where(
|
||||
BizUserWarehouse.user_id == user_id,
|
||||
BizUserWarehouse.warehouse_id == warehouse_id,
|
||||
)
|
||||
)
|
||||
).scalar_one_or_none()
|
||||
statement = select(BizUserWarehouse.user_id).where(
|
||||
BizUserWarehouse.user_id == user_id,
|
||||
BizUserWarehouse.warehouse_id == warehouse_id,
|
||||
)
|
||||
if for_update:
|
||||
statement = statement.with_for_update()
|
||||
mapped_user_id = (await db.execute(statement)).scalar_one_or_none()
|
||||
|
||||
return mapped_user_id is not None
|
||||
|
||||
|
||||
@@ -14,11 +14,22 @@ class AccessService:
|
||||
db: AsyncSession,
|
||||
current_user: CurrentUserModel,
|
||||
warehouse_id: int,
|
||||
*,
|
||||
for_update: bool = False,
|
||||
) -> None:
|
||||
"""校验当前用户是否有权访问指定仓库。"""
|
||||
if current_user.user.admin:
|
||||
return
|
||||
if not await AccessDao.user_has_warehouse(db, current_user.user.user_id, warehouse_id):
|
||||
if for_update:
|
||||
has_warehouse = await AccessDao.user_has_warehouse(
|
||||
db,
|
||||
current_user.user.user_id,
|
||||
warehouse_id,
|
||||
for_update=True,
|
||||
)
|
||||
else:
|
||||
has_warehouse = await AccessDao.user_has_warehouse(db, current_user.user.user_id, warehouse_id)
|
||||
if not has_warehouse:
|
||||
raise PermissionException(data='', message='无权访问该仓库')
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -102,9 +102,19 @@ class FillingService:
|
||||
query_db: AsyncSession,
|
||||
current_user: CurrentUserModel,
|
||||
warehouse_id: int,
|
||||
*,
|
||||
for_update: bool = False,
|
||||
) -> None:
|
||||
try:
|
||||
await AccessService.assert_warehouse_access(query_db, current_user, warehouse_id)
|
||||
if for_update:
|
||||
await AccessService.assert_warehouse_access(
|
||||
query_db,
|
||||
current_user,
|
||||
warehouse_id,
|
||||
for_update=True,
|
||||
)
|
||||
else:
|
||||
await AccessService.assert_warehouse_access(query_db, current_user, warehouse_id)
|
||||
except PermissionException:
|
||||
if cls._is_admin(current_user):
|
||||
raise
|
||||
@@ -389,13 +399,19 @@ class FillingService:
|
||||
|
||||
warehouse = await MasterDao.get_warehouse(
|
||||
query_db,
|
||||
page_object.warehouse_id,
|
||||
inventory.warehouse_id,
|
||||
for_update=True,
|
||||
)
|
||||
if warehouse is None:
|
||||
raise ServiceException(message='仓库不存在')
|
||||
if warehouse.status != '0':
|
||||
raise ServiceException(message='仓库已停用')
|
||||
await cls._assert_pallet_warehouse_access(
|
||||
query_db,
|
||||
current_user,
|
||||
inventory.warehouse_id,
|
||||
for_update=True,
|
||||
)
|
||||
strain = cls._validate_active_strain(
|
||||
await MasterDao.get_strain(
|
||||
query_db,
|
||||
|
||||
Reference in New Issue
Block a user