AccountWizard.py :  » Network » Luma » luma-2.4 » lib » luma » plugins » usermanagement » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » Luma 
Luma » luma 2.4 » lib » luma » plugins » usermanagement » AccountWizard.py
# -*- coding: utf-8 -*-

###########################################################################
#    Copyright (C) 2004 by Wido Depping                                      
#    <widod@users.sourceforge.net>                                                             
#
# Copyright: See COPYING file that comes with this distribution
#
###########################################################################

from qt import *
import os.path
import ldap
from string import strip
import random

from plugins.usermanagement.AccountWizardDesign import AccountWizardDesign
from base.backend.ServerList import ServerList
import environment
from base.utils.gui.BrowserWidget import BrowserWidget
from plugins.usermanagement.UsermanagementWidget import UsermanagementWidget
from base.backend.LumaConnection import LumaConnection
from base.utils import lumaStringDecode,lumaStringEncode
from plugins.usermanagement import addPreProcess,addPostProcess
from base.backend.SmartDataObject import SmartDataObject
from base.backend.ObjectClassAttributeInfo import ObjectClassAttributeInfo
from base.utils.gui.LumaErrorDialog import LumaErrorDialog


class AccountWizard(AccountWizardDesign):

    def __init__(self,parent = None,name = None,modal = 0,fl = 0):
        AccountWizardDesign.__init__(self,parent,name,modal,fl)
        
        self.iconPath = os.path.join (environment.lumaInstallationPrefix, "share", "luma", "icons")
        iconDir = os.path.join (environment.lumaInstallationPrefix, "share", "luma", "icons","plugins", "addressbook")
        locationIcon = QPixmap (os.path.join (iconDir, "location.png"))
        self.locationLabel.setPixmap(locationIcon)
        
        layout = QHBoxLayout(self.browserFrame)
        self.browserWidget = BrowserWidget(self.browserFrame)
        layout.addWidget(self.browserWidget)
        
        self.connect(self.browserWidget, PYSIGNAL("ldap_result"), self.updateLocation)
        self.connect(self.finishButton(), SIGNAL("clicked()"), self.saveContact)
        
        self.locationServer = None
        self.locationDN = None
        self.serverMeta = None
        
        for x in range(0,self.pageCount()):
            self.setHelpEnabled(self.page(x), 0)
            
        self.setFinishEnabled(self.page(1), 1)
        self.disconnect(self.finishButton(), SIGNAL("clicked()"), self, SLOT("accept()"))
        self.disconnect(self.nextButton(), SIGNAL("clicked()"), self, SLOT("next()"))
        
        self.connect(self.nextButton(),SIGNAL("clicked()"), self.checkNext)
           
          
        self.accountWidget = UsermanagementWidget(self.accountFrame)
        tmpLayout = QHBoxLayout(self.accountFrame)
        tmpLayout.addWidget(self.accountWidget)
        
        
        self.accountWidget.NEWENTRY = True
        
        self.accountWidget.uidEdit.setReadOnly(False)
        self.disconnect(self.accountWidget.groupButton, SIGNAL("clicked()"), self.accountWidget.editGroups)
        self.connect(self.accountWidget.groupButton, SIGNAL("clicked()"), self.checkUID)
        self.connect(self.accountWidget.addGroupButton, SIGNAL("clicked()"),self.accountWidget.addGroup)
        self.accountWidget.setEnabled(1)
        
###############################################################################

    def updateLocation(self, dataObject):
        self.locationServer = dataObject.getServerMeta().name
        self.serverMeta = dataObject.getServerMeta()
        self.locationDN = dataObject.getPrettyDN()
        tmpString = self.locationDN + "@" + self.locationServer
        self.locationEdit.setText(tmpString)
        
###############################################################################

    def saveContact(self):
        dataObject = self.accountWidget.dataObject
        
        if not dataObject.hasAttribute('uid'):
            tmpDialog = QMessageBox(self.trUtf8("Save account"),
                self.trUtf8("Please enter a username."),
                QMessageBox.Critical,
                QMessageBox.Ok,
                QMessageBox.NoButton,
                QMessageBox.NoButton,
                self)
        
            tmpDialog.setIconPixmap(QPixmap(os.path.join(self.iconPath, "warning_big.png")))
            tmpDialog.exec_loop()
            return

        if not dataObject.hasAttribute('uidNumber'):
            tmpDialog = QMessageBox(self.trUtf8("Save account"),
                self.trUtf8("Please enter a uid number."),
                QMessageBox.Critical,
                QMessageBox.Ok,
                QMessageBox.NoButton,
                QMessageBox.NoButton,
                self)
        
            tmpDialog.setIconPixmap(QPixmap(os.path.join(self.iconPath, "warning_big.png")))
            tmpDialog.exec_loop()
            return
            
        if not dataObject.hasAttribute('gidNumber'):
            tmpDialog = QMessageBox(self.trUtf8("Save account"),
                self.trUtf8("Please assign the user to a group."),
                QMessageBox.Critical,
                QMessageBox.Ok,
                QMessageBox.NoButton,
                QMessageBox.NoButton,
                self)
        
            tmpDialog.setIconPixmap(QPixmap(os.path.join(self.iconPath, "warning_big.png")))
            tmpDialog.exec_loop()
            return
        
        if not dataObject.hasAttribute('cn'):
            tmpDialog = QMessageBox(self.trUtf8("Save account"),
                self.trUtf8("Please enter a common name."),
                QMessageBox.Critical,
                QMessageBox.Ok,
                QMessageBox.NoButton,
                QMessageBox.NoButton,
                self)
        
            tmpDialog.setIconPixmap(QPixmap(os.path.join(self.iconPath, "warning_big.png")))
            tmpDialog.exec_loop()
            return

        if not dataObject.hasAttribute('homeDirectory'):
            tmpDialog = QMessageBox(self.trUtf8("Save account"),
                self.trUtf8("Please enter a homeDirectory."),
                QMessageBox.Critical,
                QMessageBox.Ok,
                QMessageBox.NoButton,
                QMessageBox.NoButton,
                self)
        
            tmpDialog.setIconPixmap(QPixmap(os.path.join(self.iconPath, "warning_big.png")))
            tmpDialog.exec_loop()
            return
        
        if not dataObject.hasAttribute("userPassword"):
            tmpDialog = QMessageBox(self.trUtf8("Missing password"),
                self.trUtf8("""It is strongly recommended that you choose 
a password for the new user. Otherwise 
it might compromise the security of your system."""),
                QMessageBox.Critical,
                QMessageBox.Ok,
                QMessageBox.NoButton,
                QMessageBox.NoButton,
                self)
        
            tmpDialog.setIconPixmap(QPixmap(os.path.join(self.iconPath, "warning_big.png")))
            tmpDialog.exec_loop()
            return
        
        dn = "uid=" + dataObject.getAttributeValue('uid', 0) + "," + self.locationDN
        dataObject.setDN(dn)
        dataObject.addAttributeValue("sn", [dataObject.getAttributeValue("uid", 0)])

        # Start preprocessing of usercreation
        groupName = unicode(self.accountWidget.groupEdit.text())
        addPreProcess(self.serverMeta, dataObject.getDN(), dataObject.data, groupName)
        
        connectionObject = LumaConnection(self.serverMeta)
        bindSuccess, exceptionObject = connectionObject.bind()
        
        if not bindSuccess:
                dialog = LumaErrorDialog()
                errorMsg = self.trUtf8("Could not bind to server.<br><br>Reason: ")
                errorMsg.append(str(exceptionObject))
                dialog.setErrorMessage(errorMsg)
                dialog.exec_loop()
                return
        
        addSuccess, exceptionObject = connectionObject.addDataObject(dataObject)
        
        if addSuccess:
            self.accountWidget.saveOtherGroups()
            
            # Start postprocessing of usercreation
            addPostProcess(self.serverMeta, dataObject.getDN(), dataObject.data, groupName)
            self.accept()
            
        else:
            dialog = LumaErrorDialog()
            errorMsg = self.trUtf8("Could not create account.<br><br>Reason: ")
            errorMsg.append(str(exceptionObject))
            dialog.setErrorMessage(errorMsg)
            dialog.exec_loop()

###############################################################################

    def checkLocation(self):
        if self.currentPage() == None:
            return 1
        
        if (self.locationServer == None) or (self.locationDN == None):
            return 0
        else:
            return 1
            
###############################################################################

    def checkNext(self):
        result = self.checkLocation()
        
        if result == 0:
            tmpDialog = QMessageBox(self.trUtf8("Warning: Location"),
                self.trUtf8("""Please select a location where to store the contact."""),
                QMessageBox.Critical,
                QMessageBox.Ok,
                QMessageBox.NoButton,
                QMessageBox.NoButton,
                self)
        
            tmpDialog.setIconPixmap(QPixmap(os.path.join(self.iconPath, "warning_big.png")))
            tmpDialog.exec_loop()
            
        elif result ==1:
            dataObject = SmartDataObject(('', {'objectClass': self.getPossibleClasses()}), self.serverMeta)
            self.accountWidget.dataObject = dataObject
            # Might be best to always fetch the current list of usedUserIDs. 
            # Methods of how to handle/generate new unused uidNumbers:
            # 1: WebService
            # 2: LDAP-object to store a sequence-like object.
            # 3: The code below.. really ugly - but it works
            if not hasattr(self.accountWidget,'usedUserIDs'):
                userIDs = self.accountWidget.retrieveUserIDs()
                self.accountWidget.usedUserIDs = userIDs
            usedUserIDs = self.accountWidget.usedUserIDs
            usedUserIDs.sort()
            if len(usedUserIDs) == 0:
                nextfreeuid = '1024'
            else:
                tmp = usedUserIDs[len(usedUserIDs)-1] + 1
                nextfreeuid = str(tmp)
            self.accountWidget.usedUserIDs.append(nextfreeuid) # for performance-reasons
            #dataObject.addAttributeValue('uidNumber', ['1024'])
            dataObject.addAttributeValue('uidNumber',[nextfreeuid])
            dataObject.addAttributeValue('homeDirectory', ['/home'])
            dataObject.addAttributeValue('loginShell', ['/bin/bash'])
            
            self.accountWidget.initView(dataObject)
            
            self.next()
            
###############################################################################

    def checkUID(self):
        uid = unicode(self.accountWidget.uidEdit.text())
        if len(uid) > 0:
            self.accountWidget.dataObject.addAttributeValue('uid', [uid], True)
            self.accountWidget.editGroups()
            
###############################################################################

    def getPossibleClasses(self):
        objectClassList = ["top", "posixAccount", "shadowAccount", "inetOrgPerson",
            "organizationalPerson", "person"]
        metaInfo = ObjectClassAttributeInfo(self.serverMeta)
        
        self.availableClasses = []
        for x in objectClassList:
            if metaInfo.hasObjectClass(x):
                self.availableClasses.append(x)
        
        return self.availableClasses

        
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.