def rm(self, uid, username, did, force=False): # is canonical? canon_deleted = True cnd, err = cond(CND_NO_DELETED, CND_CANONICAL, username=username, did=did, uid=uid) rows = self.db.select(self.TABLE, 'flags', cnd, limit=1) if not rows: canon_deleted = False # rm cnd, err = cond(CND_NO_DELETED, username=username, did=did, uid=uid) rows = self.db.select(self.TABLE, self.COLUMNS, cnd) if not rows and not force: raise Error (ENOREC, err) for row in rows: nf = set_deleted(row[self.FLAGIDX]) cnd = full_cond(self.COLUMNS, row) self.db.update(self.TABLE, {'flags': nf}, cnd) # set new canon flag if canon_deleted: cnd, err = cond(CND_NO_DELETED, uid=uid) rows = self.db.select(self.TABLE, self.COLUMNS, cnd, limit=1) if rows: nf = set_canonical(rows[0][self.FLAGIDX]) cnd = full_cond(self.COLUMNS, rows[0]) upd = {'flags': nf} self.db.update(self.TABLE, upd, cnd)
def change_domain(self, domain, flags=None, force=False): upd = {} fmask = parse_flags(flags) nflags = new_flags(0, fmask) canonical = is_canonical(nflags) # get did try: did = self.get_did(domain) except: if force: return raise # clear canon if canonical: cnd, err = cond(CND_NO_DELETED, did=did) cnd.append(CND_CANONICAL) rows = self.db.select(self.TABLE, self.COLUMNS, cnd) for row in rows: nf = clear_canonical(row[self.FLAGIDX]) cnd = full_cond(self.COLUMNS, row) upd = {'flags':nf} self.db.update(self.TABLE, upd, cnd) # update flags cnd, err = cond(CND_NO_DELETED, domain=domain) rows = self.db.select(self.TABLE, self.COLUMNS, cnd) if not rows and not force: raise Error (ENOREC, err) for row in rows: nf = new_flags(row[self.FLAGIDX], fmask) cnd = full_cond(self.COLUMNS, row) self.db.update(self.TABLE, {'flags':nf}, cnd)
def rm(self, did, domain, force=False): if self.is_last_domain(did, domain): return self.rm_did(did, force) # is canonical? canon_deleted = True cnd, err = cond(CND_NO_DELETED, CND_CANONICAL, did=did, domain=domain) rows = self.db.select(self.TABLE, 'did', cnd, limit=1) if not rows: canon_deleted = False # remove cnd, err = cond(CND_NO_DELETED, did=did, domain=domain) rows = self.db.select(self.TABLE, self.COLUMNS, cnd) if not rows: if force: return raise Error (ENOREC, err) for row in rows: nf = set_deleted(row[self.FLAGIDX]) cnd = full_cond(self.COLUMNS, row) self.db.update(self.TABLE, {'flags': nf}, cnd) # set new canon flag if canon_deleted: cnd, err = cond(CND_NO_DELETED, did=did) rows = self.db.select(self.TABLE, self.COLUMNS, cnd, limit=1) if rows: nf = set_canonical(rows[0][self.FLAGIDX]) cnd = full_cond(self.COLUMNS, rows[0]) upd = {'flags': nf} self.db.update(self.TABLE, upd, cnd)
def change(self, uid, uri, flags=None, force=False): fmask = parse_flags(flags) nflags = new_flags(0, fmask) canonical = is_canonical(nflags) try: username, did = self.uri2id(uri) except: if force: return raise cndt, err = cond(CND_NO_DELETED, uid=uid, username=username, did=did) if not self.exist(uid, username, did): if force: return raise Error (ENOREC, err) # clear canon if canonical: cnd, err = cond(CND_NO_DELETED, uid=uid) cnd.append(CND_CANONICAL) rows = self.db.select(self.TABLE, self.COLUMNS, cnd) for row in rows: nf = clear_canonical(row[self.FLAGIDX]) cnd = full_cond(self.COLUMNS, row) self.db.update(self.TABLE, {'flags':nf}, cnd) # update flags rows = self.db.select(self.TABLE, self.COLUMNS, cndt) if not rows and not force: raise Error (ENOREC, err) for row in rows: nf = new_flags(row[self.FLAGIDX], fmask) cnd = full_cond(self.COLUMNS, row) self.db.update(self.TABLE, {'flags':nf}, cnd)
def get_domain(self, did): cnd, err = cond(CND_NO_DELETED, CND_CANONICAL, did=did) rows = self.db.select(self.TABLE, 'domain', cnd, limit=1) if not rows: cnd, err = cond(CND_NO_DELETED, did=did) rows = self.db.select(self.TABLE, 'domain', cnd, limit=1) if not rows: raise Error (ENOREC, err) return rows[0][0]
def rm_did(self, did, force=False): cnd, err = cond(CND_NO_DELETED, did=did) rows = self.db.select(self.TABLE, ['username', 'uid'], cnd) if not rows and not force: raise Error (ENOREC, err) for username, uid in rows: self.rm(uid, username, did, force)
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 add(self, did, domain, flags=None, force=False): dflags = self.default_flags() fmask = parse_flags(flags) flags = new_flags(dflags, fmask) canonical = is_canonical(flags) if self.exist(did, domain): if force: return raise Error (EDUPL, errstr(did=did, domain=domain)) # set digest realm attr da = self.Domain_attrs(self.dburi, self.db) da.set_default(did, 'digest_realm', domain) # update canonical flag cnd, err = cond(CND_NO_DELETED, did=did) rows = self.db.select(self.TABLE, self.COLUMNS, cnd) canon_exist = False for row in rows: if not is_canonical(row[self.FLAGIDX]): continue if not canonical: canon_exist = True break f = clear_canonical(row[self.FLAGIDX]) cnd = full_cond(self.COLUMNS, row) self.db.update(self.TABLE, {'flags':f}, cnd) if not canonical and not canon_exist: flags = set_canonical(flags) # add new domain ins = { 'did' : did, 'domain' : domain, 'flags' : flags } self.db.insert(self.TABLE, ins)
def show(self, attrs=[], cols=None, fformat='raw', limit=0): if not cols: cols = self.COLUMNS cidx = col_idx(self.COLIDXS, cols) if not attrs: attrs=[None] rows = [] for attr in attrs: cnd, err = cond(name=attr) rows += self.db.select(self.TABLE, self.COLUMNS, cnd, limit) if limit > 0: rows = rows[:limit] new_rows = [] for row in rows: row[self.FLAGIDX] = cv_flags(fformat, row[self.FLAGIDX]) new_row = [] for i in cidx: new_row.append(row[i]) new_rows.append(new_row) desc = self.db.describe(self.TABLE) desc = [ desc[i] for i in cols ] return new_rows, desc
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 rm(self, username, realm, force=False): try: uid = self.get_uid(username, realm) except: if force: return raise cnd, err = cond(CND_NO_DELETED, uid=uid, realm=realm) return self._rm(cnd, err, force)
def rm_uid(self, uid, force=False): cnd, err = cond(CND_NO_DELETED, uid=uid) rows = self.db.select(self.TABLE, self.COLUMNS, cnd) if not rows and not force: raise Error (ENOREC, err) for row in rows: nf = set_deleted(row[self.FLAGIDX]) cnd = full_cond(self.COLUMNS, row) self.db.update(self.TABLE, {'flags': nf}, cnd)
def show(self, uri=None, cols=None, fformat='raw', limit=0): if uri: try: user, did = self.uri2id(uri) ce = cond(username=user, did=did) except: ce = (CND_FALSE, '') else: ce = (CND_TRUE, '') return self.show_cnd(ce, cols, fformat, limit)
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 change(self, uid=None, flags=None, force=False): fmask = parse_flags(flags) cnd, err = cond(CND_NO_DELETED, uid=uid) # update flags rows = self.db.select(self.TABLE, self.COLUMNS, cnd) if not rows and not force: raise Error (ENOREC, err) for row in rows: nf = new_flags(row[self.FLAGIDX], fmask) cnd = full_cond(self.COLUMNS, row) self.db.update(self.TABLE, {'flags':nf}, cnd)
def canonize(self, uri): domain = split_sip_uri(uri)[1] do = self.Domain(self.dburi, self.db) did = do.get_did(domain) cdomain = do.get_domain(did) uid = self.get_uid(uri) cnd, err = cond(CND_NO_DELETED, CND_CANONICAL, did=did, uid=uid) rows = self.db.select(self.TABLE, 'username', cnd, limit=1) if not rows: raise Error (ENOREC, err) username = rows[0][0] return username + '@' + cdomain
def rm(self, uid=None, force=False): if self.is_used(uid): if force: self._try_rm_orphans(uid) else: raise Error (EUSER, uid) # rm all uids (FIX: or only datetime_created?) cnd, err = cond(CND_NO_DELETED, uid=uid) rows = self.db.select(self.TABLE, self.COLUMNS, cnd) if not rows and not force: raise Error (ENOREC, err) for row in rows: nf = set_deleted(row[self.FLAGIDX]) cnd = full_cond(self.COLUMNS, row) self.db.update(self.TABLE, {'flags': nf}, cnd)
def add(self, uid, flags=None, force=False): dflags = self.default_flags() fmask = parse_flags(flags) flags = new_flags(dflags, fmask) # exist uid? cnd, err = cond(CND_NO_DELETED, uid=uid) rows = self.db.select(self.TABLE, 'uid', cnd, limit=1) if rows: if force: return raise Error (EDUPL, uid) # user add ins = {'uid': uid, 'name': 'datetime_created', \ 'value': timestamp(), 'type': 2, 'flags': flags } self.db.insert(self.TABLE, ins)
def rm(self, attr, value=None, force=False): if not self.exist(attr, value): if force: return if value is None: raise Error (ENOREC, errstr(attr=attr)) else: raise Error (ENOREC, errstr(attr=attr, value=value)) cnd, err = cond(CND_NO_DELETED, name=attr, value=value) rows = self.db.select(self.TABLE, self.COLUMNS, cnd) for row in rows: nf = set_deleted(row[self.FLAGIDX]) cnd = full_cond(self.COLUMNS, row) self.db.update(self.TABLE, {'flags': nf}, cnd) # FIX: purge hack self.purge()
def show_uid(self, uid, cols=None, fformat='raw', limit=0): if not cols: cols = self.COLUMNS cidx = col_idx(self.COLIDXS, cols) cnd, err = cond(uid=uid) rows = self.db.select(self.TABLE, self.COLUMNS, cnd, limit) new_rows = [] for row in rows: row[self.FLAGIDX] = cv_flags(fformat, row[self.FLAGIDX]) new_row = [] for i in cidx: new_row.append(row[i]) new_rows.append(new_row) desc = self.db.describe(self.TABLE) desc = [ desc[i] for i in cols ] return new_rows, desc
def change(self, username, realm, password=None, flags=None, force=False): upd = {} # username & realm is required for password change if password is not None: # compute hashes ha1, ha1b = self.hashes(username, realm, password) upd = {'ha1': ha1, 'ha1b': ha1b, 'password': password} fmask = parse_flags(flags) # update cnd, err = cond(CND_NO_DELETED, auth_username=username, realm=realm) rows = self.db.select(self.TABLE, self.COLUMNS, cnd) if not rows and not force: raise Error (ENOREC, err) for row in rows: cnd = full_cond(self.COLUMNS, row) if flags is not None: nf = new_flags(row[self.FLAGIDX], fmask) upd['flags'] = nf self.db.update(self.TABLE, upd, cnd)
def rm_did(self, did, force=False): da = self.Domain_attrs(self.dburi, self.db) if self.is_used(did): if force: self._try_rm_orphans(did) else: raise Error (EDOMAIN, 'did=%s' % did) da.rm_exist(did, 'digest_realm') # remove did cnd, err = cond(CND_NO_DELETED, did=did) rows = self.db.select(self.TABLE, self.COLUMNS, cnd) if not rows: if force: return raise Error (ENOREC, err) for row in rows: nf = set_deleted(row[self.FLAGIDX]) cnd = full_cond(self.COLUMNS, row) self.db.update(self.TABLE, {'flags': nf}, cnd)
def add(self, uid, uri, flags=None, force=False): dflags = self.default_flags() fmask = parse_flags(flags) flags = new_flags(dflags, fmask) canonical = is_canonical(flags) try: username, did = self.uri2id(uri) except: if force: return raise us = self.User(self.dburi, self.db) if not us.exist(uid) and not force: raise Error (ENOUSER, uid) if self.exist(uid, username, did): if force: return raise Error (EDUPL, username) # update canonical flag cnd, err = cond(CND_NO_DELETED, uid=uid) rows = self.db.select(self.TABLE, self.COLUMNS, cnd) canon_exist = False for row in rows: if not is_canonical(row[self.FLAGIDX]): continue if not canonical: canon_exist = True break f = clear_canonical(row[self.FLAGIDX]) cnd = full_cond(self.COLUMNS, row) self.db.update(self.TABLE, {'flags':f}, cnd) if not canonical and not canon_exist: flags = set_canonical(flags) # add new URI ins = { 'uid' : uid, 'did' : did, 'username' : username, \ 'flags' : flags } self.db.insert(self.TABLE, ins)
def rm_realm(self, realm, force=False): cnd, err = cond(CND_NO_DELETED, realm=realm) return self._rm(cnd, err, force)
def exist_realm(self, realm): cnd, err = cond(CND_NO_DELETED, realm=realm) rows = self.db.select(self.TABLE, 'realm', cnd, limit=1) return rows != []
def show(self, realm=None, username=None, cols=None, fformat='raw', limit=0): cnd, err = cond(auth_username=username, realm=realm) return self._show(cnd, err, cols, fformat, limit)
def show_uid(self, uid, cols=None, fformat='raw', limit=0): cnd, err = cond(uid=uid) return self._show(cnd, err, cols, fformat, limit)
def rm_uid(self, uid, force=False): cnd, err = cond(CND_NO_DELETED, uid=uid) return self._rm(cnd, err, force)
def exist_username_did(self, username, did): cnd, err = cond(CND_NO_DELETED, username=username, did=did) rows = self.db.select(self.TABLE, 'uid', cnd, limit=1) return rows != []
def exist_did(self, did): cnd, err = cond(CND_NO_DELETED, did=did) rows = self.db.select(self.TABLE, 'did', cnd, limit=1) return rows != []