diff --git a/docs/sql/codo_a_users.sql b/docs/sql/codo_a_users.sql index 3aea4fb..fd878ea 100644 --- a/docs/sql/codo_a_users.sql +++ b/docs/sql/codo_a_users.sql @@ -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', '191715031@qq.com', '15618718112', NULL, NULL, '0', NULL, NULL, NULL, NULL, NULL, '0', 'no', NULL, NULL, '127.0.0.1', '2024-01-15 10:15:55'); \ No newline at end of file +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'); \ No newline at end of file diff --git a/get_user.py b/get_user.py index 7906076..18deeaa 100644 --- a/get_user.py +++ b/get_user.py @@ -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 @@ -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: @@ -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() diff --git a/libs/sync_user_verift_v4.py b/libs/sync_user_verift_v4.py index ab10eb4..d215283 100644 --- a/libs/sync_user_verift_v4.py +++ b/libs/sync_user_verift_v4.py @@ -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: @@ -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() diff --git a/mg/handlers/login_v5_handler.py b/mg/handlers/login_v5_handler.py index ed34f10..68c7a40 100644 --- a/mg/handlers/login_v5_handler.py +++ b/mg/handlers/login_v5_handler.py @@ -8,7 +8,6 @@ import json import logging - import pyotp import base64 from abc import ABC @@ -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): @@ -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')) @@ -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) @@ -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) @@ -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() diff --git a/services/login_service.py b/services/login_service.py index ef9ec05..c54b5dc 100644 --- a/services/login_service.py +++ b/services/login_service.py @@ -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