mirror of
https://github.com/capricornxl/ad-password-self-service.git
synced 2025-08-11 16:20:10 +08:00
修复utils目录自定义模块中的处理逻辑return结果不正确,导致Django无法正常在前台显示结果的BUG
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
from django.forms import fields as c_fields
|
||||
from django import forms as c_forms
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
|
||||
class CheckForm(c_forms.Form):
|
||||
|
@@ -9,10 +9,16 @@
|
||||
from django.shortcuts import render
|
||||
from django.http import HttpResponseRedirect
|
||||
import logging
|
||||
from utils.crypto import Crypto
|
||||
from pwdselfservice.local_settings import TMPID_COOKIE_AGE
|
||||
from utils.crypto_ops import Crypto
|
||||
|
||||
from django.conf import settings
|
||||
from pwdselfservice import crypto_key
|
||||
import os
|
||||
APP_ENV = os.getenv('APP_ENV')
|
||||
if APP_ENV == 'dev':
|
||||
from conf.local_settings_dev import *
|
||||
else:
|
||||
from conf.local_settings import *
|
||||
|
||||
logger = logging.getLogger('django')
|
||||
|
||||
@@ -26,7 +32,7 @@ def code_2_user_id(ops, request, msg_template, home_url, code):
|
||||
'button_click': "window.location.href='%s'" % home_url,
|
||||
'button_display': "返回主页"
|
||||
}
|
||||
return render(request, msg_template, context)
|
||||
return False, context, user_id
|
||||
detail_status, user_info = ops.get_user_detail_by_user_id(user_id)
|
||||
if not detail_status:
|
||||
context = {
|
||||
@@ -34,38 +40,36 @@ def code_2_user_id(ops, request, msg_template, home_url, code):
|
||||
'button_click': "window.location.href='%s'" % home_url,
|
||||
'button_display': "返回主页"
|
||||
}
|
||||
return render(request, msg_template, context)
|
||||
return user_id, user_info
|
||||
return False, context, user_info
|
||||
return True, user_id, user_info
|
||||
|
||||
|
||||
def crypto_id_2_user_info(ops, request, msg_template, home_url, scan_app_tag):
|
||||
try:
|
||||
crypto_tmp_id = request.COOKIES.get('tmpid')
|
||||
if not crypto_tmp_id:
|
||||
logger.error('[异常] 请求方法:%s,请求路径:%s,未能拿到TmpID或会话己超时。' % (request.method, request.path))
|
||||
context = {
|
||||
'msg': "会话己超时,请重新扫码验证用户信息。",
|
||||
'button_click': "window.location.href='%s'" % home_url,
|
||||
'button_display': "返回主页"
|
||||
}
|
||||
return False, context
|
||||
# 解密
|
||||
crypto = Crypto(crypto_key)
|
||||
user_id = crypto.decrypt(crypto_tmp_id)
|
||||
# 通过user_id拿到用户的邮箱,并格式化为username
|
||||
userid_status, user_info = ops.get_user_detail_by_user_id(user_id)
|
||||
if not userid_status:
|
||||
context = {
|
||||
'msg': '获取{}用户信息失败,错误信息:{}'.format(user_info, scan_app_tag),
|
||||
'button_click': "window.location.href='%s'" % home_url,
|
||||
'button_display': "返回主页"
|
||||
}
|
||||
return False, context
|
||||
return True, user_info
|
||||
except Exception as e:
|
||||
crypto_tmp_id = None
|
||||
logger.error('[异常] :%s' % str(e))
|
||||
if not crypto_tmp_id:
|
||||
logger.error('[异常] 请求方法:%s,请求路径:%s,未能拿到TmpID或会话己超时。' % (request.method, request.path))
|
||||
context = {
|
||||
'msg': "会话己超时,请重新扫码验证用户信息。",
|
||||
'button_click': "window.location.href='%s'" % home_url,
|
||||
'button_display': "返回主页"
|
||||
}
|
||||
return render(request, msg_template, context)
|
||||
# 解密
|
||||
crypto = Crypto(crypto_key)
|
||||
user_id = crypto.decrypt(crypto_tmp_id)
|
||||
# 通过user_id拿到用户的邮箱,并格式化为username
|
||||
userid_status, user_info = ops.get_user_detail_by_user_id(user_id)
|
||||
if not userid_status:
|
||||
context = {
|
||||
'msg': '获取{}用户信息失败,错误信息:{}'.format(user_info, scan_app_tag),
|
||||
'button_click': "window.location.href='%s'" % home_url,
|
||||
'button_display': "返回主页"
|
||||
}
|
||||
return render(request, msg_template, context)
|
||||
|
||||
return user_info
|
||||
return False, str(e)
|
||||
|
||||
|
||||
def crypto_user_id_2_cookie(user_id):
|
||||
@@ -81,6 +85,9 @@ def crypto_user_id_2_cookie(user_id):
|
||||
def crypto_id_2_user_id(request, msg_template, home_url):
|
||||
try:
|
||||
crypto_tmp_id = request.COOKIES.get('tmpid')
|
||||
# 解密
|
||||
crypto = Crypto(crypto_key)
|
||||
return True, crypto.decrypt(crypto_tmp_id)
|
||||
except Exception as e:
|
||||
logger.error('[异常] :%s' % str(e))
|
||||
logger.error('[异常] 请求方法:%s,请求路径:%s,未能拿到TmpID或会话己超时。' % (request.method, request.path))
|
||||
@@ -89,10 +96,7 @@ def crypto_id_2_user_id(request, msg_template, home_url):
|
||||
'button_click': "window.location.href='%s'" % home_url,
|
||||
'button_display': "返回主页"
|
||||
}
|
||||
return render(request, msg_template, context)
|
||||
# 解密
|
||||
crypto = Crypto(crypto_key)
|
||||
return crypto.decrypt(crypto_tmp_id)
|
||||
return False, context
|
||||
|
||||
|
||||
def ops_account(ad_ops, request, msg_template, home_url, username, new_password):
|
||||
@@ -102,7 +106,7 @@ def ops_account(ad_ops, request, msg_template, home_url, username, new_password)
|
||||
'button_click': "window.location.href='%s'" % home_url,
|
||||
'button_display': "返回主页"
|
||||
}
|
||||
return render(request, msg_template, context)
|
||||
return False, context
|
||||
|
||||
account_code = ad_ops.ad_get_user_status_by_account(username)
|
||||
if account_code in settings.AD_ACCOUNT_DISABLE_CODE:
|
||||
@@ -111,7 +115,8 @@ def ops_account(ad_ops, request, msg_template, home_url, username, new_password)
|
||||
'button_click': "window.location.href='%s'" % home_url,
|
||||
'button_display': "返回主页"
|
||||
}
|
||||
return render(request, msg_template, context)
|
||||
return False, context
|
||||
|
||||
if new_password:
|
||||
reset_status, result = ad_ops.ad_reset_user_pwd_by_account(username=username, new_password=new_password)
|
||||
if reset_status:
|
||||
@@ -123,14 +128,14 @@ def ops_account(ad_ops, request, msg_template, home_url, username, new_password)
|
||||
'button_click': "window.location.href='%s'" % home_url,
|
||||
'button_display': "返回主页"
|
||||
}
|
||||
return render(request, msg_template, context)
|
||||
return True, context
|
||||
else:
|
||||
context = {
|
||||
'msg': "密码未修改/重置成功,错误信息:{}".format(result),
|
||||
'button_click': "window.location.href='%s'" % home_url,
|
||||
'button_display': "返回主页"
|
||||
}
|
||||
return render(request, msg_template, context)
|
||||
return False, context
|
||||
else:
|
||||
unlock_status, result = ad_ops.ad_unlock_user_by_account(username)
|
||||
if unlock_status:
|
||||
@@ -139,11 +144,11 @@ def ops_account(ad_ops, request, msg_template, home_url, username, new_password)
|
||||
'button_click': "window.location.href='%s'" % home_url,
|
||||
'button_display': "返回主页"
|
||||
}
|
||||
return render(request, msg_template, context)
|
||||
return True, context
|
||||
else:
|
||||
context = {
|
||||
'msg': "账号未能解锁,错误信息:{}".format(result),
|
||||
'button_click': "window.location.href='%s'" % home_url,
|
||||
'button_display': "返回主页"
|
||||
}
|
||||
return render(request, msg_template, context)
|
||||
return False, context
|
||||
|
@@ -1,14 +1,16 @@
|
||||
import logging
|
||||
|
||||
from django.http import HttpResponseRedirect
|
||||
import os
|
||||
from django.shortcuts import render
|
||||
from pwdselfservice.local_settings import SCAN_CODE_TYPE, DING_MO_APP_ID, WEWORK_CORP_ID, WEWORK_AGENT_ID, HOME_URL, TMPID_COOKIE_AGE
|
||||
|
||||
from utils.ad_ops import AdOps
|
||||
from utils.crypto import Crypto
|
||||
from utils.format_username import format2username, get_user_is_active
|
||||
from .form import CheckForm
|
||||
from .utils import code_2_user_id, crypto_id_2_user_info, ops_account, crypto_id_2_user_id, crypto_user_id_2_cookie
|
||||
APP_ENV = os.getenv('APP_ENV')
|
||||
if APP_ENV == 'dev':
|
||||
from conf.local_settings_dev import SCAN_CODE_TYPE, DING_MO_APP_ID, WEWORK_CORP_ID, WEWORK_AGENT_ID, HOME_URL
|
||||
else:
|
||||
from conf.local_settings import SCAN_CODE_TYPE, DING_MO_APP_ID, WEWORK_CORP_ID, WEWORK_AGENT_ID, HOME_URL
|
||||
|
||||
|
||||
msg_template = 'messages.html'
|
||||
logger = logging.getLogger('django')
|
||||
@@ -27,6 +29,12 @@ class PARAMS(object):
|
||||
SCAN_APP = '微信'
|
||||
from utils.wework_ops import WeWorkOps
|
||||
ops = WeWorkOps()
|
||||
else:
|
||||
app_id = WEWORK_CORP_ID
|
||||
agent_id = WEWORK_AGENT_ID
|
||||
SCAN_APP = '微信'
|
||||
from utils.wework_ops import WeWorkOps
|
||||
ops = WeWorkOps()
|
||||
|
||||
|
||||
scan_params = PARAMS()
|
||||
@@ -53,6 +61,8 @@ def index(request):
|
||||
return render(request, 'ding_index.html', locals())
|
||||
elif request.method == 'GET' and SCAN_CODE_TYPE == 'WEWORK':
|
||||
return render(request, 'we_index.html', locals())
|
||||
elif request.method == 'GET' and SCAN_CODE_TYPE == 'FEISHU':
|
||||
return render(request, 'feishu_index.html', locals())
|
||||
else:
|
||||
logger.error('[异常] 请求方法:%s,请求路径:%s' % (request.method, request.path))
|
||||
|
||||
@@ -112,8 +122,11 @@ def callback_check(request):
|
||||
'button_display': "返回主页"
|
||||
}
|
||||
return render(request, msg_template, context)
|
||||
print("code: {}" .format(code))
|
||||
try:
|
||||
user_id, user_info = code_2_user_id(_ops, request, msg_template, home_url, code)
|
||||
_status, user_id, user_info = code_2_user_id(_ops, request, msg_template, home_url, code)
|
||||
if not _status:
|
||||
return render(request, msg_template, user_id)
|
||||
# 账号是否是激活的
|
||||
if get_user_is_active(user_info):
|
||||
return crypto_user_id_2_cookie(user_id)
|
||||
@@ -143,7 +156,9 @@ def reset_pwd_by_callback(request):
|
||||
home_url = '%s://%s' % (request.scheme, HOME_URL)
|
||||
# 从cookie中提取union_id,并解密,然后对当前union_id的用户进行重置密码
|
||||
if request.method == 'GET':
|
||||
user_id = crypto_id_2_user_id(request, msg_template, home_url)
|
||||
_status, user_id = crypto_id_2_user_id(request, msg_template, home_url)
|
||||
if not _status:
|
||||
return render(request, msg_template, user_id)
|
||||
userid_status, user_info = _ops.get_user_detail_by_user_id(user_id)
|
||||
if not userid_status:
|
||||
context = {
|
||||
@@ -162,7 +177,7 @@ def reset_pwd_by_callback(request):
|
||||
return render(request, 'resetPassword.html', context)
|
||||
else:
|
||||
context = {
|
||||
'msg': "{},您好,企业{}中未能找到您账号的邮箱配置,请联系HR完善信息。" .format(user_info.get('name'), scan_params.SCAN_APP),
|
||||
'msg': "{},您好,企业{}中未能找到您账号的邮箱配置,请联系HR完善信息。".format(user_info.get('name'), scan_params.SCAN_APP),
|
||||
'button_click': "window.location.href='%s'" % home_url,
|
||||
'button_display': "返回主页"
|
||||
}
|
||||
@@ -170,10 +185,21 @@ def reset_pwd_by_callback(request):
|
||||
|
||||
# 重置密码页面,输入新密码后点击提交
|
||||
elif request.method == 'POST':
|
||||
_new_password = request.POST.get('new_password').strip()
|
||||
user_info = crypto_id_2_user_info(_ops, request, msg_template, home_url, scan_params.SCAN_APP)
|
||||
username = format2username(user_info.get('email'))
|
||||
return ops_account(ad_ops, request, msg_template, home_url, username, _new_password)
|
||||
try:
|
||||
_new_password = request.POST.get('new_password').strip()
|
||||
_status, user_info = crypto_id_2_user_info(_ops, request, msg_template, home_url, scan_params.SCAN_APP)
|
||||
if not _status:
|
||||
return render(request, msg_template, user_info)
|
||||
username = format2username(user_info.get('email'))
|
||||
return ops_account(ad_ops, request, msg_template, home_url, username, _new_password)
|
||||
except Exception as reset_e:
|
||||
context = {
|
||||
'msg': "错误[%s],请与管理员联系." % str(reset_e),
|
||||
'button_click': "window.location.href='%s'" % home_url,
|
||||
'button_display': "返回主页"
|
||||
}
|
||||
logger.error('[异常] :%s' % str(reset_e))
|
||||
return render(request, msg_template, context)
|
||||
else:
|
||||
context = {
|
||||
'msg': "请从主页开始进行操作。",
|
||||
@@ -191,7 +217,9 @@ def unlock_account(request):
|
||||
"""
|
||||
home_url = '%s://%s' % (request.scheme, HOME_URL)
|
||||
if request.method == 'GET':
|
||||
user_info = crypto_id_2_user_info(_ops, request, msg_template, home_url, scan_params.SCAN_APP)
|
||||
_status, user_info = crypto_id_2_user_info(_ops, request, msg_template, home_url, scan_params.SCAN_APP)
|
||||
if not _status:
|
||||
return render(request, msg_template, user_info)
|
||||
username = format2username(user_info.get('email'))
|
||||
context = {
|
||||
'username': username,
|
||||
@@ -199,7 +227,9 @@ def unlock_account(request):
|
||||
return render(request, 'resetPassword.html', context)
|
||||
|
||||
elif request.method == 'POST':
|
||||
user_info = crypto_id_2_user_info(_ops, request, msg_template, home_url, scan_params.SCAN_APP)
|
||||
_status, user_info = crypto_id_2_user_info(_ops, request, msg_template, home_url, scan_params.SCAN_APP)
|
||||
if not _status:
|
||||
return render(request, msg_template, user_info)
|
||||
username = format2username(user_info.get('email'))
|
||||
return ops_account(ad_ops, request, msg_template, home_url, username, None)
|
||||
else:
|
||||
|
Reference in New Issue
Block a user