Skip to content

Commit

Permalink
2024年6月28日 feat(login_v5&&get_user)优化登录相关
Browse files Browse the repository at this point in the history
  • Loading branch information
ss1917 committed Jun 28, 2024
1 parent 253f66a commit 04afb47
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
3 changes: 1 addition & 2 deletions docs/sql/codo_a_users.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
INSERT INTO `codo_a_users` (`ext_info`, `create_time`, `update_time`, `id`, `username`, `password`, `nickname`, `email`, `tel`, `department`, `google_key`, `superuser`, `avatar`, `source`, `source_account_id`, `manager`, `dd_id`, `status`, `have_token`, `fs_open_id`, `fs_id`, `last_ip`, `last_login`) VALUES ('{}', '2018-03-21 10:29:14', '2024-01-15 10:26:43', 1, 'admin', '03941b924d12454219648d61a7b025e1', 'admin', '191715030qq.com', '15618718111', NULL, '', '0', '', NULL, '', NULL, NULL, '0', 'no', '', '', '127.0.0.1', '2024-01-15 10:26:43');
INSERT INTO `codo_a_users` (`ext_info`, `create_time`, `update_time`, `id`, `username`, `password`, `nickname`, `email`, `tel`, `department`, `google_key`, `superuser`, `avatar`, `source`, `source_account_id`, `manager`, `dd_id`, `status`, `have_token`, `fs_open_id`, `fs_id`, `last_ip`, `last_login`) VALUES ('{}', '2023-03-07 15:44:59', '2024-01-15 10:15:55', 2, 'demo', '03941b924d12454219648d61a7b025e1', 'demo', '[email protected]', '15618718112', NULL, NULL, '0', NULL, NULL, NULL, NULL, NULL, '0', 'no', NULL, NULL, '127.0.0.1', '2024-01-15 10:15:55');
INSERT INTO `codo_a_users` (`ext_info`, `create_time`, `update_time`, `id`, `username`, `password`, `nickname`, `email`, `tel`, `department`, `google_key`, `superuser`, `avatar`, `source`, `source_account_id`, `manager`, `dd_id`, `status`, `have_token`, `fs_open_id`, `fs_id`, `last_ip`, `last_login`) VALUES ('{}', '2018-03-21 10:29:14', '2024-01-15 10:26:43', 1, 'admin', '03941b924d12454219648d61a7b025e1', 'admin', '191715030qq.com', '15618718111', NULL, '', '0', '', NULL, '', NULL, NULL, '0', 'no', '', '', '127.0.0.1', '2024-01-15 10:26:43');
8 changes: 7 additions & 1 deletion get_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import requests
from models.authority import Users
from settings import settings
from sqlalchemy import and_, or_
from websdk2.db_context import DBContextV2 as DBContext
from websdk2.model_utils import insert_or_update

Expand Down Expand Up @@ -51,8 +52,10 @@ def sync_user_from_ucenter():
def index():
logging.info(f'async_all_user_redis_lock_key {datetime.datetime.now()}')
with DBContext('w', None, True, **settings) as session:
user_id_list = []
for user in get_all_user():
user_id = str(user.get('uid'))
user_id_list.append(user_id)
username = user.get('english_name')
if not user.get('position'):
try:
Expand All @@ -73,7 +76,10 @@ def index():
source="ucenter", tel=user.get('mobile'), status='0',
avatar=user.get('avatar'), username=user.get('english_name')))
except Exception as err:
logging.info(f'\n async_all_user_redis_lock_key Exception {err}')
logging.info(f'async_all_user_redis_lock_key Exception {err}')

session.query(Users).filter(Users.source == "ucenter", Users.source_account_id.notin_(user_id_list)).update(
{"status": "20"}, synchronize_session=False)
logging.info(f'async_all_user_redis_lock_key end ')

index()
Expand Down
7 changes: 5 additions & 2 deletions libs/sync_user_verift_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,14 @@ def md5hex(sign):


def sync_user_from_uc():
# from models.authority import Users

@deco1(RedisLock("async_all_user_redis_lock_key"))
def index():
logging.info(f'开始同步用户中心数据 {datetime.datetime.now()}')
with DBContext('w', None, True, **settings) as session:
user_id_list = []
for user in get_all_user():
user_id = str(user.get('uid'))
user_id_list.append(user_id)
username = user.get('english_name')
if not user.get('position'):
try:
Expand All @@ -360,6 +360,9 @@ def index():
avatar=user.get('avatar'), username=user.get('english_name')))
except Exception as err:
logging.error(f'同步用户中心数据 出错 {err}')

session.query(Users).filter(Users.source == "ucenter", Users.source_account_id.notin_(user_id_list)).update(
{"status": "20"}, synchronize_session=False)
logging.info('开始同步用户中心数据 结束')

index()
Expand Down
29 changes: 20 additions & 9 deletions mg/handlers/login_v5_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import json
import logging

import pyotp
import base64
from abc import ABC
Expand All @@ -17,7 +16,7 @@
from libs.base_handler import BaseHandler
from services.sys_service import get_sys_conf_dict_for_me
from services.login_service import update_login_ip, base_verify, ldap_verify, feishu_verify, uc_verify, \
generate_token, get_user_info_for_id, get_domain_from_url
generate_token, get_user_info_for_id


class LoginHandler(RequestHandler, ABC):
Expand All @@ -27,8 +26,12 @@ def check_xsrf_cookie(self):

async def authenticate(self, username, password, login_type, data):
if password:
password = base64.b64decode(password).decode("utf-8")
password = base64.b64decode(password).decode("utf-8")
try:
password = base64.b64decode(password).decode("utf-8")
password = base64.b64decode(password).decode("utf-8")
except Exception as err:
logging.error(err)
return dict(code=-1, msg='账号密码错误')

if login_type == 'feishu':
fs_conf = get_sys_conf_dict_for_me(**dict(category='feishu'))
Expand Down Expand Up @@ -64,13 +67,15 @@ async def post(self, *args, **kwargs):
login_type = data.get('login_type')
user_info = await self.authenticate(username, password, login_type, data)
if not user_info:
return self.write(dict(code=-4, msg='账号异常'))
if login_type == 'feishu':
return self.write(dict(code=-3, msg='账号异常,请联系管理员'))
return self.write(dict(code=-4, msg='用户名密码错误'))

if isinstance(user_info, dict) and "code" in user_info:
return self.write(user_info)

if user_info.status != '0':
return self.write(dict(code=-4, msg='账号被禁用'))
return self.write(dict(code=-5, msg='账号被禁用'))

user_id = str(user_info.id)
generate_token_dict = await generate_token(user_info, dynamic)
Expand Down Expand Up @@ -101,8 +106,14 @@ async def post(self, *args, **kwargs):
except Exception as err:
logging.error(f"设置主域cookie失败 {err}")

real_login_dict = dict(code=0, username=user_info.username, nickname=user_info.nickname, auth_key=auth_key,
avatar=user_info.avatar, c_url=c_url, msg='登录成功')
real_login_dict = dict(code=0, msg='登录成功',
username=user_info.username,
nickname=user_info.nickname,
auth_key=auth_key,
avatar=user_info.avatar,
c_url=c_url,
data=dict(username=user_info.username, nickname=user_info.nickname, auth_key=auth_key,
avatar=user_info.avatar, c_url=c_url))
return self.write(real_login_dict)


Expand Down Expand Up @@ -154,7 +165,7 @@ def post(self):
self.clear_cookie("auth_key", domain=root_domain)
self.clear_cookie("is_login", domain=root_domain)
except Exception as err:
pass
logging.error(err)
self.set_status(401)
self.finish()

Expand Down
8 changes: 0 additions & 8 deletions services/login_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,3 @@ def get_user_info_for_id(user_id: int) -> Optional[Users]:
user_info: Optional[Users] = session.query(Users).filter(Users.id == user_id,
Users.status == "0").first()
return user_info


def get_domain_from_url(url):
parsed_url = urlparse(url)
# 提取 netloc 并去除端口号
u_domain = parsed_url.netloc.split(':')[0]
c_domain = f".{u_domain.split('.')[1]}.{u_domain.split('.')[2]}"
return c_domain

0 comments on commit 04afb47

Please sign in to comment.