Commits

Zhang Huangbin committed 0368631

Implement per-domain default user quota.

Comments (0)

Files changed (3)

controllers/ldap/user.py

             web.seeother('/profile/user/%s/%s?msg=%s' % (self.profile_type, self.mail, self.result[1]))
 
 class create(dbinit):
-    def __init__(self):
-        self.default_quota = cfg.general.get('default_quota', '1024')
-
     @base.protected
     def GET(self, domainName=None):
         if domainName is None:
-            domainName = ''
+            self.domain = ''
         else:
-            domainName = web.safestr(domainName)
+            self.domain = web.safestr(domainName)
 
         return render.user_create(
-                domainName=domainName,
+                domain=self.domain,
                 allDomains=domainLib.list(),
-                default_quota=self.default_quota,
+                default_quota=domainLib.getDomainDefaultUserQuota(self.domain),
                 )
 
     @base.protected
         i = web.input()
 
         # Get domain name, username, cn.
-        domain = i.get('domainName', None)
-        username = i.get('username', None)
+        self.domain = i.get('domainName', None)
+        self.username = i.get('username', None)
 
-        if domain is None or username is None:
+        if self.domain is None or self.username is None:
             return render.user_create(
-                    domainName=domain,
+                    domain=self.domain,
                     allDomains=domainLib.list(),
-                    default_quota=self.default_quota,
+                    default_quota=domainLib.getDomainDefaultUserQuota(self.domain),
                     )
 
         cn = i.get('cn', None)
-        quota = i.get('quota', self.default_quota)
+        quota = i.get('quota', default_quota=domainLib.getDomainDefaultUserQuota(self.domain))
 
         # Check password.
         newpw = web.safestr(i.get('newpw'))
             passwd = ldaputils.generatePasswd(newpw, pwscheme=cfg.general.get('default_pw_scheme', 'SSHA'))
         else:
             return render.user_create(
-                    domainName=domain,
+                    domain=self.domain,
                     allDomains=domainLib.list(),
-                    username=username,
-                    default_quota=self.default_quota,
+                    username=self.username,
+                    default_quota=domainLib.getDomainDefaultUserQuota(self.domain),
                     cn=cn,
                     msg='PW_ERROR',
                     )
 
         ldif = iredldif.ldif_mailuser(
-                domain=web.safestr(domain),
-                username=web.safestr(username),
+                domain=web.safestr(self.domain),
+                username=web.safestr(self.username),
                 cn=cn,
                 passwd=passwd,
                 quota=quota,
                 )
 
-        dn = ldaputils.convEmailToUserDN(username + '@' + domain)
+        dn = ldaputils.convEmailToUserDN(self.username + '@' + domain)
         result = userLib.add(dn, ldif)
         if result is True:
-            web.seeother('/profile/user/general/' + username + '@' + domain + '?msg=CREATE_SUCCESS')
+            web.seeother('/profile/user/general/' + self.username + '@' + self.domain + '?msg=CREATE_SUCCESS')
         elif result == 'ALREADY_EXISTS':
-            web.seeother('/users/' + domain + '?msg=ALREADY_EXISTS')
+            web.seeother('/users/' + self.domain + '?msg=ALREADY_EXISTS')
         else:
-            web.seeother('/users/' + domain)
+            web.seeother('/users/' + self.domain)
 
 class delete(dbinit):
     @base.protected

libs/ldaplib/domain.py

 import web
 from libs.ldaplib import core, attrs, iredldif, ldaputils, deltree
 
+cfg = web.iredconfig
 session = web.config.get('_session')
 LDAPDecorators = core.LDAPDecorators()
 
         except Exception, e:
             return str(e)
 
-    # List all domains.
+    # List all domains under control.
     def list(self, attrs=attrs.DOMAIN_SEARCH_ATTRS):
         allDomains = self.get_all_domains(attrs)
         allDomains.sort()
         return allDomains
 
+    # Get domain default user quota: domainDefaultUserQuota.
+    def getDomainDefaultUserQuota(self, domain):
+        self.domain = web.safestr(domain)
+        self.dn = ldaputils.convDomainToDN(self.domain)
+
+        try:
+            result = self.conn.search_s(
+                    self.dn,
+                    ldap.SCOPE_BASE,
+                    '(domainName=%s)' % self.domain,
+                    ['domainDefaultUserQuota'],
+                    )
+            if result[0][1].has_key('domainDefaultUserQuota'):
+                return result[0][1]['domainDefaultUserQuota'][0]
+            else:
+                return cfg.general.get('default_quota', '1024')
+        except Exception, e:
+            return cfg.general.get('default_quota', '1024')
+
     # Delete domain.
     @LDAPDecorators.check_global_admin
     def delete(self, domain=[]):

templates/default/ldap/user_create.html

-{# allDomains, domainName, default_quota, [username, newpw, confirmpw, ] #}
+{# allDomains, domain, default_quota, [username, newpw, confirmpw, ] #}
 {% extends "layout.html" %}
 
 {% from "macros.html" import display_cn, display_reset_password,
                 <label><span>{{ _('Mail address') }} <em>{{ _('(Required)') }}</em></span></label>
                 <span class="fld-input"><input type="text" size="25" name="username" value="{{username}}" />@
                     <select name="domainName">
-                        {% for domain in allDomains %}
-                            <option value="{{domain[1].domainName[0]}}" {% if domain[1].domainName[0] == domainName %}selected{%endif%}>{{domain[1].domainName[0]}}</option>
+                        {% for d in allDomains %}
+                            <option value="{{d[1].domainName[0]}}" {% if d[1].domainName[0] == domainName %}selected{%endif%}>{{d[1].domainName[0]}}</option>
                         {% endfor %}
                     </select>
                 </span>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.