iredadmin-ose / controllers / ldap / admin.py

#!/usr/bin/env python
# encoding: utf-8

# Author: Zhang Huangbin <michaelbibby (at) gmail.com>

import sys
import web
from web import render
from controllers.ldap.basic import dbinit
from controllers.ldap import base
from libs.ldaplib import admin, ldaputils, iredldif

cfg = web.iredconfig
session = web.config.get('_session')

adminLib = admin.Admin()

#
# Admin related.
#
class list(dbinit):
    @base.check_global_admin
    @base.protected
    def GET(self):
        self.admins = adminLib.list()
        return render.admins(admins=self.admins)

    @base.check_global_admin
    @base.protected
    def POST(self):
        i = web.input(dn=[])

        # Post method: add, delete.
        action = i.get('action', None)

        if action == 'add':
            # Get admin list (python list obj).
            admin = i.get('admin', None)
            passwd = i.get('passwd', None)
            domainGlobalAdmin = i.get('domainGlobalAdmin', 'no')

            if admin is not None and passwd is not None:
                # Try to add it.
                results = self.dbwrap.admin_add(admin, passwd, domainGlobalAdmin)

                # List admins.
                self.admins = adminLib.list()
                return render.admins(admins=self.admins, msg=results)
            else:
                # Show system message.
                self.admins = adminLib.list()
                return render.admins(admins=self.admins, msg='NO_DOMAIN')
        elif action == 'delete':
            dn = i.get('dn', [])

            if len(dn) >= 1:
                # Delete dn(s).
                results = self.dbwrap.delete_dn(dn)

                # List admins.
                self.admins = adminLib.list()
                return render.admins(admins=self.admins, msg=results)
            else:
                # Show system message.
                return render.admins()
        else:
            return render.admins()

class create(dbinit):
    @base.protected
    def GET(self):
        return render.admin_create(
                languagemaps=adminLib.getLanguageMaps(),
                min_passwd_length=cfg.general.get('min_passwd_length'),
                max_passwd_length=cfg.general.get('max_passwd_length'),
                )

    @base.protected
    def POST(self):
        i = web.input()
        self.username = web.safestr(i.get('username'))
        self.domain = web.safestr(i.get('domain'))
        self.mail = self.username + '@' + self.domain
        result = adminLib.add(data=i)

        if result[0] is True:
            web.seeother('/profile/admin/general/%s' % self.mail)
        else:
            self.cn = i.get('cn')
            return render.admin_create(
                    username=self.username,
                    domain=self.domain,
                    cn=self.cn,
                    languagemaps=adminLib.getLanguageMaps(),
                    msg=result[1],
                    )

class profile(dbinit):
    @base.protected
    def GET(self, profile_type, mail):
        self.mail = web.safestr(mail)
        self.profile_type = web.safestr(profile_type)

        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:
            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):
        self.profile_type = web.safestr(profile_type)
        self.mail = web.safestr(mail)
        i = web.input()

        result = adminLib.update(
                profile_type=self.profile_type,
                mail=self.mail,
                data=i,
                )
        if result[0] is True:
            web.seeother('/profile/admin/%s/%s?msg=SUCCESS' % (self.profile_type, self.mail))
        else:
            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],
                        )
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.