Ejemplo n.º 1
0
def check_resource_owners(user1, user2, resource, grantor):
    """ check that each resource has the proper number of owners """

    owners = User.objects.filter(u2urp__resource=resource,
                                 u2urp__privilege=PrivilegeCodes.OWNER)
    if user1 not in owners:
        # fix it NOW
        print("    SHARING {} {} with first owner {}".format(
            resource.short_id, resource.title.encode('ascii', 'ignore'),
            user1.username))
        UserResourcePrivilege.share(user=user1,
                                    resource=resource,
                                    privilege=PrivilegeCodes.OWNER,
                                    grantor=grantor)
        # first argument is also quota holder.
        set_quota_holder(resource, user1)

    # for CZO national group, there's only one owner.
    if user1 != user2 and user2 not in owners:

        # fix it NOW
        print("    SHARING {} {} with second owner {}".format(
            resource.short_id, resource.title.encode('ascii', 'ignore'),
            user2.username))
        UserResourcePrivilege.share(user=user2,
                                    resource=resource,
                                    privilege=PrivilegeCodes.OWNER,
                                    grantor=grantor)

    for o in owners:
        if o.username != user1.username and o.username != user2.username:
            # fix it NOW
            print("    UNSHARING {} {} with owner {}".format(
                resource.short_id, resource.title.encode('ascii', 'ignore'),
                o.username))
            UserResourcePrivilege.unshare(user=o,
                                          resource=resource,
                                          grantor=grantor)
Ejemplo n.º 2
0
def check_resource_prefix(user, group, resource, prefix, mapper, grantor):
    if not resource.title.startswith(prefix):
        print("    UNSHARING {} {}: prefix not {} (UNSHARING)".format(
            resource.short_id, resource.title.encode('ascii', 'ignore'),
            prefix))
        # not in the user's resources
        UserResourcePrivilege.unshare(resource=resource,
                                      user=user,
                                      grantor=grantor)
        # not in the group's resources
        GroupResourcePrivilege.unshare(resource=resource,
                                       group=group,
                                       grantor=grantor)

        # Where does it really go?
        new_prefix = resource.title.split(" ")[0]
        if new_prefix in mapper:
            new_username = mapper[new_prefix][0]
            new_groupname = mapper[new_prefix][1]
            new_user = User.objects.get(username=new_username)
            new_group = Group.objects.get(name=new_groupname)
            print("    SHARING {} {} with user={} group={}".format(
                resource.short_id, resource.title.encode('ascii', 'ignore'),
                new_username, new_groupname))
            UserResourcePrivilege.share(resource=resource,
                                        user=new_user,
                                        privilege=PrivilegeCodes.OWNER,
                                        grantor=grantor)
            GroupResourcePrivilege.share(resource=resource,
                                         group=new_group,
                                         privilege=PrivilegeCodes.VIEW,
                                         grantor=grantor)
        else:
            print("    ERROR {} {} unknown prefix {}".format(
                resource.short_id, resource.title.encode('ascii', 'ignore'),
                new_prefix))