def get_role_resource(): """ 根据角色获取资源 可以关联的资源 已经关联的资源 :return: """ params = request.values.get('params') params_data = json.loads(params) role_id = params_data['role_id'] if not role_id: raise Exception('param role_id is blank') response_data = {'all_can_rel_rows': [], 'aleary_rel_rows': []} group_ids = PermissionsRelService.ids_in_roleids( TPermissionsRoleGroupRel(), [role_id], 'gid') if not group_ids or len(group_ids) < 1: return make_response( json.dumps(ResponseCode.response(data=response_data), ensure_ascii=False)) all_can_rel_resource_ids = PermissionsRelService.ids_in_groupids( TPermissionsResGroupRel(), group_ids, 'rid') if not all_can_rel_resource_ids or len(all_can_rel_resource_ids) < 1: return make_response( json.dumps(ResponseCode.response(data=response_data), ensure_ascii=False)) all_aleary_rel_res_ids = PermissionsRelService.ids_in_roleids( TPermissionsResRoleRel(), [role_id], 'res_id') response_data['all_can_rel_rows'] = PermissionsResourceService. \ gen_resource_tree(CRUDHelper.execute_select(TPermissionsResource(), {'id': all_can_rel_resource_ids, 'rule': { 'id': Constants.QUERY_CONDITION_RULE_IN}})) response_data['aleary_rel_rows'] = all_aleary_rel_res_ids return make_response( json.dumps(ResponseCode.response(data=response_data), ensure_ascii=False))
def get_user_role(): """ 获取当前用户可以关联的角色 :return: """ params = request.values.get("params") params_data = json.loads(params) user_id = params_data['user_id'] conditions = params_data['condition'] data = {"total": 0, "rows": []} # 根据用户id获取所在的用户组 group_ids = PermissionsRelService.ids_by_userid( TPermissionsUserGroupRel(), user_id, 'gid') if not group_ids or len(group_ids) < 1: return make_response( json.dumps(ResponseCode.response(data=data), ensure_ascii=False)) # 根据用户组获取角色id role_ids = PermissionsRelService.ids_in_groupids( TPermissionsRoleGroupRel(), group_ids, "rid") if not role_ids or len(role_ids) < 1: return make_response( json.dumps(ResponseCode.response(data=data), ensure_ascii=False)) # 条件查找 conditions['id'] = role_ids conditions['rule']['id'] = Constants.QUERY_CONDITION_RULE_IN model = TPermissionsRole() rows = CRUDHelper.execute_select(model, conditions) total = CRUDHelper.execute_select_count(model, conditions) data['rows'] = rows data['total'] = total return make_response( json.dumps(ResponseCode.response(data=data), ensure_ascii=False))
def group_roles(): """ 用户组拥有角色列表 :return: """ params = request.values.get('params') conditions = json.loads(params) group_id = conditions['group_id'] role_ids = PermissionsRelService.ids_in_groupids( model=TPermissionsRoleGroupRel(), gids=[group_id], select_column_name='rid') if not role_ids: data = {"rows": [], "total": 0} return make_response( json.dumps(ResponseCode.response(data=data), ensure_ascii=False)) conditions['group_id'] = None conditions['id'] = role_ids conditions['rule']['id'] = Constants.QUERY_CONDITION_RULE_IN model = TPermissionsRole() rows = CRUDHelper.execute_select(model, conditions) total = CRUDHelper.execute_select_count(model, conditions) data = {"rows": rows, "total": total} return make_response( json.dumps(ResponseCode.response(data=data), ensure_ascii=False))
def resource_by_id(): """ 根据id获取数据 :return: json格式数据 """ res_id = request.values.get('res_id') if res_id: row = TPermissionsResource.query.get(res_id) model = TPermissionsResource() model.row_to_model(row) return make_response(json.dumps(ResponseCode.response(data=model.to_json()), ensure_ascii=False)) return make_response(json.dumps(ResponseCode.response(), ensure_ascii=False))
def user_roles_uid(): """ 获取用户的关联角色ids :return: """ params = request.values.get("user_id") if not params or len(params) < 1: return make_response( json.dumps(ResponseCode.response(data=[]), ensure_ascii=False)) role_ids = PermissionsRelService.ids_by_userid( TPermissionsUserRoleRel(), uid=params, select_column_name='rid') return make_response( json.dumps(ResponseCode.response(data=role_ids), ensure_ascii=False))
def post_user_group(): """ 更新用户和用户组的关联关系 :return: """ params_data = request.get_json()['params'] user_id = params_data['user_id'] group_ids = params_data['group_ids'] if not user_id or len(user_id) < 1: raise Exception("参数 user_id is blank") # 删除用户和角色的关联关系 PermissionsRelService.del_in_uids(TPermissionsUserRoleRel(), [user_id]) # 清空老的用户和用户组之间的关联关系 PermissionsRelService.del_in_uids(TPermissionsUserGroupRel(), [user_id]) # 重新关联 if group_ids and len(group_ids) > 0: for group_id in group_ids: DbHelper.get_db().session.add( TPermissionsUserGroupRel( uid=user_id, gid=group_id, create_time=DateUtils.get_current_time(), last_update_time=DateUtils.get_current_time())) return make_response( json.dumps(ResponseCode.response(), ensure_ascii=False))
def post_role_group(): """ 更新角色和用户组的关联关系 :return: """ params_data = request.get_json()['params'] role_id = params_data['role_id'] group_ids = params_data['group_ids'] if not role_id or len(role_id) < 1: raise Exception("参数 role_id is blank") # 删除角色和用户组的关联关系 PermissionsRelService.del_in_roleids(TPermissionsRoleGroupRel(), [role_id]) # 删除角色和用户的关联关系 PermissionsRelService.del_in_roleids(TPermissionsUserRoleRel(), [role_id]) # 删除角色和资源的关联关系 PermissionsRelService.del_in_roleids(TPermissionsResRoleRel(), [role_id]) # 重新关联 if group_ids and len(group_ids) > 0: for group_id in group_ids: DbHelper.get_db().session.add( TPermissionsRoleGroupRel( rid=role_id, gid=group_id, create_time=DateUtils.get_current_time(), last_update_time=DateUtils.get_current_time())) return make_response( json.dumps(ResponseCode.response(), ensure_ascii=False))
def post_user_role(): """ 关联角色 :return: """ params_data = request.get_json()['params'] user_id = params_data['user_id'] role_ids = params_data['role_ids'] if not user_id or len(user_id) < 1: raise Exception("参数 user_id is blank") # 删除旧的用户和角色的关联关系 PermissionsRelService.del_in_uids(TPermissionsUserRoleRel(), [user_id]) # 重新关联用户和角色 if role_ids and len(role_ids) > 0: for role_id in role_ids: DbHelper.get_db().session.add( TPermissionsUserRoleRel( uid=user_id, rid=role_id, create_time=DateUtils.get_current_time(), last_update_time=DateUtils.get_current_time())) return make_response( json.dumps(ResponseCode.response(), ensure_ascii=False))
def put_session(): """ 登录session :return: """ params_data = request.get_json()['params'] username = params_data['username'] password = params_data['pass'] row_user_info = DbHelper.get_db().session.query(TPermissionsUser).filter_by( login_name=username, valid_status=Constants.VALID_STATUS_Y).first() # 如果查不到 if not row_user_info: # 返回用户不存在提示信息 return make_response( json.dumps(ResponseCode.response(res_json=Code.ACCOUNT_NOT_EXISTS), ensure_ascii=False)) # 如果存在,则检查密码 user_model = TPermissionsUser() user_model.row_to_model(row_user_info) if not user_model.check_password(password): return make_response( json.dumps(ResponseCode.response(res_json=Code.LOGIN_PASS_ERROR), ensure_ascii=False)) # 用户名密码正确,生成token salt = user_model.salt payload = { 'user': {"username": user_model.login_name, "password": user_model.password}, 'aud': salt, 'exp': datetime.datetime.utcnow() + datetime.timedelta(seconds=int(SysConfHelper.get_item('jwt', 'exp'))) } token = jwt.encode(payload, SysConfHelper.get_item('jwt', 'secret_key'), algorithm='HS256') user = user_model.to_json() # 权限资源 nav_menus = PermissionsResourceService.resources_by_user(uid=user['id'], res_type=Constants.RES_TYPE_MENU) # f_uris = PermissionsResourceService.resources_by_user(uid=user['id'], # user_type=user_type, res_type=Constants.RES_TYPE_FURI) uris = PermissionsResourceService.resources_by_user(uid=user['id'], res_type=Constants.RES_TYPE_URI) data = dict() data['token'] = str(bytes(token), encoding='utf-8') data['user'] = json.dumps(user) data['salt'] = salt data['nav_menus'] = json.dumps(nav_menus) # data['f_uris'] = json.dumps(f_uris) data['uris'] = json.dumps(uris) data['upd_resource_time'] = DateUtils.get_current_time() return make_response( json.dumps(ResponseCode.response(data=data), ensure_ascii=False))
def get_resource(): """ 获取权限资源列表 :return: """ model = TPermissionsResource() rows = CRUDHelper.execute_select(model, {}) data = {"rows": PermissionsResourceService.gen_resource_tree(rows)} return make_response(json.dumps(ResponseCode.response(data=data), ensure_ascii=False))
def resource_check_uniq(): """ 检查唯一性约束 :return: 结果对象 """ params = request.values.get('params') conditions = json.loads(params) model = TPermissionsResource() rows = CRUDHelper.execute_select(model=model, conditions=conditions) return make_response(json.dumps(ResponseCode.response(data=rows), ensure_ascii=False))
def post_resource(): """ 修改权限资源 :return: """ params_data = request.get_json()['params'] params_data['last_update_time'] = DateUtils.get_current_time() DbHelper.get_db().session.query(TPermissionsResource).filter_by(id=params_data['id']).update(params_data) DbHelper.get_db().session.commit() return make_response(json.dumps(ResponseCode.response(), ensure_ascii=False))
def get_group(): """ 获取用户组列表 :return: """ model = TPermissionsGroup() rows = CRUDHelper.execute_select(model, {}) DbHelper.get_db().session.close() data = {"rows": PermissionsGroupService.gen_group_tree(rows)} return make_response( json.dumps(ResponseCode.response(data=data), ensure_ascii=False))
def delete_example(): """ 删除 :return: json """ params_data = json.loads(request.args.get('ids')) DbHelper.get_db().session.execute( SqlHelper.gen_del_sql(TExample(), params_data)) DbHelper.get_db().session.commit() return make_response( json.dumps(ResponseCode.response(), ensure_ascii=False))
def delete_group_resources(): """ 删除用户组和资源的关联关系 :return: """ params_data = json.loads(request.args.get('params')) if params_data['gid'] == Constants.ADMIN_ID: """ 过滤系统菜单关联关系 """ sys_rel = [1, 2, 3, 4, 5, 6] for i, v in enumerate(sys_rel): if params_data['rid'] == str(v): return make_response( json.dumps(ResponseCode.response(), ensure_ascii=False)) model = TPermissionsResGroupRel() CRUDHelper.execute_delete_by_conditions(model, params_data) return make_response( json.dumps(ResponseCode.response(), ensure_ascii=False))
def before(): """ 验证token :return: """ url = request.url_rule method = request.method if method == 'OPTIONS': return for i, white in enumerate(white_list): if white['method'] == str(method) and white['url'] == str(url): break else: token_str = request.headers.get('Py-Vue-Token') if not token_str: return make_response( json.dumps(ResponseCode.response(res_json=Code.NO_TOKEN))) token_json = json.loads(token_str) if not 'token' in token_json.keys(): return make_response( json.dumps(ResponseCode.response(res_json=Code.NO_TOKEN))) token = bytes(token_json['token'], encoding='utf-8') salt = token_json['salt'] try: user = jwt.decode(token, SysConfHelper.get_item('jwt', 'secret_key'), audience=salt, algorithm='HS256') if not user: return make_response( json.dumps( ResponseCode.response(res_json=Code.SIGN_ERROR))) except jwt.InvalidAudienceError as ie: error('before', 'InvalidAudienceError:' + str(ie)) return make_response( json.dumps(ResponseCode.response(res_json=Code.SALT_ERROR))) except jwt.ExpiredSignatureError as se: error('before', 'InvalidAudienceError:' + str(se)) return make_response( json.dumps(ResponseCode.response(res_json=Code.EXP_TOKEN))) return
def get_user_group(): """ 获取用户关联的用户组数据 :return: """ params = request.values.get('params') params_data = json.loads(params) group_ids = PermissionsRelService.ids_by_userid( TPermissionsUserGroupRel(), params_data['user_id'], 'gid') return make_response( json.dumps(ResponseCode.response(data=group_ids), ensure_ascii=False))
def get_permission_submenus(): params = request.values.get('params') params = json.loads(params) pid = params['pid'] uid = params['uid'] submenus = PermissionsResourceService.resources_by_user( uid=uid, res_type=Constants.RES_TYPE_MENU, pid=None) # 没有子菜单 if not submenus or len(submenus) < 1: return make_response( json.dumps(ResponseCode.response(data=[]), ensure_ascii=False)) one_submenus_by_pid = [] # 从所有的子菜单中找出该pid下的一级子菜单 for i, item in enumerate(submenus): if item['pid'] == pid: one_submenus_by_pid.append(item) # 递归遍历出一级子菜单的子菜单 for i, item in enumerate(one_submenus_by_pid): PermissionsResourceService.gen_submenu_tree(item, submenus) return make_response( json.dumps(ResponseCode.response(data=one_submenus_by_pid), ensure_ascii=False))
def get_role_group(): """ 获取角色关联的用户组数据 :return: """ params = request.values.get('params') params_data = json.loads(params) group_ids = PermissionsRelService.ids_in_roleids( TPermissionsRoleGroupRel(), [params_data['role_id']], 'gid') return make_response( json.dumps(ResponseCode.response(data=group_ids), ensure_ascii=False))
def delete_resource(): """ 删除权限资源 :return: """ params_data = json.loads(request.args.get('ids')) if len(params_data) > 0: # 删除资源和用户组的关联关系 PermissionsRelService.del_in_resids(TPermissionsResGroupRel(), params_data) # 删除角色和资源的关联关系 PermissionsRelService.del_in_resids(TPermissionsResRoleRel(), params_data) CRUDHelper.execute_delete_by_field_in(TPermissionsResource(), params_data, 'id') return make_response(json.dumps(ResponseCode.response(), ensure_ascii=False))
def get_user(): """ 获取用户列表 :return: """ params = request.values.get('params') conditions = json.loads(params) model = TPermissionsUser() rows = CRUDHelper.execute_select(model, conditions) total = CRUDHelper.execute_select_count(model, conditions) DbHelper.get_db().session.close() data = {"total": total, "rows": rows} return make_response( json.dumps(ResponseCode.response(data=data), ensure_ascii=False))
def put_example(): """ 新增 :return: json """ params_data = request.get_json()['params'] example = TExample() example.set_attr_by_dist(params_data) example.create_time = DateUtils.get_current_time() example.last_update_time = example.create_time DbHelper.get_db().session.add(example) DbHelper.get_db().session.commit() return make_response( json.dumps(ResponseCode.response(), ensure_ascii=False))
def get_example(): """ example 查询 :return: json """ params = request.values.get('params') conditions = json.loads(params) example_model = TExample() rows = CRUDHelper.execute_select(example_model, conditions) total = CRUDHelper.execute_select_count(example_model, conditions) data = {"total": total, "rows": rows} return make_response( json.dumps(ResponseCode.response(data=data), ensure_ascii=False))
def put_resource(): """ 新增权限资源 :return: """ params_data = request.get_json()['params'] params_data['pid'] = params_data['pid'] if params_data['pid'] else Constants.ROOT_PID resource = TPermissionsResource() resource.set_attr_by_dist(params_data) resource.create_time = DateUtils.get_current_time() resource.last_update_time = resource.create_time DbHelper.get_db().session.add(resource) DbHelper.get_db().session.commit() return make_response(json.dumps(ResponseCode.response(), ensure_ascii=False))
def put_user(): """ 新增用户 :return: """ params_data = request.get_json()['params'] user = TPermissionsUser() user.set_attr_by_dist(params_data) user.password = generate_password_hash(user.password) user.create_time = DateUtils.get_current_time() user.last_update_time = user.create_time user.salt = uuidUtils.gen_uuid() DbHelper.get_db().session.add(user) DbHelper.get_db().session.commit() return make_response( json.dumps(ResponseCode.response(), ensure_ascii=False))
def put_group(): """ 新增用户组 :return: """ params_data = request.get_json()['params'] params_data['pid'] = params_data['pid'] if params_data[ 'pid'] else Constants.ROOT_PID group = TPermissionsGroup() group.set_attr_by_dist(params_data) group.create_time = DateUtils.get_current_time() group.last_update_time = group.create_time DbHelper.get_db().session.add(group) DbHelper.get_db().session.commit() return make_response( json.dumps(ResponseCode.response(), ensure_ascii=False))
def delete_user(): """ 删除用户 :return: """ params_data = json.loads(request.args.get('ids')) if not params_data or len(params_data) < 1: raise Exception("参数 ids is blank") # 删除用户和用户组的关联关系 PermissionsRelService.del_in_uids(TPermissionsUserGroupRel(), params_data) # 删除用户和角色的关联关系 PermissionsRelService.del_in_uids(TPermissionsUserRoleRel(), params_data) CRUDHelper.execute_delete_by_field_in(TPermissionsUser(), params_data, 'id') return make_response( json.dumps(ResponseCode.response(), ensure_ascii=False))
def post_group_resources(): params_data = request.get_json()['params'] params_data = params_data['params'] group_id = params_data['group_id'] res_ids = params_data['res_ids'] if not group_id: raise Exception('param group_id is none') PermissionsRelService.del_in_groupids(model=TPermissionsResGroupRel(), groupids=[group_id]) for i, rid in enumerate(res_ids): DbHelper.get_db().session.add( TPermissionsResGroupRel( rid=rid, gid=group_id, valid_status=Constants.VALID_STATUS_Y, create_time=DateUtils.get_current_time(), last_update_time=DateUtils.get_current_time())) return make_response( json.dumps(ResponseCode.response(), ensure_ascii=False))
def delete_group(): """ 删除用户组 :return: """ params_data = json.loads(request.args.get('ids')) if len(params_data) > 0: # 删除用户和用户组的关联关系 PermissionsRelService.del_in_groupids(TPermissionsUserGroupRel(), params_data) # 删除角色和用户组的关联关系 PermissionsRelService.del_in_groupids(TPermissionsRoleGroupRel(), params_data) # 删除资源和用户组的关联关系 PermissionsRelService.del_in_groupids(TPermissionsResGroupRel(), params_data) CRUDHelper.execute_upd(TPermissionsGroup(), {'valid_status': Constants.VALID_STATUS_N}, params_data) return make_response( json.dumps(ResponseCode.response(), ensure_ascii=False))
def post_role_resource(): """ 重新关联资源 :return: """ params_data = request.get_json()['params'] role_id = params_data['role_id'] res_ids = params_data['res_ids'] if not role_id: raise Exception('param role_id is none') PermissionsRelService.del_in_roleids(model=TPermissionsResRoleRel(), rids=[role_id]) for i, rid in enumerate(res_ids): DbHelper.get_db().session.add( TPermissionsResRoleRel( res_id=rid, role_id=role_id, valid_status=Constants.VALID_STATUS_Y, create_time=DateUtils.get_current_time(), last_update_time=DateUtils.get_current_time())) return make_response( json.dumps(ResponseCode.response(), ensure_ascii=False))