async def get(self): """ 1. 自己当前的组织下的资源 2. 生成share的内容 支持手机端的 :return: """ rep_id = self.get_argument('rep_id', None) # 测试报告的ID if list_have_none_mem(*[rep_id]): return ConstData.msg_args_wrong db = self.get_async_mongo() share_col = db.share_test_report utest_col = db.unit_test_data proj_col = db.test_project # todo 做资源归属和权限的判断 test_data = await utest_col.find_one({'_id': ObjectId(rep_id)}) project = await proj_col.find_one({'_id': test_data['pro_id']}) # if (project is None) or (project.organization is None): if project is None: return ConstData.msg_forbidden user_org = await self.get_organization() pro_org_id = project['organization'] if pro_org_id != user_org: return ConstData.msg_forbidden share_obj = await share_col.find_one({'rep_id': rep_id}) if share_obj is None: # 如果不存在分享Link则创建一组 stoken = generate_uuid_token() # 生成一组随机串,分享的时候会看到 share_data = dict(rep_id=rep_id, stoken=stoken, cnt=0, is_del=False, project=project['_id'], p_name=project['project_name'], owner=project['owner'], owner_name=project['owner_name'], organization=project['organization'], org_name=project['org_name']) # await share_obj.set_project_tag(project) # await share_obj.set_org_user_tag(http_req=self) share_data = set_default_rc_tag(share_data) await share_col.insert(share_data) share_url = share_page + stoken else: # 如果有,则直接使用 share_url = share_page + share_obj['stoken'] res_dict = dict(share_url=share_url) return get_std_json_response(data=jsontool.dumps(res_dict))
async def create_app_session(self, app_id, **kwargs): """ 设置app的登录会话 :return: """ client_type = kwargs.get('client_type', ClientTypeDict.api) ip = self.request.remote_ip db = self.get_async_mongo() app_col = db.test_data_app session_col = db.ttl_app_session test_app = await app_col.find_one({'app_id': app_id}) app_session = await session_col.find_one({'app_id': app_id}) if app_session is not None: # 如果已经存在,则只需要更新时间 current_time = get_current_utc_time() await session_col.update({'app_id': app_id}, {'$set': { 'last_use_time': current_time }}) # app_session.last_use_time = get_current_utc_time() # 更新在线时间 # app_session = await app_session.save() app_session['last_use_time'] = current_time return app_session # 如果不存在登录状态记录,则重新创建session new_session = dict(app_id=app_id, c_type=client_type.value, c_name=client_type.key, organization=test_app['organization'], o_name=test_app['o_name'], ip=ip, token=randtool.generate_uuid_token(), last_use_time=get_current_utc_time()) new_session.update(self.set_http_tag()) new_session = set_default_rc_tag(new_session) await session_col.insert(new_session) self.cache_session = new_session return new_session
async def create_token_session(self, user, **kwargs): """ 区别:加入了 平台的描述 创建 或者 刷新 token, 包括各种 ttl_token 现在是进行了扩展了,所以用这种方式, - 允许有匿名的存在 :type self:MyBaseHandler :type cls:UserToken :type user:User :param client_type: 客户端类型 :return: :rtype:UserToken """ client_type = kwargs.get('client_type', None) # 默认的是浏览器端 """:type:ValueDict""" # cls = kwargs.get('token_cls', None) # """:type:UserToken""" if list_have_none_mem(*[client_type]): return None # 读取 mongodb,获取用户对应的 token,单点登录情况下使用 # my_token = await cls.objects.get(user=user, c_type=client_type.value) """:type:WebToken""" ip = self.request.remote_ip user_agent = self.request.headers['User-Agent'] finger_prt = hashlib.md5(user_agent.encode("utf-8")).hexdigest() # # if my_token is None: # 单点登录情况下使用 # my_token = cls( # c_type=client_type.value, # c_name=client_type.key, # ) # """:type:UserToken""" db = self.get_async_mongo() token_col = db['ttl_access_token'] user_col = db['g_users'] user_res = await user_col.find_one({'_id': user}) if not user_res: return None new_token = dict( user=user_res['_id'], # todo: save user_id u_name=user_res['nickname']) # if user is not None: # my_token.user = user.get_id() # my_token.u_name = user.nickname # 允许有匿名用户的存在,这个时候就表明没有被签名的匿名登录 new_token = set_default_rc_tag(new_token) new_token['token'] = randtool.generate_uuid_token() new_token['last_use_time'] = get_current_utc_time() new_token['ip'] = ip new_token['user_agent'] = user_agent new_token['finger_prt'] = finger_prt # my_token = await my_token.save() # 通过save获取的my_token并没有lazy出来,后面赋值的时候会有影响 # """:type:UserToken""" # # my_token = await cls.objects.get(id=my_token.get_id()) # 相当于使用了 loadReference功能 await token_col.insert(new_token) return new_token
async def get(self): """ 1. 自己当前的组织下的资源 2. 生成share的内容 支持手机端的 :return: """ pro_id = self.get_argument('project_id', None) # 测试报项目的ID tag = self.get_argument('tag', 'default') # 测试报项目的ID if list_have_none_mem(*[pro_id]): return ConstData.msg_args_wrong # todo 做资源归属和权限的判断 user_org = await self.get_organization() db = self.get_async_mongo() proj_col = db.test_project share_col = db.share_project_report project = await proj_col.find_one({'_id': ObjectId(str(pro_id))}) if tag != 'default' and tag not in project['tags']: return ConstData.msg_args_wrong pro_org_id = project['organization'] if (project is None) or (pro_org_id is None): return ConstData.msg_forbidden if pro_org_id != user_org: return ConstData.msg_forbidden #todo: delete default next release condition = { 'pro_id': pro_id } if tag == 'default': condition['$or'] = [ {'tag': 'default'}, {'tag': {'$exists': False}} ] else: condition['tag'] = tag share_obj = await share_col.find_one(condition) if share_obj is None: # 如果不存在分享Link则创建一组 stoken = generate_uuid_token() # 生成一组随机串,分享的时候会看到 share_data = dict( pro_id=pro_id, stoken=stoken, cnt=0, project=project['_id'], p_name = project['project_name'], owner = project['owner'], owner_name = project['owner_name'], organization = project['organization'], org_name = project['org_name'], tag=tag ) share_data = set_default_rc_tag(share_data) await share_col.insert(share_data) share_url = pro_share_page + stoken else: # 如果有,则直接使用 share_url = pro_share_page + share_obj['stoken'] res_dict = dict( share_url=share_url ) return get_std_json_response(data=jsontool.dumps(res_dict))