def can_set_new_secret(self, account_id, client_id): """Whether account_id can set a new sipSecret on client_id. """ if self.is_superuser(account_id): return True if self._is_voip_admin(account_id): return True # We allow resetting a secret to the owner of client_id. # # The test goes like this: find voip_address to which client_id is # bound. Compare it to account_id's owner_id. For non-personal # accounts this test is bound to fail. acc = Factory.get("Account")(self._db) acc.find(account_id) client = VoipClient(self._db) client.find(client_id) address = VoipAddress(self._db) address.find(client.voip_address_id) if address.owner_entity_id == acc.owner_id: return True raise PermissionDenied("Account id=%d cannot change sipSecret of " "voip_client id=%d" % (account_id, client_id))
def _get_voip_address_by_owner_entity_id(self, designation): if not self._is_numeric_id(designation): return list() value = int(designation) try: va = VoipAddress(self.db) va.find_by_owner_id(value) return [va, ] except Errors.NotFoundError: return list() assert False, "NOTREACHED"
def generate_voip_addresses(sink, *args): va = VoipAddress(db) sink.write(container_entry_string('VOIP_ADDRESS')) addr_id2dn = dict() for entry in va.list_voip_attributes(*args): entry['objectClass'] = ['top', 'voipAddress'] dn = "voipOwnerId={},{}".format(entry['voipOwnerId'], ldapconf('VOIP_ADDRESS', 'dn', None)) entity_id = entry.pop("entity_id") addr_id2dn[entity_id] = dn if not entry.get("cn"): entry["cn"] = () sink.write(entry_string(dn, entry)) return addr_id2dn
def _get_voip_address_by_entity_id(self, designation): """Return all voip_addresses matching the specified entity_id.""" if not self._is_numeric_id(designation): return list() value = int(designation) try: va = VoipAddress(self.db) va.find(value) return [va, ] except Errors.NotFoundError: return list() assert False, "NOTREACHED"
def _get_voip_address_by_owner_entity_id(self, designation): if not self._is_numeric_id(designation): return list() value = int(designation) try: va = VoipAddress(self.db) va.find_by_owner_id(value) return [ va, ] except Errors.NotFoundError: return list() assert False, "NOTREACHED"
def generate_voip_addresses(sink, encoding, *args): db = Factory.get("Database")() va = VoipAddress(db) sink.write(container_entry_string("VOIP_ADDRESS")) addr_id2dn = dict() for entry in va.list_voip_attributes(*args): entry["objectClass"] = ["top", "voipAddress"] dn = "voipOwnerId=%s,%s" % (entry["voipOwnerId"], ldapconf("VOIP_ADDRESS", "dn", None)) entity_id = entry.pop("entity_id") addr_id2dn[entity_id] = dn entry = object2encoding(entry, encoding) if not entry.get("cn"): entry["cn"] = () sink.write(entry_string(object2encoding(dn, encoding), entry)) return addr_id2dn
def generate_voip_addresses(sink, encoding, *args): db = Factory.get("Database")() va = VoipAddress(db) sink.write(container_entry_string('VOIP_ADDRESS')) addr_id2dn = dict() for entry in va.list_voip_attributes(*args): entry['objectClass'] = ['top', 'voipAddress'] dn = "voipOwnerId=%s,%s" % (entry['voipOwnerId'], ldapconf('VOIP_ADDRESS', 'dn', None)) entity_id = entry.pop("entity_id") addr_id2dn[entity_id] = dn entry = object2encoding(entry, encoding) if not entry.get("cn"): entry["cn"] = () sink.write(entry_string(object2encoding(dn, encoding), entry)) return addr_id2dn
def get_voip_persons_and_primary_accounts(): va = VoipAddress(db) ac = Factory.get("Account")(db) const = Factory.get("Constants")() voippersons = list() for row in va.search(owner_entity_type=const.entity_person): voippersons.append(row["owner_entity_id"]) sysadm_aid = ac.list_sysadm_accounts() primary2pid = dict( (r["account_id"], r["person_id"]) for r in ac.list_accounts_by_type(primary_only=True, person_id=voippersons, exclude_account_id=sysadm_aid)) return voippersons, primary2pid, sysadm_aid
def _get_voip_address_by_entity_id(self, designation): """Return all voip_addresses matching the specified entity_id.""" if not self._is_numeric_id(designation): return list() value = int(designation) try: va = VoipAddress(self.db) va.find(value) return [ va, ] except Errors.NotFoundError: return list() assert False, "NOTREACHED"
def report_invalid_voip_addresses(logger, report): """Find voipAddress-objects owned by persons without primary account. @param logger @type list @param report is a list of dicts. """ logger.debug('-' * 8 + 'voipAddresses' + '-' * 8) db = Factory.get("Database")() va = VoipAddress(db) for entry in va.list_voip_attributes(): if entry['voipOwnerType'] != 'person': continue # skip objects owned by services if not entry.get("cn"): entry["cn"] = () # find addresses owner va.clear() va.find(entry['entity_id']) entry["voipOwnerId"] = va.owner_entity_id if entry["uid"] is None: logger.debug('uid is None') entry['reason'] = find_reason(db, entry) entr = {} for k, v in entry.iteritems(): if k in ('entity_id', 'voipOwnerId', 'cn', 'reason', 'voipExtensionUri', 'uid'): if k == 'voipExtensionUri': k = 'extension' if isinstance(v, basestring): v = v.strip('sip:@uio.no') entr[k] = v report.append(entr) logger.debug('-' * 8 + 'end voipAddresses' + '-' * 8)
def _get_or_create_voip_address(self, owner_id, with_softphone=True): """Much like _get_voip_address(), except this one creates it as well if it does not exist, rather than failing. with_softphone controls whether we want to create a softphone for the new voip_address, should voip_address be created. """ address = VoipAddress(self.db) try: address.find_by_owner_id(owner_id) except Errors.NotFoundError: address.clear() address.populate(owner_id) address.write_db() address.write_db() if with_softphone: self._create_default_softphone_client(address.entity_id) return address
def report_invalid_voip_addresses(logger, report): """Find voipAddress-objects owned by persons without primary account. @param logger @type list @param report is a list of dicts. """ logger.debug("-" * 8 + "voipAddresses" + "-" * 8) db = Factory.get("Database")() va = VoipAddress(db) for entry in va.list_voip_attributes(): if entry["voipOwnerType"] != "person": continue # skip objects owned by services if not entry.get("cn"): entry["cn"] = () # find addresses owner va.clear() va.find(entry["entity_id"]) entry["voipOwnerId"] = va.owner_entity_id if entry["uid"] is None: logger.debug("uid is None") entry["reason"] = find_reason(db, entry) entr = {} for k, v in entry.iteritems(): if k in ("entity_id", "voipOwnerId", "cn", "reason", "voipExtensionUri", "uid"): if k == "voipExtensionUri": k = "extension" if isinstance(v, basestring): v = v.strip("sip:@uio.no") entr[k] = v report.append(entr) logger.debug("-" * 8 + "end voipAddresses" + "-" * 8)
def join_uio_voip_objects(old_id, new_id): """Transfer voip objects from person old_id to person new_id. Respect that a person can have at most one voip_address, i.e. transfer happens only if old_id owns one address and new_id owns none. In case old_id owns no voip_address, nothing is transfered and join continues. Otherwise, join rolls back. @type int @param old_id person id @type int @param new_id person id """ from Cerebrum.modules.no.uio.voip.voipAddress import VoipAddress va = VoipAddress(db) va.clear() old_person_voip_addr = va.search(owner_entity_id=old_id) new_person_voip_addr = va.search(owner_entity_id=new_id) if (len(old_person_voip_addr) == 1 and not new_person_voip_addr): # Transfer va.clear() try: va.find_by_owner_id(old_id) except Errors.NotFoundError: logger.info("No voip address found for owner id %s" % (old_id)) return logger.debug("Change owner of voip_address %s to %s" % (va.entity_id, new_id)) va.populate(new_id) va.write_db() elif not old_person_voip_addr: logger.info("Nothing to transfer." " Person %s owns no voip addresses" % (old_id)) else: logger.warn("Source person %s owns voip addresses: %s" % (old_id, old_person_voip_addr)) logger.warn("Target person %s owns voip addresses:%s" % (new_id, new_person_voip_addr)) db.rollback() logger.warn("Cannot transfer, rollback all changes." "Manual intervention required to join voip objects.") sys.exit(1)
def join_uio_voip_objects(old_id, new_id): """Transfer voip objects from person old_id to person new_id. Respect that a person can have at most one voip_address, i.e. transfer happens only if old_id owns one address and new_id owns none. In case old_id owns no voip_address, nothing is transfered and join continues. Otherwise, join rolls back. @type int @param old_id person id @type int @param new_id person id """ from Cerebrum.modules.no.uio.voip.voipAddress import VoipAddress va = VoipAddress(db) va.clear() old_person_voip_addr = va.search(owner_entity_id=old_id) new_person_voip_addr = va.search(owner_entity_id=new_id) if len(old_person_voip_addr) == 1 and not new_person_voip_addr: # Transfer va.clear() try: va.find_by_owner_id(old_id) except Errors.NotFoundError: logger.info("No voip address found for owner id %s" % (old_id)) return logger.debug("Change owner of voip_address %s to %s" % (va.entity_id, new_id)) va.populate(new_id) va.write_db() elif not old_person_voip_addr: logger.info("Nothing to transfer." " Person %s owns no voip addresses" % (old_id)) else: logger.warn("Source person %s owns voip addresses: %s" % (old_id, old_person_voip_addr)) logger.warn("Target person %s owns voip addresses:%s" % (new_id, new_person_voip_addr)) db.rollback() logger.warn("Cannot transfer, rollback all changes." "Manual intervention required to join voip objects.") sys.exit(1)