# Path: opennode/oms/endpoint/ssh/cmd/directives.py
# class command(martian.Directive):
#     """Use this directive in a class in order to set its command name.
#     Only classes marked with this directive will be valid commands.
# 
#     """
# 
#     scope = martian.CLASS
#     store = martian.ONCE
#     default = None
# 
# Path: opennode/oms/model/traversal.py
# def canonical_path(item):
#     path = []
#     from opennode.oms.security.authentication import sudo
#     while item:
#         p = sudo(follow_symlinks(sudo(item)))
#         assert p.__name__ is not None, '%s.__name__ is None' % p
#         path.insert(0, p.__name__)
#         item = p.__parent__
#     return '/'.join(path)
# 
# Path: opennode/oms/security/acl.py
# class NoSuchPermission(Exception):
#     pass
# 
# Path: opennode/oms/security/checker.py
# def proxy_factory(value, interaction):
#     if type(value) is Proxy:
#         return value
#     # ignore proxies on deferreds
#     if isinstance(value, Deferred):
#         return value
# 
#     return Proxy(value, _select_checker(value, 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 collections
import functools
import logging
import os
import transaction
from grokcore.component import implements
from twisted.internet import defer
from zope.authentication.interfaces import IAuthentication
from zope.component import getUtility
from zope.securitypolicy.interfaces import IPrincipalRoleManager
from zope.securitypolicy.rolepermission import rolePermissionManager
from zope.securitypolicy.principalrole import principalRoleManager as prinroleG
from opennode.oms.endpoint.ssh.cmd.base import Cmd
from opennode.oms.endpoint.ssh.cmd.directives import command
from opennode.oms.endpoint.ssh.cmdline import ICmdArgumentsSyntax, VirtualConsoleArgumentParser
from opennode.oms.endpoint.ssh.cmdline import MergeListAction
from opennode.oms.model.model.base import IContainer
from opennode.oms.model.model.symlink import follow_symlinks
from opennode.oms.model.traversal import canonical_path
from opennode.oms.security.acl import NoSuchPermission
from opennode.oms.security.checker import proxy_factory
from opennode.oms.security.passwd import add_user, update_passwd, UserManagementError
from opennode.oms.security.permissions import Role
from opennode.oms.security.principals import User, Group, effective_principals
from opennode.oms.zodb import db
        p = VirtualConsoleArgumentParser()
        principals = map(lambda p: p.id, effective_principals(self.user))
        if 'admins' in principals:
            p.add_argument('-u', '--user', help='Check user by name (admins only)')
        return p

    def execute(self, args):
        principals = map(lambda p: p.id, effective_principals(self.user))

        if 'admins' in principals and getattr(args, 'user', None):
            auth = getUtility(IAuthentication, context=None)
            user = auth.getPrincipal(args.user)
            if not user:
                self.write('User not found: %s\n' % args.user)
            else:
                self._print_user(user)
            return

        interaction = self.protocol.interaction

        if not interaction:
            return self.write('user: oms.anonymous\n')

        for participation in interaction.participations:
            user = participation.principal
            self._print_user(user)


class AddUserCmd(Cmd):
    implements(ICmdArgumentsSyntax)

# Path: opennode/oms/endpoint/ssh/cmd/directives.py
# class command(martian.Directive):
#     """Use this directive in a class in order to set its command name.
#     Only classes marked with this directive will be valid commands.
# 
#     """
# 
#     scope = martian.CLASS
#     store = martian.ONCE
#     default = None
# 
# Path: opennode/oms/model/traversal.py
# def canonical_path(item):
#     path = []
#     from opennode.oms.security.authentication import sudo
#     while item:
#         p = sudo(follow_symlinks(sudo(item)))
#         assert p.__name__ is not None, '%s.__name__ is None' % p
#         path.insert(0, p.__name__)
#         item = p.__parent__
#     return '/'.join(path)
# 
# Path: opennode/oms/security/acl.py
# class NoSuchPermission(Exception):
#     pass
# 
# Path: opennode/oms/security/checker.py
# def proxy_factory(value, interaction):
#     if type(value) is Proxy:
#         return value
#     # ignore proxies on deferreds
#     if isinstance(value, Deferred):
#         return value
# 
#     return Proxy(value, _select_checker(value, 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/endpoint/ssh/cmd/security.py
import collections
import functools
import logging
import os
import transaction
from grokcore.component import implements
from twisted.internet import defer
from zope.authentication.interfaces import IAuthentication
from zope.component import getUtility
from zope.securitypolicy.interfaces import IPrincipalRoleManager
from zope.securitypolicy.rolepermission import rolePermissionManager
from zope.securitypolicy.principalrole import principalRoleManager as prinroleG
from opennode.oms.endpoint.ssh.cmd.base import Cmd
from opennode.oms.endpoint.ssh.cmd.directives import command
from opennode.oms.endpoint.ssh.cmdline import ICmdArgumentsSyntax, VirtualConsoleArgumentParser
from opennode.oms.endpoint.ssh.cmdline import MergeListAction
from opennode.oms.model.model.base import IContainer
from opennode.oms.model.model.symlink import follow_symlinks
from opennode.oms.model.traversal import canonical_path
from opennode.oms.security.acl import NoSuchPermission
from opennode.oms.security.checker import proxy_factory
from opennode.oms.security.passwd import add_user, update_passwd, UserManagementError
from opennode.oms.security.permissions import Role
from opennode.oms.security.principals import User, Group, effective_principals
from opennode.oms.zodb import db

        p = VirtualConsoleArgumentParser()
        principals = map(lambda p: p.id, effective_principals(self.user))
        if 'admins' in principals:
            p.add_argument('-u', '--user', help='Check user by name (admins only)')
        return p

    def execute(self, args):
        principals = map(lambda p: p.id, effective_principals(self.user))

        if 'admins' in principals and getattr(args, 'user', None):
            auth = getUtility(IAuthentication, context=None)
            user = auth.getPrincipal(args.user)
            if not user:
                self.write('User not found: %s\n' % args.user)
            else:
                self._print_user(user)
            return

        interaction = self.protocol.interaction

        if not interaction:
            return self.write('user: oms.anonymous\n')

        for participation in interaction.participations:
            user = participation.principal
            self._print_user(user)


class AddUserCmd(Cmd):
    implements(ICmdArgumentsSyntax)

    command('adduser')