Code Repositories xandikos / wip/current-user
Add current_user argument. Jelmer Vernooń≥ 2 years ago
9 changed file(s) with 82 addition(s) and 73 deletion(s). Raw diff Collapse all Expand all
4040 in_allprops = False
4141 live = True
4242
43 def get_value(self, resource, el):
43 def get_value(self, current_user, resource, el):
4444 privilege = ET.SubElement(el, '{DAV:}privilege')
4545 # TODO(jelmer): Use something other than all
4646 priv_all = ET.SubElement(privilege, '{DAV:}all')
5656 in_allprops = False
5757 live = True
5858
59 def get_value(self, resource, el):
59 def get_value(self, current_user, resource, el):
6060 owner_href = resource.get_owner()
6161 if owner_href is not None:
6262 el.append(webdav.create_href(owner_href))
131131 in_allprops = False
132132 live = True
133133
134 def get_value(self, resource, el):
134 def get_value(self, current_user, resource, el):
135135 for href in resource.get_calendar_home_set():
136136 el.append(webdav.create_href(href))
137137
145145 name = '{urn:ietf:params:xml:ns:caldav}calendar-description'
146146 resource_type = CALENDAR_RESOURCE_TYPE
147147
148 def get_value(self, resource, el):
148 def get_value(self, current_user, resource, el):
149149 el.text = resource.get_calendar_description()
150150
151151 # TODO(jelmer): allow modification of this property
152 def set_value(self, resource, el):
152 def set_value(self, current_user, resource, el):
153153 raise NotImplementedError
154154
155155
189189 def supported_on(self, resource):
190190 return (resource.get_content_type() == 'text/calendar')
191191
192 def get_value(self, resource, el, requested):
192 def get_value(self, current_user, resource, el, requested):
193193 if len(requested) == 0:
194194 el.text = b''.join(resource.get_body()).decode('utf-8')
195195 else:
498498 name = '{http://apple.com/ns/ical/}calendar-color'
499499 resource_type = CALENDAR_RESOURCE_TYPE
500500
501 def get_value(self, resource, el):
501 def get_value(self, current_user, resource, el):
502502 el.text = resource.get_calendar_color()
503503
504504
515515 in_allprops = False
516516 live = True
517517
518 def get_value(self, resource, el):
518 def get_value(self, current_user, resource, el):
519519 for component in resource.get_supported_calendar_components():
520520 subel = ET.SubElement(el, '{urn:ietf:params:xml:ns:caldav}comp')
521521 subel.set('name', component)
531531 resource_type = CALENDAR_RESOURCE_TYPE
532532 in_allprops = False
533533
534 def get_value(self, resource, el):
534 def get_value(self, current_user, resource, el):
535535 for (content_type, version) in (
536536 resource.get_supported_calendar_data_types()):
537537 subel = ET.SubElement(
550550 resource_type = CALENDAR_RESOURCE_TYPE
551551 in_allprops = False
552552
553 def get_value(self, resource, el):
553 def get_value(self, current_user, resource, el):
554554 el.text = resource.get_calendar_timezone()
555555
556 def set_value(self, resource, el):
556 def set_value(self, current_user, resource, el):
557557 resource.set_calendar_timezone(el.text)
558558
559559
568568 in_allprops = False
569569 live = True
570570
571 def get_value(self, resource, el):
571 def get_value(self, current_user, resource, el):
572572 el.text = resource.get_min_date_time()
573573
574574
583583 in_allprops = False
584584 live = True
585585
586 def get_value(self, resource, el):
586 def get_value(self, current_user, resource, el):
587587 el.text = resource.get_max_date_time()
588588
589589
4545 in_allprops = False
4646 live = True
4747
48 def get_value(self, resource, el):
48 def get_value(self, current_user, resource, el):
4949 for href in resource.get_addressbook_home_set():
5050 el.append(webdav.create_href(href))
5151
6464 def supported_on(self, resource):
6565 return (resource.get_content_type() == 'text/vcard')
6666
67 def get_value(self, resource, el, requested):
67 def get_value(self, current_user, resource, el, requested):
6868 # TODO(jelmer): Support subproperties
6969 # TODO(jelmer): Don't hardcode encoding
7070 el.text = b''.join(resource.get_body()).decode('utf-8')
7979 name = '{%s}addressbook-description' % NAMESPACE
8080 resource_type = ADDRESSBOOK_RESOURCE_TYPE
8181
82 def get_value(self, resource, el):
82 def get_value(self, current_user, resource, el):
8383 el.text = resource.get_addressbook_description()
8484
8585 # TODO(jelmer): allow modification of this property
86 def set_value(self, resource, el):
86 def set_value(self, current_user, resource, el):
8787 raise NotImplementedError
8888
8989
152152 resource_type = '{DAV:}principal'
153153 in_allprops = False
154154
155 def get_value(self, resource, el):
155 def get_value(self, current_user, resource, el):
156156 el.append(webdav.create_href(
157157 resource.get_principal_address()))
158158
168168 in_allprops = False
169169 live = True
170170
171 def get_value(self, resource, el):
171 def get_value(self, current_user, resource, el):
172172 for (content_type, version) in resource.get_supported_address_data_types():
173173 subel = ET.SubElement(el, '{%s}content-type' % NAMESPACE)
174174 subel.set('content-type', content_type)
186186 in_allprops = False
187187 live = True
188188
189 def get_value(self, resource, el):
189 def get_value(self, current_user, resource, el):
190190 el.text = str(resource.get_max_resource_size())
191191
192192
201201 in_allprops = False
202202 live = True
203203
204 def get_value(self, resource, el):
204 def get_value(self, current_user, resource, el):
205205 el.text = str(resource.get_max_image_size())
2424 class SubbedProperty(webdav.Property):
2525 """Property with sub-components that can be queried."""
2626
27 def get_value(self, resource, el, requested):
27 def get_value(self, current_user, resource, el, requested):
2828 """Get the value of a data property.
2929
3030 :param resource: Resource to get value for
3434 raise NotImplementedError(self.get_value)
3535
3636
37 def get_properties_with_data(data_property, resource, properties, requested):
37 def get_properties_with_data(data_property, current_user, resource, properties, requested):
3838 for propreq in list(requested):
3939 if propreq.tag == data_property.name:
4040 ret = ET.Element(propreq.tag)
4141 if data_property.supported_on(resource):
42 data_property.get_value(resource, ret, propreq)
42 data_property.get_value(current_user, resource, ret, propreq)
4343 statuscode = '200 OK'
4444 else:
4545 statuscode = '404 Not Found'
6060 def report(self, environ, body, resources_by_hrefs, properties, base_href, resource,
6161 depth):
6262 # TODO(jelmer): Verify that depth == "0"
63 # TODO(jelmer): Verify that resource is an addressbook
63 current_user = None # TODO
6464 requested = None
6565 hrefs = []
6666 for el in body:
7575 yield webdav.Status(href, '404 Not Found', propstat=[])
7676 else:
7777 propstat = get_properties_with_data(
78 kls.data_property, resource, properties, requested)
78 kls.data_property, current_user, resource, properties, requested)
7979 yield webdav.Status(href, '200 OK', propstat=list(propstat))
3434 resource_type = webdav.PRINCIPAL_RESOURCE_TYPE
3535 live = False
3636
37 def get_value(self, resource, el):
37 def get_value(self, current_user, resource, el):
3838 el.text = resource.get_infit_settings()
3939
40 def set_value(self, resource, el):
40 def set_value(self, current_user, resource, el):
4141 resource.set_infit_settings(el.text)
4242
4343
5151 resource_type = carddav.ADDRESSBOOK_RESOURCE_TYPE
5252 in_allprops = False
5353
54 def get_value(self, resource, el):
54 def get_value(self, current_user, resource, el):
5555 el.text = resource.get_addressbook_color()
5656
57 def set_value(self, resource, el):
57 def set_value(self, current_user, resource, el):
5858 # TODO
5959 raise NotImplementedError
6060
7171 in_allprops = False
7272 live = False
7373
74 def get_value(self, resource, el):
74 def get_value(self, current_user, resource, el):
7575 el.text = resource.get_headervalue()
7676
77 def set_value(self, resource, el):
77 def set_value(self, current_user, resource, el):
7878 # TODO
7979 raise NotImplementedError
5858 resource_type = caldav.CALENDAR_RESOURCE_TYPE
5959 in_allprops = True
6060
61 def get_value(self, resource, el):
61 def get_value(self, current_user, resource, el):
6262 el.append(webdav.create_href(resource.get_schedule_inbox_url()))
6363
6464
7272 resource_type = caldav.CALENDAR_RESOURCE_TYPE
7373 in_allprops = True
7474
75 def get_value(self, resource, el):
75 def get_value(self, current_user, resource, el):
7676 el.append(webdav.create_href(resource.get_schedule_outbox_url()))
7777
7878
8686 resource_type = webdav.PRINCIPAL_RESOURCE_TYPE
8787 in_allprops = False
8888
89 def get_value(self, resource, el):
89 def get_value(self, current_user, resource, el):
9090 for href in resource.get_calendar_user_address_set():
9191 el.append(webdav.create_href(href))
9292
101101 resource_type = caldav.CALENDAR_RESOURCE_TYPE
102102 in_allprops = False
103103
104 def get_value(self, resource, el):
104 def get_value(self, current_user, resource, el):
105105 el.text = resource.get_calendar_user_type()
113113 in_allprops = False
114114 live = True
115115
116 def get_value(self, resource, el):
116 def get_value(self, current_user, resource, el):
117117 el.text = resource.get_sync_token()
4444 super(TimezoneServiceSetProperty, self).__init__()
4545 self._timezone_services = timezone_services
4646
47 def get_value(self, resource, el):
47 def get_value(self, current_user, resource, el):
4848 for timezone_service_href in self._timezone_services:
4949 el.append(webdav.create_href(timezone_service_href))
140140 return wrapper
141141
142142
143 class User(object):
144 """A DAV user."""
145
146 def get_principal(self):
147 raise NotImplementedError(self.get_principal)
148
149
143150 class Property(object):
144151 """Handler for listing, retrieving and updating DAV Properties."""
145152
161168 return (self.resource_type is None or
162169 self.resource_type in resource.resource_types)
163170
164 def is_set(self, resource):
171 def is_set(self, current_user, resource):
165172 """Check if this property is set on a resource."""
166173 if not self.supported_on(resource):
167174 return False
168175 try:
169 self.get_value(resource, ET.Element(self.name))
176 self.get_value(current_user, resource, ET.Element(self.name))
170177 except KeyError:
171178 return False
172179 else:
173180 return True
174181
175 def get_value(self, resource, el):
182 def get_value(self, current_user, resource, el):
176183 """Get property with specified name.
177184
185 :param current_user: Current user object
178186 :param resource: Resource for which to retrieve the property
179187 :param el: Element to populate
180188 :raise KeyError: if this property is not present
181189 """
182190 raise KeyError(self.name)
183191
184 def set_value(self, resource, el):
192 def set_value(self, current_user, resource, el):
185193 """Set property.
186194
195 :param current_user: Current user object
187196 :param resource: Resource to modify
188197 :param el: Element to get new value from (None to remove property)
189198 :raise NotImplementedError: to indicate this property can not be set
201210
202211 live = True
203212
204 def get_value(self, resource, el):
213 def get_value(self, current_user, resource, el):
205214 for rt in resource.resource_types:
206215 ET.SubElement(el, rt)
207216
215224 name = '{DAV:}displayname'
216225 resource_type = None
217226
218 def get_value(self, resource, el):
227 def get_value(self, current_user, resource, el):
219228 el.text = resource.get_displayname()
220229
221230 # TODO(jelmer): allow modification of this property
222 def set_value(self, resource, el):
231 def set_value(self, current_user, resource, el):
223232 raise NotImplementedError
224233
225234
233242 resource_type = None
234243 live = True
235244
236 def get_value(self, resource, el):
245 def get_value(self, current_user, resource, el):
237246 el.text = resource.get_etag()
238247
239248
248257 live = True
249258 in_allprops = True
250259
251 def get_value(self, resource, el):
260 def get_value(self, current_user, resource, el):
252261 # Use rfc1123 date (section 3.3.1 of RFC2616)
253262 el.text = resource.get_last_modified().strftime(
254263 '%a, %d %b %Y %H:%M:%S GMT')
276285 resource_type = None
277286 live = True
278287
279 def get_value(self, resource, el):
288 def get_value(self, current_user, resource, el):
280289 el.text = format_datetime(resource.get_creationdate())
281290
282291
289298 name = '{DAV:}getcontentlanguage'
290299 resource_type = None
291300
292 def get_value(self, resource, el):
301 def get_value(self, current_user, resource, el):
293302 el.text = ', '.join(resource.get_content_language())
294303
295304
302311 name = '{DAV:}getcontentlength'
303312 resource_type = None
304313
305 def get_value(self, resource, el):
314 def get_value(self, current_user, resource, el):
306315 el.text = str(resource.get_content_length())
307316
308317
315324 name = '{DAV:}getcontenttype'
316325 resource_type = None
317326
318 def get_value(self, resource, el):
327 def get_value(self, current_user, resource, el):
319328 el.text = resource.get_content_type()
320329
321330
330339 in_allprops = False
331340 live = True
332341
333 def __init__(self, current_user_principal):
334 super(CurrentUserPrincipalProperty, self).__init__()
335 self.current_user_principal = current_user_principal
336
337 def get_value(self, resource, el):
342 def get_value(self, current_user, resource, el):
338343 """Get property with specified name.
339344
340345 :param name: A property name.
341346 """
342 el.append(create_href(self.current_user_principal))
347 el.append(create_href(current_user.get_principal()))
343348
344349
345350 class PrincipalURLProperty(Property):
349354 in_allprops = True
350355 live = True
351356
352 def get_value(self, resource, el):
357 def get_value(self, current_user, resource, el):
353358 """Get property with specified name.
354359
355360 :param name: A property name.
367372 def __init__(self, reporters):
368373 self._reporters = reporters
369374
370 def get_value(self, resource, el):
375 def get_value(self, current_user, resource, el):
371376 for name, reporter in self._reporters.items():
372377 if reporter.supported_on(resource):
373378 ET.SubElement(el, name)
383388 in_allprops = False
384389 live = True
385390
386 def get_value(self, resource, el):
391 def get_value(self, current_user, resource, el):
387392 el.text = resource.get_ctag()
388393
389394
613618 raise NotImplementedError(self.get_infit_settings)
614619
615620
616 def get_property(resource, properties, name):
621 def get_property(current_user, resource, properties, name):
617622 """Get a single property on a resource.
618623
624 :param current_user: Current user object
619625 :param resource: Resource object
620626 :param properties: Dictionary of properties
621627 :param name: name of property to resolve
634640 try:
635641 if not prop.supported_on(resource):
636642 raise KeyError
637 prop.get_value(resource, ret)
643 prop.get_value(current_user, resource, ret)
638644 except KeyError:
639645 statuscode = '404 Not Found'
640646 else:
642648 return PropStatus(statuscode, responsedescription, ret)
643649
644650
645 def get_properties(resource, properties, requested):
651 def get_properties(current_user, resource, properties, requested):
646652 """Get a set of properties.
647653
654 :param current_user: Current user object
648655 :param resource: Resource object
649656 :param properties: Dictionary of properties
650657 :param requested: XML {DAV:}prop element with properties to look up
651658 :return: Iterator over PropStatus items
652659 """
653660 for propreq in list(requested):
654 yield get_property(resource, properties, propreq.tag)
661 yield get_property(current_user, resource, properties, propreq.tag)
655662
656663
657664 def traverse_resource(base_resource, base_href, depth):
734741
735742 name = '{DAV:}expand-property'
736743
737 def _populate(self, prop_list, resources_by_hrefs, properties, href,
744 def _populate(self, current_user, prop_list, resources_by_hrefs, properties, href,
738745 resource):
739746 """Expand properties for a resource.
740747
749756 for prop in prop_list:
750757 prop_name = prop.get('name')
751758 # FIXME: Resolve prop_name on resource
752 propstat = get_property(resource, properties, prop_name)
759 propstat = get_property(current_user, resource, properties, prop_name)
753760 new_prop = ET.Element(propstat.prop.tag)
754761 child_hrefs = [
755762 read_href_element(prop_child)
778785 @multistatus
779786 def report(self, environ, request_body, resources_by_hrefs, properties, href,
780787 resource, depth):
781 return self._populate(request_body, resources_by_hrefs, properties,
788 current_user = None # FIXME
789 return self._populate(current_user, request_body, resources_by_hrefs, properties,
782790 href, resource)
783791
784792
792800 resource_type = None
793801 live = True
794802
795 def get_value(self, resource, el):
803 def get_value(self, current_user, resource, el):
796804 for (lockscope, locktype) in resource.get_supported_locks():
797805 entry = ET.SubElement(el, '{DAV:}lockentry')
798806 scope_el = ET.SubElement(entry, '{DAV:}lockscope')
811819 resource_type = None
812820 live = True
813821
814 def get_value(self, resource, el):
822 def get_value(self, current_user, resource, el):
815823 for activelock in resource.get_active_locks():
816824 entry = ET.SubElement(el, '{DAV:}activelock')
817825 type_el = ET.SubElement(entry, '{DAV:}locktype')
840848 live = False
841849 in_allprops = False
842850
843 def get_value(self, resource, el):
851 def get_value(self, current_user, resource, el):
844852 el.text = resource.get_comment()
845853
846 def set_value(self, resource, el):
854 def set_value(self, current_user, resource, el):
847855 resource.set_comment(el.text)
848856
849857
11131121 base_resource, self._request_href(environ), depth):
11141122 propstat = []
11151123 for name in self.properties:
1116 ps = get_property(resource, self.properties, name)
1124 ps = get_property(current_user, resource, self.properties,
1125 name)
11171126 if ps.statuscode == '200 OK':
11181127 propstat.append(ps)
11191128 ret.append(Status(href, '200 OK', propstat=propstat))
11751184 elif el.tag == '{DAV:}set':
11761185 newval = propel
11771186 try:
1178 handler.set_value(resource, newval)
1187 handler.set_value(current_user, resource, newval)
11791188 except NotImplementedError:
11801189 # TODO(jelmer): Signal
11811190 # {DAV:}cannot-modify-protected-property error