Ejemplo n.º 1
0
 def grant(rid, perms, resource_id=None, group_id=None):
     for perm in perms:
         perm = PermissionCache.get(perm)
         existed = RolePermission.get_by(rid=rid,
                                         perm_id=perm.id,
                                         group_id=group_id,
                                         resource_id=resource_id)
         existed or RolePermission.create(rid=rid,
                                          perm_id=perm.id,
                                          group_id=group_id,
                                          resource_id=resource_id)
Ejemplo n.º 2
0
    def grant(rid, perms, resource_id=None, group_id=None):
        for perm in perms:
            perm = PermissionCache.get(perm)
            existed = RolePermission.get_by(rid=rid,
                                            perm_id=perm.id,
                                            group_id=group_id,
                                            resource_id=resource_id)
            existed or RolePermission.create(rid=rid,
                                             perm_id=perm.id,
                                             group_id=group_id,
                                             resource_id=resource_id)

        role_rebuild.apply_async(args=(rid, ), queue=ACL_QUEUE)
Ejemplo n.º 3
0
    def get_all(resource_id=None, group_id=None):
        result = dict()
        if resource_id is not None:
            perms = RolePermission.get_by(resource_id=resource_id,
                                          to_dict=False)
        else:
            perms = RolePermission.get_by(group_id=group_id, to_dict=False)

        for perm in perms:
            perm_dict = PermissionCache.get(perm.perm_id).to_dict()
            perm_dict.update(dict(rid=perm.rid))
            result.setdefault(RoleCache.get(perm.rid).name,
                              []).append(perm_dict)

        return result
Ejemplo n.º 4
0
    def delete(_id):
        resource = Resource.get_by_id(_id) or abort(404, "Resource <{0}> is not found".format(_id))

        resource.soft_delete()

        for i in RolePermission.get_by(resource_id=_id, to_dict=False):
            i.soft_delete()
            role_rebuild.apply_async(args=(i.rid,), queue=ACL_QUEUE)
Ejemplo n.º 5
0
 def revoke(rid, perms, resource_id=None, group_id=None):
     for perm in perms:
         perm = PermissionCache.get(perm)
         existed = RolePermission.get_by(rid=rid,
                                         perm_id=perm.id,
                                         group_id=group_id,
                                         resource_id=resource_id,
                                         first=True,
                                         to_dict=False)
         existed and existed.soft_delete()
Ejemplo n.º 6
0
    def delete(rg_id):
        rg = ResourceGroup.get_by_id(rg_id) or abort(404, "ResourceGroup <{0}> is not found".format(rg_id))

        rg.soft_delete()

        items = ResourceGroupItems.get_by(group_id=rg_id, to_dict=False)
        for item in items:
            item.soft_delete()

        for i in RolePermission.get_by(group_id=rg_id, to_dict=False):
            i.soft_delete()
            role_rebuild.apply_async(args=(i.rid,), queue=ACL_QUEUE)
Ejemplo n.º 7
0
    def get_resources(rid):
        res = RolePermission.get_by(rid=rid, to_dict=False)
        id2perms = dict(id2perms={}, group2perms={})
        for i in res:
            if i.resource_id:
                id2perms['id2perms'].setdefault(i.resource_id,
                                                []).append(i.perm.name)
            elif i.group_id:
                id2perms['group2perms'].setdefault(i.group_id,
                                                   []).append(i.perm.name)

        return id2perms
Ejemplo n.º 8
0
    def revoke(rid, perms, resource_id=None, group_id=None):
        for perm in perms:
            perm = PermissionCache.get(perm)
            existed = RolePermission.get_by(rid=rid,
                                            perm_id=perm.id,
                                            group_id=group_id,
                                            resource_id=resource_id,
                                            first=True,
                                            to_dict=False)
            existed and existed.soft_delete()

        role_rebuild.apply_async(args=(rid, ), queue=ACL_QUEUE)
Ejemplo n.º 9
0
    def delete_role(cls, rid):
        role = Role.get_by_id(rid) or abort(
            404, "Role <{0}> does not exist".format(rid))

        for i in RoleRelation.get_by(parent_id=rid, to_dict=False):
            i.soft_delete()
        for i in RoleRelation.get_by(child_id=rid, to_dict=False):
            i.soft_delete()

        for i in RolePermission.get_by(rid=rid, to_dict=False):
            i.soft_delete()

        role_rebuild.apply_async(args=(list(
            RoleRelationCRUD.recursive_child_ids(rid)), ),
                                 queue=ACL_QUEUE)

        RoleCache.clean(rid)
        RoleRelationCache.clean(rid)

        role.soft_delete()