Advertisement
Guest User

Untitled

a guest
May 29th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.97 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import grok
  3. import dolmen.content as content
  4. import dolmen.forms.crud as crud
  5. from zope.i18nmessageid import MessageFactory
  6. import zope.schema
  7. import megrok.z3cform.base as z3cform
  8. from zope.interface import Interface
  9. from zope.container.constraints import contains
  10. from aeon.consolelog.consoleLog import *
  11. from zope.app.authentication import PluggableAuthentication as PAU
  12. from zope.app.security.interfaces import IAuthentication
  13. from dolmen.app.authentication import initialize_pau
  14. from zope.interface import implements
  15. from zope.intid.interfaces import IIntIds
  16. from dolmen.relations import RelationCatalog, ICatalog, values, any, RelationValue, RelationsContainer
  17. from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
  18. from zope.schema.interfaces import IVocabularyFactory, ISource
  19. from zope.component import getUtility
  20. from zope.pluggableauth.interfaces import IPrincipalInfo
  21. from zope.pluggableauth.interfaces import IAuthenticatorPlugin
  22. import os
  23. import glob
  24. import aeon
  25. import dolmen
  26. from aeon.contenttype.base import IBase, Base
  27. from zope.app.component.hooks import getSite
  28. from zope.component import getSiteManager
  29. from zope.component import getSiteManager, queryUtility
  30. from dolmen.relations import RelationCatalog, ICatalog as IRelationCatalog
  31. from zope.catalog.interfaces import ICatalog
  32. from zope.authentication.interfaces import IAuthentication
  33. import BTrees
  34.  
  35.  
  36.  
  37. _ = MessageFactory('dolmen')
  38.  
  39. BASIC_MEMBERSHIP = u'dolmen.member.default'
  40.  
  41. class ITypedRelation(Interface):
  42.     """A typed relation
  43.    """
  44.     type = zope.schema.TextLine(
  45.         title = _(u"type", default=u"Type of the relation"),
  46.         required = True,
  47.         default = u"")
  48.  
  49. class IGroupDirectory(Interface):
  50.     contains('.IGroup')
  51.  
  52.     prefix = zope.schema.TextLine(
  53.         title=u'Prefix of the group id',
  54.         default=u"group.",
  55.         required=True)
  56.  
  57. class IGroupsFolder(content.IBaseContent):
  58.     contains('.IGroup')
  59.  
  60. class GroupsFolder(content.Container):
  61.     content.nofactory()
  62.     content.name(_(u'GroupsFolder'))
  63.     content.schema(IGroupsFolder)
  64.     content.require('dolmen.security.ManageUsers')
  65.     implements(IGroupDirectory)
  66.     prefix = u"dolmen.group."
  67.  
  68.  
  69.     def addRelationIndexes(self):
  70.         rcat = queryUtility(IRelationCatalog)
  71.         if rcat is None:
  72.             self.sm.registerUtility(RelationCatalog(), ICatalog)
  73.         else:
  74.             rcat.addValueIndex(
  75.                 IGroupMembership['active'],
  76.                 btree = BTrees.family32.OI
  77.                 )
  78.             rcat.addValueIndex(
  79.                 IGroupMembership['visible'],
  80.                 btree = BTrees.family32.OI
  81.                 )
  82.             rcat.addValueIndex(
  83.                 ITypedRelation['type'],
  84.                 btree = BTrees.family32.OI
  85.                 )
  86.             rcat.addValueIndex(
  87.                 IGroupMembership['login'],
  88.                 btree = BTrees.family32.OI
  89.                 )
  90.  
  91.  
  92.     def addGroupsFolder(self):
  93.         ob = getSite()
  94.         if 'groups' not in ob:
  95.             groups = ob['groups'] = GroupsFolder()
  96.             groups.title = u"Groups"
  97.             sitemanager = getSiteManager(context=ob)
  98.             sitemanager.registerUtility(groups, IGroupDirectory, name=u'', info=u'')
  99.  
  100.  
  101.     def addUserAuthenticator(self):
  102.         auth = queryUtility(IAuthentication)
  103.         if auth is not None and 'groups' not in auth.authenticatorPlugins:
  104.             plugins = auth.authenticatorPlugins
  105.             plugins += ('groups',)
  106.             auth.authenticatorPlugins = plugins
  107.  
  108.  
  109.  
  110. class IGroup(dolmen.authentication.IGroup, content.IBaseContent):
  111.     contains('.IGroupMembership')
  112.  
  113. class Group(RelationsContainer, Base):
  114.     content.name(_(u'Group'))
  115.     content.schema(IGroup)
  116.     content.require('dolmen.security.ManageUsers')
  117.  
  118.     def __init__(self):
  119.         content.Content.__init__(self)
  120.         RelationsContainer.__init__(self)
  121.  
  122.     def getMembers(self):
  123.         for rel in self.values():
  124.             yield rel.target
  125.  
  126.     @property
  127.     def members(self):
  128.         return self.getMembersId()
  129.  
  130.     def getMembersId(self):
  131.         return self.keys()
  132.  
  133.     def setMembers(self, members):
  134.         raise NotImplementedError
  135.  
  136.     def removeMember(self, member):
  137.         if not member.id in self:
  138.             raise KeyError
  139.         del self[member.id]
  140.  
  141.     def addMember(self, member):
  142.         if not member.id in self:
  143.             ids = getUtility(IIntIds)
  144.             source = ids.getId(self)
  145.             target = ids.getId(member)
  146.             relation = GroupMembership(target, source, member.id)
  147.             self[member.id] = relation
  148.         return True
  149.  
  150.  
  151. class IGroupManagement(Interface):
  152.     """Defines the managers of a group.
  153.    """
  154.     managers = zope.schema.Set(
  155.         title=u"Managers of the group",
  156.         required=True,
  157.         value_type=zope.schema.Choice(vocabulary='recipients'))
  158.  
  159.  
  160. class IGroupMembership(IBase):
  161.     """A relation that is a membership link.
  162.    """
  163.     #login = zope.schema.TextLine(
  164.     #    title=_(u"Unique id of the member"),
  165.     #    required=True)
  166.  
  167.     login = zope.schema.Choice(
  168.         title=_(u'Member'),
  169.         required = False,
  170.         vocabulary = 'aeon.all.members'
  171.         )
  172.  
  173.     active = zope.schema.Bool(
  174.         title=_(u"Is the membership active ?"),
  175.         required=True,
  176.         default=True)
  177.  
  178.     visible = zope.schema.Bool(
  179.         title=_(u"Is the membership visible to others ?"),
  180.         required=True,
  181.         default=True)
  182.  
  183. class GroupMembership(RelationValue, Base):
  184.     implements(IGroupMembership, ITypedRelation)
  185.     content.name(_(u'Group Membership'))
  186.     content.schema(IGroupMembership)
  187.     content.require('dolmen.security.ManageUsers')
  188.  
  189.     type = BASIC_MEMBERSHIP
  190.  
  191.     def __init__(self, target_id, source_id, login, active=True, visible=True):
  192.         RelationValue.__init__(self, target_id, source_id)
  193.         self.login = login
  194.         self.active = active
  195.         self.visible = visible
  196.  
  197.  
  198. class GroupInfo(grok.Adapter):
  199.     grok.context(dolmen.authentication.IGroup)
  200.     grok.implements(IPrincipalInfo)
  201.  
  202.     def __init__(self, context):
  203.         self.id = context.id
  204.         self.title = context.title
  205.         self.description = context.title
  206.         self.credentialsPlugin = None
  207.         self.authenticatorPlugin = None
  208.         self.context = context
  209.  
  210.     @property
  211.     def members(self):
  212.         return self.context.getMembers()
  213.  
  214.  
  215. class GroupAuthenticatorPlugin(grok.GlobalUtility):
  216.     grok.provides(IAuthenticatorPlugin)
  217.     grok.name('groups')
  218.  
  219.     def getAccount(self, id):
  220.         groups = queryUtility(IGroupDirectory)
  221.         if groups is None:
  222.             return
  223.         return groups.get(id)
  224.  
  225.     def authenticateCredentials(self, credentials):
  226.         """We do not authenticate.
  227.        """
  228.         pass
  229.  
  230.     def principalInfo(self, id):
  231.         account = self.getAccount(id)
  232.         if account is None:
  233.             return None
  234.         return IPrincipalInfo(account)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement