def calcUserCode(CD): monthcode="%4d%2d"%time.localtime() lsn = RedisCli.get("registion:lsn") if lsn is None: lsn=1 RedisCli.set("registion:lsn",lsn) return "%s%s-%06d"%(CD,monthcode,lsn)
def createSession(userid): ssid = RedisCli.hget("sessions:users",userid) if ssid==None or ssid=="": clearSession(ssid) ssid=uuid.uuid1() RedisCli.hset("sessions:users",userid, ssid) return ssid
def set_right(objectid,rights): """ set list [{"name":"value(0:allow|1:deny|Otherwise:inherits)"},....] """ name = "Security:rights:%s"%objectid for i in rights: RedisCli.hset(name, i["name"], i["value"])
def get_rights(objectid): """ return:rights [{"name":"value(0:allow|1:deny|Otherwise:inherits)"},....] e.g. [{"set_password_others":"0"}] """ return RedisCli.hgetall("Security:rights:%s"%objectid)
def loadSession(ssid): dict = RedisCli.hgetall("sessions:data:%s"%ssid) ss={} for k,v in dict.items(): try: ss[k] = json.loads(v) except: ss[k] = v return ss
def popRegistQueue(): e = RedisCli.lpop("registion:queue") return None if e==None else json.loads(e)
def pushRegistQueue(username,password,info=[]): e = {"username":username,"password":password,"info":info} RedisCli.rpush("registion:queue",json.dumps(e))
def clear_right(objectid): name = "Security:rights:%s"%objectid RedisCli.hdel(name)
def check_right(objectid,right): name = "Security:rights:%s"%objectid return RedisCli.hget(name, right)
def exists(code): return RedisCli.exists("Security:bindCode:%s"%code)
def loadCode(code,keepAlive=False): if keepAlive: js=RedisCli.get("Security:bindCode:%s"%code) else: js = RedisCli.getset("Security:bindCode:%s"%code, "") return None if js=="" else json.loads(js)
def bindCode(code,info={},expire=30): RedisCli.set("Security:bindCode:%s"%code, json.dumps(info),ex=expire*60)
def clearSession(ssid): RedisCli.delete("sessions:data:%s"%ssid)#2小时超时
def updateSession(ssid,dict={}): RedisCli.expire("sessions:data:%s"%ssid, 120*60)#2小时超时 RedisCli.hmset("sessions:data:%s"%ssid, dict)