|
1 # |
|
2 # -*- encoding: utf-8 -*- |
|
3 # |
|
4 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net> |
|
5 # All Rights Reserved. |
|
6 # |
|
7 # This software is subject to the provisions of the Zope Public License, |
|
8 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
|
9 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
|
10 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
11 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
|
12 # FOR A PARTICULAR PURPOSE. |
|
13 # |
|
14 |
|
15 __docformat__ = 'restructuredtext' |
|
16 |
|
17 |
|
18 # import standard library |
|
19 |
|
20 # import interfaces |
|
21 from pyams_content.root.interfaces import ISiteRootRoles, ISiteRootConfiguration, ISiteRoot, \ |
|
22 ISiteRootToolsConfiguration, ISiteRootBackOfficeConfiguration |
|
23 from pyams_portal.interfaces import IPortalContext |
|
24 from pyams_security.interfaces import IDefaultProtectionPolicy, IGrantedRoleEvent, ISecurityManager |
|
25 from pyams_skin.interfaces.configuration import IStaticConfiguration |
|
26 from pyams_utils.interfaces.site import IConfigurationFactory, IBackOfficeConfigurationFactory |
|
27 from zope.annotation.interfaces import IAnnotations |
|
28 |
|
29 # import packages |
|
30 from persistent import Persistent |
|
31 from pyams_security.property import RolePrincipalsFieldProperty |
|
32 from pyams_security.security import ProtectedObject |
|
33 from pyams_skin.configuration import Configuration, StaticConfiguration, BackOfficeConfiguration |
|
34 from pyams_utils.adapter import adapter_config |
|
35 from pyams_utils.registry import get_utility |
|
36 from pyams_utils.site import BaseSiteRoot |
|
37 from pyams_utils.traversing import get_parent |
|
38 from pyramid.events import subscriber |
|
39 from zope.interface import implementer, Interface |
|
40 |
|
41 |
|
42 @adapter_config(context=(ISiteRoot, Interface, Interface), provides=IStaticConfiguration) |
|
43 class SiteRootStaticConfiguration(StaticConfiguration): |
|
44 """Site root static configuration""" |
|
45 |
|
46 application_package = 'pyams_content' |
|
47 application_name = 'PyAMS CMS' |
|
48 |
|
49 include_reload_button = False |
|
50 |
|
51 |
|
52 @implementer(IDefaultProtectionPolicy, ISiteRoot, ISiteRootRoles, IPortalContext) |
|
53 class SiteRoot(ProtectedObject, BaseSiteRoot): |
|
54 """Main site root""" |
|
55 |
|
56 __roles__ = ('system.Manager', 'pyams.Webmaster', 'pyams.Operator') |
|
57 |
|
58 roles_interface = ISiteRootRoles |
|
59 |
|
60 managers = RolePrincipalsFieldProperty(ISiteRootRoles['managers']) |
|
61 webmasters = RolePrincipalsFieldProperty(ISiteRootRoles['webmasters']) |
|
62 operators = RolePrincipalsFieldProperty(ISiteRootRoles['operators']) |
|
63 |
|
64 |
|
65 @implementer(ISiteRootConfiguration) |
|
66 class SiteRootConfiguration(Configuration): |
|
67 """Site root configuration""" |
|
68 |
|
69 |
|
70 @adapter_config(context=ISiteRoot, provides=IConfigurationFactory) |
|
71 def SiteRootConfigurationFactory(context): |
|
72 return SiteRootConfiguration |
|
73 |
|
74 |
|
75 @implementer(ISiteRootBackOfficeConfiguration) |
|
76 class SiteRootBackOfficeConfiguration(BackOfficeConfiguration): |
|
77 """Site root back-office configuration""" |
|
78 |
|
79 |
|
80 @adapter_config(context=ISiteRoot, provides=IBackOfficeConfigurationFactory) |
|
81 def SiteRootBackOfficeConfigurationFactory(context): |
|
82 return SiteRootBackOfficeConfiguration |
|
83 |
|
84 |
|
85 @subscriber(IGrantedRoleEvent) |
|
86 def handle_granted_role(event): |
|
87 """Add principals to operators group when a role is granted""" |
|
88 role_id = event.role_id |
|
89 if (role_id == 'pyams.Operator') or (not role_id.startswith('pyams.')): |
|
90 return |
|
91 root = get_parent(event.object, ISiteRoot) |
|
92 if not root.operators: |
|
93 return |
|
94 security = get_utility(ISecurityManager) |
|
95 for principal_id in root.operators: |
|
96 if not principal_id: |
|
97 continue |
|
98 group = security.get_principal(principal_id, info=False) |
|
99 if event.principal_id not in group.principals: |
|
100 group.principals = group.principals | {event.principal_id} |
|
101 |
|
102 |
|
103 # |
|
104 # Tools configuration |
|
105 # |
|
106 |
|
107 @implementer(ISiteRootToolsConfiguration) |
|
108 class SiteRootToolsConfiguration(Persistent): |
|
109 """Site root tools configuration""" |
|
110 |
|
111 tools_name = None |
|
112 news_tool_name = None |
|
113 |
|
114 |
|
115 SITEROOT_TOOLS_CONFIGURATION_KEY = 'pyams_config.tools.configuration' |
|
116 |
|
117 |
|
118 @adapter_config(context=ISiteRoot, provides=ISiteRootToolsConfiguration) |
|
119 def site_root_tools_configuration_factory(context): |
|
120 """Site root tools configuration factory""" |
|
121 annotations = IAnnotations(context) |
|
122 config = annotations.get(SITEROOT_TOOLS_CONFIGURATION_KEY) |
|
123 if config is None: |
|
124 config = annotations[SITEROOT_TOOLS_CONFIGURATION_KEY] = SiteRootToolsConfiguration() |
|
125 return config |