async def post(self): res = {'code': 0} user_id = self.get_argument('manager_id', '') user = await User.get_by_id(user_id) origin_role_code_list = copy.deepcopy(user.role_code_list) or [] role_code_list = self.get_arguments('role_code_list[]') perm_code_list = self.get_arguments('perm_code_list[]') user.permission_code_list = perm_code_list user.role_code_list = role_code_list # 接入权限 roles = await Role.find( dict(permission_code_list={ '$in': [PERMISSION_TYPE_SYSTEM_DOCKING_MANAGEMENT] }, code={'$in': role_code_list})).to_list(None) if roles or PERMISSION_TYPE_SYSTEM_DOCKING_MANAGEMENT in perm_code_list: if not user.access_secret_id or not user.access_secret_key: user.access_secret_id = get_random_str(32) user.access_secret_key = get_random_str(64) else: user.access_secret_id = None user.access_secret_key = None try: user.updated_id = self.current_user.oid user.updated_dt = datetime.now() await user.save() # 更新角色用户数量needless # 新增 new_role_code_list = list( set(role_code_list).difference(set(origin_role_code_list))) # 删除 remove_role_code_list = list( set(origin_role_code_list).difference(set(role_code_list))) for role_code in new_role_code_list: role = await Role.find_one(dict(code=role_code)) if not role.needless: role.needless = {'user_amount': 1} else: role.needless['user_amount'] = role.needless.get( 'user_amount', 0) + 1 role.updated_id = self.current_user.oid role.updated_dt = datetime.now() await role.save() for role_code in remove_role_code_list: role = await Role.find_one(dict(code=role_code)) if role.needless: role.needless['user_amount'] = role.needless.get( 'user_amount', 1) - 1 role.updated_id = self.current_user.oid role.updated_dt = datetime.now() await role.save() res['code'] = 1 except Exception: logger.error(traceback.format_exc()) return res
async def post(self, role_id): r_dict = {'code': 0} try: perm_code_list = self.get_body_arguments('perm_code_list[]', []) role = await Role.get_by_id(role_id) if role: role.permission_code_list = perm_code_list role.updated_dt = datetime.now() role.updated_id = self.current_user.oid await role.save() # 用户接入权限 user_list = User.find( dict(role_code_list={'$in': [role.code]})) async for user in user_list: if PERMISSION_TYPE_SYSTEM_DOCKING_MANAGEMENT in perm_code_list and ( not user.access_secret_id or not user.access_secret_key): user.access_secret_id = get_random_str(32) user.access_secret_key = get_random_str(64) if PERMISSION_TYPE_SYSTEM_DOCKING_MANAGEMENT not in perm_code_list and \ PERMISSION_TYPE_SYSTEM_DOCKING_MANAGEMENT not in user.permission_code_list: user.access_secret_id = None user.access_secret_key = None user.updated_dt = datetime.now() user.updated_id = self.current_user.oid await user.save() r_dict['code'] = 1 except RuntimeError: logger.error(traceback.format_exc()) return r_dict
async def generate_new_token(self, access_id, access_secret): """ 生成新的TOKEN :param access_id: ACCESS KEY ID :param access_secret: ACCESS KEY SECRET :return: """ if access_id and access_secret: count = await User.count( dict(access_secret_id=access_id, access_secret_key=access_secret, status=STATUS_USER_ACTIVE)) if count > 0: token = get_random_str(32) key = md5(token) RedisCache.set(key, token, 60 * 60 * 2) return token return None