Separate system accounts and household people, add generation directory and consented member location sharing
This commit is contained in:
+59
-16
@@ -54,13 +54,33 @@ class Households:
|
||||
raise self.a.Problem('账号尚未分配有效家庭,请联系平台管理员', 403)
|
||||
return family
|
||||
|
||||
def genealogy_of(self, family):
|
||||
home = self.a.get_object('households', family)
|
||||
return (home or {}).get('genealogyId') or family
|
||||
|
||||
def owns_person(self, user, item):
|
||||
if not item or self.genealogy_of(self.family_of(item)) != self.genealogy_of(self.scope(user)):
|
||||
raise self.a.Problem('人物或亲属关系不属于当前共用族谱', 404)
|
||||
return item
|
||||
|
||||
def grant(self, user, family):
|
||||
if user and '_familyId' in user:
|
||||
# A role in the current household is never a global or primary-home role.
|
||||
user = self.a.get_object('users', user.get('id'))
|
||||
if not user or user.get('disabled'): return None
|
||||
if user.get('role') == 'admin': return dict(role='admin')
|
||||
if self.family_of(user) == family:
|
||||
return {k: user.get(k) for k in ('role','siteIds','includeSubspaces','ptzControl','familyAccess','heritageAccess')}
|
||||
return user.get('familyGrants', {}).get(family)
|
||||
|
||||
def context(self, user, requested=None):
|
||||
if not user:
|
||||
return None
|
||||
own = self.family_of(user)
|
||||
if requested and user['role'] != 'admin' and requested != own:
|
||||
grant = self.grant(user, requested or own)
|
||||
if not grant:
|
||||
raise self.a.Problem('没有这个家庭的访问权限', 403)
|
||||
result = dict(user, _familyId=requested or own)
|
||||
result = dict(user, **({'_familyId':requested or own, **grant}))
|
||||
self.scope(result)
|
||||
return result
|
||||
|
||||
@@ -71,6 +91,16 @@ class Households:
|
||||
|
||||
def objects(self, user, table):
|
||||
family = self.scope(user)
|
||||
if table in ('people', 'family_links'):
|
||||
tree = self.genealogy_of(family)
|
||||
return [x for x in self.a.objects(table) if self.genealogy_of(self.family_of(x)) == tree]
|
||||
if table == 'users':
|
||||
rows=[]
|
||||
for x in self.a.objects(table):
|
||||
active=dict(x,disabled=False)
|
||||
if self.grant(active,family) and (x.get('role')!='admin' or self.family_of(x)==family):
|
||||
rows.append(dict(self.context(active,family),disabled=x.get('disabled',False)))
|
||||
return rows
|
||||
return [x for x in self.a.objects(table) if self.family_of(x) == family]
|
||||
|
||||
def site_ids(self, user):
|
||||
@@ -91,15 +121,16 @@ class Households:
|
||||
|
||||
def prepare(self, user, table, body, references=()):
|
||||
family = self.scope(user)
|
||||
if body.get('familyId', family) != family:
|
||||
shared = table in ('people', 'family_links')
|
||||
if body.get('familyId', family) != family and not shared:
|
||||
raise self.a.Problem('不能修改记录的家庭归属', 403)
|
||||
if body.get('id'):
|
||||
if not isinstance(body['id'], str) or not re.fullmatch('[a-f0-9]{16}', body['id']):
|
||||
raise self.a.Problem('对象编号不正确')
|
||||
self.owns(user, self.a.get_object(table, body['id']))
|
||||
(self.owns_person if shared else self.owns)(user, self.a.get_object(table, body['id']))
|
||||
for target, key in references:
|
||||
if body.get(key):
|
||||
self.owns(user, self.a.get_object(target, body[key]))
|
||||
(self.owns_person if target == 'people' else self.owns)(user, self.a.get_object(target, body[key]))
|
||||
return family
|
||||
|
||||
def endpoint(self, user, host):
|
||||
@@ -109,20 +140,23 @@ class Households:
|
||||
raise self.a.Problem('该设备地址已归属其他家庭,不能重复接入', 403)
|
||||
|
||||
def list(self, user):
|
||||
rows = self.a.objects('households') if user['role'] == 'admin' else [self.a.get_object('households', self.scope(user))]
|
||||
account = self.a.get_object('users', user.get('id')) or user
|
||||
rows = [h for h in self.a.objects('households') if self.grant(account, h['id'])]
|
||||
def label(uid):
|
||||
account = self.a.get_object('users', uid)
|
||||
return account.get('name', '') if account else ''
|
||||
inventory = [x for table in ('devices','assets','recorders') for x in self.a.objects(table)]
|
||||
def network_summary(home):
|
||||
context=self.context(account,home['id'])
|
||||
devices = [x for x in inventory if self.family_of(x) == home['id'] and
|
||||
(self.manager(user) or x.get('siteId') in self.site_ids(user))]
|
||||
(self.manager(context) or x.get('siteId') in self.site_ids(context))]
|
||||
return dict(deviceCount=len(devices), networkAccesses=sorted({x.get('networkAccess','unknown') for x in devices}))
|
||||
return [dict({k: v for k, v in row.items() if k != 'coverData' and (k != 'leadershipHistory' or self.manager(user))}, ownerName=label(row.get('ownerId')), successorName=label(row.get('successorId')),
|
||||
return [dict({k: v for k, v in row.items() if k != 'coverData' and (k != 'leadershipHistory' or self.manager(self.context(account,row['id'])))}, ownerName=label(row.get('ownerId')), successorName=label(row.get('successorId')),
|
||||
**network_summary(row),
|
||||
coverUrl=('/api/households/cover?id='+row['id']+'&v='+row.get('coverRevision','0')) if row.get('coverData') or row.get('coverRef') else '',
|
||||
canAssignOwner=user['role'] == 'admin' or row.get('ownerId') == user['id'],
|
||||
members=sum(self.family_of(u) == row['id'] for u in self.a.objects('users')),
|
||||
members=sum(m.get('status') != 'historical' for m in self.a.MEMBERS.rows(row['id'])),
|
||||
accounts=sum(bool(self.grant(u, row['id'])) for u in self.a.objects('users')),
|
||||
sites=sum(self.family_of(s) == row['id'] for s in self.a.objects('sites'))) for row in rows]
|
||||
|
||||
def layout(self, user):
|
||||
@@ -170,13 +204,22 @@ class Households:
|
||||
raise a.Problem('管理员授权确认格式不正确')
|
||||
promotions = []
|
||||
for uid in filter(None, (owner_id, successor_id)):
|
||||
account = self.owns(user, a.get_object('users', uid))
|
||||
if account['disabled'] or account['role'] not in ('family_admin', 'member'):
|
||||
raise a.Problem('请选择本家庭已启用的成员或家庭管理员')
|
||||
if account['role'] == 'member':
|
||||
account = a.get_object('users', uid)
|
||||
access = self.grant(dict(account, disabled=False), home['id']) if account else None
|
||||
if not access:
|
||||
raise a.Problem('账号不存在或未获本家庭访问权限', 404)
|
||||
if account.get('disabled') or access['role'] not in ('family_admin', 'member'):
|
||||
raise a.Problem('请选择已获本家庭访问授权的启用账号')
|
||||
if access['role'] == 'member':
|
||||
if not grant:
|
||||
raise a.Problem('所选成员还不是家庭管理员,请确认授予本家庭管理权限')
|
||||
promotions.append(dict(account, role='family_admin'))
|
||||
raise a.Problem('所选账号还不是家庭管理员,请确认授予本家庭管理权限')
|
||||
if self.family_of(account) == home['id']:
|
||||
promoted = dict(account, role='family_admin')
|
||||
else:
|
||||
grants = dict(account.get('familyGrants', {}))
|
||||
grants[home['id']] = dict(access, role='family_admin')
|
||||
promoted = dict(account, familyGrants=grants)
|
||||
promotions.append(promoted)
|
||||
record = dict(at=dt.datetime.now(dt.timezone.utc).isoformat(), actor=user['username'], reason=reason,
|
||||
previousOwnerId=home.get('ownerId', ''), ownerId=owner_id, successorId=successor_id,
|
||||
promotedUserIds=[u['id'] for u in promotions])
|
||||
@@ -241,7 +284,7 @@ class Households:
|
||||
return committed
|
||||
|
||||
def revision_key(self, user):
|
||||
family = self.scope(user)
|
||||
family = self.genealogy_of(self.scope(user))
|
||||
return 'familyRevision' if family == self.a.setting('defaultHousehold') else 'familyRevision:' + family
|
||||
|
||||
def save_view(self, data, user):
|
||||
|
||||
Reference in New Issue
Block a user