def project_get(project=None): try: rep = jsonify({'error': 'None', 'url': request.url}) Key = 'op_project_get_%s' %time.strftime('%H%M%S',time.localtime()) if project: db_project = db_op.project_list db_servers = db_idc.idc_servers if project == 'all_list': vals = db_project.query.with_entities(distinct(db_project.project)).all() projects = [val[0] for val in vals] rep = jsonify({project: projects, 'md5': Md5.Md5_make(str(projects)), 'url': request.url}) else: projects = [] vals = db_project.query.with_entities(db_project.ip,db_project.ssh_port).filter(db_project.project==project).all() if vals: for ip,ssh_port in vals: host_vals = db_servers.query.with_entities(db_servers.hostname,db_servers.ip).filter(and_(db_servers.ip==ip,db_servers.ssh_port==ssh_port)).all() if host_vals: RC.sadd(Key,list(host_vals[0])) for val in RC.smembers(Key): projects.append(eval(val)) RC.delete(Key) rep = jsonify({project:projects,'md5':Md5.Md5_make(str(projects)),'url':request.url}) except Exception as e: rep = jsonify({'error':str(e),'url':request.url}) finally: return rep
def m_login(): try: form = MyForm.MyFormInput() if form.submit.data: username = form.input.data password = form.passwd.data token = tools.Produce(24) db_sso = db_op.user_sso val = db_sso.query.with_entities( db_sso.realName, db_sso.dingunionid, db_sso.ticket).filter(db_sso.userName == username).all() if val and password == '%sok' % username: realName, dingunionid, ticket = val[0] app_resp = make_response(redirect(url_for('mobile.mobile'))) app_resp.set_cookie('user', Md5.Md5_make(realName), path='/') app_resp.set_cookie('dingId', Md5.Md5_make(dingunionid), path='/') app_resp.set_cookie('ticket', Md5.Md5_make(ticket), path='/') app_resp.set_cookie('token', token, path='/') RC.set('OP_dingId_%s' % Md5.Md5_make(dingunionid), dingunionid) RC.set('OP_user_%s' % Md5.Md5_make(realName), realName) RC.set('OP_token_%s' % dingunionid, token) RC.set('OP_ticket_%s' % Md5.Md5_make(ticket), ticket) RC.set('OP_logout_ticket_%s' % ticket, ticket) return app_resp except Exception as e: logging.error(e) return render_template('mobile/m_login.html', form=form)
def login(): try: db_sso = db_op.user_sso ticket = tools.http_args(request,'ticket') if ticket: try: cas_resp = cas_client.perform_service_validate(ticket=ticket,service_url=service_url) if cas_resp and cas_resp.success: try: infos = cas_resp.attributes departmentName = infos['departmentName'] mail = infos['mail'] mobilePhone = infos['mobilePhone'] userName = infos['userName'] realName = infos['realName'] dingunionid = infos['dingunionid'] token = tools.Produce(24) update_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) except Exception as e: logging.error(e) else: try: val = db_sso.query.filter(db_sso.dingunionid == dingunionid).all() if val: db_sso.query.filter(db_sso.dingunionid == dingunionid).update({db_sso.department:departmentName, db_sso.mail:mail, db_sso.mobilePhone:mobilePhone, db_sso.userName: userName, db_sso.ticket: ticket, db_sso.realName: realName, db_sso.update_time:update_time}) db_op.DB.session.commit() else: c =db_sso(userName=userName,realName=realName,mail=mail,mobilePhone=mobilePhone,department=departmentName, dingunionid=dingunionid,ticket=ticket,grade='9,10',update_time=update_time) db_op.DB.session.add(c) db_op.DB.session.commit() except Exception as e: logging.error(e) else: URL = url_for('main') app_resp = make_response(redirect(URL)) app_resp.set_cookie('user', Md5.Md5_make(realName),path='/') app_resp.set_cookie('dingId', Md5.Md5_make(dingunionid),path='/') app_resp.set_cookie('ticket', Md5.Md5_make(ticket),path='/') app_resp.set_cookie('token',token,path='/') Redis.set('OP_dingId_%s' % Md5.Md5_make(dingunionid), dingunionid) Redis.set('OP_user_%s' % Md5.Md5_make(realName), realName) Redis.set('OP_token_%s' %dingunionid,token) Redis.set('OP_ticket_%s' %Md5.Md5_make(ticket),ticket) Redis.set('OP_logout_ticket_%s' %ticket, ticket) Redis.set('OP_dingid_ticket_%s' %ticket, dingunionid) return app_resp except Exception as e: logging.error(e) except Exception as e: logging.error(e) return redirect(url_for('error')) return redirect(cas_client.get_login_url(service_url=service_url))
def platform_token(action=None, id=None, args=None): tools.Async_log(g.user, request.url) db_token = db_op.platform_token tm = time.strftime('%Y-%m-%d', time.localtime()) form = MyForm.Form_platform_token() tables = ['第三方平台', '连接方式', 'Token', '颁发日期', '失效日期', '管理'] if action == 'add': expire_date = "2999-12-30" if id > 0: expire_date = datetime.datetime.now() + datetime.timedelta(days=id) expire_date = expire_date.strftime('%Y-%m-%d') try: c = db_token(platform=args, channel='api', token=Md5.Md5_make(tools.Produce(8, string.digits)), award=tm, expire=expire_date) db_op.DB.session.add(c) db_op.DB.session.commit() return render_template_string('success') except Exception as e: logging.error(e) return render_template_string('fail') if action == 'modify': try: db_token.query.filter(db_token.id == id).update( {db_token.expire: args}) db_op.DB.session.commit() return render_template_string('success') except Exception as e: logging.error(e) return render_template_string('fail') if action == 'drop': try: v = db_token.query.filter(db_token.id == id).all() for c in v: db_op.DB.session.delete(c) db_op.DB.session.commit() return render_template_string('success') except Exception as e: logging.error(e) return render_template_string('fail') vals = db_token.query.with_entities( db_token.id, db_token.platform, db_token.channel, db_token.token, db_token.award, db_token.expire).order_by(desc(db_token.id)).all() return render_template('platform_token.html', form=form, vals=vals, tables=tables, tm=tm)
def Login(*args, **kwargs): try: user = Redis.get('OP_user_%s' %request.cookies.get('user')) dingId = Redis.get('OP_dingId_%s' %request.cookies.get('dingId')) token = request.cookies.get('token') ticket = Redis.get('OP_ticket_%s' % request.cookies.get('ticket')) except: pass else: try: if user and dingId and token and ticket and Redis.exists('OP_logout_ticket_%s' % ticket): if token == Redis.get('OP_token_%s' %dingId): g.user = user g.dingId = dingId g.secret_key = request.cookies.get('secret_key') g.token = token db_sso = db_op.user_sso val = db_sso.query.with_entities(db_sso.grade,db_sso.mail,db_sso.mobilePhone,db_sso.department).filter(db_sso.dingunionid == dingId).all() if val: g.grade,g.mail,g.phone,g.department = val[0] g.grade = g.grade.split(',') if str(grade) in g.grade: g.ip = request.headers.get('X-Forwarded-For') if not g.ip: g.ip = request.remote_addr if ',' in g.ip: g.ip = g.ip.split(',')[0] session['remote_ip'] = g.ip tm = time.strftime('%Y%m%d', time.localtime()) td = time.strftime('%Y-%m-%d', time.localtime()) g.date = td g.ym = time.strftime('%Y', time.localtime()) g.active_users = Redis.scard('op_active_users_%s' % td) #页面菜单缓存加速 menu_key = f'op_menu_{tm}' user_menu_key = f'menu_{dingId}' if Redis.hexists(menu_key,user_menu_key): g.Base_Menu = eval(Redis.hget(menu_key,user_menu_key)) else: # 生成用户权限对应的页面菜单 grades = g.grade DB = db_op.op_menu sub_val = defaultdict() menu_md5 = defaultdict() submenu = DB.query.with_entities(distinct(DB.Menu_name)).filter(DB.grade.in_(grades)).order_by( DB.Menu_id).all() if submenu: submenu = [menu[0] for menu in submenu] menu_md5 = {menu:Md5.Md5_make(menu) for menu in submenu} for Menu in submenu: val = DB.query.with_entities(DB.module_name, DB.action_name).filter( and_(DB.grade.in_(grades), DB.Menu_name == Menu)).order_by( DB.sub_id).all() if val: sub_val[Menu] = val g.Base_Menu = {'submenu': submenu,'sub_val': sub_val,'menu_md5':menu_md5} #缓存页面菜单1小时 Redis.hset(menu_key,user_menu_key,g.Base_Menu) Redis.expire(menu_key,3600) return func(*args, **kwargs) except Exception as e: logging.error(e) return redirect(url_for('error')) finally: db_op.DB.session.remove() return redirect(url_for('logout.logout'))