unis-oa/backend/app/service/starters.py

162 lines
6.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""「这条流程谁能发起」的统一口径。
配置挂在流程定义上(``oa_process``):
* ``open_create`` 是否对所有人开放发起(设计器「谁能发起」那格开关);
* ``start_identity_list`` 限定范围时的来源清单,写法与环节审批人同源,
身份 DN / 登录账号 / 姓名 / 部门路径或部门名 / 岗位角色名 / 分组名都认,
运行时时现解析(见 ``service/org.py``),所以组织改名之后靠 ``hub_refs`` 一起搬。
以前这两列只有 ``start_identity_list`` 在发起接口里被拿当前身份硬比了一次 DN,
``open_create`` 只在界面上摆着——等于「谁能发起」既没地方配、配了也不生效,
发起页还把全部已发布流程一股脑列出来,点进去才被弹一句没权限。
现在**发起判定、发起页过滤、发布校验、设计器预览**都从这一个模块取值,一把尺子。
"""
from __future__ import annotations
from collections.abc import Sequence
from typing import Iterable
from sqlalchemy.orm import Session
from .. import models
from . import org
#: 一次请求内的解析缓存:{来源原值: 解析出的人}
Cache = dict[str, list[str]]
def clean_selectors(values: Iterable[object]) -> list[str]:
"""去空白、去重保序。脏值(空串、非字符串)在这里就丢掉。"""
out: list[str] = []
for raw in values or ():
value = str(raw or "").strip()
if value and value not in out:
out.append(value)
return out
def selectors(process: models.Process) -> list[str]:
return clean_selectors(process.start_identity_list or [])
def is_open(process: models.Process) -> bool:
"""开放发起:所有人都能在发起页看到并发起。"""
return bool(process.open_create)
def actor_identities(db: Session, user) -> list[str]:
"""这个登录人此刻算得上的身份:当前身份 + 本人名下所有身份。
只比当前身份会让人莫名其妙发不起单:同一个人有「本人」和「兼岗」两个身份,
切到另一个身份干活就被挡。范围配的是"这个人",所以把人身上所有身份都摆进来。
"""
values: list[str] = []
current = str(getattr(user, "identity", "") or "").strip()
if current:
values.append(current)
person_id = getattr(user, "person_id", "") or ""
for row in org.person_identities(db, person_id):
dn = row.distinguished_name
if dn and dn not in values:
values.append(dn)
return values
def expand(db: Session, values: Iterable[str], cache: Cache | None = None) -> tuple[list[str], list[str]]:
"""来源清单 -> (解析出的身份 DN, 认不出来的原值)。
解析走 ``org`` 那一套(DN / 人员 / 部门 / 角色 / 分组),**部门额外含下级**:
管理员在发起范围里挑一个部门,本意是这个部门以及下面的科室都能发起,
和环节审批人里勾了「含下级」的口径一致。认不出来的原值照样返回,
手工写进配置的历史身份串不会被悄悄丢掉。
"""
bag: Cache = cache if cache is not None else {}
identities: list[str] = []
unknown: list[str] = []
for value in clean_selectors(values):
if value in bag:
members = bag[value]
else:
members, missed = org.split_selectors(db, [value])
# 值正好指着某个部门时,把下级部门的人一起算进来;
# 不是部门时这一句解析不出人,什么也不加。
deeper, _ = org.resolve_selector_row(db, "unit", [value], contains_sub=True)
members = list(members) + [dn for dn in deeper if dn not in members]
unknown.extend(name for name in missed if name not in unknown)
bag[value] = members
for dn in members:
if dn and dn not in identities:
identities.append(dn)
return identities, unknown
def allowed(
db: Session,
process: models.Process,
*,
identities: Sequence[str] | None = None,
is_admin: bool = False,
cache: Cache | None = None,
) -> bool:
"""能不能发起这条流程。管理员永远能,开放发起所有人都能,其余按范围名单。"""
if is_admin:
return True
if is_open(process):
return True
wanted = set(identities or ())
if not wanted:
return False
members, _ = expand(db, selectors(process), cache)
return any(dn in wanted for dn in members)
def startable_map(
db: Session,
processes: Iterable[models.Process],
*,
identities: Sequence[str] | None = None,
is_admin: bool = False,
) -> dict[str, bool]:
"""一批流程各自能不能发起(发起页、工作台、首页共用,一次解析算完全部)。"""
cache: Cache = {}
return {
row.id: allowed(db, row, identities=identities, is_admin=is_admin, cache=cache)
for row in processes
}
def preview(db: Session, values: Iterable[str]) -> dict:
"""设计器预览:这批发起范围现在解析成谁、哪些名字认不出来。"""
identities, unknown = expand(db, values)
return {
"members": org.member_briefs(db, identities),
"unknown": unknown,
"count": len(identities),
}
def scope_problems(db: Session, process: models.Process) -> list[str]:
"""发布前的提醒:范围配成了没人能发起,一定要在发布这一刻说清楚。
返回的是 ``definition.validate`` 同款的人话问题串,空列表 = 没毛病。
"""
if is_open(process):
return []
values = selectors(process)
if not values:
return [f"流程「{process.name}」限定发起范围但没有配置任何来源,发布后除管理员外没人能发起"]
members, unknown = expand(db, values)
# 解析出来的人还得在职,否则和没配一样:单据发不出去,界面上一个错都不报。
live = set(org.live_identities(db, members))
dead = [dn for dn in members if dn not in live]
problems: list[str] = []
if unknown:
problems.append(f"发起范围里的来源认不出对应的组织对象:{'、'.join(unknown[:6])}")
if dead:
problems.append(f"发起范围里有 {len(dead)} 个身份对应的人已不在职")
if not live:
problems.append("发起范围现在解析不到任何在职人员,发布后除管理员外没人能发起")
return problems