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 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 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 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 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 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 resources_by_user(uid, res_type, pid=Constants.ROOT_PID, valid_status=Constants.VALID_STATUS_Y): """ 通过用户id、用户类型、资源类型查资源列表(导航菜单,前台uri,后台uri) :param valid_status: :param pid: 父级id :param res_type: 资源类型 :param uid: 用户id :return: """ # 根据用户查所属的用户组 group_ids = PermissionsRelService.ids_by_userid(TPermissionsUserGroupRel(), uid, "gid") if not group_ids or len(group_ids) < 1: return [] # 获取用户所在的所有用户组的权限数据 group_res_ids = PermissionsRelService.ids_in_groupids(TPermissionsResGroupRel(), group_ids, "rid") if not group_res_ids or len(group_res_ids) < 1: return [] # 根据用户查角色ids role_ids = PermissionsRelService.ids_by_userid(model=TPermissionsUserRoleRel(), uid=uid, select_column_name='rid') if uid else [] if not role_ids or len(role_ids) < 1: return [] # 根据角色查资源ids role_res_ids = PermissionsRelService.ids_in_roleids(TPermissionsResRoleRel(), role_ids, 'res_id') if not role_res_ids or len(role_res_ids) < 1: return [] # 计算用户组资源集合和角色资源集合的交集得到最终用户的所有资源id user_res_ids = list(set(group_res_ids).intersection(set(role_res_ids))) # 创建查找条件 conditions = { "id": user_res_ids, "res_type": res_type, "valid_status": valid_status } # 菜单资源时才有根据父查子数据 if pid and res_type == Constants.RES_TYPE_MENU: conditions['pid'] = pid conditions['rule'] = {"id": Constants.QUERY_CONDITION_RULE_IN} rows = CRUDHelper.execute_select(TPermissionsResource(), conditions) return rows
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_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 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_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))
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 get_resources_by_groupid(): """ 通过用户组id获取资源列表 :return: """ params = request.values.get('params') params = json.loads(params) group_id = params['group_id'] res_ids = PermissionsRelService.ids_in_groupids( model=TPermissionsResGroupRel(), gids=[group_id], select_column_name='rid') model = TPermissionsResource() conditions = dict(id=None, rule={}) current_resources = None if res_ids: conditions['id'] = res_ids conditions['rule']['id'] = Constants.QUERY_CONDITION_RULE_IN current_resources = CRUDHelper.execute_select(model, conditions) return make_response( json.dumps(ResponseCode.response(data={"rows": current_resources}), 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))