示例#1
0
    def voip_service_new(self, operator, service_type, ou_tag, description):
        """Create a new voip_service.

        @param service_type: Type of voip_service entry.

        @param ou_tag: OU where the voip_service located (stedkode)

        @param description: service's description. Must be globally unique.
        """

        self.ba.can_create_voip_service(operator.get_entity_id())
        self._assert_unused_service_description(description)
        service = VoipService(self.db)

        ou = self._get_ou(ou_tag)
        service_type = self._get_constant(service_type,
                                          self.const.VoipServiceTypeCode)
        if service_type is None:
            raise CerebrumError("Unknown voip_service_type: %r" %
                                service_type)
        service.populate(description, service_type, ou.entity_id)
        service.write_db()

        # Create a corresponding voip_address...
        self._get_or_create_voip_address(service.entity_id)
        return "OK, new voip_service (%s), entity_id=%s" % (
            text_type(service_type),
            service.entity_id)
示例#2
0
 def _assert_unused_service_description(self, description):
     """Check that a description is not in use by any voip_service.
     """
     vs = VoipService(self.db)
     services = vs.search_voip_service_by_description(description,
                                                      exact_match=True)
     if services:
         raise CerebrumError("Description must be unique. In use by id=%s." \
                             % services[0]['entity_id'])
示例#3
0
 def _assert_unused_service_description(self, description):
     """Check that a description is not in use by any voip_service.
     """
     vs = VoipService(self.db)
     services = vs.search_voip_service_by_description(description,
                                                      exact_match=True)
     if services:
         raise CerebrumError("Description must be unique. In use by id=%s."
                             % services[0]['entity_id'])
示例#4
0
 def get_owner(self):
     """Return the owner object of the proper type."""
     ent = Entity(self._db)
     ent.find(self.owner_entity_id)
     if ent.entity_type == self.const.entity_voip_service:
         result = VoipService(self._db)
         result.find(self.owner_entity_id)
         return result
     ent.clear()
     return ent.get_subclassed_object(self.owner_entity_id)
示例#5
0
    def _get_voip_service(self, designation):
        """Locate a specific voip_service.

        We try to be a bit lax when it comes to identifying voip_services. A
        numeric designation is interpreted as entity_id. Everything else is
        interpreted as description.
        """

        service = VoipService(self.db)
        if self._get_entity_id(designation):
            try:
                service.find(self._get_entity_id(designation))
                return service
            except Errors.NotFoundError:
                raise CerebrumError("Could not find voip service with id=%s" %
                                    str(designation))

        ids = service.search_voip_service_by_description(designation,
                                                         exact_match=True)
        if len(ids) == 1:
            service.find(ids[0]["entity_id"])
            return service

        raise CerebrumError("Could not uniquely determine voip_service "
                            "from description %s" % str(designation))
示例#6
0
 def _cache_owner_voip_service_attrs(self, ou2sko):
     # First cache the constants
     const2str = dict()
     for cnst in self.const.fetch_constants(self.const.VoipServiceTypeCode):
         assert int(cnst) not in const2str
         const2str[int(cnst)] = str(cnst)
     # Second the voipServices...
     vp = VoipService(self._db)
     owner_data = dict()
     for row in vp.search():
         uid = str(row["entity_id"])
         ou = row["ou_id"]
         owner_data[row["entity_id"]] = {"uid": uid,
                                         "mail": uid + "@usit.uio.no",
                                         "cn": row["description"],
                                         "voipOwnerType": "service",
                                         "voipServiceType": const2str[row["service_type"]],
                                         "voipSKO": (ou2sko[ou],)}
     return owner_data
示例#7
0
 def _cache_owner_voip_service_attrs(self, ou2sko):
     # First cache the constants
     const2str = dict()
     for cnst in self.const.fetch_constants(self.const.VoipServiceTypeCode):
         assert int(cnst) not in const2str
         const2str[int(cnst)] = str(cnst)
     # Second the voipServices...
     vp = VoipService(self._db)
     owner_data = dict()
     for row in vp.search():
         uid = str(row["entity_id"])
         ou = row["ou_id"]
         owner_data[row["entity_id"]] = {
             "uid": uid,
             "mail": uid + "@usit.uio.no",
             "cn": row["description"],
             "voipOwnerType": "service",
             "voipServiceType": const2str[row["service_type"]],
             "voipSKO": (ou2sko[ou], )
         }
     return owner_data
示例#8
0
    def _get_voip_service(self, designation):
        """Locate a specific voip_service.

        We try to be a bit lax when it comes to identifying voip_services. A
        numeric designation is interpreted as entity_id. Everything else is
        interpreted as description.
        """

        service = VoipService(self.db)
        if self._get_entity_id(designation):
            try:
                service.find(self._get_entity_id(designation))
                return service
            except Errors.NotFoundError:
                raise CerebrumError("Could not find voip service with id=%r" %
                                    designation)

        ids = service.search_voip_service_by_description(designation,
                                                         exact_match=True)
        if len(ids) == 1:
            service.find(ids[0]["entity_id"])
            return service

        raise CerebrumError("Could not uniquely determine voip_service "
                            "from description %r" % designation)
示例#9
0
    def voip_service_new(self, operator, service_type, ou_tag, description):
        """Create a new voip_service.

        @param service_type: Type of voip_service entry.

        @param ou_tag: OU where the voip_service located (stedkode)

        @param description: service's description. Must be globally unique.
        """

        self.ba.can_create_voip_service(operator.get_entity_id())
        self._assert_unused_service_description(description)
        service = VoipService(self.db)

        ou = self._get_ou(ou_tag)
        service_type = self._get_constant(service_type,
                                          self.const.VoipServiceTypeCode)
        if service_type is None:
            raise CerebrumError("Unknown voip_service_type: %s" %
                                str(service_type))
        service.populate(description, service_type, ou.entity_id)
        service.write_db()

        # Create a corresponding voip_address...
        self._get_or_create_voip_address(service.entity_id)
        return "OK, new voip_service (%s), entity_id=%s" % (str(service_type),
                                                            service.entity_id)
示例#10
0
    def voip_service_find(self, operator, designation):
        """List all voip_services matched in some way by designation.

        This has been requested to ease up looking up voip_services for users.

        designation is used to look up voip_services in the following fashion:

          - if all digits -> by entity_id
          - by description (exactly)
          - by description (substring search)
          - if all digits -> by ou_id
          - if all digits -> by stedkode

        All the matching voip_services are collected and returned as a sequence
        so people can pluck out the entities they want and use them in
        subsequent commands.
        """

        def fold_description(s):
            cutoff = 15
            suffix = "(...)"
            if len(s) > cutoff:
                return s[:(cutoff - len(suffix))] + suffix
            return s

        self.ba.can_view_voip_service(operator.get_entity_id())
        ident = designation.strip()
        collect = dict()
        vs = VoipService(self.db)

        # let's try by-id lookup first
        if ident.isdigit():
            try:
                vs.find(int(ident))
                collect[vs.entity_id] = (vs.description,
                                         vs.service_type,
                                         vs.ou_id)
            except Errors.NotFoundError:
                pass

        # then by-description...
        for exact_match in (False, True):
            results = vs.search_voip_service_by_description(
                             designation, exact_match=exact_match)
            for row in results:
                collect[row["entity_id"]] = (row["description"],
                                             row["service_type"],
                                             row["ou_id"])

        # then by OU (stedkode and ou_id)
        try:
            ou = self._get_ou(designation)
            for row in vs.search(ou_id=ou.entity_id):
                collect[row["entity_id"]] = (row["description"],
                                             row["service_type"],
                                             row["ou_id"])
        except CerebrumError:
            pass

        # Finally, the presentation layer
        if len(collect) > cereconf.BOFHD_MAX_MATCHES:
            raise CerebrumError("More than %d (%d) matches, please narrow "
                                "search criteria" %
                                (cereconf.BOFHD_MAX_MATCHES, len(collect)))

        answer = list()
        for entity_id in collect:
            description, service_type, ou_id = collect[entity_id]
            service_type = self.const.VoipServiceTypeCode(service_type)
            answer.append({
                "entity_id": entity_id,
                "description": fold_description(description),
                "service_type": text_type(service_type),
                "ou": self._typeset_ou(ou_id),
            })
        return answer
示例#11
0
    def voip_service_find(self, operator, designation):
        """List all voip_services matched in some way by designation.

        This has been requested to ease up looking up voip_services for users.

        designation is used to look up voip_services in the following fashion:
        
          - if all digits -> by entity_id
          - by description (exactly)
          - by description (substring search)
          - if all digits -> by ou_id
          - if all digits -> by stedkode

        All the matching voip_services are collected and returned as a sequence
        so people can pluck out the entities they want and use them in
        subsequent commands. 
        """
        def fold_description(s):
            cutoff = 15
            suffix = "(...)"
            if len(s) > cutoff:
                return s[:(cutoff - len(suffix))] + suffix
            return s

        # end fold_description

        self.ba.can_view_voip_service(operator.get_entity_id())
        ident = designation.strip()
        collect = dict()
        vs = VoipService(self.db)

        # let's try by-id lookup first
        if ident.isdigit():
            try:
                vs.find(int(ident))
                collect[vs.entity_id] = (vs.description, vs.service_type,
                                         vs.ou_id)
            except Errors.NotFoundError:
                pass

        # then by-description...
        for exact_match in (False, True):
            results = vs.search_voip_service_by_description(
                designation, exact_match=exact_match)
            for row in results:
                collect[row["entity_id"]] = (row["description"],
                                             row["service_type"], row["ou_id"])

        # then by OU (stedkode and ou_id)
        try:
            ou = self._get_ou(designation)
            for row in vs.search(ou_id=ou.entity_id):
                collect[row["entity_id"]] = (row["description"],
                                             row["service_type"], row["ou_id"])
        except CerebrumError:
            pass

        # Finally, the presentation layer
        if len(collect) > cereconf.BOFHD_MAX_MATCHES:
            raise CerebrumError("More than %d (%d) matches, please narrow "
                                "search criteria" %
                                (cereconf.BOFHD_MAX_MATCHES, len(collect)))

        answer = list()
        for entity_id in collect:
            description, service_type, ou_id = collect[entity_id]
            answer.append({
                "entity_id":
                entity_id,
                "description":
                fold_description(description),
                "service_type":
                str(self.const.VoipServiceTypeCode(service_type)),
                "ou":
                self._typeset_ou(ou_id),
            })
        return answer