<commit_before># -*- coding: utf-8 -*-

"""
bootstrap.authorization
~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2016 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

from byceps.blueprints.authorization_admin.authorization import RolePermission
from byceps.blueprints.board.authorization import BoardPostingPermission, \
    BoardTopicPermission
from byceps.blueprints.orga_admin.authorization import OrgaTeamPermission
from byceps.blueprints.party_admin.authorization import PartyPermission
from byceps.blueprints.snippet_admin.authorization import \
    MountpointPermission, SnippetPermission
from byceps.blueprints.terms_admin.authorization import TermsPermission
from byceps.blueprints.user_admin.authorization import UserPermission
from byceps.database import db
from byceps.services.authorization.models import Permission as DbPermission, \
    Role
from byceps.services.authorization import service as authorization_service

from .util import add_all_to_database, add_to_database


def create_roles_and_permissions():
    create_role_with_permissions_from_enum(
        'authorization_admin',
        'Rechte und Rollen verwalten',
        RolePermission)

    create_role_with_permissions_from_enum_members(
        'board_user',
        'im Forum schreiben',
        [
            BoardPostingPermission.create,
            BoardPostingPermission.update,
            BoardTopicPermission.create,
            BoardTopicPermission.update,
        ])

    create_role_with_permissions_from_enum_members(
        'board_orga',
        'versteckte Themen und Beiträge im Forum lesen',
        [
            BoardPostingPermission.view_hidden,
            BoardTopicPermission.view_hidden
        ])

    create_role_with_permissions_from_enum_members(
        'board_moderator',
        'Forum moderieren',
        [
            BoardPostingPermission.hide,
            BoardTopicPermission.hide,
            BoardTopicPermission.lock,
            BoardTopicPermission.pin,
        ])

    create_role_with_permissions_from_enum(
        'orga_team_admin',
        'Orgateams verwalten',
        OrgaTeamPermission)

    create_role_with_permissions_from_enum(
        'party_admin',
        'Partys verwalten',
        PartyPermission)

    create_role_with_permissions_from_enum(
        'snippet_admin',
        'Snippets verwalten',
        MountpointPermission)

    create_role_with_permissions_from_enum(
        'snippet_editor',
        'Snippets bearbeiten',
        SnippetPermission)

    create_role_with_permissions_from_enum(
        'terms_editor',
        'AGB verwalten',
        TermsPermission)

    create_role_with_permissions_from_enum(
        'user_admin',
        'Benutzer verwalten',
        UserPermission)

    db.session.commit()


# -------------------------------------------------------------------- #
# helpers


def create_role_with_permissions_from_enum(role_id, role_title, permission_enum):
    return create_role_with_permissions_from_enum_members(role_id, role_title, iter(permission_enum))


def create_role_with_permissions_from_enum_members(role_id, role_title, permission_enum_members):
    role = create_role(role_id, role_title)
    permissions = create_permissions_from_enum_members(permission_enum_members)
    add_permissions_to_role(permissions, role)


@add_to_database
def create_role(id, title):
    return Role(id, title)


def get_role(id):
    return Role.query.get(id)


@add_all_to_database
def create_permissions_from_enum_members(enum_members):
    return list(map(DbPermission.from_enum_member, enum_members))


def add_permissions_to_role(permissions, role):
    for permission in permissions:
        authorization_service.assign_permission_to_role(permission, role)


def add_roles_to_user(roles, user):
    for role in roles:
        authorization_service.assign_role_to_user(role, user)<commit_msg>Don't hide built-in `id` function<commit_after># -*- coding: utf-8 -*-

"""
bootstrap.authorization
~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2016 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

from byceps.blueprints.authorization_admin.authorization import RolePermission
from byceps.blueprints.board.authorization import BoardPostingPermission, \
    BoardTopicPermission
from byceps.blueprints.orga_admin.authorization import OrgaTeamPermission
from byceps.blueprints.party_admin.authorization import PartyPermission
from byceps.blueprints.snippet_admin.authorization import \
    MountpointPermission, SnippetPermission
from byceps.blueprints.terms_admin.authorization import TermsPermission
from byceps.blueprints.user_admin.authorization import UserPermission
from byceps.database import db
from byceps.services.authorization.models import Permission as DbPermission, \
    Role
from byceps.services.authorization import service as authorization_service

from .util import add_all_to_database, add_to_database


def create_roles_and_permissions():
    create_role_with_permissions_from_enum(
        'authorization_admin',
        'Rechte und Rollen verwalten',
        RolePermission)

    create_role_with_permissions_from_enum_members(
        'board_user',
        'im Forum schreiben',
        [
            BoardPostingPermission.create,
            BoardPostingPermission.update,
            BoardTopicPermission.create,
            BoardTopicPermission.update,
        ])

    create_role_with_permissions_from_enum_members(
        'board_orga',
        'versteckte Themen und Beiträge im Forum lesen',
        [
            BoardPostingPermission.view_hidden,
            BoardTopicPermission.view_hidden
        ])

    create_role_with_permissions_from_enum_members(
        'board_moderator',
        'Forum moderieren',
        [
            BoardPostingPermission.hide,
            BoardTopicPermission.hide,
            BoardTopicPermission.lock,
            BoardTopicPermission.pin,
        ])

    create_role_with_permissions_from_enum(
        'orga_team_admin',
        'Orgateams verwalten',
        OrgaTeamPermission)

    create_role_with_permissions_from_enum(
        'party_admin',
        'Partys verwalten',
        PartyPermission)

    create_role_with_permissions_from_enum(
        'snippet_admin',
        'Snippets verwalten',
        MountpointPermission)

    create_role_with_permissions_from_enum(
        'snippet_editor',
        'Snippets bearbeiten',
        SnippetPermission)

    create_role_with_permissions_from_enum(
        'terms_editor',
        'AGB verwalten',
        TermsPermission)

    create_role_with_permissions_from_enum(
        'user_admin',
        'Benutzer verwalten',
        UserPermission)

    db.session.commit()


# -------------------------------------------------------------------- #
# helpers


def create_role_with_permissions_from_enum(role_id, role_title, permission_enum):
    return create_role_with_permissions_from_enum_members(role_id, role_title, iter(permission_enum))


def create_role_with_permissions_from_enum_members(role_id, role_title, permission_enum_members):
    role = create_role(role_id, role_title)
    permissions = create_permissions_from_enum_members(permission_enum_members)
    add_permissions_to_role(permissions, role)


@add_to_database
def create_role(role_id, title):
    return Role(role_id, title)


def get_role(role_id):
    return Role.query.get(role_id)


@add_all_to_database
def create_permissions_from_enum_members(enum_members):
    return list(map(DbPermission.from_enum_member, enum_members))


def add_permissions_to_role(permissions, role):
    for permission in permissions:
        authorization_service.assign_permission_to_role(permission, role)


def add_roles_to_user(roles, user):
    for role in roles:
        authorization_service.assign_role_to_user(role, user)
{'fraction_non_alphanumeric': 0.08717948717948718, 'fraction_numerical': 0.002307692307692308, 'mean_word_length': 3.540745052386496, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7429763', 'n_tokens_mistral': 2299, 'n_tokens_neox': 2230, 'n_words': 381}