def wrapper(*args, **kwargs): user_account = session.get('username', '') if user_account: login_user = SfoServerUser.query_user_by_account(user_account) is_clusteradmin = login_user.is_clusteradmin if login_user else 0 if is_clusteradmin: return func(*args, **kwargs) prams = request.json url = str(request.url)[str(request.url).find('/api'):] sys_auths = SfoSystemInput.query.filter( and_(SfoSystemInput.sys_id == prams['id'], SfoSystemInput.sys_key == prams['key'], SfoSystemInput.sys_stat == '1')).all() if sys_auths and len(sys_auths) > 0: for auth in sys_auths: if str(auth.sys_url).strip().upper() == str( url).strip().upper(): return func(*args, **kwargs) elif 'string:' in str(auth.sys_url).strip().lower(): auth_url = str( auth.sys_url)[:str(auth.sys_url).find('<string:')] if str(url).strip().lower().find( auth_url.strip().lower()) == 0: return func(*args, **kwargs) else: pass status = 403 message = "Lack of authority" return {'status': status, "message": message}, status else: status = 401 message = "auth failed" return {'status': status, "message": message}, status
def update_user(user_json, user_account): """ :param user_json: :param user_account: :return: """ status = '' message = '' resp = {"status": status, "message": message} sfo_server_user = SfoServerUser.query_user_by_account(user_account) try: if user_json and sfo_server_user: for key, value in user_json.items(): if hasattr(sfo_server_user, key): if key == 'roles': value = SfoServerRole.query_roles(value) if key == "is_clusteradmin" or key == 'active_status': value = value if sfo_server_user.is_clusteradmin else 0 setattr(sfo_server_user, key, value) db.session.add(sfo_server_user) db.session.commit() status = 200 message = 'OK' else: status = 501 message = 'NULL VALUE %s' % user_json except Exception, ex: status = '502' message = str(ex)
def create_or_update_userinfo(username, display_name, is_clusteradmin=False, sfo_server_user=None): if sfo_server_user is None: sfo_server_user = SfoServerUser.create_default_user( username, display_name, is_clusteradmin) else: sfo_server_user.last_login_time = timestamp_format(time.time()) db.session.add(sfo_server_user) db.session.commit()
def wrapper(*args, **kwargs): user_account = session.get('username', '') if user_account: login_user = SfoServerUser.query_user_by_account(user_account) g.user = login_user return func(*args, **kwargs) else: return ResponseBase(json.dumps({ 'status': 401, "message": u'请先登录' }), status=401, content_type='application/json')
def get_user_info(): status = '' message = '' data = {} resp = {"status": status, "message": message, "data": data} sfo_server_userlist = SfoServerUser.query_active_user_list() if sfo_server_userlist: data = sfo_server_userlist status = 200 message = 'SUCCESS' else: status = 404 message = 'Not Found Record' resp.update({"status": status, "message": message, "data": data}) return resp, status
def get_user_info(user_account): status = '' message = '' data = '' resp = {"status": status, "message": message, "data": data} sfo_server_user = SfoServerUser.query_user_by_account(user_account) if sfo_server_user: data = sfo_server_user status = 200 message = 'OK' else: status = 404 message = 'Not Found Record' resp.update({"status": status, "message": message, "data": data}) return resp, status
def delete_user(user_account): status = '' message = '' resp = {"status": status, "message": message} sfo_server_user = SfoServerUser.query_user_by_account(user_account) if sfo_server_user: sfo_server_user.active_status = 0 db.session.add(sfo_server_user) db.session.commit() status = 204 message = 'DELETE SUCCESS' else: status = 404 message = 'Not Found Record' resp.update({"status": status, "message": message}) return resp, status
def user_login(login_json): """ :param login_json: 登录传入的json格式 :return: """ data = {} status = '' message = '' resp = {"status": status, "data": data, "message": message} username = login_json.get('username') password = login_json.get('password') logined_username = session.get('username', '') if username == logined_username: status = 200 message = u'您已经登录' data = {} if username == 'root': data.update({'token': str(uuid.uuid4())}) data.update({'roles': ['superadmin']}) resp.update({"status": status, "message": message, "data": data}) return resp, status if username and password: if not is_super_admin(username, password): status = 401 message = u'请检查你的账户和密码' else: token = str(uuid.uuid4()) roles = ['superadmin'] sfo_cluster_admin = SfoServerUser.query_user_by_account(username) create_or_update_userinfo(username, '超级管理员', is_clusteradmin=True, sfo_server_user=sfo_cluster_admin) data.update({'token': token}) data.update({"roles": roles}) session['token'] = token session['username'] = username status = 200 message = 'OK' else: status = 401 message = u'请输入账号和密码' resp.update({"status": status, "data": data, "message": message}) return resp, status