Commits

Zhang Huangbin  committed 73ceb90

Fix last commit.

  • Participants
  • Parent commits 825303d

Comments (0)

Files changed (6)

File controllers/ldap/domain.py

     @base.protected
     def POST(self):
         i = web.input()
-        domainName = i.get('domainName', None)
-        cn = i.get('cn', None)
-        result = domainLib.add(domainName=domainName, cn=cn)
-        if result is True:
-            web.seeother('/domains')
+        result = domainLib.add(data=i)
+        if result[0] is True:
+            web.seeother('/domains?msg=CREATE_SUCCESS')
         else:
-            return render.domain_create(msg=result)
+            return render.domain_create(msg=result[1])

File libs/ldaplib/domain.py

         pass
 
     @LDAPDecorators.check_global_admin
-    def add(self, domainName, cn=None):
+    def add(self):
         # msg: {key: value}
         msg = {}
-        domainName = ldaputils.removeSpaceAndDot(web.safestr(domainName)).lower()
-        if domainName == '' or domainName == 'None' or domainName is None:
-            return False
+        self.domain = web.safestr(data.get('domainName', None))
+        if self.domain == 'None' or self.domain == '':
+            return (False, 'EMPTY_DOMAIN')
+        
+        self.domain = ldaputils.removeSpaceAndDot(self.domain.lower())
+        self.dn = ldaputils.convDomainToDN(self.domain)
 
-        dn = ldaputils.convDomainToDN(domainName)
-        ldif = iredldif.ldif_maildomain(domainName, cn)
+        self.cn = data.get('cn', None)
+        ldif = iredldif.ldif_maildomain(domain=self.domain, cn=self.cn,)
 
         # Add domain dn.
         try:
-            result = self.conn.add_s(dn, ldif)
+            result = self.conn.add_s(self.dn, ldif)
         except ldap.ALREADY_EXISTS:
-            msg[domainName] = 'ALREADY_EXISTS'
+            msg[self.domain] = 'ALREADY_EXISTS'
         except ldap.LDAPError, e:
-            msg[domainName] = str(e)
+            msg[self.domain] = str(e)
 
         # Add domain groups.
         if len(attrs.DEFAULT_GROUPS) >= 1:
             for i in attrs.DEFAULT_GROUPS:
                 try:
-                    group_dn = 'ou=' + str(i) + ',' + str(dn)
+                    group_dn = 'ou=' + str(i) + ',' + str(self.dn)
                     group_ldif = iredldif.ldif_group(str(i))
 
                     self.conn.add_s(group_dn, group_ldif)
             pass
 
         if len(msg) == 0:
-            return True
+            return (True, 'SUCCESS')
         else:
-            return msg
+            return (False, msg)
 
     # List all domain admins.
     def admins(self, domain):

File libs/ldaplib/iredldif.py

 cfg = web.iredconfig
 
 # Define and return LDIF structure of domain.
-def ldif_maildomain(domainName, cn=None,
+def ldif_maildomain(domain, cn=None,
         mtaTransport=cfg.general.get('mtaTransport', 'dovecot'),
         enabledService=['mail'], ):
+    domain = web.safestr(domain).lower()
     ldif = [
             ('objectCLass',     ['mailDomain']),
-            ('domainName',      [domainName.lower()]),
+            ('domainName',      [domain]),
             ('mtaTransport',    [mtaTransport]),
             ('accountStatus',   ['active']),
             ('enabledService',  enabledService),
             ]
 
-    ldif += ldaputils.getSingleModAttr(attr='cn', value=cn, default=domainName)
+    ldif += ldaputils.getSingleLDIF(attr='cn', value=cn, default=domain,)
 
     return ldif
 
             ('hasMember',       'no'),
             ]
 
-    ldif += ldaputils.getSingleModAttr(attr='cn', value=cn, default=group)
+    ldif += ldaputils.getSingleLDIF(attr='cn', value=cn, default=group)
 
     if desc is not None:
         ldif += [('description', desc.encode('utf-8'))]
             ('domainGlobalAdmin',   [web.safestr(domainGlobalAdmin)]),
             ]
 
-    ldif += ldaputils.getSingleModAttr(attr='cn', value=cn, default=mail.split('@', 1)[0])
+    ldif += ldaputils.getSingleLDIF(attr='cn', value=cn, default=mail.split('@', 1)[0])
 
     return ldif
 
         ('memberOfGroup',       ['all@'+domain]), # Make all users belong to group 'all@domain.ltd'.
         ]
 
-    ldif += ldaputils.getSingleModAttr(attr='cn', value=cn, default=username)
+    ldif += ldaputils.getSingleLDIF(attr='cn', value=cn, default=username)
 
     return ldif

File libs/ldaplib/ldaputils.py

 
     return pw
 
+def getSingleLDIF(attr, value, default='None'):
+    if value is not None and value != u'' and value != '':
+        ldif = [(attr, [value.encode('utf-8')])]
+    else:
+        ldif = [(attr, [default.encode('utf-8')])]
+
+    return ldif
+
 def getSingleModAttr(attr, value, default='None'):
     if value is not None and value != '' and value != u'':
         mod_attrs = [ ( ldap.MOD_REPLACE, attr, value.encode('utf-8') ) ]

File templates/default/ldap/domains.html

 {# Show system message #}
 {% if msg is defined and msg is not sameas none %}
     <div class="ct-box info-box">
-        <div class="error">
-        {% if msg == 'SIZELIMIT_EXCEEDED' %}
-            {{ _('Server-side size limit exceeded.') }}
-        {% elif msg == 'PERMISSION_DENIED' %}
-            {{ _('Permission denied.') }}
-        {% elif msg == 'NO_SUCH_OBJECT' %}
-            {{ _('No such domain.') }}
+        {% if msg == 'CREATE_SUCCESS' %}
+            <div class="success">{{ _('Domain created success.') }}</div>
         {% else %}
-            {# Catch-all #}
-            {{ msg }}
+            <div class="error">
+            {% if msg == 'SIZELIMIT_EXCEEDED' %}
+                {{ _('Server-side size limit exceeded.') }}
+            {% elif msg == 'PERMISSION_DENIED' %}
+                {{ _('Permission denied.') }}
+            {% elif msg == 'NO_SUCH_OBJECT' %}
+                {{ _('No such domain.') }}
+            {% else %}
+                {# Catch-all #}
+                {{ msg }}
+            {% endif %}
+            </div>
         {% endif %}
-        </div>
     </div>
 {% endif %}
 

File templates/default/ldap/user_create.html

                 <span class="fld-input"><input type="text" size="25" name="username" value="{{username}}" />@
                     <select name="domainName">
                         {% for d in allDomains %}
-                            <option value="{{d[1].domainName[0]}}" {% if d[1].domainName[0] == domainName %}selected{%endif%}>{{d[1].domainName[0]}}</option>
+                            <option value="{{d[1].domainName[0]}}" {% if d[1].domainName[0] == domain %}selected{%endif%}>{{d[1].domainName[0]}}</option>
                         {% endfor %}
                     </select>
                 </span>