rbac/wwwroot/users/get_users.dspy
yumoqing 449a0415a5 fix: users CRUD — 三个修复
1. add_users.dspy: 新增用户时 created_at 默认设为当天日期
2. add/update: 清除 Tabular 发送的 _text 后缀字段
   (user_status_text, orgid_text 等不是真实列,会导致 sor.U/C 失败)
3. update_users.dspy: 密码为空时从 ns 中移除,避免覆盖为空白
2026-06-29 14:46:58 +08:00

170 lines
3.9 KiB
Plaintext

ns = params_kw.copy()
userorgid = await get_userorgid()
if not userorgid:
return {
"widgettype":"Error",
"options":{
"title":"Authorization Error",
"timeout":3,
"cwidth":16,
"cheight":9,
"message":"Please login"
}
}
ns['orgid'] = userorgid
ns['userorgid'] = userorgid
debug(f'get_users.dspy:{ns=}')
if not ns.get('page'):
ns['page'] = 1
if not ns.get('sort'):
ns['sort'] = 'username'
sql = '''select a.*, b.user_status_text, c.sync_from_text, d.orgid_text
from (select * from users where 1=1 [[filterstr]]) a left join (select k as user_status,
v as user_status_text from appcodes_kv where parentid='user_status') b on a.user_status = b.user_status left join (select id as sync_from,
name as sync_from_text from downapp where 1 = 1) c on a.sync_from = c.sync_from left join (select id as orgid,
orgname as orgid_text from organization where 1 = 1) d on a.orgid = d.orgid'''
filterjson = params_kw.get('data_filter')
if filterjson and isinstance(filterjson, str):
try:
filterjson = json.loads(filterjson)
except (json.JSONDecodeError, TypeError):
filterjson = None
fields_str=r'''[
{
"name": "id",
"title": "用户id",
"type": "str",
"length": 32
},
{
"name": "username",
"title": "用户名",
"type": "str",
"length": 255
},
{
"name": "password",
"title": "密码",
"type": "str",
"length": 255
},
{
"name": "email",
"title": "邮件地址",
"type": "str",
"length": 255
},
{
"name": "orgid",
"title": "所属机构",
"type": "str",
"length": 32
},
{
"name": "nick_name",
"title": "显示名",
"type": "str",
"length": 255
},
{
"name": "address",
"title": "地址",
"type": "str",
"length": 255
},
{
"name": "mobile",
"title": "手机",
"type": "str",
"length": 255
},
{
"name": "user_status",
"title": "用户状态",
"type": "str",
"length": 1,
"default": "0"
},
{
"name": "created_at",
"title": "注册日期",
"type": "date"
},
{
"name": "last_login",
"title": "最后登录",
"type": "timestamp",
"nullable": true
},
{
"name": "login_fail_count",
"title": "连续失败次数",
"type": "short",
"default": "0"
},
{
"name": "last_login_fail",
"title": "最后登录失败时间",
"type": "timestamp",
"nullable": true
},
{
"name": "sync_from",
"title": "同步应用id",
"type": "str",
"length": 32,
"nullable": true
}
]'''
ori_fields = json.loads(fields_str)
if not filterjson:
fields = [ f['name'] for f in ori_fields ]
filterjson = default_filterjson(fields, ns)
# 确保 logined 过滤条件始终生效
if filterjson:
if not isinstance(filterjson, dict) or 'AND' not in filterjson:
filterjson = {'AND': [filterjson] if filterjson else []}
filterjson['AND'].append({'field': 'orgid', 'op': '=', 'var': '__logined_orgid__'})
ns['__logined_orgid__'] = userorgid
filterdic = ns.copy()
filterdic['filterstr'] = ''
filterdic['userorgid'] = '${userorgid}$'
filterdic['userid'] = '${userid}$'
if filterjson:
dbf = DBFilter(filterjson)
conds = dbf.gen(ns)
if conds:
ns.update(dbf.consts)
conds = f' and {conds}'
filterdic['filterstr'] = conds
ac = ArgsConvert('[[', ']]')
vars = ac.findAllVariables(sql)
NameSpace = {v:'${' + v + '}$' for v in vars if v != 'filterstr' }
filterdic.update(NameSpace)
sql = ac.convert(sql, filterdic)
debug(f'{sql=}')
db = DBPools()
dbname = get_module_dbname('rbac')
async with db.sqlorContext(dbname) as sor:
r = await sor.sqlPaging(sql, ns)
return r
return {
"total":0,
"rows":[]
}