1 from zope.interface import implements
2 from twisted.internet import defer
3 from twisted.python import plugin
4 from webut.skin import iskin
5
6 from ldaptor.protocols.ldap import ldapsyntax, distinguishedname
7 from ldaptor.protocols.ldap import fetchschema
8 from ldaptor import numberalloc, interfaces
9 from ldaptor.apps.webui import iwebui
10 from ldaptor.apps.webui.uriquote import uriQuote, uriUnquote
11 from ldaptor.apps.webui.i18n import _
12 from ldaptor.apps.webui import i18n
13
14 import os
15 from nevow import rend, inevow, loaders, url, tags
16 from formless import annotate, webform, iformless, configurable
17
26
28 name = name.upper()
29 attrtype = None
30 for at in attributeTypes:
31 for atName in at.name:
32 if atName.upper()==name:
33 attrtype = at
34 return attrtype
35
37 """LDAP Attribute type not known"""
38
42
44 return self.__doc__ + ': ' + repr(self.name)
45
75
76 formFields = [
77 annotate.Argument('ctx',
78 annotate.Context()),
79 annotate.Argument('request',
80 annotate.Request()),
81 annotate.Argument('structuralObjectClass',
82 annotate.Choice(label=_('Object type to create'),
83 choices=[KludgeNevowChoice(x) for x in structural],
84 stringify=strObjectClass)),
85 ]
86 for oc in auxiliary:
87 formFields.append(annotate.Argument(
88 'auxiliary_%s' % oc.name[0],
89 annotate.Boolean(label=oc.name[0],
90 description=oc.desc or '')))
91 self.formFields = formFields
92
95
102
117
308
334
335 -class ReallyAddPage(rend.Page):
336 implements(iskin.ISkinnable)
337
338 title = _('Ldaptor Add Page')
339
340 addSlash = True
341
342 docFactory = loaders.xmlfile(
343 'add-really.xhtml',
344 templateDir=os.path.split(os.path.abspath(__file__))[0])
345
347 u=url.URL.fromContext(ctx)
348 u=u.parentdir().parentdir().parentdir().clear()
349 l=[]
350 l.append(tags.a(href=u.sibling("search"))[_("Search")])
351 l.append(tags.a(href=u.sibling("add"))[_("add new entry")])
352
353 return l
354
356 return webform.renderForms()
357
358 - def render_passthrough(self, context, data):
359 return context.tag.clear()[data]
360
361 - def render_status(self, context, data):
362 try:
363 obj = context.locate(inevow.IHand)
364 except KeyError:
365 return context.tag.clear()
366
367 e = interfaces.ILDAPEntry(obj, None)
368 if e is None:
369 return context.tag.clear()[obj]
370
371 u=url.URL.fromContext(context)
372 u=u.parentdir().parentdir().parentdir().clear()
373
374 return context.tag.clear()[
375 _("Added "),
376 tags.a(href=u.parentdir().child(e.dn).child("search"))[e.dn],
377 _(" successfully. "),
378
379
380 '[',
381 tags.a(href=u.sibling('edit').child(uriQuote(e.dn)))[_('edit')],
382 '|',
383 tags.a(href=u.sibling('move').child(uriQuote(e.dn)))[_('move')],
384 '|',
385 tags.a(href=u.sibling('delete').child(uriQuote(e.dn)))[_('delete')],
386 '|',
387 tags.a(href=u.sibling('change_password').child(uriQuote(e.dn)))[_('change password')],
388 ']',
389 ]
390
391 render_i18n = i18n.render()
392
393 -class SmartObjectAddPage(ReallyAddPage):
394 - def __init__(self, smartObject):
395 super(SmartObjectAddPage, self).__init__()
396 self.smartObject = smartObject
397
398 - def configurable_(self, context):
399 return self.smartObject
400
401 - def render_overview(self, ctx, data):
402 return tags.invisible()
403
404 -class ManualAddPage(ReallyAddPage):
405 - def __init__(self,
406 structuralObjectClass,
407 auxiliaryObjectClasses,
408 attributeTypes,
409 objectClasses):
410 super(ManualAddPage, self).__init__()
411 self.structuralObjectClass = structuralObjectClass
412 self.auxiliaryObjectClasses = auxiliaryObjectClasses
413 self.attributeTypes = attributeTypes
414 self.objectClasses = objectClasses
415
416 - def configurable_(self, context):
417 a = AddForm(chosenObjectClasses=[self.structuralObjectClass]
418 + self.auxiliaryObjectClasses,
419 attributeTypes=self.attributeTypes,
420 objectClasses=self.objectClasses)
421 return a
422
423 - def render_overview(self, ctx, data):
424 if self.auxiliaryObjectClasses:
425 return ctx.tag.clear()[
426 _('Using objectclasses %s and %s.') % (
427 self.structuralObjectClass.name[0],
428 ', '.join([oc.name[0] for oc in self.auxiliaryObjectClasses]),
429 )]
430 else:
431 return ctx.tag.clear()[
432 _('Using objectclass %s.') % (
433 self.structuralObjectClass.name[0],
434 )]
435
437 if oc.desc is not None:
438 return '%s: %s' % (oc.name[0], oc.desc)
439 else:
440 return '%s' % (oc.name[0],)
441
444 self.plugins = list(pluginNames)
445 self.plugins.sort()
446
449
451 return annotate.MethodBinding(
452 'add',
453 annotate.Method(arguments=[
454 annotate.Argument('context', annotate.Context()),
455 annotate.Argument('smartObjectClass', annotate.Choice(choicesAttribute='plugins')),
456 ],
457 label=_('Add')),
458 action=_('Add'))
459
460 - def add(self, context, smartObjectClass):
461 request = context.locate(inevow.IRequest)
462 u = url.URL.fromContext(context)
463 return u.child('smart').child(smartObjectClass)
464
465 -class AddPage(rend.Page):
466 implements(iskin.ISkinnable)
467
468 title = _('Ldaptor Add Page')
469
470 addSlash = True
471
472 docFactory = loaders.xmlfile(
473 'add.xhtml',
474 templateDir=os.path.split(os.path.abspath(__file__))[0])
475
476 - def __init__(self, attributeTypes, objectClasses):
477 super(AddPage, self).__init__()
478 self.attributeTypes = attributeTypes
479 self.objectClasses = objectClasses
480
481 - def listPlugins(self):
482 for plug in plugin.getPlugIns('ldaptor.apps.webui.smartObject'):
483 yield plug.name
484
485 - def havePlugins(self):
486 for plug in plugin.getPlugIns('ldaptor.apps.webui.smartObject'):
487 return True
488 return False
489
490 - def getPlugin(self, name):
491 for plug in plugin.getPlugIns('ldaptor.apps.webui.smartObject'):
492 if plug.name == name:
493 return plug
494 raise KeyError, name
495
497 u=url.URL.fromContext(ctx)
498 u=u.parentdir().clear()
499 l=[]
500 l.append(tags.a(href=u.sibling("search"))[_("Search")])
501 return l
502
503 - def configurable_objectClass(self, context):
504 return AddOCForm(self.objectClasses)
505
507 return webform.renderForms('objectClass')
508
509 - def configurable_smartObject(self, context):
511
513 if self.havePlugins():
514 return webform.renderForms('smartObject')
515 else:
516 return context.tag.clear()
517
518 - def render_passthrough(self, context, data):
519 return context.tag.clear()[data]
520
521 - def locateChild(self, request, segments):
522 ret = super(AddPage, self).locateChild(request, segments)
523 if ret != rend.NotFound:
524 return ret
525
526 if segments[0] == 'manual':
527 if not segments[1:]:
528 return rend.NotFound
529 path=segments[1]
530 unquoted=uriUnquote(path)
531 objectClasses = unquoted.split('+')
532 assert len(objectClasses) >= 1
533
534 structName=objectClasses[0]
535 structuralObjectClass = mapNameToObjectClass(self.objectClasses,
536 structName)
537 assert structuralObjectClass is not None, \
538 "objectClass %s must have schema"%structName
539
540 auxiliaryObjectClasses = []
541 for auxName in objectClasses[1:]:
542 oc = mapNameToObjectClass(self.objectClasses, auxName)
543 assert oc is not None, "objectClass %s must have schema"%oc
544 auxiliaryObjectClasses.append(oc)
545 r = ManualAddPage(structuralObjectClass=structuralObjectClass,
546 auxiliaryObjectClasses=auxiliaryObjectClasses,
547 attributeTypes=self.attributeTypes,
548 objectClasses=self.objectClasses)
549 return r, segments[2:]
550 elif segments[0] == 'smart':
551 if not segments[1:]:
552 return rend.NotFound
553 name = segments[1]
554 if not name:
555 return rend.NotFound
556 plug = self.getPlugin(name)
557 module = plug.load()
558 add = module.add()
559 r = SmartObjectAddPage(add)
560 return r, segments[2:]
561 else:
562 return rend.NotFound
563
564 render_i18n = i18n.render()
565
574 d.addCallback(cbAddPage)
575 return d
576