18 |
18 |
19 # import interfaces |
19 # import interfaces |
20 from pyams_security.interfaces import ISocialUser, IPrincipalInfo, ISocialUsersFolderPlugin, ISecurityManager, \ |
20 from pyams_security.interfaces import ISocialUser, IPrincipalInfo, ISocialUsersFolderPlugin, ISecurityManager, \ |
21 IAuthenticatedPrincipalEvent, ISocialLoginConfiguration, ISocialLoginProviderInfo, ISocialLoginProviderConnection |
21 IAuthenticatedPrincipalEvent, ISocialLoginConfiguration, ISocialLoginProviderInfo, ISocialLoginProviderConnection |
22 from zope.annotation.interfaces import IAnnotations |
22 from zope.annotation.interfaces import IAnnotations |
23 from zope.schema.interfaces import IVocabularyFactory |
|
24 from zope.traversing.interfaces import ITraversable |
23 from zope.traversing.interfaces import ITraversable |
25 |
24 |
26 # import packages |
25 # import packages |
27 from authomatic.providers import oauth1, oauth2 |
26 from authomatic.providers import oauth1, oauth2 |
28 from persistent import Persistent |
27 from persistent import Persistent |
29 from pyams_security.principal import PrincipalInfo |
28 from pyams_security.principal import PrincipalInfo |
30 from pyams_utils.adapter import adapter_config, ContextAdapter |
29 from pyams_utils.adapter import adapter_config, ContextAdapter |
31 from pyams_utils.registry import query_utility |
30 from pyams_utils.registry import query_utility |
32 from pyams_utils.request import check_request |
31 from pyams_utils.request import check_request |
|
32 from pyams_utils.vocabulary import vocabulary_config |
33 from pyramid.events import subscriber |
33 from pyramid.events import subscriber |
34 from zope.container.contained import Contained |
34 from zope.container.contained import Contained |
35 from zope.container.folder import Folder |
35 from zope.container.folder import Folder |
36 from zope.interface import implementer, provider |
36 from zope.interface import implementer |
37 from zope.lifecycleevent import ObjectCreatedEvent |
37 from zope.lifecycleevent import ObjectCreatedEvent |
38 from zope.location import locate |
38 from zope.location import locate |
39 from zope.schema.fieldproperty import FieldProperty |
39 from zope.schema.fieldproperty import FieldProperty |
40 from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm, getVocabularyRegistry |
40 from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm |
41 |
41 |
42 |
42 |
43 @implementer(ISocialUser) |
43 @implementer(ISocialUser) |
44 class SocialUser(Persistent, Contained): |
44 class SocialUser(Persistent, Contained): |
45 """Social user persistent class""" |
45 """Social user persistent class""" |
141 query in (user.name or '').lower() or |
141 query in (user.name or '').lower() or |
142 query in (user.email or '').lower()): |
142 query in (user.email or '').lower()): |
143 yield user |
143 yield user |
144 |
144 |
145 |
145 |
146 @provider(IVocabularyFactory) |
146 @vocabulary_config(name='PyAMS social users folders') |
147 class SocialUsersFolderVocabulary(SimpleVocabulary): |
147 class SocialUsersFolderVocabulary(SimpleVocabulary): |
148 """'PyAMS users folders' vocabulary""" |
148 """'PyAMS users folders' vocabulary""" |
149 |
149 |
150 def __init__(self, *args, **kwargs): |
150 def __init__(self, *args, **kwargs): |
151 terms = [] |
151 terms = [] |
153 if manager is not None: |
153 if manager is not None: |
154 for name, plugin in manager.items(): |
154 for name, plugin in manager.items(): |
155 if ISocialUsersFolderPlugin.providedBy(plugin): |
155 if ISocialUsersFolderPlugin.providedBy(plugin): |
156 terms.append(SimpleTerm(name, title=plugin.title)) |
156 terms.append(SimpleTerm(name, title=plugin.title)) |
157 super(SocialUsersFolderVocabulary, self).__init__(terms) |
157 super(SocialUsersFolderVocabulary, self).__init__(terms) |
158 |
|
159 getVocabularyRegistry().register('PyAMS social users folders', SocialUsersFolderVocabulary) |
|
160 |
158 |
161 |
159 |
162 @subscriber(IAuthenticatedPrincipalEvent, plugin_selector='oauth') |
160 @subscriber(IAuthenticatedPrincipalEvent, plugin_selector='oauth') |
163 def handle_authenticated_principal(event): |
161 def handle_authenticated_principal(event): |
164 """Handle authenticated social principal""" |
162 """Handle authenticated social principal""" |
345 def get_provider_info(provider_name): |
343 def get_provider_info(provider_name): |
346 """Get provider info matching given provider name""" |
344 """Get provider info matching given provider name""" |
347 return PROVIDERS_INFO.get(provider_name) |
345 return PROVIDERS_INFO.get(provider_name) |
348 |
346 |
349 |
347 |
350 @provider(IVocabularyFactory) |
348 @vocabulary_config(name='PyAMS OAuth providers') |
351 class OAuthProvidersVocabulary(SimpleVocabulary): |
349 class OAuthProvidersVocabulary(SimpleVocabulary): |
352 """OAuth providers vocabulary""" |
350 """OAuth providers vocabulary""" |
353 |
351 |
354 def __init__(self, *args, **kwargs): |
352 def __init__(self, *args, **kwargs): |
355 terms = [] |
353 terms = [] |
356 for key, provider in PROVIDERS_INFO.items(): |
354 for key, provider in PROVIDERS_INFO.items(): |
357 terms.append(SimpleTerm(key, title=provider.name)) |
355 terms.append(SimpleTerm(key, title=provider.name)) |
358 terms.sort(key=lambda x: x.title) |
356 terms.sort(key=lambda x: x.title) |
359 super(OAuthProvidersVocabulary, self).__init__(terms) |
357 super(OAuthProvidersVocabulary, self).__init__(terms) |
360 |
|
361 getVocabularyRegistry().register('PyAMS OAuth providers', OAuthProvidersVocabulary) |
|
362 |
358 |
363 |
359 |
364 @implementer(ISocialLoginConfiguration) |
360 @implementer(ISocialLoginConfiguration) |
365 class SocialLoginConfiguration(Folder): |
361 class SocialLoginConfiguration(Folder): |
366 """Social login configuration""" |
362 """Social login configuration""" |