def rm(self, uri, idtype=ID_URI, force=False): us = User(self.dburi, self.db) ur = Uri(self.dburi, self.db) cr = Cred(self.dburi, self.db) try: uid = ur.get_uid(uri) except: if force: return raise try: ur.rm_uid(uid, force=force) except Error: pass try: cr.rm_uid(uid, force=force) except Error: pass try: us.rm(uid, force=force) except Error: pass us.purge() ur.purge() cr.purge() self._reload()
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 passwd(self, uri, password, force): cred = Cred(self.dburi, self.db) user, domain = split_sip_uri(uri) cred.change(username=user, realm=domain, password=password) 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