{
  "tags": [
    "CC 防护"
  ],
  "description": "为当前登录用户创建一个 CC 规则组，创建前会按当前账号的网站数量和系统配置检查站点关联对象配额；同一所属用户下规则组名称不可重复。普通用户只能创建归属于自己的自定义规则组，管理员可指定所属用户、创建系统内置规则或设置定向可见用户。每个规则步骤必须指定执行模式、动作、已启用的匹配器和第一过滤器，可选第二过滤器；步骤数量不得超过运行时配置 `cc-rule-max-limit`，初始安装值为 5。记录创建成功后会立即创建节点同步任务，规则组可按请求以启用或禁用状态创建。",
  "operationId": "user_post_v1_cc_rules",
  "security": [
    {
      "AccessToken": []
    },
    {
      "ApiKey": [],
      "ApiSecret": []
    }
  ],
  "responses": {
    "200": {
      "description": "业务结果通过 HTTP 200 返回。规则组、关联同步任务创建并提交后返回新规则组 ID；配额、名称、排序、步骤数量或结构、模式、动作、匹配器、过滤器、步骤状态、定向用户、启用/显示状态、管理员专属字段、系统维护或认证校验失败时返回字符串业务错误码，事务回滚。",
      "content": {
        "application/json": {
          "schema": {
            "oneOf": [
              {
                "type": "object",
                "required": [
                  "code",
                  "msg",
                  "data"
                ],
                "properties": {
                  "code": {
                    "type": "integer",
                    "enum": [
                      0
                    ],
                    "description": "成功状态码，规则组及同步任务创建并提交后固定为 0。"
                  },
                  "msg": {
                    "type": "string",
                    "enum": [
                      "cc规则添加成功"
                    ],
                    "description": "CC 规则组创建成功后返回的确认消息。"
                  },
                  "data": {
                    "type": "integer",
                    "format": "int64",
                    "minimum": 1,
                    "description": "数据库为新 CC 规则组生成的唯一 ID。"
                  }
                },
                "additionalProperties": false
              },
              {
                "type": "object",
                "required": [
                  "code",
                  "msg",
                  "data"
                ],
                "properties": {
                  "code": {
                    "type": "string",
                    "enum": [
                      "cc_rule-10",
                      "cc_rule-11",
                      "cc_rule-13",
                      "cc_rule-14",
                      "cc_rule-17",
                      "cc_rule-18",
                      "cc_rule-19",
                      "cc_rule-20",
                      "cc_rule-21",
                      "cc_rule-22",
                      "cc_rule-23",
                      "cc_rule-24",
                      "cc_rule-25",
                      "cc_rule-26",
                      "cc_rule-33",
                      "cc_rule-34",
                      "cc_rule-34-1",
                      "cc_rule-35",
                      "cc_rule-35-1",
                      "cc_rule-35-2",
                      "cc_rule-36",
                      "cc_rule-37",
                      "cc_rule-38",
                      "cc_rule-39",
                      "cc_rule-41",
                      "cc_rule-41-1",
                      "cc_rule-42",
                      "cc_rule-371",
                      "cc_rule-372",
                      "cc_rule-377",
                      "cc_rule-378",
                      "cc_rule-379",
                      "cc_rule-380",
                      "maintain",
                      "login_required"
                    ],
                    "description": "业务错误码：10 表示创建数量超过站点关联对象配额；11、13、14 表示名称为空、重复或缺失；17、18、24 表示 data 类型错误、步骤过多或缺失；19 至 23 表示动作、引用对象、步骤状态或第二过滤器无效；25、26 表示 enable 为空或无效；33 至 35-2 表示步骤缺少 action、matcher、filter1 或其 ID 格式错误；36、37 表示排序为空或不是数字；38、39 表示 is_show 为空或无效；41、41-1 表示定向用户不存在或格式错误；42 表示 internal 无效；371、372 表示 mode 缺失或无效；377 至 380 表示普通用户使用管理员专属参数；`maintain` 表示当前禁止写操作，`login_required` 表示认证无效。"
                  },
                  "msg": {
                    "type": "string",
                    "description": "与业务错误码对应的中文配额、字段、规则步骤、引用对象、可见性、权限、维护状态或认证失败原因。"
                  },
                  "data": {
                    "type": "string",
                    "enum": [
                      ""
                    ],
                    "description": "业务错误时固定为空字符串。"
                  }
                },
                "additionalProperties": false
              }
            ]
          },
          "example": {
            "code": 0,
            "msg": "cc规则添加成功",
            "data": 10021
          }
        }
      }
    }
  },
  "x-python-handler": "site.CCRuleAPI",
  "x-python-route-line": 410,
  "x-audience": "user",
  "x-auth": "user",
  "x-reviewed": true,
  "requestBody": {
    "required": true,
    "content": {
      "application/json": {
        "schema": {
          "type": "object",
          "required": [
            "name",
            "data"
          ],
          "properties": {
            "name": {
              "type": "string",
              "minLength": 1,
              "maxLength": 255,
              "description": "规则组名称；服务端会过滤可执行的 HTML 内容，且要求处理后的名称非空、在所属用户下唯一。"
            },
            "sort": {
              "oneOf": [
                {
                  "type": "integer",
                  "minimum": 0
                },
                {
                  "type": "string",
                  "pattern": "^[0-9]+$"
                }
              ],
              "default": 100,
              "description": "规则组排序值，数值越小越靠前；接受非负整数或仅含十进制数字的字符串，省略时为 100。"
            },
            "des": {
              "type": "string",
              "maxLength": 255,
              "default": "",
              "description": "规则组备注；服务端会过滤可执行的 HTML 内容，省略时保存为空字符串。"
            },
            "data": {
              "type": "array",
              "description": "按顺序执行的 CC 规则步骤数组，可为空数组；元素数量不得超过系统配置 `cc-rule-max-limit`，初始安装值为 5。",
              "items": {
                "type": "object",
                "required": [
                  "mode",
                  "action",
                  "matcher",
                  "filter1"
                ],
                "properties": {
                  "mode": {
                    "type": "string",
                    "enum": [
                      "break",
                      "continue"
                    ],
                    "description": "步骤命中后的流程模式：`break` 表示执行后结束该规则组，`continue` 表示继续评估后续步骤。"
                  },
                  "action": {
                    "type": "string",
                    "enum": [
                      "block",
                      "log"
                    ],
                    "description": "步骤动作：`block` 执行所选过滤器防护，`log` 记录命中行为。"
                  },
                  "matcher": {
                    "oneOf": [
                      {
                        "type": "integer",
                        "minimum": 1
                      },
                      {
                        "type": "string",
                        "pattern": "^[0-9]+$"
                      }
                    ],
                    "description": "用于判断该步骤是否命中的 CC 匹配器 ID；接受整数或数字字符串，目标匹配器必须存在且处于启用状态，服务端最终保存为字符串。"
                  },
                  "filter1": {
                    "oneOf": [
                      {
                        "type": "integer",
                        "minimum": 1
                      },
                      {
                        "type": "string",
                        "pattern": "^[0-9]+$"
                      }
                    ],
                    "description": "步骤使用的第一 CC 过滤器 ID；接受整数或数字字符串，目标过滤器必须存在且处于启用状态，服务端最终保存为字符串。"
                  },
                  "filter2": {
                    "oneOf": [
                      {
                        "type": "integer",
                        "minimum": 1
                      },
                      {
                        "type": "string",
                        "pattern": "^(?:[0-9]+)?$"
                      }
                    ],
                    "default": "",
                    "description": "可选的第二 CC 过滤器 ID；省略或空字符串表示不使用第二过滤器，非空时必须是已启用过滤器的整数或数字字符串。"
                  },
                  "state": {
                    "oneOf": [
                      {
                        "type": "boolean"
                      },
                      {
                        "type": "integer",
                        "enum": [
                          0,
                          1
                        ]
                      }
                    ],
                    "default": true,
                    "description": "该规则步骤是否启用；省略时为 true，也接受 0 或 1。"
                  },
                  "matcher_name": {
                    "type": "string",
                    "description": "匹配器名称快照，仅供界面展示；服务端不据此查找匹配器，会与步骤一起原样保存。"
                  },
                  "filter1_name": {
                    "type": "string",
                    "description": "第一过滤器名称快照，仅供界面展示；服务端不据此查找过滤器，会与步骤一起原样保存。"
                  },
                  "filter2_name": {
                    "type": "string",
                    "description": "第二过滤器名称快照，仅供界面展示；没有第二过滤器时通常为空字符串。"
                  }
                },
                "additionalProperties": true,
                "description": "单个 CC 规则步骤；附加字段不会被 Python 校验器移除，会随步骤一起序列化保存。"
              }
            },
            "show_to": {
              "oneOf": [
                {
                  "type": "integer",
                  "minimum": 1
                },
                {
                  "type": "string",
                  "pattern": "^(?:[0-9]+(?:,[0-9]+)*)?$"
                }
              ],
              "default": "",
              "description": "允许看到该规则组的用户 ID，多个 ID 使用英文逗号分隔；空字符串表示未指定定向用户。仅管理员可设置，每个 ID 都必须对应现有用户。"
            },
            "uid": {
              "type": "integer",
              "format": "int64",
              "minimum": 1,
              "description": "新规则组所属用户的 ID；省略时为当前登录用户，仅管理员可指定该字段，否则返回 `cc_rule-378`。"
            },
            "internal": {
              "oneOf": [
                {
                  "type": "boolean"
                },
                {
                  "type": "integer",
                  "enum": [
                    0,
                    1
                  ]
                }
              ],
              "default": 0,
              "description": "是否创建为系统内置规则组：true 或 1 表示内置，false 或 0 表示用户自定义；省略时为 0，创建内置规则要求管理员身份。"
            },
            "enable": {
              "oneOf": [
                {
                  "type": "boolean"
                },
                {
                  "type": "integer",
                  "enum": [
                    0,
                    1
                  ]
                }
              ],
              "default": true,
              "description": "规则组初始启用状态；true 或 1 表示启用，false 或 0 表示禁用，省略时启用。"
            },
            "is_show": {
              "oneOf": [
                {
                  "type": "boolean"
                },
                {
                  "type": "integer",
                  "enum": [
                    0,
                    1
                  ]
                }
              ],
              "default": true,
              "description": "规则组初始前台显示状态；true 或 1 表示展示，false 或 0 表示隐藏，省略时展示。"
            }
          },
          "additionalProperties": true,
          "description": "创建 CC 规则组的 JSON 对象；未识别的顶层字段不会参与创建逻辑。"
        },
        "example": {
          "name": "验证码防护",
          "sort": 100,
          "des": "匹配 API 请求后执行验证码过滤器",
          "data": [
            {
              "mode": "break",
              "action": "block",
              "matcher": "10021",
              "matcher_name": "匹配 API 请求",
              "filter1": "10021",
              "filter1_name": "验证码",
              "filter2": "",
              "filter2_name": "",
              "state": true
            }
          ],
          "enable": true,
          "is_show": true
        }
      }
    }
  },
  "parameters": [
    {
      "name": "internal",
      "in": "query",
      "required": false,
      "description": "兼容性权限检查参数。传 1 时要求管理员身份，否则返回 `cc_rule-377`；该查询参数本身不会把新规则组标记为内置，创建内置规则应在 JSON 对象中传 `internal: 1`。",
      "schema": {
        "type": "integer",
        "enum": [
          1
        ]
      },
      "example": 1
    }
  ],
  "x-itecdn-source-reviewed": true,
  "extensions": [
    {
      "key": "x-python-handler",
      "value": "site.CCRuleAPI"
    },
    {
      "key": "x-python-route-line",
      "value": 410
    },
    {
      "key": "x-audience",
      "value": "user"
    },
    {
      "key": "x-auth",
      "value": "user"
    },
    {
      "key": "x-reviewed",
      "value": true
    },
    {
      "key": "x-itecdn-source-reviewed",
      "value": true
    }
  ],
  "method": "post",
  "path": "/v1/cc-rules",
  "servers": [
    {
      "url": "https://master.example.invalid",
      "description": "不可调用的示例地址；复制示例后替换为自己的主控域名。"
    }
  ],
  "securitySchemes": {
    "AccessToken": {
      "type": "apiKey",
      "in": "header",
      "name": "access-token",
      "description": "通过登录接口获得的短期访问令牌。"
    },
    "ApiKey": {
      "type": "apiKey",
      "in": "header",
      "name": "api-key",
      "description": "API Key；必须和 api-secret 同时发送。"
    },
    "ApiSecret": {
      "type": "apiKey",
      "in": "header",
      "name": "api-secret",
      "description": "API Secret；必须和 api-key 同时发送。"
    }
  },
  "jsonRequestBodyExample": {
    "name": "string",
    "sort": 0,
    "des": "",
    "data": [
      {
        "mode": "break",
        "action": "block",
        "matcher": 0,
        "filter1": 0,
        "filter2": 0,
        "state": true,
        "matcher_name": "string",
        "filter1_name": "string",
        "filter2_name": "string"
      }
    ],
    "show_to": 0,
    "uid": 0,
    "internal": true,
    "enable": true,
    "is_show": true
  },
  "info": {
    "title": "itecdn v5 用户 API",
    "version": "5.x",
    "description": "itecdn v5.x Python 主控用户 API。本文档不适用于 v6 或其它实现。"
  },
  "postman": {
    "name": "创建 CC 规则组",
    "description": {
      "content": "为当前登录用户创建一个 CC 规则组，创建前会按当前账号的网站数量和系统配置检查站点关联对象配额；同一所属用户下规则组名称不可重复。普通用户只能创建归属于自己的自定义规则组，管理员可指定所属用户、创建系统内置规则或设置定向可见用户。每个规则步骤必须指定执行模式、动作、已启用的匹配器和第一过滤器，可选第二过滤器；步骤数量不得超过运行时配置 `cc-rule-max-limit`，初始安装值为 5。记录创建成功后会立即创建节点同步任务，规则组可按请求以启用或禁用状态创建。",
      "type": "text/plain"
    },
    "url": {
      "path": [
        "v1",
        "cc-rules"
      ],
      "host": [
        "{{baseUrl}}"
      ],
      "query": [
        {
          "disabled": false,
          "description": {
            "content": "兼容性权限检查参数。传 1 时要求管理员身份，否则返回 `cc_rule-377`；该查询参数本身不会把新规则组标记为内置，创建内置规则应在 JSON 对象中传 `internal: 1`。",
            "type": "text/plain"
          },
          "key": "internal",
          "value": ""
        }
      ],
      "variable": []
    },
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "Accept",
        "value": "application/json"
      }
    ],
    "method": "POST",
    "body": {
      "mode": "raw",
      "raw": "",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "auth": {
      "type": "apikey",
      "apikey": [
        {
          "type": "any",
          "value": "access-token",
          "key": "key"
        },
        {
          "type": "any",
          "value": "{{apiKey}}",
          "key": "value"
        },
        {
          "type": "any",
          "value": "header",
          "key": "in"
        }
      ]
    }
  }
}