def auth(user_id): """ 用户登录认证信息 """ form = UserAuthForm(request.form) condition = { 'user_id': user_id, 'type_auth': TYPE_AUTH_ACCOUNT, } user_auth_info = get_user_auth_row(**condition) if user_auth_info: form.id.data = user_auth_info.id form.type_auth.data = user_auth_info.type_auth form.create_time.data = user_auth_info.create_time form.update_time.data = user_auth_info.update_time if request.method == 'GET': form.id.data = user_auth_info.id form.user_id.data = user_id form.auth_key.data = user_auth_info.auth_key form.auth_secret.data = '' form.status_verified.data = user_auth_info.status_verified if request.method == 'POST': if form.validate_on_submit(): # 权限校验 condition = { 'id': form.id.data, 'user_id': user_id, 'type_auth': TYPE_AUTH_ACCOUNT, } op_right = get_user_auth_row(**condition) if not op_right: flash(u'修改失败', 'warning') return redirect(url_for('index')) current_time = datetime.utcnow() user_auth_data = { # 'type_auth': TYPE_AUTH_ACCOUNT, 'auth_key': form.auth_key.data, # 'status_verified': form.status_verified.data, 'update_time': current_time, } if form.auth_secret.data: user_auth_data['auth_secret'] = md5(form.auth_secret.data) result = edit_user_auth(form.id.data, user_auth_data) if result: flash(u'修改成功', 'success') return redirect(url_for('.auth', user_id=user_id)) else: flash(u'信息不变', 'info') else: flash(u'修改失败', 'warning') # flash(form.errors, 'warning') # 调试打开 # flash(u'Hello, %s' % current_user.id, 'info') # 测试打开 return render_template('user/auth.html', title='auth', form=form)
def __call__(self, form, field): condition = [ UserAuth.type_auth == form.type_auth.data, UserAuth.auth_key == field.data, ] row = get_user_auth_row(*condition) if row: raise ValidationError(self.message or _('Data duplication'))
def index(): """ 账号登录认证 """ if current_user and current_user.is_authenticated: return redirect(url_for('index')) template_name = 'user_auth/index.html' # 文档信息 document_info = DOCUMENT_INFO.copy() document_info['TITLE'] = _('Sign in with account') # 加载表单 form = UserAuthForm(request.form) # 进入页面 if request.method == 'GET': # 渲染页面 return render_template( template_name, form=form, t=get_tc(), **document_info ) # 处理认证 if request.method == 'POST': # 表单校验失败 if not form.validate_on_submit(): flash(_('Auth Failure'), 'danger') return render_template( template_name, form=form, t=get_tc(), **document_info ) # 表单校验成功 condition = { 'type_auth': TYPE_AUTH_ACCOUNT, 'auth_key': form.auth_key.data, # 'auth_secret': form.auth_secret.data } user_auth_info = get_user_auth_row(**condition) if not user_auth_info: form.auth_key.errors.append(_('Username Error')) flash(_('Auth Failure'), 'danger') return render_template( template_name, form=form, t=get_tc(), **document_info ) if user_auth_info.status_verified != STATUS_VERIFIED_OK: form.auth_key.errors.append(_('Need Verify')) flash(_('Auth Failure'), 'danger') return render_template( template_name, form=form, t=get_tc(), **document_info ) if user_auth_info.auth_secret != form.auth_secret.data: form.auth_secret.errors.append(_('Password Error')) flash(_('Auth Failure'), 'danger') return render_template( template_name, form=form, t=get_tc(), **document_info ) # 认证成功 # 用户登录 login_user(get_login_user_row_by_id(user_auth_info.user_id), remember=form.remember.data) # 加载权限信号通知(Tell Flask-Principal the identity changed) identity_changed.send(app, identity=Identity(user_auth_info.user_id)) flash(_('Auth Success'), 'success') return redirect(request.args.get('next') or url_for('index'))
def change_password(): """ 修改密码 """ template_name = 'user_auth/change_password.html' document_info = DOCUMENT_INFO.copy() document_info['TITLE'] = _('change password') # 加载表单 form = UserAuthChangePasswordForm(request.form) if request.method == 'POST': # 表单校验失败 if not form.validate_on_submit(): flash(_('Change Password Failure'), 'danger') return render_template( template_name, form=form, **document_info ) # 获取 user_auth_id condition = { 'user_id': current_user.id, 'type_auth': TYPE_AUTH_ACCOUNT, } user_auth_info = get_user_auth_row(**condition) if not user_auth_info: flash(_('Cann\'t Change Password'), 'danger') return render_template( template_name, form=form, **document_info ) elif user_auth_info.auth_secret != form.password_current.data: flash(_('Current Password Error'), 'danger') return render_template( template_name, form=form, **document_info ) # 表单校验成功 user_auth_id = user_auth_info.id current_time = datetime.utcnow() user_auth_data = { 'auth_secret': form.password_new.data, 'status_verified': STATUS_VERIFIED_OK, 'update_time': current_time, } result = edit_user_auth(user_auth_id, user_auth_data) # 编辑操作成功 if result: flash(_('Edit Success'), 'success') return redirect(request.args.get('next') or url_for('change_password')) # 编辑操作失败 else: flash(_('Edit Failure'), 'danger') return render_template( template_name, form=form, **document_info ) return render_template( template_name, form=form, **document_info )
def email(): """ 邮箱登录(邮箱链接登录) """ if current_user and current_user.is_authenticated: return redirect(url_for('index')) template_name = 'user_auth/email.html' # 文档信息 document_info = DOCUMENT_INFO.copy() document_info['TITLE'] = _('Sign in with email') # 加载表单 form = UserAuthEmailForm(request.form) # 进入页面 if request.method == 'GET': # 渲染页面 return render_template( template_name, form=form, t=get_tc(), **document_info ) # 处理认证 if request.method == 'POST': # 表单校验失败 if not form.validate_on_submit(): flash(_('Auth Failure'), 'danger') return render_template( template_name, form=form, t=get_tc(), **document_info ) # 表单校验成功 condition = { 'type_auth': TYPE_AUTH_EMAIL, 'auth_key': form.auth_key.data, # 'auth_secret': form.auth_secret.data } user_auth_info = get_user_auth_row(**condition) if not user_auth_info: form.auth_key.errors.append(_('Email not exist')) flash(_('Auth Failure'), 'danger') return render_template( template_name, form=form, t=get_tc(), **document_info ) user_id = user_auth_info.user_id auth_token_obj = AuthToken(app.secret_key) token = auth_token_obj.create(user_id) # send email task message = json.dumps( { 'name': '尊敬的用户', 'email': form.auth_key.data, 'link': url_for('sign', auth_token=token, _external=True, _scheme=PREFERRED_URL_SCHEME) } ) result = pub(form.auth_key.data, message) if not result: flash(_('Repeat application, Enter mailbox, click the authentication link to sign in'), 'success') else: flash(_('Auth Success, Enter mailbox, click the authentication link to sign in'), 'success') return render_template( template_name, form=form, t=get_tc(), **document_info )
def edit(user_id): """ 用户编辑 """ user_info = get_user_row_by_id(user_id) # 检查资源是否存在 if not user_info: abort(404) # 检查资源是否删除 if user_info.status_delete == STATUS_DEL_OK: abort(410) template_name = 'user/edit.html' # 加载编辑表单 form = UserEditForm(request.form) form.id.data = user_id # id 仅作为编辑重复校验 # 文档信息 document_info = DOCUMENT_INFO.copy() document_info['TITLE'] = _('user edit') # 进入编辑页面 if request.method == 'GET': # 表单赋值 form.id.data = user_info.id form.name.data = user_info.name form.salutation.data = user_info.salutation form.mobile.data = user_info.mobile form.tel.data = user_info.tel form.fax.data = user_info.fax form.email.data = user_info.email form.role_id.data = user_info.role_id form.create_time.data = user_info.create_time form.update_time.data = user_info.update_time # 渲染页面 return render_template( template_name, user_id=user_id, form=form, **document_info ) # 处理编辑请求 if request.method == 'POST': # 表单校验失败 if not form.validate_on_submit(): flash(_('Edit Failure'), 'danger') # flash(form.errors, 'danger') return render_template( template_name, user_id=user_id, form=form, **document_info ) # 非系统角色,仅能修改自己的信息 if not permission_role_administrator.can(): if getattr(current_user, 'id') != form.id.data: flash(_('Permission denied, only the user\'s own information can be modified'), 'danger') # flash(form.errors, 'danger') return render_template( template_name, user_id=user_id, form=form, **document_info ) # 表单校验成功 # 编辑用户基本信息 current_time = datetime.utcnow() user_data = { 'name': form.name.data, 'salutation': form.salutation.data, 'mobile': form.mobile.data, 'tel': form.tel.data, 'fax': form.fax.data, 'email': form.email.data, 'role_id': form.role_id.data, 'update_time': current_time, } result = edit_user(user_id, user_data) if not result: # 编辑操作失败 flash(_('Edit Failure'), 'danger') return render_template( template_name, user_id=user_id, form=form, **document_info ) user_auth_row = get_user_auth_row(user_id=user_id) if not user_auth_row: # 编辑操作失败 flash(_('Edit Failure'), 'danger') return render_template( template_name, user_id=user_id, form=form, **document_info ) # 编辑用户认证信息 user_auth_data = { 'user_id': user_id, 'type_auth': TYPE_AUTH_ACCOUNT, 'auth_key': form.name.data, 'update_time': current_time, } result = edit_user_auth(user_auth_row.id, user_auth_data) if not result: # 编辑操作失败 flash(_('Edit Failure'), 'danger') return render_template( template_name, user_id=user_id, form=form, **document_info ) # 编辑操作成功 flash(_('Edit Success'), 'success') return redirect(request.args.get('next') or url_for('user.lists'))