def get_uids_for_username(self, username): cnd, err = cond(CND_NO_DELETED, username=username) rows = self.db.select(self.TABLE, 'uid', cnd) if not rows: raise Error (ENOREC, err) uids = [ i[0] for i in rows ] return uniq(uids)
def rm(self, domains, force=False): do = Domain(self.dburi, self.db) da = Domain_attrs(self.dburi, self.db) ur = Uri(self.dburi, self.db) cr = Cred(self.dburi, self.db) domains = uniq(domains) doms = [] for d in domains: try: did = do.get_did(d) except: if not force: raise Error (ENODOMAIN, d) doms.append((did, d)) for i, d in doms: try: ur.rm_did(i, force=force) except: pass try: cr.rm_realm(d, force=force) except: pass do.rm(i, d, force=force) do.purge() ur.purge() cr.purge() da.purge() self._reload()
def get_uids(self, uri): username, did = self.uri2id(uri) cnd, err = cond(CND_NO_DELETED, username=username, did=did) rows = self.db.select(self.TABLE, 'uid', cnd) if not rows: raise Error (ENOREC, err) uids = [ i[0] for i in rows ] return uniq(uids)
def set_many(self, alist, flags=None, force=False): attrs = [ i[0] for i in alist ] attrs = uniq(attrs) for a in attrs: if self.exist(a): self.rm(a, force=force) for a, v in alist: self.add(a, v, flags, force)
def get_uid(self, username, realm): cnd, err = cond(CND_NO_DELETED, auth_username=username, realm=realm) rows = self.db.select(self.TABLE, 'uid', cnd) if not rows: raise Error (ENOREC, err) uids = uniq([ i[0] for i in rows ]) if len(uids) > 1: raise Error (EDB, '%s@%s=%s' % (username, realm, str(uids))) uid = uids[0] return uid
def rm(self, aliases, force=False): ur = Uri(self.dburi, self.db) aliases = uniq(aliases) for a in aliases: if not ur.exist_uri(a) and not force: raise Error (ENOREC, a) for a in aliases: try: ur.rm_uri(a, force=force) except Error, inst: warning(str(inst))
def add(self, uri, aliases, force=False): do = Domain(self.dburi, self.db) ur = Uri(self.dburi, self.db) us = User(self.dburi, self.db) try: uid = ur.get_uid(uri) except: if force: return raise try: user, did = ur.uri2id(uri) except: if force: return raise if not us.exist(uid): if force: return raise Error (ENOUSER, user) aliases = uniq(aliases) try: n = aliases.index(uri) aliases = aliases[:n] + aliases[n+1:] except: pass for a in aliases: if ur.exist_uri(a) and not force: raise Error (EDUPL, a) u, d = split_sip_uri(a) if not do.exist_domain(d) and not force: raise Error (ENODOMAIN, d) for a in aliases: try: ur.add(uid, a, force=force) except Error, inst: warning(str(inst))
def add(self, domain, aliases=[], idtype=ID_URI, force=False): do = Domain(self.dburi, self.db) did = id(domain, idtype) if do.exist_did(did) and not force: raise Error (EDOMAIN, errstr(did=did)) if do.exist_domain(domain) and not force: raise Error (EDOMAIN, domain) ualiases = uniq(aliases) naliases = [] for alias in ualiases: if do.exist(did, alias): if force: continue raise Error (EDUPL, errstr(did=did, domain=alias)) else: naliases.append(alias) do.add(did, domain, None, force) for alias in naliases: do.add(did, alias, None, force) self._reload()
def add(self, uri, aliases, password, idtype=ID_URI, force=False): do = Domain(self.dburi, self.db) da = Domain_attrs(self.dburi, self.db) ur = Uri(self.dburi, self.db) cr = Cred(self.dburi, self.db) us = User(self.dburi, self.db) user, domain = split_sip_uri(uri) aliases = uniq(aliases) try: n = aliases.index(uri) aliases = aliases[:n] + aliases[n+1:] except: pass aliases = [ split_sip_uri(a) for a in aliases ] uid = id(user + '@' + domain, idtype) if us.exist(uid): if not force: raise Error (EDUPL, errstr(uid=uid)) if cr.exist(user, domain): if not force: raise Error (EDUPL, errstr(auth_username=user, domain=domain)) if not do.exist_domain(domain): if force: i = id(domain, idtype) do.add(i, domain, force=force) else: raise Error (ENODOMAIN, domain) did = do.get_did(domain) try: realm = da.get(did, 'digest_realm') except: if force: realm = domain else: raise for u, d in aliases: if not do.exist_domain(d): if force: i = id(d, idtype) do.add(i, d, force=force) else: raise Error (ENODOMAIN, d) did = do.get_did(d) if ur.exist_username_did(u, did) and not force: raise Error (EDUPL, '%s@%s' % (u, d)) did = do.get_did(domain) if ur.exist(uid, user, did): if not force: raise Error(EDUPL, errstr(uid=uid, username=user, did=did)) us.add(uid, force=force) cr.add(uid, user, did, realm, password, force=force) ur.add(uid, uri, force=force) for u, d in aliases: uri = '%s@%s' % (u, d) try: ur.add(uid, uri, force=force) except Error, inst: warning(str(inst))
def show(self, uri, cols=None, fformat=False, limit=0): ur = Uri(self.dburi, self.db) us = User(self.dburi, self.db) ua = User_attrs(self.dburi, self.db) do = Domain(self.dburi, self.db) cr = Cred(self.dburi, self.db) try: uids = ur.get_uids(uri) except: try: uids = ur.get_uids_for_username(uri) except: try: uids = us.get(uri) except: u, d = split_sip_uri(uri) try: uids = [cr.get_uid(u, d)] except: try: uids = cr.get_uids_for_username(u) except: try: uids = cr.get_uids_for_username(uri) except: uids = [] dids = [] # get uris uris = [] for uid in uids: u, d = ur.show_uid(uid, ['uid', 'username', 'did'], fformat=fformat, limit=limit) uris += u for row in uris: dids.append(row[2]) # get credentials creds = [] for uid in uids: u, d = cr.show_uid(uid, ['uid', 'auth_username', 'realm', 'password'], fformat=fformat, limit=limit) creds += u for row in creds: try: did = do.get_did(row[2]) except: continue dids.append(did) dids = uniq(dids) domains = {} for did in dids: try: dom = do.get_domain(did) except: continue domains[did] = dom attrs = {} for uid in uids: ce = cond(CND_NO_DELETED, uid=uid) rows = ua.show_cnd(ce, ['name', 'value'], fformat, limit)[0] line = [] for row in rows: line.append('%s=%s' % tuple(row)) attrs[uid] = ', '.join(line) # show desc = [('uid',), ('source',), ('value',)] ret = [] for u in uris: dom = domains.get(u[2]) uri = '%s@%s' % (u[1], dom) uid = u[0] ret.append([uid, 'uri', uri ]) for c in creds: cred = 'username=%s realm=%s password=%s' % (c[1], c[2], c[3]) uid = c[0] ret.append([uid, 'credentials', cred ]) for uid, attr in attrs.items(): ret.append([uid, 'attr', attr ]) # limit output if limit > 0: ret = ret[:limit] return ret, desc