def get(self): form = AccountActiveForm(self.request.arguments) if not form: raise HTTPError(404) response_data = {} uid = form.uid.data code = form.code.data user = yield UserDocument.find_one({'_id': ObjectId(uid)}) if not user: raise HTTPError(404) code = yield CodeDocument.find_one({'uid': user["_id"], 'code': code}) if not code: raise HTTPError(404) if user['activated']: response_data.update({'error': '该账号已经激活!'}) elif code['expired_time'] < datetime.now(): response_data.update({'error': '激活码已失效! 请返回到登录界面重新发送激活码!'}) else: yield UserDocument.update({'_id': user['_id']}, {'$set': { 'activated': True }}) response_data.update({'error': '激活成功!'}) yield CodeDocument.remove({'_id': code['_id']}) self.render('user/template/feedback.html', response_data=response_data)
def post(self): form = PasswordResetSendmailForm(self.request.arguments) if not form.validate(): raise HTTPError(404) response_data = {} email = form.email.data user = yield UserDocument.find_one({'email': email}) if not user: raise HTTPError(404) if not user['activated']: response_data.update({'error': '该账号尚未激活, 请先激活账号!'}) else: document = { 'uid': user["_id"], 'code': CodeDocument.generate_code(), 'expired_time': datetime.now() + timedelta(days=USER_SETTINGS['code_expired_after']) } yield CodeDocument.remove({"uid": user["_id"]}, multi=True) code_id = yield CodeDocument.insert(document) WriterManager.pub(MessageTopic.SEND_RESET_PASSWORD_EMAIL, code_id) self.finish(json.dumps(response_data))
def post(self): form = PasswordResetSendmailForm(self.request.arguments) if not form.validate(): raise HTTPError(404) response_data = {} email = form.email.data user = yield UserDocument.find_one({'email': email}) if not user: raise HTTPError(404) if not user['activated']: response_data.update({'error': '该账号尚未激活, 请先激活账号!'}) else: document = { 'uid': user["_id"], 'code': CodeDocument.generate_code(), 'expired_time': datetime.now() + timedelta( days=USER_SETTINGS['code_expired_after'] ) } yield CodeDocument.remove({"uid": user["_id"]}, multi=True) code_id = yield CodeDocument.insert(document) WriterManager.pub(MessageTopic.SEND_RESET_PASSWORD_EMAIL, code_id) self.write_json(response_data)
def get(self): form = AccountActiveForm(self.request.arguments) if not form: raise HTTPError(404) response_data = {} uid = form.uid.data code = form.code.data user = yield UserDocument.find_one({'_id': ObjectId(uid)}) if not user: raise HTTPError(404) code = yield CodeDocument.find_one({'uid': user["_id"], 'code': code}) if not code: raise HTTPError(404) if user['activated']: response_data.update({'error': '该账号已经激活!'}) elif code['expired_time'] < datetime.now(): response_data.update({ 'error': '激活码已失效! 请返回到登录界面重新发送激活码!' }) else: yield UserDocument.update( {'_id': user['_id']}, {'$set': {'activated': True}} ) response_data.update({'error': '激活成功!'}) yield CodeDocument.remove({'_id': code['_id']}) self.render('user/template/feedback.html', response_data=response_data)
def post(self): form = AccountActiveSendmailForm(self.request.arguments) if not form.validate(): raise HTTPError(404) response_data = {} email = form.email.data user = yield UserDocument.find_one({'email': email}) if not user: raise HTTPError(404) if user['activated']: response_data.update({'error': '该账号已经激活!'}) else: document = { 'uid': user["_id"], 'code': CodeDocument.generate_code(), 'expired_time': datetime.now() + timedelta(days=USER_SETTINGS['code_expired_after']) } yield CodeDocument.remove({"uid": user["_id"]}, multi=True) code_id = yield CodeDocument.insert(document) WriterManager.pub(MessageTopic.SEND_ACTIVATION_EMAIL, code_id) self.write_json(response_data)
def get(self): # 刷新失效 session_id = self.get_secure_cookie('sid') if session_id: session = self.session_manager.load_session(session_id) yield CodeDocument.remove({ 'uid': ObjectId(session["uid"]), 'code': session["code"] }) session.clear() self.clear_cookie("sid") form = PasswordResetGetForm(self.request.arguments) if not form: raise HTTPError(404) response_data = {} uid = form.uid.data code = form.code.data user = yield UserDocument.find_one({'_id': ObjectId(uid)}) if not user: raise HTTPError(404) code = yield CodeDocument.find_one({ 'uid': user["_id"], 'code': code }) if not code or datetime.now() > code['expired_time']: response_data.update({ 'error': '验证码已失效! 请返回到登录界面重新发送验证邮件!' }) if response_data: self.clear_cookie('sid') self.render( 'user/template/feedback.html', response_data=response_data ) else: session = self.session_manager.new_session() session["uid"] = uid session["code"] = code["code"] self.set_secure_cookie('sid', session.id, httponly=True) self.render('user/template/password-reset.html')
def post(self): form = PasswordResetPostForm(self.request.arguments) if not form: raise HTTPError(404) password = form.password.data session_id = self.get_secure_cookie('sid') if not session_id: raise HTTPError(404) self.session = self.session_manager.load_session(session_id) uid = self.session.get('uid') code = self.session.get('code') if not uid or not code: raise HTTPError(404) code = yield CodeDocument.find_one({ 'uid': ObjectId(uid), 'code': code }) if not code: raise HTTPError(404) user = yield UserDocument.find_one({'_id': ObjectId(uid)}) if not user: raise HTTPError(404) password = yield UserDocument.encrypt_password(password) yield UserDocument.update({'_id': user["_id"]}, {'$set': { 'password': password }}) yield CodeDocument.remove({'_id': ObjectId(code['_id'])}) try: Ejabberd.unregister(user['_id']) Ejabberd.register(user['_id'], password) except: pass self.session.clear() self.clear_cookie('sid') self.finish()
def post(self): form = PasswordResetPostForm(self.request.arguments) if not form: raise HTTPError(404) password = form.password.data session_id = self.get_secure_cookie('sid') if not session_id: raise HTTPError(404) self.session = self.session_manager.load_session(session_id) uid = self.session.get('uid') code = self.session.get('code') if not uid or not code: raise HTTPError(404) code = yield CodeDocument.find_one({ 'uid': ObjectId(uid), 'code': code }) if not code: raise HTTPError(404) user = yield UserDocument.find_one({'_id': ObjectId(uid)}) if not user: raise HTTPError(404) password = yield UserDocument.encrypt_password(password) yield UserDocument.update( {'_id': user["_id"]}, {'$set': {'password': password}} ) yield CodeDocument.remove({'_id': ObjectId(code['_id'])}) try: Ejabberd.unregister(user['_id']) Ejabberd.register(user['_id'], password) except: pass self.session.clear() self.clear_cookie('sid') self.finish()
def get(self): # 刷新失效 session_id = self.get_secure_cookie('sid') if session_id: session = self.session_manager.load_session(session_id) yield CodeDocument.remove({ 'uid': ObjectId(session["uid"]), 'code': session["code"] }) session.clear() self.clear_cookie("sid") form = PasswordResetGetForm(self.request.arguments) if not form: raise HTTPError(404) response_data = {} uid = form.uid.data code = form.code.data user = yield UserDocument.find_one({'_id': ObjectId(uid)}) if not user: raise HTTPError(404) code = yield CodeDocument.find_one({'uid': user["_id"], 'code': code}) if not code or datetime.now() > code['expired_time']: response_data.update({'error': '验证码已失效! 请返回到登录界面重新发送验证邮件!'}) if response_data: self.clear_cookie('sid') self.render('user/template/feedback.html', response_data=response_data) else: session = self.session_manager.new_session() session["uid"] = uid session["code"] = code["code"] self.set_secure_cookie('sid', session.id, httponly=True) self.render('user/template/password-reset.html')
def _send_email_handler(message, type_="activate"): from app.user.document import CodeDocument, UserDocument code_id = message.body code = CodeDocument.get_collection(True).find_one({ '_id': ObjectId(code_id) }) if not code: return True user = UserDocument.get_collection(True).find_one({ "_id": ObjectId(code["uid"]) }) if not user: return True if type_ == "activate": subject = "账号激活【Young社区】" tpl = ''.join([ '请点击链接激活你的Young社区帐号!', '<a href="{0}/account/active?uid={1}&code={2}">', '{0}/account/active?uid={1}&code={2}</a>' ]) else: subject = "密码重置【Young社区】" tpl = ''.join([ '请点击链接重置你的Young社区账号密码!', '<a href="{0}/password/reset?uid={1}&code={2}">', '{0}/password/reset?uid={1}&code={2}</a>' ]) body = tpl.format(EMAIL_SETTINGS['url'], code['uid'], code['code']) msg = MIMEText(body, "html", 'utf-8') msg["subject"] = subject try: send_email(user['email'], msg) except Exception as e: logging.error('%s' % e) return True
def _send_email_handler(message, type_="activate"): from app.user.document import CodeDocument, UserDocument code_id = message.body code = CodeDocument.get_collection(True).find_one( {'_id': ObjectId(code_id)}) if not code: return True user = UserDocument.get_collection(True).find_one( {"_id": ObjectId(code["uid"])}) if not user: return True if type_ == "activate": subject = "账号激活【Young社区】" tpl = ''.join([ '请点击链接激活你的Young社区帐号!', '<a href="{0}/account/active?uid={1}&code={2}">', '{0}/account/active?uid={1}&code={2}</a>' ]) else: subject = "密码重置【Young社区】" tpl = ''.join([ '请点击链接重置你的Young社区账号密码!', '<a href="{0}/password/reset?uid={1}&code={2}">', '{0}/password/reset?uid={1}&code={2}</a>' ]) body = tpl.format(EMAIL_SETTINGS['url'], code['uid'], code['code']) msg = MIMEText(body, "html", 'utf-8') msg["subject"] = subject try: send_email(user['email'], msg) except Exception as e: logging.error('%s' % e) return True
def post(self): response_data = {} form = RegisterForm(self.request.arguments) if form.validate(): name = form.name.data email = form.email.data password = form.password.data if (yield UserDocument.find_one({'name': name})): response_data["error"] = "用户名已被占用" if (yield UserDocument.find_one({"email": email})): response_data["error"] = "邮箱已被注册" if not response_data: password = yield UserDocument.encrypt_password(password) document = { 'email': email, 'name': name, 'password': password, 'user_type': "person", 'register_date': datetime.now() } try: user_id = yield UserDocument.insert(document) except: raise HTTPError(500) # 头像初始化 avatar = open( os.path.join(APPLICATION_SETTINGS['static_path'], 'img/default.jpg')) content = avatar.read() avatar.close() document = { 'name': 'default.jpg', 'upload_time': datetime.now(), 'content_type': 'jpeg', 'owner': DBRef(UserDocument.meta['collection'], ObjectId(user_id)), 'content': Binary(content), 'thumbnail50x50': Binary(content), 'thumbnail180x180': Binary(content) } yield AvatarDocument.insert(document) # 用户设置初始化 _ = yield OfficialProfileCoverDocument.get_profile_cover_list() profile_cover = random.sample(_, 1)[0] document = { 'user': DBRef(UserDocument.meta['collection'], ObjectId(user_id)), 'profile_cover': DBRef(OfficialProfileCoverDocument.meta['collection'], ObjectId(profile_cover['_id'])) } yield UserSettingDocument.insert(document) # Ejabberd注册 try: Ejabberd.register(user_id, password) except: pass # 给用户发送验证邮件 document = { 'uid': user_id, 'code': CodeDocument.generate_code(), 'expired_time': datetime.now() + timedelta(days=USER_SETTINGS['code_expired_after']) } code_id = yield CodeDocument.insert(document) WriterManager.pub(MessageTopic.SEND_ACTIVATION_EMAIL, code_id) response_data.update( {'success': '注册成功! 系统已经向你的注册邮箱发送了一封激活' '邮件, 请验证后登录!'}) else: for field in form.errors: response_data.update({'error': form.errors[field][0]}) break self.finish(json.dumps(response_data))
def post(self): response_data = {} form = RegisterForm(self.request.arguments) if form.validate(): name = form.name.data email = form.email.data password = form.password.data if (yield UserDocument.find_one({'name': name})): response_data["error"] = "用户名已被占用" if (yield UserDocument.find_one({"email": email})): response_data["error"] = "邮箱已被注册" if not response_data: password = yield UserDocument.encrypt_password(password) document = { 'email': email, 'name': name, 'password': password, 'user_type': "person", 'register_date': datetime.now() } try: user_id = yield UserDocument.insert(document) except: raise HTTPError(500) # 头像初始化 avatar = open(os.path.join( APPLICATION_SETTINGS['static_path'], 'img/default.jpg') ) content = avatar.read() avatar.close() document = { 'name': 'default.jpg', 'upload_time': datetime.now(), 'content_type': 'jpeg', 'owner': DBRef( UserDocument.meta['collection'], ObjectId(user_id) ), 'content': Binary(content), 'thumbnail50x50': Binary(content), 'thumbnail180x180': Binary(content) } yield AvatarDocument.insert(document) # 用户设置初始化 _ = yield OfficialProfileCoverDocument.get_profile_cover_list() profile_cover = random.sample(_, 1)[0] document = { 'user': DBRef( UserDocument.meta['collection'], ObjectId(user_id) ), 'profile_cover': DBRef( OfficialProfileCoverDocument.meta['collection'], ObjectId(profile_cover['_id']) ) } yield UserSettingDocument.insert(document) # Ejabberd注册 try: Ejabberd.register(user_id, password) except: pass # 给用户发送验证邮件 document = { 'uid': user_id, 'code': CodeDocument.generate_code(), 'expired_time': datetime.now() + timedelta( days=USER_SETTINGS['code_expired_after'] ) } code_id = yield CodeDocument.insert(document) WriterManager.pub(MessageTopic.SEND_ACTIVATION_EMAIL, code_id) response_data.update({ 'success': '注册成功! 系统已经向你的注册邮箱发送了一封激活' '邮件, 请验证后登录!' }) else: for field in form.errors: response_data.update({'error': form.errors[field][0]}) break self.write_json(response_data)