Frappe Core Permissions 标志

Frappe Core Permissions

组织
Impertio-Studio
frappe-core-permissions

Use when implementing the Frappe/ERPNext permission system. Covers roles, user permissions, perm levels, data masking, and permission hooks for v14/v15/v16. Prevents common access control mistakes and security issues. Keywords: permissions, roles, user permissions, perm levels, data masking,, restrict records, who can see what, department access, row-level, user cannot see document, access denied. access control, security, has_permission.

Add to TypingMind

概览

发布者Impertio-Studio
代码仓库Frappe_Claude_Skill_Package
技能名称frappe-core-permissions
星标数
180
复刻数
53
附带文件
5
许可协议MIT
链接
  • Markdown 格式的指令

    模型按需加载 SKILL.md 文件,因此只有在请求确实与该技能匹配时才会消耗 Token。

  • 兼容任何大语言模型

    AI 技能采用纯 Markdown,而非特定提供商的代码,因此可搭配 GPT、Claude、Gemini、Grok 或本地模型使用。

  • 5 个附带文件

    模型执行任务时可读取的脚本、模板和参考资料。这些文件为只读文件,绝不会被执行。

  • 开源

    由 Impertio-Studio 发布于 GitHub。请在安装前查看技能的源文件。

安装

在 TypingMind 中安装 Frappe Core Permissions AI 技能,即可搭配任意大语言模型使用;也可以将其放入其他支持读取 SKILL.md 的智能体中。

1

安装到 TypingMind

TypingMind 可直接从 GitHub 文件夹安装技能:读取 SKILL.md,打包资源文件,并将结果保存在本地。

  1. 打开应用,前往“插件 → 技能”。
  2. 选择“从 GitHub 安装”。
  3. 粘贴下方的技能文件夹 URL 并确认。
    https://github.com/Impertio-Studio/Frappe_Claude_Skill_Package/tree/main/skills/source/core/frappe-core-permissions
  4. 请在每个需要使用该技能的聊天中启用它。
依次选择“插件 → 技能 → 添加技能 → 从 GitHub URL 导入”,然后粘贴文件夹 URL 并点击“继续”。
2

安装到其他智能体

任何支持 Agent Skills 格式的智能体都可以使用此技能,只需将文件夹复制到该智能体的技能目录中。

Claude Code — .claude/skills
git clone --depth 1 https://github.com/Impertio-Studio/Frappe_Claude_Skill_Package.git /tmp/Frappe_Claude_Skill_Package
mkdir -p .claude/skills
cp -r /tmp/Frappe_Claude_Skill_Package/skills/source/core/frappe-core-permissions .claude/skills/frappe-core-permissions
复制后,请重启 Claude Code,以加载新技能。

在 TypingMind 中使用此技能

在任意 TypingMind 聊天中启用 Frappe Core Permissions,后续即可交由模型处理。技能的名称和描述会放在系统提示词中,一旦请求匹配,模型就会自动加载完整指令,无需您手动调用,且在实际使用前不会消耗 Token。

一旦请求与技能匹配,模型就会自动加载 Frappe Core Permissions。

兼容任何 AI 模型

AI 技能是纯 Markdown 指令,而非特定提供商的代码,因此 Frappe Core Permissions 不局限于最初适配的模型。只需在 TypingMind 中安装一次,即可搭配 GPT-5、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 或您自行运行的本地模型使用,全程使用您自己的 API 密钥。

  • 仅在需要时加载

    系统提示词中仅包含技能名称和描述。首次遇到匹配的请求时才会加载完整指令,因此技能未使用时,这些指令不会消耗 Token。

  • 在聊天中切换模型

    技能是指令而非代码,因此切换模型不会导致技能失效,新模型会读取同一个 SKILL.md。

技能指令

这是模型会加载的 SKILL.md 内容。请在安装前阅读,因为技能就是模型将遵循的指令。

Frappe Permissions

Deterministic patterns for the five-layer Frappe permission system.


Permission Layers

LayerControlsConfigured ViaVersion
Role PermissionsWhat users CAN doDocType permissions tableAll
User PermissionsWHICH records users seeUser Permission DocTypeAll
Perm LevelsWHICH fields users see/editField permlevel propertyAll
Permission HooksCustom deny logichooks.pyAll
Data MaskingMasked field valuesField mask property[v16+]

Decision Tree

Need to control access?
├── Who can Create/Read/Write/Delete a DocType? → Role Permissions
├── Which specific records can a user see? → User Permissions
├── Which fields should be hidden? → Perm Levels (permlevel 1+)
├── Which fields show masked values? → Data Masking [v16+]
├── Custom runtime deny logic? → has_permission hook
├── Filter list queries dynamically? → permission_query_conditions hook
└── Share one document with one user? → frappe.share

Checking permissions in code?
├── Before action → frappe.has_permission() or doc.has_permission()
├── Raise on denial → doc.check_permission() or throw=True
├── System bypass → doc.flags.ignore_permissions = True (ALWAYS document why)
└── List query → ALWAYS use frappe.get_list() for user-facing data

Permission Types

TypeAPI CheckApplies To
readfrappe.has_permission(dt, "read")All DocTypes
writefrappe.has_permission(dt, "write")All DocTypes
createfrappe.has_permission(dt, "create")All DocTypes
deletefrappe.has_permission(dt, "delete")All DocTypes
submitfrappe.has_permission(dt, "submit")Submittable only
cancelfrappe.has_permission(dt, "cancel")Submittable only
amendfrappe.has_permission(dt, "amend")Submittable only
selectfrappe.has_permission(dt, "select")Link fields [v14+]
reportN/AReport Builder access
exportN/AExcel/CSV export
importN/AData Import Tool
shareN/AShare with other users
printN/APrint/PDF generation
emailN/ASend email
maskRole permission for unmasked viewData Masking [v16+]

Automatic Roles

RoleAssigned ToNotes
GuestEveryone (including anonymous)Public pages
AllAll registered usersBasic authenticated access
AdministratorOnly the Administrator userALWAYS has all permissions
Desk UserSystem Users only[v15+]

Essential API

Check Permission

python
# DocType-level
frappe.has_permission("Sales Order", "write")

# Document-level (by name or object)
frappe.has_permission("Sales Order", "write", "SO-00001")
frappe.has_permission("Sales Order", "write", doc=doc)

# For specific user
frappe.has_permission("Sales Order", "read", user="john@example.com")

# Throw on denial
frappe.has_permission("Sales Order", "delete", throw=True)

# Debug mode — prints evaluation steps
frappe.has_permission("Sales Order", "read", debug=True)
print(frappe.local.permission_debug_log)

Document Instance Methods

python
doc = frappe.get_doc("Sales Order", "SO-00001")

# Returns bool
if doc.has_permission("write"):
    doc.status = "Approved"
    doc.save()

# Raises frappe.PermissionError if denied
doc.check_permission("write")

Get Effective Permissions

python
from frappe.permissions import get_doc_permissions

perms = get_doc_permissions(doc)
# {'read': 1, 'write': 1, 'create': 0, 'delete': 0, ...}

perms = get_doc_permissions(doc, user="john@example.com")

User Permissions (Record-Level)

Restrict users to specific Link field values (e.g., specific Company, Territory).

python
from frappe.permissions import add_user_permission, remove_user_permission

# Restrict user to one company
add_user_permission(
    doctype="Company",
    name="My Company",
    user="john@example.com",
    is_default=1,            # auto-fill in new documents
    applicable_for="Sales Order"  # only for this DocType (optional)
)

# Remove restriction
remove_user_permission("Company", "My Company", "john@example.com")

# Query current restrictions
from frappe.permissions import get_user_permissions
perms = get_user_permissions("john@example.com")
# {"Company": [{"doc": "My Company", "is_default": 1}], ...}

Sharing (Document-Level)

Grant access to a single document for a specific user.

python
from frappe.share import add as add_share, remove as remove_share

add_share("Sales Order", "SO-00001", "jane@example.com",
          read=1, write=1, share=0, notify=1)

remove_share("Sales Order", "SO-00001", "jane@example.com")

# Share with everyone
add_share("Sales Order", "SO-00001", everyone=1, read=1)

Field-Level Permissions (Perm Levels)

Group fields by permlevel (0-9). Level 0 MUST be granted before higher levels.

json
{
  "fields": [
    {"fieldname": "employee_name", "permlevel": 0},
    {"fieldname": "salary",        "permlevel": 1}
  ],
  "permissions": [
    {"role": "Employee",   "permlevel": 0, "read": 1},
    {"role": "HR Manager", "permlevel": 0, "read": 1, "write": 1},
    {"role": "HR Manager", "permlevel": 1, "read": 1, "write": 1}
  ]
}

Rule: Levels do NOT imply hierarchy. Level 2 is not "higher" than level 1. They are independent field groups.


Data Masking [v16+]

Fields with mask=1 show masked values (e.g., ****, +91-811XXXXXXX) to users without mask permission.

json
{
  "fieldname": "phone_number", "fieldtype": "Data", "mask": 1
}

Grant mask permission to roles that MUST see unmasked values:

json
{"role": "HR Manager", "permlevel": 0, "read": 1, "mask": 1}

CRITICAL: Data masking does NOT apply to frappe.db.sql() or Query Reports with raw SQL. You MUST mask manually in custom SQL queries.


Permission Hooks

has_permission: Custom Deny Logic

Can only deny access. NEVER returns True to grant. ALWAYS returns None to continue standard checks.

python
# hooks.py
has_permission = {
    "Sales Order": "myapp.permissions.check_order_permission"
}
python
# myapp/permissions.py
def check_order_permission(doc, ptype, user):
    if ptype == "write" and doc.docstatus == 2:
        if "Sales Manager" not in frappe.get_roles(user):
            return False
    return None  # ALWAYS return None by default

permission_query_conditions: Filter List Queries

Returns SQL WHERE clause fragment. Only affects get_list(), NOT get_all().

python
# hooks.py
permission_query_conditions = {
    "Customer": "myapp.permissions.customer_query"
}
python
def customer_query(user):
    if not user:
        user = frappe.session.user
    if "Sales Manager" in frappe.get_roles(user):
        return ""
    return f"`tabCustomer`.owner = {frappe.db.escape(user)}"

ALWAYS use frappe.db.escape() — NEVER use string concatenation with raw user input.


get_list vs get_all

MethodUser PermissionsQuery HookUse For
frappe.get_list()AppliedAppliedUser-facing queries
frappe.get_all()IgnoredIgnoredSystem/background queries

ALWAYS use get_list() when returning data to users. get_all() bypasses ALL permission filtering.


Common Patterns

Owner-Only Edit

json
{"role": "Sales User", "read": 1, "write": 1, "create": 1, "if_owner": 1}

Role-Restricted Endpoint

python
@frappe.whitelist()
def sensitive_action():
    frappe.only_for(["Manager", "Administrator"])
    # Only reaches here if user has one of these roles

Bypass Permissions (Document Why!)

python
# On document — ALWAYS add a comment explaining the reason
doc.flags.ignore_permissions = True
doc.save()

# On method call
doc.save(ignore_permissions=True)
doc.insert(ignore_permissions=True)

Critical Rules

  1. ALWAYS use frappe.has_permission() — NEVER check roles directly for access control
  2. ALWAYS use frappe.get_list() for user-facing queries — NEVER get_all()
  3. ALWAYS escape SQL in query hooks — frappe.db.escape(user)
  4. ALWAYS prefix table names in query hooks — `tabDocType`.fieldname
  5. ALWAYS return None in has_permission hooks by default — NEVER True
  6. ALWAYS clear cache after permission changes — frappe.clear_cache()
  7. ALWAYS document ignore_permissions usage with a comment
  8. NEVER throw errors in has_permission hooks — return False to deny
  9. NEVER grant permlevel 1+ without granting permlevel 0 first
  10. NEVER assume data masking applies to custom SQL queries [v16+]

Anti-Patterns

Do NOTDo Instead
if "Role" in frappe.get_roles() for accessfrappe.has_permission(dt, ptype)
frappe.get_all() for user queriesfrappe.get_list()
return True in has_permission hookreturn None
f"owner = '{user}'" in SQLf"owner = {frappe.db.escape(user)}"
frappe.throw() in permission hooksreturn False
frappe.db.set_value() for user-facing updatesdoc.save() with permission check
Sensitive data in error messagesGeneric frappe.PermissionError

Version Differences

Featurev14v15v16
select permissionYesYesYes
Desk User roleNoYesYes
Data Masking (mask field)NoNoYes
mask permission typeNoNoYes
Custom Permission TypesNoNoExperimental

Permission Precedence

  1. Administrator — ALWAYS has all permissions (cannot be restricted)
  2. Role Permissions — Based on assigned roles
  3. User Permissions — Restricts to specific document values
  4. has_permission hook — Can only deny (any False = denied)
  5. Sharing — Grants access to shared documents
  6. if_owner — Further restricts to owned documents

Reference Files

FileContents
permission-types-reference.mdAll permission types with options
permission-api-reference.mdComplete API with all signatures
permission-hooks-reference.mdHook patterns and examples
examples.mdWorking implementation examples
anti-patterns.mdCommon mistakes and fixes

Related Skills

  • frappe-core-database — Database operations that respect permissions
  • frappe-core-api — API endpoints with permission checks
  • frappe-syntax-controllers — Controller permission validation
  • frappe-syntax-hooks — Hook configuration patterns

Verified against Frappe docs 2026-03-20 | Frappe v14/v15/v16

附带文件

技能加载后,模型会按需读取这些文件。它们仅作为可读文件提供,绝不会被执行。

常见问题

Frappe Core Permissions AI 技能有什么作用?

Use when implementing the Frappe/ERPNext permission system. Covers roles, user permissions, perm levels, data masking, and permission hooks for v14/v15/v16. Prevents common access control mistakes and security issues. Keywords: permissions, roles, user permissions, perm levels, data masking,, restrict records, who can see what, department access, row-level, user cannot see document, access denied. access control, security, has_permission.

为什么在 TypingMind 中使用 Frappe Core Permissions?

因为您只需安装一次,就能搭配任何模型使用。Frappe Core Permissions 采用纯 Markdown 格式,而非特定提供商专用的代码,因此同一技能可用于 GPT-5、Claude、Gemini、Grok 或本地模型。您甚至可以在聊天中途切换模型,而不会影响技能的正常使用。TypingMind 使用您自己的 API 密钥,因此您直接向提供商付费,而不是按席位订阅;您的技能和聊天记录也都保留在您自己的存储空间中。

如何在 TypingMind 中安装 Frappe Core Permissions?

在 TypingMind 中打开“插件 → 技能 → 从 GitHub 安装”,然后粘贴 https://github.com/Impertio-Studio/Frappe_Claude_Skill_Package/tree/main/skills/source/core/frappe-core-permissions。TypingMind 会读取其中的 SKILL.md,打包相关文件,并将其安装为技能,供您在各个聊天中按需启用。

哪些 AI 模型可以使用 Frappe Core Permissions?

您在 TypingMind 中连接的任何模型都可以。AI 技能采用纯 Markdown 指令,而非特定提供商专用的代码,因此 GPT、Claude、Gemini、Grok 和本地模型都能在请求匹配时加载此技能。

Frappe Core Permissions 可以搭配多少个 AI 模型使用?

数量不限。只要模型支持技能,您就可以将它与 Frappe Core Permissions 搭配使用。GPT、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 等模型都可以通过您自己的 API 密钥接入 TypingMind。

Frappe Core Permissions AI 技能免费吗?

是的。Impertio-Studio 已在 GitHub 上根据 MIT 许可证发布了该技能。您只需向自己的 AI 提供商支付所用 Token 的费用。

什么是 AI 技能?

AI 技能是一组可复用的指令,用于教会 AI 模型完成某项特定任务。它遵循开放的 Agent Skills 格式:包含名称和描述的 SKILL.md 文件,以及模型可能需要的脚本、模板或参考文件。模型仅在您的请求与技能匹配时才会读取这些指令,因此已安装的技能在使用前不会产生任何费用。

AI 技能与插件或 MCP 服务器有何不同?

插件或 MCP 服务器为模型提供可调用的新工具,也就是在某处运行并返回结果的代码。AI 技能则为模型提供知识和流程:如何处理任务、遵循哪些步骤,以及怎样的输出才算优质。技能采用纯 Markdown 格式,因此不需要服务器、API 密钥或运行时环境,并且适用于任何模型。

查看全部

立即创建您自己的 AI 工作区

订阅我们的电子简报,获取新功能和后续赠奖活动的通知 👇