Esempio n. 1
0
    def _get_contact_info(self, value):
        """Return a sequence of dicts containing entity_contact_info entries
        for the specified contact value.

        We scan system_voip ONLY.

        value is matched both against contact_value and contact_alias.
        """

        result = list()
        eci = EntityContactInfo(self.db)
        for row in eci.list_contact_info(source_system=self.const.system_voip,
                                         contact_value=value):
            result.append(row.dict())

        for row in eci.list_contact_info(source_system=self.const.system_voip,
                                         contact_alias=value):
            result.append(row.dict())

        return result
Esempio n. 2
0
    def _get_contact_info(self, value):
        """Return a sequence of dicts containing entity_contact_info entries
        for the specified contact value.

        We scan system_voip ONLY.

        value is matched both against contact_value and contact_alias.
        """

        result = list()
        eci = EntityContactInfo(self.db)
        for row in eci.list_contact_info(source_system=self.const.system_voip,
                                         contact_value=str(value)):
            result.append(row.dict())

        for row in eci.list_contact_info(source_system=self.const.system_voip,
                                         contact_alias=str(value)):
            result.append(row.dict())

        return result
Esempio n. 3
0
    def voip_address_delete_number(self, operator, value, owner=None):
        """Delete a previously registered phone number.

        If a phone number is associated with several entities, we cowardly
        refuse, unless owner is specified.

        @param value:
          Contact info string. Could be full or short version of the phone
          number.
        """

        self.ba.can_alter_number(operator.get_entity_id())
        contacts = self._get_contact_info(value)
        if not contacts:
            return "OK, nothing to delete (no record of %s)" % (str(value))

        if owner is not None:
            owner = self._get_voip_owner(owner)

        if len(contacts) > 1 and owner is None:
            raise CerebrumError("Multiple entities have %s registered. You "
                                "must specified entity to update." % (value, ))

        eci = EntityContactInfo(self.db)
        victims = set()
        for d in contacts:
            if owner and owner.entity_id != d["entity_id"]:
                continue

            eci.clear()
            eci.find(d["entity_id"])
            eci.delete_contact_info(d["source_system"], d["contact_type"],
                                    d["contact_pref"])
            victims.add(eci.entity_id)

        return "OK, removed %s from %d entit%s: %s" % (
            str(value), len(victims), len(victims) != 1 and "ies"
            or "y", ", ".join("id=%s" % x for x in victims))
Esempio n. 4
0
    def voip_address_delete_number(self, operator, value, owner=None):
        """Delete a previously registered phone number.

        If a phone number is associated with several entities, we cowardly
        refuse, unless owner is specified.

        @param value:
          Contact info string. Could be full or short version of the phone
          number.
        """

        self.ba.can_alter_number(operator.get_entity_id())
        contacts = self._get_contact_info(value)
        if not contacts:
            return "OK, nothing to delete (no record of %r)" % (value)

        if owner is not None:
            owner = self._get_voip_owner(owner)

        if len(contacts) > 1 and owner is None:
            raise CerebrumError("Multiple entities have %s registered. You "
                                "must specified entity to update." %
                                (value,))

        eci = EntityContactInfo(self.db)
        victims = set()
        for d in contacts:
            if owner and owner.entity_id != d["entity_id"]:
                continue

            eci.clear()
            eci.find(d["entity_id"])
            eci.delete_contact_info(d["source_system"],
                                    d["contact_type"],
                                    d["contact_pref"])
            victims.add(eci.entity_id)

        return "OK, removed %r from %d entit%s: %s" % (
            value, len(victims),
            len(victims) != 1 and "ies" or "y",
            ", ".join("id=%s" % x for x in victims))
Esempio n. 5
0
    def list_voip_attributes(self, *args):
        """Return a generator over what looks like get_voip_attributes() return
        value.

        *args passes cached look ups from generate_voip_ldif.py to
        _cache_owner_person_attrs.

        This is a speed up function for LDAP export. The idea is to cache all
        the attributes up front, so we don't have to pay a penalty associated
        with multiple database lookups per voipAddress.
        """

        # ou_id -> stedkode
        # Will pass it to the cache functions so we can get stedkode and not OUs back
        ou = Factory.get("OU")(self._db)
        ou2sko = dict(
            (x["ou_id"],
             "%02d%02d%02d" % (x["fakultet"], x["institutt"], x["avdeling"]))
            for x in ou.get_stedkoder())

        owner_data = self._cache_owner_voip_service_attrs(ou2sko)
        owner_data.update(self._cache_owner_person_attrs(ou2sko, *args))
        voipify = self._voipify
        voipify_short = self._voipify_short

        # owner_id -> sequence of contact info (value, alias)-pairs
        owner2contact_info = dict()
        eci = EntityContactInfo(self._db)
        for row in eci.list_contact_info(source_system=self.const.system_voip):
            owner_id = row["entity_id"]
            tpl = (row["contact_value"], row["contact_alias"])
            owner2contact_info.setdefault(owner_id, list()).append(tpl)

        # owner_id -> mobile
        owner2mobiles = dict()
        for row in eci.list_contact_info(
                source_system=self.const.system_sap,
                contact_type=self.const.contact_mobile_phone,
                entity_type=self.const.entity_person):
            owner2mobiles.setdefault(row["entity_id"],
                                     list()).append(row["contact_value"])

        for row in self.search():
            entry = dict((key, None) for key in self._required_voip_attributes)
            entry["voipOwnerId"] = str(row["owner_entity_id"])
            entry["voipSipUri"] = list()
            entry["entity_id"] = row["entity_id"]
            owner_id = row["owner_entity_id"]
            address_id = row["entity_id"]

            # Why this way? Well, if owner_data has required attributes and they
            # are missing, the LDAP import will fail (by design). We cannot
            # allow that, if the sole reason for missing entry is the fact that
            # owner_data cache dict is out of sync at this point (it takes a
            # while to construct that dict)
            if owner_id not in owner_data:
                continue

            entry.update(owner_data[owner_id])

            # voipSipUri
            for item in owner2contact_info.get(owner_id, list()):
                full, alias = item
                entry["voipSipUri"].append(voipify(full))
                if alias:
                    entry["voipSipUri"].append(voipify_short(alias))
            if entry["mail"]:
                value = self._voipify(entry["mail"], None)
                entry["voipSipPrimaryUri"] = value
                if entry["voipOwnerType"] != "person":
                    entry["voipSipUri"].append(value)

            entry["mobile"] = owner2mobiles.get(owner_id, list())

            # voipE164Uri + voipExtensionUri
            if owner2contact_info.get(owner_id):
                full, alias = owner2contact_info[owner_id][0]
                entry["voipE164Uri"] = voipify(full)
                if alias:
                    entry["voipExtensionUri"] = voipify_short(alias)

            yield entry
Esempio n. 6
0
    def list_voip_attributes(self, *args):
        """Return a generator over what looks like get_voip_attributes() return
        value.

        *args passes cached look ups from generate_voip_ldif.py to
        _cache_owner_person_attrs.

        This is a speed up function for LDAP export. The idea is to cache all
        the attributes up front, so we don't have to pay a penalty associated
        with multiple database lookups per voipAddress.
        """

        # ou_id -> stedkode
        # Will pass it to the cache functions so we can get stedkode and not OUs back
        ou = Factory.get("OU")(self._db)
        ou2sko = dict((x["ou_id"], "%02d%02d%02d" % (x["fakultet"], x["institutt"], x["avdeling"]))
                      for x in ou.get_stedkoder())

        owner_data = self._cache_owner_voip_service_attrs(ou2sko)
        owner_data.update(self._cache_owner_person_attrs(ou2sko, *args))
        voipify = self._voipify
        voipify_short = self._voipify_short
        
        # owner_id -> sequence of contact info (value, alias)-pairs
        owner2contact_info = dict()
        eci = EntityContactInfo(self._db)
        for row in eci.list_contact_info(source_system=self.const.system_voip):
            owner_id = row["entity_id"]
            tpl = (row["contact_value"], row["contact_alias"])
            owner2contact_info.setdefault(owner_id, list()).append(tpl)

        # owner_id -> mobile
        owner2mobiles = dict()
        for row in eci.list_contact_info(
                           source_system=self.const.system_sap,
                           contact_type=self.const.contact_mobile_phone,
                           entity_type=self.const.entity_person):
            owner2mobiles.setdefault(row["entity_id"], list()).append(
                row["contact_value"])

        for row in self.search():
            entry = dict((key, None) for key in self._required_voip_attributes)
            entry["voipOwnerId"] = str(row["owner_entity_id"])
            entry["voipSipUri"] = list()
            entry["entity_id"] = row["entity_id"]
            owner_id = row["owner_entity_id"]
            address_id = row["entity_id"]

            # Why this way? Well, if owner_data has required attributes and they
            # are missing, the LDAP import will fail (by design). We cannot
            # allow that, if the sole reason for missing entry is the fact that
            # owner_data cache dict is out of sync at this point (it takes a
            # while to construct that dict)
            if owner_id not in owner_data:
                continue

            entry.update(owner_data[owner_id])

            # voipSipUri
            for item in owner2contact_info.get(owner_id, list()):
                full, alias = item
                entry["voipSipUri"].append(voipify(full))
                if alias:
                    entry["voipSipUri"].append(voipify_short(alias))
            if entry["mail"]:
                value = self._voipify(entry["mail"], None)
                entry["voipSipPrimaryUri"] = value
                if entry["voipOwnerType"] != "person":
                    entry["voipSipUri"].append(value)

            entry["mobile"] = owner2mobiles.get(owner_id, list())

            # voipE164Uri + voipExtensionUri
            if owner2contact_info.get(owner_id):
                full, alias = owner2contact_info[owner_id][0]
                entry["voipE164Uri"] = voipify(full)
                if alias:
                    entry["voipExtensionUri"] = voipify_short(alias)

            yield entry