Show More
@@ -1,225 +1,223 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2016-2018 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import os |
|
22 | 22 | |
|
23 | 23 | import deform |
|
24 | 24 | import colander |
|
25 | 25 | |
|
26 | 26 | from rhodecode.translation import _ |
|
27 | 27 | from rhodecode.model.db import Repository, RepoGroup |
|
28 | 28 | from rhodecode.model.validation_schema import validators, preparers |
|
29 | 29 | |
|
30 | 30 | |
|
31 | 31 | def integration_scope_choices(permissions): |
|
32 | 32 | """ |
|
33 | 33 | Return list of (value, label) choices for integration scopes depending on |
|
34 | 34 | the permissions |
|
35 | 35 | """ |
|
36 | 36 | result = [('', _('Pick a scope:'))] |
|
37 | 37 | if 'hg.admin' in permissions['global']: |
|
38 | 38 | result.extend([ |
|
39 | 39 | ('global', _('Global (all repositories)')), |
|
40 | 40 | ('root-repos', _('Top level repositories only')), |
|
41 | 41 | ]) |
|
42 | 42 | |
|
43 | 43 | repo_choices = [ |
|
44 | 44 | ('repo:%s' % repo_name, '/' + repo_name) |
|
45 | 45 | for repo_name, repo_perm |
|
46 | 46 | in permissions['repositories'].items() |
|
47 | 47 | if repo_perm == 'repository.admin' |
|
48 | 48 | ] |
|
49 | 49 | repogroup_choices = [ |
|
50 | 50 | ('repogroup:%s' % repo_group_name, '/' + repo_group_name + '/ (child repos only)') |
|
51 | 51 | for repo_group_name, repo_group_perm |
|
52 | 52 | in permissions['repositories_groups'].items() |
|
53 | 53 | if repo_group_perm == 'group.admin' |
|
54 | 54 | ] |
|
55 | 55 | repogroup_recursive_choices = [ |
|
56 | 56 | ('repogroup-recursive:%s' % repo_group_name, '/' + repo_group_name + '/ (recursive)') |
|
57 | 57 | for repo_group_name, repo_group_perm |
|
58 | 58 | in permissions['repositories_groups'].items() |
|
59 | 59 | if repo_group_perm == 'group.admin' |
|
60 | 60 | ] |
|
61 | 61 | result.extend( |
|
62 | 62 | sorted(repogroup_recursive_choices + repogroup_choices + repo_choices, |
|
63 | 63 | key=lambda (choice, label): choice.split(':', 1)[1] |
|
64 | 64 | ) |
|
65 | 65 | ) |
|
66 | 66 | return result |
|
67 | 67 | |
|
68 | 68 | |
|
69 | 69 | @colander.deferred |
|
70 | 70 | def deferred_integration_scopes_validator(node, kw): |
|
71 | 71 | perms = kw.get('permissions') |
|
72 | 72 | def _scope_validator(_node, scope): |
|
73 | 73 | is_super_admin = 'hg.admin' in perms['global'] |
|
74 | 74 | |
|
75 | 75 | if scope.get('repo'): |
|
76 | 76 | if (is_super_admin or perms['repositories'].get( |
|
77 | 77 | scope['repo'].repo_name) == 'repository.admin'): |
|
78 | 78 | return True |
|
79 | 79 | msg = _('Only repo admins can create integrations') |
|
80 | 80 | raise colander.Invalid(_node, msg) |
|
81 | 81 | elif scope.get('repo_group'): |
|
82 | 82 | if (is_super_admin or perms['repositories_groups'].get( |
|
83 | 83 | scope['repo_group'].group_name) == 'group.admin'): |
|
84 | 84 | return True |
|
85 | 85 | |
|
86 | 86 | msg = _('Only repogroup admins can create integrations') |
|
87 | 87 | raise colander.Invalid(_node, msg) |
|
88 | 88 | else: |
|
89 | 89 | if is_super_admin: |
|
90 | 90 | return True |
|
91 | 91 | msg = _('Only superadmins can create global integrations') |
|
92 | 92 | raise colander.Invalid(_node, msg) |
|
93 | 93 | |
|
94 | 94 | return _scope_validator |
|
95 | 95 | |
|
96 | 96 | |
|
97 | 97 | @colander.deferred |
|
98 | 98 | def deferred_integration_scopes_widget(node, kw): |
|
99 | 99 | if kw.get('no_scope'): |
|
100 | 100 | return deform.widget.TextInputWidget(readonly=True) |
|
101 | 101 | |
|
102 | 102 | choices = integration_scope_choices(kw.get('permissions')) |
|
103 | 103 | widget = deform.widget.Select2Widget(values=choices) |
|
104 | 104 | return widget |
|
105 | 105 | |
|
106 | 106 | |
|
107 | 107 | class IntegrationScopeType(colander.SchemaType): |
|
108 | 108 | def serialize(self, node, appstruct): |
|
109 | 109 | if appstruct is colander.null: |
|
110 | 110 | return colander.null |
|
111 | 111 | |
|
112 | 112 | if appstruct.get('repo'): |
|
113 | 113 | return 'repo:%s' % appstruct['repo'].repo_name |
|
114 | 114 | elif appstruct.get('repo_group'): |
|
115 | 115 | if appstruct.get('child_repos_only'): |
|
116 | 116 | return 'repogroup:%s' % appstruct['repo_group'].group_name |
|
117 | 117 | else: |
|
118 | 118 | return 'repogroup-recursive:%s' % ( |
|
119 | 119 | appstruct['repo_group'].group_name) |
|
120 | 120 | else: |
|
121 | 121 | if appstruct.get('child_repos_only'): |
|
122 | 122 | return 'root-repos' |
|
123 | 123 | else: |
|
124 | 124 | return 'global' |
|
125 | 125 | |
|
126 | raise colander.Invalid(node, '%r is not a valid scope' % appstruct) | |
|
127 | ||
|
128 | 126 | def deserialize(self, node, cstruct): |
|
129 | 127 | if cstruct is colander.null: |
|
130 | 128 | return colander.null |
|
131 | 129 | |
|
132 | 130 | if cstruct.startswith('repo:'): |
|
133 | 131 | repo = Repository.get_by_repo_name(cstruct.split(':')[1]) |
|
134 | 132 | if repo: |
|
135 | 133 | return { |
|
136 | 134 | 'repo': repo, |
|
137 | 135 | 'repo_group': None, |
|
138 | 136 | 'child_repos_only': False, |
|
139 | 137 | } |
|
140 | 138 | elif cstruct.startswith('repogroup-recursive:'): |
|
141 | 139 | repo_group = RepoGroup.get_by_group_name(cstruct.split(':')[1]) |
|
142 | 140 | if repo_group: |
|
143 | 141 | return { |
|
144 | 142 | 'repo': None, |
|
145 | 143 | 'repo_group': repo_group, |
|
146 | 144 | 'child_repos_only': False |
|
147 | 145 | } |
|
148 | 146 | elif cstruct.startswith('repogroup:'): |
|
149 | 147 | repo_group = RepoGroup.get_by_group_name(cstruct.split(':')[1]) |
|
150 | 148 | if repo_group: |
|
151 | 149 | return { |
|
152 | 150 | 'repo': None, |
|
153 | 151 | 'repo_group': repo_group, |
|
154 | 152 | 'child_repos_only': True |
|
155 | 153 | } |
|
156 | 154 | elif cstruct == 'global': |
|
157 | 155 | return { |
|
158 | 156 | 'repo': None, |
|
159 | 157 | 'repo_group': None, |
|
160 | 158 | 'child_repos_only': False |
|
161 | 159 | } |
|
162 | 160 | elif cstruct == 'root-repos': |
|
163 | 161 | return { |
|
164 | 162 | 'repo': None, |
|
165 | 163 | 'repo_group': None, |
|
166 | 164 | 'child_repos_only': True |
|
167 | 165 | } |
|
168 | 166 | |
|
169 | 167 | raise colander.Invalid(node, '%r is not a valid scope' % cstruct) |
|
170 | 168 | |
|
171 | 169 | |
|
172 | 170 | class IntegrationOptionsSchemaBase(colander.MappingSchema): |
|
173 | 171 | |
|
174 | 172 | name = colander.SchemaNode( |
|
175 | 173 | colander.String(), |
|
176 | 174 | description=_('Short name for this integration.'), |
|
177 | 175 | missing=colander.required, |
|
178 | 176 | title=_('Integration name'), |
|
179 | 177 | ) |
|
180 | 178 | |
|
181 | 179 | scope = colander.SchemaNode( |
|
182 | 180 | IntegrationScopeType(), |
|
183 | 181 | description=_( |
|
184 | 182 | 'Scope of the integration. Recursive means the integration ' |
|
185 | 183 | ' runs on all repos of that group and children recursively.'), |
|
186 | 184 | title=_('Integration scope'), |
|
187 | 185 | validator=deferred_integration_scopes_validator, |
|
188 | 186 | widget=deferred_integration_scopes_widget, |
|
189 | 187 | missing=colander.required, |
|
190 | 188 | ) |
|
191 | 189 | |
|
192 | 190 | enabled = colander.SchemaNode( |
|
193 | 191 | colander.Bool(), |
|
194 | 192 | default=True, |
|
195 | 193 | description=_('Enable or disable this integration.'), |
|
196 | 194 | missing=False, |
|
197 | 195 | title=_('Enabled'), |
|
198 | 196 | ) |
|
199 | 197 | |
|
200 | 198 | |
|
201 | 199 | def make_integration_schema(IntegrationType, settings=None): |
|
202 | 200 | """ |
|
203 | 201 | Return a colander schema for an integration type |
|
204 | 202 | |
|
205 | 203 | :param IntegrationType: the integration type class |
|
206 | 204 | :param settings: existing integration settings dict (optional) |
|
207 | 205 | """ |
|
208 | 206 | |
|
209 | 207 | settings = settings or {} |
|
210 | 208 | settings_schema = IntegrationType(settings=settings).settings_schema() |
|
211 | 209 | |
|
212 | 210 | class IntegrationSchema(colander.Schema): |
|
213 | 211 | options = IntegrationOptionsSchemaBase() |
|
214 | 212 | |
|
215 | 213 | schema = IntegrationSchema() |
|
216 | 214 | schema['options'].title = _('General integration options') |
|
217 | 215 | |
|
218 | 216 | settings_schema.name = 'settings' |
|
219 | 217 | settings_schema.title = _('{integration_type} settings').format( |
|
220 | 218 | integration_type=IntegrationType.display_name) |
|
221 | 219 | schema.add(settings_schema) |
|
222 | 220 | |
|
223 | 221 | return schema |
|
224 | 222 | |
|
225 | 223 |
General Comments 0
You need to be logged in to leave comments.
Login now