forked from indico/indico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.py
183 lines (147 loc) · 6.27 KB
/
settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# This file is part of Indico.
# Copyright (C) 2002 - 2016 European Organization for Nuclear Research (CERN).
#
# Indico is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# Indico is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Indico; if not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals
from functools import wraps
from indico.core.settings import SettingsProxyBase, ACLProxyBase
from indico.core.settings.util import get_setting, get_all_settings, get_setting_acl
from indico.modules.events import Event
from indico.modules.events.models.settings import EventSettingPrincipal, EventSetting
from indico.util.user import iter_acl
def event_or_id(f):
@wraps(f)
def wrapper(self, event, *args, **kwargs):
from MaKaC.conference import Conference
if isinstance(event, (Conference, Event)):
event = event.id
return f(self, int(event), *args, **kwargs)
return wrapper
class EventACLProxy(ACLProxyBase):
"""Proxy class for event-specific ACL settings"""
@event_or_id
def get(self, event, name):
"""Retrieves an ACL setting
:param event: Event (or its ID)
:param name: Setting name
"""
self._check_name(name)
return get_setting_acl(EventSettingPrincipal, self, name, self._cache, event_id=event)
@event_or_id
def set(self, event, name, acl):
"""Replaces an ACL with a new one
:param event: Event (or its ID)
:param name: Setting name
:param acl: A set containing principals (users/groups)
"""
self._check_name(name)
EventSettingPrincipal.set_acl(self.module, name, acl, event_id=event)
self._flush_cache()
@event_or_id
def contains_user(self, event, name, user):
"""Checks if a user is in an ACL.
To pass this check, the user can either be in the ACL itself
or in a group in the ACL.
:param event: Event (or its ID)
:param name: Setting name
:param user: A :class:`.User`
"""
return any(user in principal for principal in iter_acl(self.get(event, name)))
@event_or_id
def add_principal(self, event, name, principal):
"""Adds a principal to an ACL
:param event: Event (or its ID)
:param name: Setting name
:param principal: A :class:`.User` or a :class:`.GroupProxy`
"""
self._check_name(name)
EventSettingPrincipal.add_principal(self.module, name, principal, event_id=event)
self._flush_cache()
@event_or_id
def remove_principal(self, event, name, principal):
"""Removes a principal from an ACL
:param event: Event (or its ID)
:param name: Setting name
:param principal: A :class:`.User` or a :class:`.GroupProxy`
"""
self._check_name(name)
EventSettingPrincipal.remove_principal(self.module, name, principal, event_id=event)
self._flush_cache()
def merge_users(self, target, source):
"""Replaces all ACL user entries for `source` with `target`"""
EventSettingPrincipal.merge_users(self.module, target, source)
self._flush_cache()
class EventSettingsProxy(SettingsProxyBase):
"""Proxy class to access event-specific settings for a certain module"""
acl_proxy_class = EventACLProxy
@property
def query(self):
"""Returns a query object filtering by the proxy's module."""
return EventSetting.find(module=self.module)
@event_or_id
def get_all(self, event, no_defaults=False):
"""Retrieves all settings
:param event: Event (or its ID)
:param no_defaults: Only return existing settings and ignore defaults.
:return: Dict containing the settings
"""
return get_all_settings(EventSetting, EventSettingPrincipal, self, no_defaults, event_id=event)
@event_or_id
def get(self, event, name, default=SettingsProxyBase.default_sentinel):
"""Retrieves the value of a single setting.
:param event: Event (or its ID)
:param name: Setting name
:param default: Default value in case the setting does not exist
:return: The settings's value or the default value
"""
self._check_name(name)
return get_setting(EventSetting, self, name, default, self._cache, event_id=event)
@event_or_id
def set(self, event, name, value):
"""Sets a single setting.
:param event: Event (or its ID)
:param name: Setting name
:param value: Setting value; must be JSON-serializable
"""
self._check_name(name)
EventSetting.set(self.module, name, value, event_id=event)
self._flush_cache()
@event_or_id
def set_multi(self, event, items):
"""Sets multiple settings at once.
:param event: Event (or its ID)
:param items: Dict containing the new settings
"""
self._split_call(items,
lambda x: EventSetting.set_multi(self.module, x, event_id=event),
lambda x: EventSettingPrincipal.set_acl_multi(self.module, x, event_id=event))
self._flush_cache()
@event_or_id
def delete(self, event, *names):
"""Deletes settings.
:param event: Event (or its ID)
:param names: One or more names of settings to delete
"""
self._split_call(names,
lambda name: EventSetting.delete(self.module, *name, event_id=event),
lambda name: EventSettingPrincipal.delete(self.module, *name, event_id=event))
self._flush_cache()
@event_or_id
def delete_all(self, event):
"""Deletes all settings.
:param event: Event (or its ID)
"""
EventSetting.delete_all(self.module, event_id=event)
EventSettingPrincipal.delete_all(self.module, event_id=event)
self._flush_cache()