Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import grok
- import dolmen.content as content
- import dolmen.forms.crud as crud
- from zope.i18nmessageid import MessageFactory
- import zope.schema
- import megrok.z3cform.base as z3cform
- from zope.interface import Interface
- from zope.container.constraints import contains
- from aeon.consolelog.consoleLog import *
- from zope.app.authentication import PluggableAuthentication as PAU
- from zope.app.security.interfaces import IAuthentication
- from dolmen.app.authentication import initialize_pau
- from zope.interface import implements
- from zope.intid.interfaces import IIntIds
- from dolmen.relations import RelationCatalog, ICatalog, values, any, RelationValue, RelationsContainer
- from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
- from zope.schema.interfaces import IVocabularyFactory, ISource
- from zope.component import getUtility
- from zope.pluggableauth.interfaces import IPrincipalInfo
- from zope.pluggableauth.interfaces import IAuthenticatorPlugin
- import os
- import glob
- import aeon
- import dolmen
- from aeon.contenttype.base import IBase, Base
- from zope.app.component.hooks import getSite
- from zope.component import getSiteManager
- from zope.component import getSiteManager, queryUtility
- from dolmen.relations import RelationCatalog, ICatalog as IRelationCatalog
- from zope.catalog.interfaces import ICatalog
- from zope.authentication.interfaces import IAuthentication
- import BTrees
- _ = MessageFactory('dolmen')
- BASIC_MEMBERSHIP = u'dolmen.member.default'
- class ITypedRelation(Interface):
- """A typed relation
- """
- type = zope.schema.TextLine(
- title = _(u"type", default=u"Type of the relation"),
- required = True,
- default = u"")
- class IGroupDirectory(Interface):
- contains('.IGroup')
- prefix = zope.schema.TextLine(
- title=u'Prefix of the group id',
- default=u"group.",
- required=True)
- class IGroupsFolder(content.IBaseContent):
- contains('.IGroup')
- class GroupsFolder(content.Container):
- content.nofactory()
- content.name(_(u'GroupsFolder'))
- content.schema(IGroupsFolder)
- content.require('dolmen.security.ManageUsers')
- implements(IGroupDirectory)
- prefix = u"dolmen.group."
- def addRelationIndexes(self):
- rcat = queryUtility(IRelationCatalog)
- if rcat is None:
- self.sm.registerUtility(RelationCatalog(), ICatalog)
- else:
- rcat.addValueIndex(
- IGroupMembership['active'],
- btree = BTrees.family32.OI
- )
- rcat.addValueIndex(
- IGroupMembership['visible'],
- btree = BTrees.family32.OI
- )
- rcat.addValueIndex(
- ITypedRelation['type'],
- btree = BTrees.family32.OI
- )
- rcat.addValueIndex(
- IGroupMembership['login'],
- btree = BTrees.family32.OI
- )
- def addGroupsFolder(self):
- ob = getSite()
- if 'groups' not in ob:
- groups = ob['groups'] = GroupsFolder()
- groups.title = u"Groups"
- sitemanager = getSiteManager(context=ob)
- sitemanager.registerUtility(groups, IGroupDirectory, name=u'', info=u'')
- def addUserAuthenticator(self):
- auth = queryUtility(IAuthentication)
- if auth is not None and 'groups' not in auth.authenticatorPlugins:
- plugins = auth.authenticatorPlugins
- plugins += ('groups',)
- auth.authenticatorPlugins = plugins
- class IGroup(dolmen.authentication.IGroup, content.IBaseContent):
- contains('.IGroupMembership')
- class Group(RelationsContainer, Base):
- content.name(_(u'Group'))
- content.schema(IGroup)
- content.require('dolmen.security.ManageUsers')
- def __init__(self):
- content.Content.__init__(self)
- RelationsContainer.__init__(self)
- def getMembers(self):
- for rel in self.values():
- yield rel.target
- @property
- def members(self):
- return self.getMembersId()
- def getMembersId(self):
- return self.keys()
- def setMembers(self, members):
- raise NotImplementedError
- def removeMember(self, member):
- if not member.id in self:
- raise KeyError
- del self[member.id]
- def addMember(self, member):
- if not member.id in self:
- ids = getUtility(IIntIds)
- source = ids.getId(self)
- target = ids.getId(member)
- relation = GroupMembership(target, source, member.id)
- self[member.id] = relation
- return True
- class IGroupManagement(Interface):
- """Defines the managers of a group.
- """
- managers = zope.schema.Set(
- title=u"Managers of the group",
- required=True,
- value_type=zope.schema.Choice(vocabulary='recipients'))
- class IGroupMembership(IBase):
- """A relation that is a membership link.
- """
- #login = zope.schema.TextLine(
- # title=_(u"Unique id of the member"),
- # required=True)
- login = zope.schema.Choice(
- title=_(u'Member'),
- required = False,
- vocabulary = 'aeon.all.members'
- )
- active = zope.schema.Bool(
- title=_(u"Is the membership active ?"),
- required=True,
- default=True)
- visible = zope.schema.Bool(
- title=_(u"Is the membership visible to others ?"),
- required=True,
- default=True)
- class GroupMembership(RelationValue, Base):
- implements(IGroupMembership, ITypedRelation)
- content.name(_(u'Group Membership'))
- content.schema(IGroupMembership)
- content.require('dolmen.security.ManageUsers')
- type = BASIC_MEMBERSHIP
- def __init__(self, target_id, source_id, login, active=True, visible=True):
- RelationValue.__init__(self, target_id, source_id)
- self.login = login
- self.active = active
- self.visible = visible
- class GroupInfo(grok.Adapter):
- grok.context(dolmen.authentication.IGroup)
- grok.implements(IPrincipalInfo)
- def __init__(self, context):
- self.id = context.id
- self.title = context.title
- self.description = context.title
- self.credentialsPlugin = None
- self.authenticatorPlugin = None
- self.context = context
- @property
- def members(self):
- return self.context.getMembers()
- class GroupAuthenticatorPlugin(grok.GlobalUtility):
- grok.provides(IAuthenticatorPlugin)
- grok.name('groups')
- def getAccount(self, id):
- groups = queryUtility(IGroupDirectory)
- if groups is None:
- return
- return groups.get(id)
- def authenticateCredentials(self, credentials):
- """We do not authenticate.
- """
- pass
- def principalInfo(self, id):
- account = self.getAccount(id)
- if account is None:
- return None
- return IPrincipalInfo(account)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement