---
title: 访问控制
description: 此页列出了使用特权、所有权和访问控制的常见例子
---

本页列出了使用权限、所有权和访问控制的常见示例。

## 如何使用 Ownable trait检查发件人权限

```tact
// Ownable has to be imported from stdlib:
import "@stdlib/ownable";

message FooBarMsg {
    newVal: Int as uint32;
}

// Ownable trait can limit certain actions to the owner only
contract SenderChecker with Ownable {
    // Persistent state variables
    owner: Address;     // Ownable trait requires you to add this exact state variable
    val: Int as uint32; // some value

    init() {
        // we can initialize owner to any value we want, the deployer in this case:
        self.owner = sender();
        self.val = 0;
    }

    receive() { cashback(sender()) } // for the deployment

    receive("inc") {
        self.requireOwner(); // throws exit code 132 if the sender isn't an owner
        self.val += 1;
    }

    receive(msg: FooBarMsg) {
        self.requireOwner(); // throws exit code 132 if the sender isn't an owner
        self.val = msg.newVal;
    }
}
```

:::note[Useful links:]

  [核心库中的 `trait Ownable{:tact}`](/zh-cn/ref/stdlib-ownable#ownable)

:::

:::tip[Hey there!]

  没有找到您最喜欢的访问控制范例？  您有很酷的实施方案吗？ [欢迎投稿！](https://github.com/tact-lang/tact/issues)

:::
