def api_register_user(*, UserID, Phone, name, Pass): print(UserID) if not UserID: raise APIValueError('身份证号') if not name: raise APIValueError('姓名') if not Pass or not _RE_SHA1.match(Pass): raise APIValueError('密码') if not Phone: raise APIValueError('手机号') users = yield from User.findAll('Phone=?', [Phone]) if len(users) > 0: raise APIError('register:failed', 'phone', 'Phone is already in use.') sha1_Pass = '******' % (Phone, Pass) user = User(UserID=UserID, User=name, Pass=hashlib.sha1(sha1_Pass.encode('utf-8')).hexdigest(), Phone=Phone) yield from user.save() r = web.Response() r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True) user.Pass = '******' r.content_type = 'application/json' r.body = json.dumps(user, ensure_ascii=True).encode('utf-8') return r
def api_register_user(*, email, name, passwd): # https://www.python.org/dev/peps/pep-3102/ if not name or not name.strip(): raise APIValueError('name') if not email or not _RE_EMAIL.match(email): raise APIValueError('email') if not passwd or not _RE_SHA1.match(passwd): raise APIValueError('passwd') users = yield from User.findAll('email=?', [email]) if len(users) > 0: raise APIError('register:failed', 'email', 'email already in use') uid = next_id() sha1_passwd = '%s:%s' % (uid, passwd) # get SHA1 for uid+passwd user = User(id=uid, name=name.strip(), email=email, passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(), image='http://www.gravatar.com/avatar/%s?d=mm&s=120' % hashlib.md5(email.encode('utf-8')).hexdigest()) yield from user.save() # make session cookie: r = web.Response() r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True) user.passwd = '******' r.content_type = 'application/json' r.body = json.dumps(user, ensure_ascii=False).encode('utf-8') return r
def authenticate(*, Phone, Pass): print('进来验证了。。。') if not Phone: raise APIValueError('Phone', 'Invalid PhoneNumber') if not Pass: raise APIValueError('Pass', 'Invalid password') users = yield from User.findAll('Phone=?', [Phone]) if len(users) == 0: raise APIValueError('Phone', 'Phone not exists') user = users[0] # 检查密码 sha1 = hashlib.sha1() sha1.update(user.Phone.encode('utf-8')) sha1.update(b':') sha1.update(Pass.encode('utf-8')) if user.Pass != sha1.hexdigest(): raise APIValueError('password', 'invalid password') # 验证通过,设置cookie r = web.Response() r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True) user.Pass = '******' r.content_type = 'application/json' r.body = json.dumps(user, ensure_ascii=False).encode('utf-8') return r
def api_register(*, name, email, password): logging.info("enter") check_string(name=name) check_email_passwd(email, name) users = yield from User.findAll('email = ?', [email]) if users: raise APIValueError("email", "Email is already in used") uid = next_id() sha1_passwd = '%s:%s' % (email, password) user = User(name=name.strip(), email=email, password=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(), image="/static/img/user.png", admin=0) yield from user.save() #make session cooike r = web.Response() r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True) user.password = '******' r.content_type = "application/json" r.body = json.dumps(user, ensure_ascii=False).encode("utf-8") return r
def authenticate(*, email, passwd): if not email: raise APIValueError('email', 'Invalid email.') if not passwd: raise APIValueError('passwd', 'Invalid password.') users = yield from User.findAll('email=?', [email]) if len(users) == 0: raise APIValueError('email', 'Email not exist.') user = users[0] # check passwd: sha1 = hashlib.sha1() sha1.update(user.id.encode('utf-8')) sha1.update(b':') sha1.update(passwd.encode('utf-8')) if user.passwd != sha1.hexdigest(): raise APIValueError('passwd', 'Invalid password.') # authenticate ok, set cookie: r = web.Response() r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True) user.passwd = '******' r.content_type = 'application/json' r.body = json.dumps(user, ensure_ascii=False).encode('utf-8') return r
def api_register_user(*, email, name, passwd): #判断name是否为空: if not name or not name.strip(): raise APIValueError('name') #判断email是否为空及是否满足email格式: if not email or not _RE_EMAIL.match(email): raise APIValueError('email') #判断password首付为空及是否满足password格式: if not passwd or not _RE_SHA1.match(passwd): raise APIValueError('passwd') #数据中查询对应的email信息: users = yield from User.findAll('email=?', [email]) #判断查询结果是否存在,若存在则返回异常提示邮件已存在: if len(users) > 0: raise APIError('register:failed', 'email', 'Email is already in use.') #生成唯一ID: uid = next_id() #重构唯一ID和password成新的字符串: sha1_passwd = '%s:%s' % (uid, passwd) #构建用户对象信息: #hashlib.sha1().hexdigest():取得SHA1哈希摘要算法的摘要值。 user = User(id=uid, name=name.strip(), email=email, passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(), image='http://www(first).gravatar.com/avatar/%s?d=mm&s=120' % hashlib.md5(email.encode('utf-8')).hexdigest()) #将用户信息存储到数据库: yield from user.save() # make session cookie: #构造session cookie信息: r = web.Response() #aiohttp.web.StreamResponse().set_cookie():设置cookie的方法。 r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True) #max_age:定义cookie的有效期(秒); user.passwd = '******' r.content_type = 'application/json' #以json格式序列化响应信息; ensure_ascii默认为True,非ASCII字符也进行转义。如果为False,这些字符将保持原样。 r.body = json.dumps(user, ensure_ascii=False).encode('utf-8') return r
def authenticate(*, email, passwd): #判断email(用户名)及password是否为空;为空则抛出异常: if not email: raise APIValueError('email', 'Invalid email.') if not passwd: raise APIValueError('passwd', 'Invalid password.') #数据中查询对应的email信息: users = yield from User.findAll('email=?', [email]) #判断查询结果是否存在,若不存在则抛出异常: if len(users) == 0: raise APIValueError('email', 'Email not exist.') #获取查询结果集的第一条数据: user = users[0] # check passwd: #调用摘要算法SHA1组装登陆信息;计算摘要值同数据库中的信息进行比配: sha1 = hashlib.sha1() sha1.update(user.id.encode('utf-8')) sha1.update(b':') sha1.update(passwd.encode('utf-8')) if user.passwd != sha1.hexdigest(): #登陆信息不匹配则跑出异常: raise APIValueError('passwd', 'Invalid password.') # authenticate ok, set cookie: #构造session cookie信息: r = web.Response() #aiohttp.web.StreamResponse().set_cookie():设置cookie的方法。 r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True) user.passwd = '******' r.content_type = 'application/json' #以json格式序列化响应信息; ensure_ascii默认为True,非ASCII字符也进行转义。如果为False,这些字符将保持原样。 r.body = json.dumps(user, ensure_ascii=False).encode('utf-8') return r
def registerUser(*, email, name, password): if not name or not name.strip(): raise APIValueError('name') if not email or not _RE_EMAIL.match(email): raise APIValueError('email') if not password or not password.strip(): raise APIValueError('password') users = yield from User.findAll('email=?', email) if len(users) > 0: raise APIError('register:failed', 'email', 'Email is already in use.') uid = nextId() sha1Password = '******' % (uid, password) sha1 = hashlib.sha1(sha1Password.encode('utf-8')).hexdigest() user = User( id=uid, name=name.strip(), email=email, password=sha1, image= 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1542105917178&di=d1f6b6a11859ff9a2436460ed3c691dd&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2Fbba1cd11728b47104c5c00e9c9cec3fdfc0323a0.jpg' ) yield from user.save() r = web.Response() r.set_cookie(COOKIE_NAME, user2Cookie(user, 86400), max_age=86400, httponly=True) user.password = '******' r.content_type = 'application/json' r.body = json.dumps(user, ensure_ascii=False).encode('utf-8') return r
def api_register_user(*,email,name,passwd): if not name or not name.strip(): raise APIValueError('name') if not email or not _RE_EMAIL.match(email): raise APIValueError('email') if not passwd or not _RE_SHA1.match(passwd): raise APIValueError('passwd') users=yield from User.findAll('email=?',[email]) if len(users)>0: raise APIError('register:failed','email','Email is already in use') uid=next_id() sha1_passwd='%s:%s'%(uid,passwd) user=User(id=uid,name=name.strip(),email=email, passwd=hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest(), image='http://www.gravator.com/avatar/%s?d=mm&s=120'%hashlib.md5(email.encode('utf-8')).hexdigest()) yield from user.save() r=web.Response() r.set_cookie(COOKIE_NAME,user2cookie(user,86400),max_age=86400,httponly=True) user.passwd='******' r.content_type='application/json' r.body=json.dumps(user,ensure_ascii=False).encode('utf-8') return r
def api_get_users(*, page='1'): page_index = get_page_index(page) num = yield from User.findNumber('count(id)') p = Page(num, page_index) if num == 0: return dict(page=p, users=()) users = yield from User.findAll(orderBy='created_at desc', limit=(p.offset, p.limit)) return dict(page=p, users=users)
def apiGetUsers(*, page='1'): pageIndex = getPageIndex(page) num = yield from User.findNumber('count(id)') p = Page(num, pageIndex) if num == 0: return dict(page=p, users=()) users = yield from User.findAll(orderBy='createTime desc', limit=(p.offset, p.limit)) for u in users: u.password = '******' return dict(page=p, users=users)
def manage_users(*, page=1, size=10): num = yield from User.findNumber('count(id)') page = Page(num, set_valid_value(page), set_valid_value(size, 10)) if num == 0: return dict(page=page, users=()) users = yield from User.findAll(orderBy='created_at desc', limit=(page.offset, page.limit + num % page.limit)) for u in users: u.password = '******' return dict(page=page, users=users)
def api_get_users(*, page='1'): #获取页面索引,默认为1: page_index = get_page_index(page) #查询数据库中User表中用户总数: num = yield from User.findNumber('count(id)') p = Page(num, page_index) if num == 0: return dict(page=p, users=()) #查询数据库中User表中对应分页的用户结果;(limit为mysql的分页查询条件) users = yield from User.findAll(orderBy='created_at desc', limit=(p.offset, p.limit)) for u in users: u.passwd = '******' return dict(page=p, users=users)
def authenticate(*,email,passwd): if not email: raise APIValueError('email','Invalid email') if not passwd: raise APIValueError('passwd','Invalid password') users=yield from User.findAll('email=?',[email]) if len(users)==0: raise APIValueError('email','Email not exits') user=users[0] if user.passwd!= passwd: raise APIValueError('passwd', 'Invalid password.') r=web.Response() return r
def api_register_user(*,email,name,passwd): if not name or not name.strip(): raise APIValueError('email','Invalid email.') if not passwd: raise APIValueError('passwd','Invalid password') users=yield from User.findAll('email=?',[email]) if len(users)>0: raise APIValueError('register:failed', 'email', 'Email is already in use.') uid=next_id() user=User(id=uid,name=name,email=email,passwd=passwd,image='http://www.gravatar.com/avatar/%s?d=mm&s=120',created_at='1532590440.177') yield from user.save() r=web.Response() r.set_cookie(COOKIE_NAME,user,max_age=0,httponly=True) r.content_type='' r.body=json.dump(user,ensure_ascii=False).encode('utf-8') return r
def api_register_user(*, email, name, passwd): if not name or not name.strip(): raise APIValueError('name') if not email or not _RE_EMAIL.match(email): raise APIValueError('email') if not passwd or not _RE_SHA1.match(passwd): raise APIValueError('passwd') users = yield from User.findAll('email=?', [email]) if len(users) > 0: raise APIError('register:failed', 'email', 'Email is already in use.') uid = next_id() sha1_password = '******' % (uid, passwd) user = User(id=uid, name=name.strip(), email=email,passwd=hashlib.sha1(sha1_password.encode('utf-8').hexdigest(), image='')) yield from user.save() r = web.Response() r.set_cookie(COOKIE_NAME,user2cookie(user, 86400), max_age=86400, httponly=True) user.passwd = '******' r.content_type = 'application/json' r.body = json.dumps(user,ensure_ascii=False).enconde('utf-8') return r
def api_register_user(*, email, name, passwd): if not name or not name.strip(): raise APIValueError('name') if not email or not _RE_EMAIL.match(email): raise APIValueError('email') if not passwd or not _RE_SHA1.match(passwd): raise APIValueError('passwd') users = yield from User.findAll('email=?', [email]) if len(users) > 0: raise APIError('register:failed', 'email', 'Email is already in use.') uid = next_id() shal_passwd = '%s:%s' % (uid, passwd) user = User(id=uid, name=name.strip(), email=email, passwd=hashlib.sha1(shal_passwd.encode('utf-8')).hexdigest(), image='http://test.download.cycore.cn/test/5ed5fc74-f110-42df-ade8-c5a2f10d572a.png') yield from user.save() # make session cookie: r = web.Response() r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True) user.passwd = '******' r.content_type = 'application/json' r.body = json.dumps(user, ensure_ascii=False).encode('utf-8') return r
def authenticate(*, email, passwd): if not email: raise APIValueError('email', 'Invalid email.') if not passwd: raise APIValueError('passwd', 'Invalid password.') users = yield from User.findAll('email=?', [email]) if len(users) == 0: raise APIValueError('email', 'Email not exist.') user = users[0] # check passwd: sha1 = hashlib.sha1() sha1.update(user.id.encode('utf-8')) sha1.update(b':') sha1.update(passwd.encode('utf-8')) if user.passwd != sha1.hexdigest(): raise APIValueError('passwd', 'Invalid password.') # authenticate ok, set cookie: r = web.Response() r.set_cookie(COOKIE_NAME, user2cookie(user, 86400), max_age=86400, httponly=True) user.passwd = '******' r.content_type = 'application/json' r.body = json.dumps(user, ensure_ascii=False).encode('utf-8') return r
def authenticate(*, email, password): if not email: raise APIValueError('email', 'Invalid email') if not password: raise APIValueError('password', 'Invalid password') users = yield from User.findAll('email=?', email) if len(users) == 0: raise APIValueError('email', 'Email not exist') user = users[0] sha1Password = '******' % (user.id, password) sha1 = hashlib.sha1(sha1Password.encode('utf-8')).hexdigest() if user.password != sha1: raise APIValueError('password', 'Invalid password') r = web.Response() r.set_cookie(COOKIE_NAME, user2Cookie(user, 86400), max_age=86400, httponly=True) user.password = '******' r.content_type = 'application/json' r.body = json.dumps(user, ensure_ascii=False).encode('utf-8') return r
def cookie2user(cookie_str): if not cookie_str: return None try: L = cookie_str.split('-') if len(L) != 3: return None uid, expires, sha1 = L print(uid) if int(expires) < time.time(): return None user = yield from User.findAll('Phone=?', [uid]) if user is None: print('无') return None s = '%s-%s-%s-%s' % (uid, user[0].Pass, expires, _COOKIE_KEY) if sha1 != hashlib.sha1(s.encode('utf-8')).hexdigest(): logging.info('invalid sha1') return None user[0].Pass = '******' return user except Exception as e: logging.exception(e) return None
def api_get_users(): users = yield from User.findAll(orderBy='created_at desc') for u in users: u.passwd = '******' return dict(users=users)
def getUsers(): users = yield from User.findAll() print(users)
def api_get_users(): users = yield from User.findAll() for u in users: u.passwd = '******' return dict(users=users)
def index(request): users = yield from User.findAll() return {'__template__': 'test.html', 'users': users}
def aip_get_user(request): users=yield from User.findAll(orderBy='created_at desc'); return dict(userList=users);
def admin_users(): users = yield from User.findAll() return {'__template__': 'users.html', 'users': users}
def api_get_users(): users = yield from User.findAll() for u in users: u.passwd = '******' return dict(users=users)
def getUsers(): users = yield from User.findAll() print(users)
def api_get_users(): users = yield from User.findAll(orderBy='created_at desc') for u in users: u.passwd = '******' return dict(users=users)