[python与CSP的姻缘]202206-3 角色授权

该文章介绍了一个用Python编写的系统,该系统处理用户、角色、权限的关系,用于检查用户是否有权执行特定操作。通过建立用户到角色的映射,判断用户所属的角色是否具有执行操作的权限。代码目前得分为60分,作者认为转换为C++可能可以得到满分。
摘要由CSDN通过智能技术生成

题目比较长,就不粘贴啦大家看202206第三题就好

思路

这个题第一次看的时候感觉关系很乱,理不清
其实这个题只要弄清楚各个量之间的关系就很好出来啦

题干他说的是有角色去关联它授权的对象,后来输入的时候给的是用这个用户对象,查看它所属的所有角色是否能执行这个操作,那这里就转换了一下,用用户去关联角色,然后看到用户名或用户组其实在处理起来层次是一样的,就把用户和用户组在关联角色时一致了一下。
用一个字典进行用户到角色的关联,key用户名或用户组名,value是所关联的所有角色的集合

但是现在还有点错误,是60分,再改改再更新啦
(PS:用python的话这个大概能拿70分,据说一样的代码改成Cpp应该能满,python要满分的话估计要多考虑很多边边角角啊啊啊)

代码

60分

class User:
    def __init__(self,name,group:set) -> None:
        self.name = name
        self.group = group

class Source:
    def __init__(self,type,name) -> None:
        self.type = type
        self.name = name

class Active:
    def __init__(self,host:User,op:str,source:Source) -> None:
        self.host = host
        self.op = op
        self.source = source

class Role:
    def __init__(self,uname:str,opList:list,sourceTypeList:list,sourceNameList:list) -> None:
        self.uname = uname
        self.opList = opList
        self.sourceTypeList = sourceTypeList
        self.sourceNameList = sourceNameList

class RoleConnect:
    def __init__(self,roleName:str,authObjSet:list) -> None:
        self.roleName = roleName
        self.authObjSet = authObjSet

n,m,q = map(int,input().split())  # 角色数量、关联数量、待检查操作数量
roleLst = {}        # key:角色名称-value:角色对象
roleConnect = {}    # key:角色名称-value:角色关联对象
ug_to_role = {}     # key:用户或用户组名称-value:角色对象集合

# 处理角色
for i in range(n):
    tmp = input().split()
    rname = tmp[0]              # 角色名称
    nv = int(tmp[1])
    if nv == 0:
        opLst = ['*']
    else:
        opLst = tmp[2:2+nv]         # 角色允许的操作
    no = int(tmp[2+nv])
    if no == 0:
        source_type_list = ['*']
    else:
        source_type_list = tmp[3+nv:3+nv+no]     # 资源种类清单
    nn = int(tmp[3+nv+no])
    if nn == 0:
        source_name_list = ['*']
    else:
        source_name_list = tmp[6+nv+no:6+nv+no+nn]   # 资源名称清单
    roleLst[rname] = Role(rname,opLst,source_type_list,source_name_list)

# 处理角色关联
for i in range(m):
    tmp = input().split()
    rname = tmp[0]
    nv = int(tmp[1])
    for j in range(3,len(tmp),2):
        ug = tmp[j]
        if ug not in ug_to_role:
            ug_to_role[ug] = set()
        ug_to_role[ug].add(roleLst[rname])
    # authObjSet = tmp[2:2+nv]
    # roleConnect[rname] = RoleConnect(rname,authObjSet)

    '''
    这里暂时没有用到角色关联对象
    '''

def role_can_op(role,active:Active):
    opLst = role.opList
    op_check = active.op
    if op_check not in opLst and '*' not in opLst:
        return False
    if active.source.type not in role.sourceTypeList and '*' not in role.sourceTypeList:
        return False
    if active.source.name not in role.sourceNameList and '*' not in role.sourceNameList:
        return False
    return True

def user_can_op(active:Active):
    user = active.host
    uname = user.name
    user_group = user.group
    op = active.op
    user_with_role = set()
    if uname in ug_to_role:
        user_with_role |= ug_to_role[uname]
    for ugroup in user_group:
        if ugroup in ug_to_role:
            user_with_role |= ug_to_role[ugroup]
    for role in user_with_role:
        if role_can_op(role,active):
            return True
    return False


# 处理待授权行为
for i in range(q):
    tmp = input().split()
    uname = tmp[0]
    ng = int(tmp[1])
    user_2_group = set(tmp[2:2+ng])
    op_name,op_source_type,op_source_name = tmp[2+ng:]

    user = User(uname,user_2_group)
    source = Source(op_source_type,op_source_name)

    active = Active(user,op_name,source)
    if user_can_op(active):
        print(1)
    else:
        print(0)



'''
1 2 3
op 1 open 1 door 0
op 1 g sre
op 1 u xiaop
xiaoc 2 sre ops open door room302
xiaop 1 ops open door room501
xiaoc 2 sre ops remove door room302

'''

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值