# Path: opennode/oms/model/traversal.py
# def traverse1(path):
#     """Provides a shortcut for absolute path traversals without
#     needing to pass in the root object.
# 
#     """
# 
#     # Do it here just in case; to avoid circular imports:
#     from opennode.oms.zodb import db
# 
#     oms_root = db.get_root()['oms_root']
#     objs, untraversed_path = traverse_path(oms_root, path)
#     if objs and not untraversed_path:
#         return objs[-1]
#     else:
#         return None
# 
# Path: opennode/oms/security/interaction.py
# def new_interaction(principal):
#     if not IPrincipal.providedBy(principal):
#         auth = getUtility(IAuthentication, context=None)
#         principal = auth.getPrincipal(principal)
# 
#     interaction = OmsSecurityPolicy()
#     interaction.add(SessionStub(principal))
#     return interaction
# 
# Path: opennode/oms/security/permissions.py
# class Role(ZopeRole):
#     """Oms roles act as permissions"""
# 
#     nick_to_role = {}
#     role_to_nick = {}
# 
#     def __init__(self, name, nick):
#         super(Role, self).__init__(name, name)
#         self.nick = nick
# 
#         if nick:
#             self.nick_to_role[nick] = self
#             self.role_to_nick[self.id] = nick


import logging
import transaction
from zope.authentication.interfaces import IAuthentication
from zope.component import getUtility
from zope.securitypolicy.interfaces import IPrincipalRoleManager
from opennode.oms.model.traversal import traverse1
from opennode.oms.security.interaction import new_interaction
from opennode.oms.security.permissions import Role
from opennode.oms.zodb import db
@db.ro_transact
def preload_acl_file(iterable, filename=''):
    log.info('Preloading ACL rules...')
    lineno = 0
    try:
        for line in iterable:
            specline = line.split('#', 1)[0]
            if not specline:
                continue
            path, permspec = specline.split(':', 1)
            lineno += 1
            preload_acl_line(path, permspec, filename, lineno)
        transaction.commit()
    except NoSuchPermission as e:
        log.error('No such permission: \'%s\'; file: \'%s\' line: %s', e, filename, lineno)
        log.info('Available permissions: %s', Role.nick_to_role.keys())
        transaction.abort()


def preload_acl_line(path, permspec, filename='-', lineno='-'):
    obj = traverse1(path[1:])

    if obj is None:
        log.warning('No such object: \'%s\'; file: \'%s\' line: %s', path, filename, lineno)
        return

    if obj.__transient__:
        log.warning("Transient object %s always inherits permissions from its parent", path)
        return


# Path: opennode/oms/model/traversal.py
# def traverse1(path):
#     """Provides a shortcut for absolute path traversals without
#     needing to pass in the root object.
# 
#     """
# 
#     # Do it here just in case; to avoid circular imports:
#     from opennode.oms.zodb import db
# 
#     oms_root = db.get_root()['oms_root']
#     objs, untraversed_path = traverse_path(oms_root, path)
#     if objs and not untraversed_path:
#         return objs[-1]
#     else:
#         return None
# 
# Path: opennode/oms/security/interaction.py
# def new_interaction(principal):
#     if not IPrincipal.providedBy(principal):
#         auth = getUtility(IAuthentication, context=None)
#         principal = auth.getPrincipal(principal)
# 
#     interaction = OmsSecurityPolicy()
#     interaction.add(SessionStub(principal))
#     return interaction
# 
# Path: opennode/oms/security/permissions.py
# class Role(ZopeRole):
#     """Oms roles act as permissions"""
# 
#     nick_to_role = {}
#     role_to_nick = {}
# 
#     def __init__(self, name, nick):
#         super(Role, self).__init__(name, name)
#         self.nick = nick
# 
#         if nick:
#             self.nick_to_role[nick] = self
#             self.role_to_nick[self.id] = nick


# Path: opennode/oms/security/acl.py
import logging
import transaction
from zope.authentication.interfaces import IAuthentication
from zope.component import getUtility
from zope.securitypolicy.interfaces import IPrincipalRoleManager
from opennode.oms.model.traversal import traverse1
from opennode.oms.security.interaction import new_interaction
from opennode.oms.security.permissions import Role
from opennode.oms.zodb import db

@db.ro_transact
def preload_acl_file(iterable, filename=''):
    log.info('Preloading ACL rules...')
    lineno = 0
    try:
        for line in iterable:
            specline = line.split('#', 1)[0]
            if not specline:
                continue
            path, permspec = specline.split(':', 1)
            lineno += 1
            preload_acl_line(path, permspec, filename, lineno)
        transaction.commit()
    except NoSuchPermission as e:
        log.error('No such permission: \'%s\'; file: \'%s\' line: %s', e, filename, lineno)
        log.info('Available permissions: %s', Role.nick_to_role.keys())
        transaction.abort()


def preload_acl_line(path, permspec, filename='-', lineno='-'):
    obj = traverse1(path[1:])

    if obj is None:
        log.warning('No such object: \'%s\'; file: \'%s\' line: %s', path, filename, lineno)
        return

    if obj.__transient__:
        log.warning("Transient object %s always inherits permissions from its parent", path)
        return


    if permspec in ('inherit', 'noinherit'):