Commits

Zhang Huangbin  committed 2cde19f

Ability to edit 'cn' of admin profile.

  • Participants
  • Parent commits 8f79c7f

Comments (0)

Files changed (5)

File controllers/ldap/admin.py

     def GET(self, profile_type, mail):
         self.mail = web.safestr(mail)
         self.profile_type = web.safestr(profile_type)
-        i = web.input()
-
-        self.langs = adminLib.get_langs()
 
         if session.get('domainGlobalAdmin') != 'yes' and session.get('username') != self.mail:
             # Don't allow to view/update other admins' profile.
             web.seeother('/profile/admin/%s/%s?msg=PERMISSION_DENIED' % ( self.profile_type, session.get('username') ))
         else:
-            return render.admin_profile(
-                    mail=self.mail,
-                    profile_type=self.profile_type,
-                    cur_lang=self.langs['cur_lang'],
-                    langmaps=self.langs['langmaps'],
-                    min_passwd_length=cfg.general.get('min_passwd_length'),
-                    max_passwd_length=cfg.general.get('max_passwd_length'),
-                    msg=i.get('msg', None),
-                    )
+            i = web.input()
+            if self.profile_type == 'general':
+                # Get admin profile.
+                self.profile = adminLib.profile(self.mail)
+
+                # Get available languages.
+                return render.admin_profile(
+                        mail=self.mail,
+                        profile_type=self.profile_type,
+                        profile=self.profile,
+                        languagemaps=adminLib.getLanguageMaps(),
+                        msg=i.get('msg', None),
+                        )
+            elif self.profile_type == 'password':
+                return render.admin_profile(
+                        mail=self.mail,
+                        profile_type=self.profile_type,
+                        min_passwd_length=cfg.general.get('min_passwd_length'),
+                        max_passwd_length=cfg.general.get('max_passwd_length'),
+                        msg=i.get('msg', None),
+                        )
 
     @base.protected
     def POST(self, profile_type, mail):
         if result[0] is True:
             web.seeother('/profile/admin/%s/%s?msg=SUCCESS' % (self.profile_type, self.mail))
         else:
-            self.langs = adminLib.get_langs()
-            self.cur_lang = self.langs['cur_lang']
-            return render.admin_profile(
-                    mail=self.mail,
-                    profile_type=self.profile_type,
-                    cur_lang=self.cur_lang,
-                    langmaps=self.langs['langmaps'],
-                    min_passwd_length=cfg.general.get('min_passwd_length'),
-                    max_passwd_length=cfg.general.get('max_passwd_length'),
-                    msg=result[1],
-                    )
+            if self.profile_type == 'general':
+                return render.admin_profile(
+                        mail=self.mail,
+                        profile_type=self.profile_type,
+                        languagemaps=adminLib.getLanguageMaps(),
+                        msg=result[1],
+                        )
+            elif self.profile_type == 'password':
+                return render.admin_profile(
+                        mail=self.mail,
+                        profile_type=self.profile_type,
+                        min_passwd_length=cfg.general.get('min_passwd_length'),
+                        max_passwd_length=cfg.general.get('max_passwd_length'),
+                        msg=result[1],
+                        )

File libs/ldaplib/admin.py

         return lang
 
     # Get available languages.
-    def get_langs(self):
+    def getLanguageMaps(self):
         # Get available languages.
         self.available_langs = [ web.safestr(v)
                 for v in os.listdir(cfg.get('rootdir')+'i18n')
                 ]
 
         # Get language maps.
-        self.langmaps = {}
-        [ self.langmaps.update({i: languages.langmaps[i]})
+        self.languagemaps = {}
+        [ self.languagemaps.update({i: languages.langmaps[i]})
                 for i in self.available_langs
                 if i in languages.langmaps
                 ]
-
-        # Get current language.
-        self.cur_lang = self.conn.search_s(
-                ldaputils.convEmailToAdminDN(session.get('username')),
-                ldap.SCOPE_BASE,
-                '(&(objectClass=mailAdmin)(%s=%s))' % (attrs.USER_RDN, session.get('username')),
-                ['preferredLanguage'],
-                )
-
-        if len(self.cur_lang[0][1]) != 0:
-            self.cur_lang = self.cur_lang[0][1]['preferredLanguage'][0]
-        else:
-            self.cur_lang = session.get('lang')
-
-        return {'cur_lang': self.cur_lang, 'langmaps': self.langmaps}
+        return self.languagemaps
 
     # List all admin accounts.
     def list(self):
 
         return self.admins
 
+    # Get admin profile.
+    def profile(self, mail):
+        self.mail = web.safestr(mail)
+        self.dn = ldaputils.convEmailToAdminDN(self.mail)
+        try:
+            self.admin_profile = self.conn.search_s(
+                    self.dn,
+                    ldap.SCOPE_BASE,
+                    '(&(objectClass=mailAdmin)(mail=%s))' % self.mail,
+                    attrs.ADMIN_ATTRS_ALL,
+                    )
+            return (True, self.admin_profile)
+        except Exception, e:
+            return (False, str(e))
+
     def add(self, admin, passwd, domainGlobalAdmin):
         # msg: {'admin': 'result'}
         msg = {}
                     (ldap.MOD_REPLACE, 'preferredLanguage', self.lang)
                     ]
 
+            cn = data.get('cn', None)
+            if cn is not None:
+                mod_attrs += [ ( ldap.MOD_REPLACE, 'cn', cn.encode('utf-8') ) ]
+            else:
+                # Delete attribute.
+                mod_attrs += [ ( ldap.MOD_DELETE, 'cn', None) ]
+
             try:
                 # Modify profiles.
                 self.conn.modify_s(self.dn, mod_attrs)

File libs/ldaplib/attrs.py

 DOMAINADMIN_SEARCH_ATTRS = ['mail', 'accountStatus', 'domainGlobalAdmin', 'cn', 'enabledService']
 
 # ---------------------------------------------------------
+# Admin related.
+# ---------------------------------------------------------
+ADMIN_ATTRS_ALL = ['cn', 'preferredLanguage', ]
+
+# ---------------------------------------------------------
 # Domain related.
 # ---------------------------------------------------------
 DOMAIN_FILTER = '(objectClass=mailDomain)'

File libs/ldaplib/user.py

         self.mail = web.safestr(mail)
         self.dn = ldaputils.convEmailToUserDN(self.mail)
         self.user_profile = self.conn.search_s(
-                str(self.dn),
+                self.dn,
                 ldap.SCOPE_BASE,
                 '(objectClass=mailUser)',
                 attrs.USER_ATTRS_ALL,

File templates/default/ldap/admin_profile.html

-{# defined: profile_type, mail, cur_lang, langmaps, [msg], #}
+{# defined: profile_type, mail, profile, languagemaps, [msg], #}
 {% extends "layout.html" %}
 {% from "macros.html" import display_reset_password, %}
 
 
 <form class="frm-form" method="post" action="{{ctx.homepath}}/profile/admin/{{profile_type}}/{{mail}}">
 {% if profile_type == 'general' %}
+    {% if profile[0] is sameas true %}
+        {% set entry = profile[1][0][1] %}
+        {% set cn = entry.get('cn', [''])[0].decode('utf-8') %}
+        {% set preferredLanguage = entry.get('preferredLanguage', ['en_US'])[0] |string %}
+    {% endif %}
+
     <div class="main-frm">
+        <fieldset class="frm-group group1">
+            <legend class="group-legend"><strong>{{ _('Name') }}</strong></legend>
+            <div class="sf-set set1">
+                <div class="sf-box text">
+                    <label><span>{{ _('Display name') }}</span></label>
+                    <span class="fld-input"><input type="text" name="cn" value="{{cn}}" size="25" /></span>
+                </div>
+            </div>
+        </fieldset>
+
         <fieldset class="sf-set">
             <legend><span>{{ _('Default language') }}</span></legend>
             <div class="sf-box select">
                 <label><span>{{ _('Default language') }}</span></label>
                 <span class="fld-input">
                     <select name="preferredLanguage">
-                        {% for lang in langmaps %}
-                            <option value="{{lang}}" {% if lang == cur_lang %}selected{%endif%}>{{langmaps[lang]}}</option>
+                        {% for lang in languagemaps %}
+                            <option value="{{lang}}" {% if preferredLanguage == lang %}selected{%endif%}>{{languagemaps[lang]}}</option>
                         {% endfor %}
                     </select>
                 </span>