Ejemplo n.º 1
0
    def runCommand(self):
        self.parseArgs(usage="""
    cdb-add-user --username=USERNAME --first-name=FIRSTNAME --last-name=LASTNAME
        [--middle-name=MIDDLENAME]
        [--email=EMAIL]
        [--description=DESCRIPTION]
        [--password=PASSWORD|--password-file=PASSWORDFILE]

Description:
    Adds new user into CDB database. This command goes directly to the
    database and must be run from a CDB administrator account.
        """)
        self.checkArgs()
        api = UserDbApi()
        username = self.getUsername()
        firstName = self.getFirstName()
        lastName = self.getLastName()
        middleName = self.getMiddleName()
        email = self.getEmail()
        description = self.getDescription()
        password = self.getPassword()
        cryptedPassword = None
        if password:
            cryptedPassword = CryptUtility.cryptPasswordWithPbkdf2(password)
        userInfo = api.addUser(username, firstName, lastName, middleName,
                               email, description, cryptedPassword)
        print userInfo.getDisplayString(self.getDisplayKeys(),
                                        self.getDisplayFormat())
Ejemplo n.º 2
0
 def __init__(self):
     self.itemDbApi = ItemDbApi()
     self.userDbApi = UserDbApi()
     self.propertyDbApi = PropertyDbApi()
     self.logDbApi = LogDbApi()
     self.emailUtility = CdbEmailUtility.getInstance()
     self.cdbPortalUrlUtility = CdbPortalUrlUtility.getInstance()
     self.logger = LoggingManager.getInstance().getLogger(self.__class__.__name__)
Ejemplo n.º 3
0
    def runCommand(self):
        self.parseArgs(usage="""
    get-groups

Description:
    Get all registered Groups.
        """)
        api = UserDbApi()
        groups = api.getUserGroups()
        for group in groups:
           print group.getDisplayString(self.getDisplayKeys(), self.getDisplayFormat())
class UserGroupControllerImpl(CdbObjectManager):
    """ User group controller implementation class. """
    def __init__(self):
        CdbObjectManager.__init__(self)
        self.userDbApi = UserDbApi()

    def getUserGroups(self):
        return self.userDbApi.getUserGroups()

    def getUserGroupByName(self, groupName):
        return self.userDbApi.getUserGroupByName(groupName)
class UserInfoControllerImpl(CdbObjectManager):
    """ User info controller implementation class. """
    def __init__(self):
        CdbObjectManager.__init__(self)
        self.userDbApi = UserDbApi()

    def getUsers(self):
        return self.userDbApi.getUsers()

    def getUserById(self, id):
        return self.userDbApi.getUserById(id)

    def getUserByUsername(self, username):
        return self.userDbApi.getUserByUsername(username)
Ejemplo n.º 6
0
    def runCommand(self):
        self.parseArgs(usage="""
    add-group --name=GROUPNAME
        [--description=DESCRIPTION]
Description:
    Adds a group into CDB database. This command goes directly to the
    database and must be run from a CDB administrator account.
        """)
        self.checkArgs()
        api = UserDbApi()
        name = self.getName()
        description = self.getDescription()
        info = api.addGroup(name, description)
        print info.getDisplayString(self.getDisplayKeys(), self.getDisplayFormat())
Ejemplo n.º 7
0
    def runCommand(self):
        self.parseArgs(usage="""
    cdb-add-user-to-group --username=USERNAME --group-name=GROUPNAME

Description:
    Adds user to group. This command goes directly to the
    database and must be run from a CDB administrator account.
        """)
        self.checkArgs()
        api = UserDbApi()
        username = self.getUsername()
        groupName = self.getGroupName()
        userUserGroup = api.addUserToGroup(username, groupName)
        print userUserGroup.getDisplayString(self.getDisplayKeys(),
                                             self.getDisplayFormat())
Ejemplo n.º 8
0
class DbPrincipalRetriever(AuthorizationPrincipalRetriever):
    def __init__(self):
        AuthorizationPrincipalRetriever.__init__(self, self.__class__.__name__)
        self.dbApi = UserDbApi()

    def getAuthorizationPrincipal(self, username):
        principal = None
        try:
            user = self.dbApi.getUserWithPasswordByUsername(username)
            principal = AuthorizationPrincipal(username, user.get('password'))
            principal.setRole(cdbRole.CDB_USER_ROLE)
            principal.setUserInfo(user)
            if self.adminGroupName is not None:
                for userGroup in user.get('userGroupList', []):
                    if userGroup.get('name') == self.adminGroupName:
                        principal.setRole(cdbRole.CDB_ADMIN_ROLE)
        except Exception as ex:
            self.logger.debug(ex)
        return principal
Ejemplo n.º 9
0
class SparePartsTask:

    ITEM_DOMAIN_INVENTORY_STATUS_PROPERTY_TYPE_NAME = "Component Instance Status"
    ITEM_DOMAIN_INVENTORY_STATUS_SPARE_VALUE = "Spare"

    SPARE_PARTS_CONFIGURATION_PROPERTY_TYPE_NAME = 'Spare Parts Configuration'
    SPARE_PARTS_CONFIGURATION_MIN_KEY = 'minQuantity'
    SPARE_PARTS_CONFIGURATION_EMAIL_KEY = 'email'
    SPARE_PARTS_CONFIGURATION_NOEMAIL_VALUE = 'None'
    SPARE_PARTS_WARNING_LOG_LEVEL = 'Spares Warning'
    CDB_SYSTEM_ACCOUNT_USERNAME = '******'
    CATALOG_DOMAIN_NAME = 'Catalog'
    SPARE_PARTS_EMAIL_NOTIFICATION_NAME = 'Spare Parts'

    def __init__(self):
        self.itemDbApi = ItemDbApi()
        self.userDbApi = UserDbApi()
        self.propertyDbApi = PropertyDbApi()
        self.logDbApi = LogDbApi()
        self.emailUtility = CdbEmailUtility.getInstance()
        self.cdbPortalUrlUtility = CdbPortalUrlUtility.getInstance()
        self.logger = LoggingManager.getInstance().getLogger(
            self.__class__.__name__)

    def getPropertyMetadataDict(self, propertyValue):
        propertyValueId = propertyValue.data['id']
        propertyMetadataList = self.propertyDbApi.getPropertyMetadataForPropertyValueId(
            propertyValueId)
        propertyMetadataDict = {}
        for propertyMetadata in propertyMetadataList:
            key = propertyMetadata.data['metadataKey']
            value = propertyMetadata.data['metadataValue']
            propertyMetadataDict[key] = value
        return propertyMetadataDict

    def getOwnerUserEmail(self, itemId):
        selfItemElement = self.itemDbApi.getSelfElementByItemId(itemId)
        entityInfo = selfItemElement.data['entityInfo']
        ownerUserInfo = entityInfo['ownerUserInfo']
        return ownerUserInfo.data['email']

    def getSystemAccountUserId(self):
        cdbUser = self.userDbApi.getUserByUsername(
            self.CDB_SYSTEM_ACCOUNT_USERNAME)
        return cdbUser.data['id']

    def getSelfElementIdForItemId(self, itemId):
        selfItemElement = self.itemDbApi.getSelfElementByItemId(itemId)
        return selfItemElement.data['id']

    def addSparePartsWarningLogEntryToItem(self, itemElementId, message):
        systemAccountUserId = self.getSystemAccountUserId()
        self.itemDbApi.addItemElementLog(itemElementId, message,
                                         systemAccountUserId, None, None, None,
                                         None,
                                         self.SPARE_PARTS_WARNING_LOG_LEVEL)

    @staticmethod
    def generateSparesMessage(minSpares, currentSpares):
        if currentSpares == 1:
            plural = ''
        else:
            plural = 's'
        return "Item has %s spare part%s & requires %s" % (currentSpares,
                                                           plural, minSpares)

    def checkSpares(self):
        self.logger.debug('Checking status of spare parts.')
        catalogItemsWithSparePartsConfiguration = self.itemDbApi.getItemsWithPropertyType(
            self.SPARE_PARTS_CONFIGURATION_PROPERTY_TYPE_NAME,
            itemDomainName=self.CATALOG_DOMAIN_NAME)

        for catalogItem in catalogItemsWithSparePartsConfiguration:
            catalogItemId = catalogItem.data['id']
            catalogSelfElementId = self.getSelfElementIdForItemId(
                catalogItemId)

            sparePartsConfigurationPropertyValue = self.propertyDbApi.getPropertyValueListForItemElementId(
                catalogSelfElementId,
                propertyTypeName=self.
                SPARE_PARTS_CONFIGURATION_PROPERTY_TYPE_NAME)

            if sparePartsConfigurationPropertyValue:
                metadataDict = self.getPropertyMetadataDict(
                    sparePartsConfigurationPropertyValue[0])
            else:
                raise ObjectNotFound(
                    "Could not find required property: %s" %
                    self.SPARE_PARTS_CONFIGURATION_PROPERTY_TYPE_NAME)

            if self.SPARE_PARTS_CONFIGURATION_MIN_KEY in metadataDict:
                minSpares = int(
                    metadataDict[self.SPARE_PARTS_CONFIGURATION_MIN_KEY])
            else:
                raise CdbException(
                    "required metadata %s not specified for spare part configuration"
                    % self.SPARE_PARTS_CONFIGURATION_MIN_KEY)

            email = ''
            if metadataDict.has_key(self.SPARE_PARTS_CONFIGURATION_EMAIL_KEY):
                emailValue = metadataDict[
                    self.SPARE_PARTS_CONFIGURATION_EMAIL_KEY]
                if emailValue == self.SPARE_PARTS_CONFIGURATION_NOEMAIL_VALUE:
                    email = None
                else:
                    email = emailValue
            else:
                # Use owner user email.
                email = self.getOwnerUserEmail(catalogItemId)

            sparePartsList = self.itemDbApi.getItemsWithPropertyType(
                self.ITEM_DOMAIN_INVENTORY_STATUS_PROPERTY_TYPE_NAME,
                itemDerivedFromItemId=catalogItemId,
                propertyValueMatch=self.
                ITEM_DOMAIN_INVENTORY_STATUS_SPARE_VALUE)
            spares = sparePartsList.__len__()

            if minSpares > spares:
                validNotification = True
                sparesMessage = self.generateSparesMessage(minSpares, spares)

                spareLogEntries = self.logDbApi.getLogEntriesForItemElementId(
                    catalogSelfElementId, self.SPARE_PARTS_WARNING_LOG_LEVEL)
                if spareLogEntries:
                    lastSpareLogEntry = spareLogEntries[-1]
                    lastSparesLogMessage = lastSpareLogEntry.data['text']
                    if lastSparesLogMessage == sparesMessage:
                        validNotification = False

                if validNotification:
                    self.addSparePartsWarningLogEntryToItem(
                        catalogSelfElementId, sparesMessage)
                    if email is not None:
                        itemUrl = self.cdbPortalUrlUtility.getItemUrlAddress(
                            catalogItemId)
                        catalogItemName = catalogItem.data['name']
                        catalogItemModel = catalogItem.data['item_identifier1']
                        catalogItemAlternateName = catalogItem.data[
                            'item_identifier2']
                        itemDictValue = '<a href=%s>%s</a>' % (itemUrl,
                                                               catalogItemName)

                        # Create an ordered dict for the table emailed to user.
                        informationDict = OrderedDict()
                        informationDict['Name'] = itemDictValue
                        informationDict['Model'] = catalogItemModel
                        informationDict[
                            'Alternate Name'] = catalogItemAlternateName
                        informationDict['Spares On Hand'] = spares
                        informationDict['Minimum Spares Required '] = minSpares

                        emailTable = CdbEmailUtility.generateSimpleHtmlTableMessage(
                            informationDict)

                        emailMessage = '%s <br/><br/> %s' % (sparesMessage,
                                                             emailTable)

                        self.emailUtility.sendEmailNotification(
                            email, self.SPARE_PARTS_EMAIL_NOTIFICATION_NAME,
                            emailMessage)
Ejemplo n.º 10
0
 def __init__(self):
     AuthorizationPrincipalRetriever.__init__(self, self.__class__.__name__)
     self.dbApi = UserDbApi()
Ejemplo n.º 11
0
 def __init__(self):
     CdbObjectManager.__init__(self)
     self.userDbApi = UserDbApi()