Exemple #1
0
def get_tables_with_access(user_profile, candidate_tables_queryset):
    """Return tables from given queryset to which the user_profile has access to"""
    # This should be more optimized once we refactor the ACL model into a single row bit
    # See https://github.com/dracidoupe/graveyard/issues/233

    related_permissions = TavernAccess.objects.filter(
        user_nick=misencode(user_profile.nick),
        tavern_table_id__in=[i.pk for i in candidate_tables_queryset],
    )

    related_permissions_map = {}

    for perm in related_permissions:
        if perm.tavern_table.pk not in related_permissions_map:
            related_permissions_map[perm.tavern_table.pk] = set()
        related_permissions_map[perm.tavern_table.pk].add(perm.access_type)

    return [
        table
        for table in candidate_tables_queryset
        if table.is_user_access_allowed(
            user_profile,
            acls=related_permissions_map.get(table.pk, set()),
        )
    ]
Exemple #2
0
    def handle(self, *args, **options):
        i = 0
        for comment in CreationComment.objects.filter(user__isnull=True):
            i += 1
            if i % 100 == 0:
                sys.stdout.write(".")
                sys.stdout.flush()
            try:
                comment.user = UserProfile.objects.get(nick=misencode(comment.nickname))
                comment.save()

            except UserProfile.DoesNotExist:
                logger.info("Can't find author for nickname %s" % comment.nickname)
Exemple #3
0
    def get_author(self, creation, creation_model):
        if creation.author_nick:
            author_type = Author.USER_TYPE
            author_name = creation.author_nick.lower()
        else:
            author_type = Author.WEBSITE_TYPE
            author_name = creation.original_web.lower()

        if (author_type, author_name) in self.authors:
            return self.authors[(author_type, author_name)]

        else:
            if author_type is Author.WEBSITE_TYPE:
                author = Author.objects.create(
                    website=creation.original_web,
                    website_email=creation.original_web_mail,
                    author_type=author_type,
                )
            else:
                # Shouldn't be needed in theory (tm)
                try:
                    profile = UserProfile.objects.get(
                        nick=misencode(author_name))

                    try:
                        author = Author.objects.get(user=profile,
                                                    author_type=author_type)
                    except Author.DoesNotExist:

                        try:
                            author_encoded = creation.author_nick.encode(
                                "latin2")
                        except UnicodeEncodeError:
                            print(
                                "Can't do standalone encoding for registered user, attempting skipping bad characters"
                            )
                            author_encoded = creation.author_nick.encode(
                                "latin2", "ignore")
                            print("Author's name replaced from %s to %s" % (
                                creation.author_nick,
                                author_encoded.decode("latin2"),
                            ))

                        author = Author.objects.create(
                            user=profile,
                            author_type=author_type,
                            user_nick=creation.author_nick,
                        )

                except UserProfile.DoesNotExist as err:
                    author_type = Author.ANONYMOUS_USER_TYPE

                    try:
                        author_encoded = creation.author_nick.encode("latin2")
                    except UnicodeEncodeError:
                        print(
                            "Can't do standalone encoding for anonymous user, attempting skipping bad characters"
                        )
                        print("This is for creation %s from model %s" %
                              (creation, creation_model))

                        author_encoded = creation.author_nick.encode(
                            "latin2", "ignore")
                        print("Author's name replaced from %s to %s" %
                              (creation.author_nick,
                               author_encoded.decode("latin2")))

                    author = Author.objects.create(
                        author_type=author_type,
                        anonymous_user_nick=author_encoded.decode("latin2"),
                    )

            self.authors[(author_type, author_name)] = author
            return author
Exemple #4
0
def migrate_tavern_access(
    print_progress=True,
    table_visitor_model=None,
    tavern_access_model=None,
    user_profile_model=None,
):
    """Migrate access privileges from putyka v0 (the DDCZ 2001 version) to putyka v1 (DDCZ 2004 version)"""

    i = 0

    if not table_visitor_model:
        table_visitor_model = TavernTableVisitor

    if not tavern_access_model:
        tavern_access_model = TavernAccess

    if not user_profile_model:
        user_profile_model = UserProfile

    # Completely accidentally, the `-typ_pristupu` gives us correct enough
    # ordering of access priorities
    for tavern_access in (
        tavern_access_model.objects.all()
        .select_related("tavern_table")
        .order_by("-access_type")
    ):
        if print_progress and i % 100 == 0:
            sys.stdout.write(".")
            sys.stdout.flush()

        try:
            user_profile = user_profile_model.objects.get(
                nick=misencode(tavern_access.user_nick)
            )
        except user_profile_model.DoesNotExist:
            # OK, this opens up a potential security problem, but so far, we don't have
            # Users with nick that would be composed of numbers only.
            # So let's try: sometimes, there is an integer in nick_usera that looks like
            # corresponding to a user ID, so let's use that for a secondary search
            try:
                user_profile = user_profile_model.objects.get(
                    id=int(tavern_access.user_nick)
                )
            except (ValueError, user_profile_model.DoesNotExist):
                # OK, _now_ we give up
                logging.warn(
                    f"Can't fetch user record for nick {tavern_access.user_nick}. Maybe we should purge the record?"
                )
        else:
            try:
                table_visitor = table_visitor_model.objects.get(
                    tavern_table=tavern_access.tavern_table, user_profile=user_profile
                )
            except table_visitor_model.DoesNotExist:
                table_visitor = table_visitor_model(
                    tavern_table=tavern_access.tavern_table, user_profile=user_profile
                )

            if tavern_access.access_type == "vstpo":
                table_visitor.access = 1
            elif tavern_access.access_type == "vstza":
                table_visitor.access = -2
            elif tavern_access.access_type == "zapis":
                table_visitor.access = 2
            elif tavern_access.access_type == "asist":
                table_visitor.moderator = 1
            else:
                logger.warn(
                    f"Unknown access type {tavern_access.access_type} for user {tavern_access.user_nick} to table {tavern_access.tavern_table}"
                )

            table_visitor.save()

        i += 1