1 追蹤者

類別 yii\filters\AccessControl

繼承關係yii\filters\AccessControl » yii\base\ActionFilter » yii\base\Behavior » yii\base\BaseObject
實作介面yii\base\Configurable
自版本起可用2.0
原始碼 https://github.com/yiisoft/yii2/blob/master/framework/filters/AccessControl.php

AccessControl 基於一組規則提供簡單的存取控制。

AccessControl 是一個動作篩選器。它會檢查其 $rules 以找到第一個符合當前上下文變數(例如使用者 IP 位址、使用者角色)的規則。符合的規則將決定是否允許或拒絕存取請求的控制器動作。如果沒有規則符合,存取將被拒絕。

若要使用 AccessControl,請在其控制器類別的 behaviors() 方法中宣告它。例如,以下宣告將允許已驗證的使用者存取 "create" 和 "update" 動作,並拒絕所有其他使用者存取這兩個動作。

public function behaviors()
{
    return [
        'access' => [
            'class' => \yii\filters\AccessControl::class,
            'only' => ['create', 'update'],
            'rules' => [
                // deny all POST requests
                [
                    'allow' => false,
                    'verbs' => ['POST']
                ],
                // allow authenticated users
                [
                    'allow' => true,
                    'roles' => ['@'],
                ],
                // everything else is denied
            ],
        ],
    ];
}

公共屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$denyCallback callable|null 當應拒絕當前使用者存取時,將呼叫的回呼函數。 yii\filters\AccessControl
$except array 此篩選器不應套用的動作 ID 列表。 yii\base\ActionFilter
$only array 此篩選器應套用的動作 ID 列表。 yii\base\ActionFilter
$owner yii\base\Component|null 此行為的擁有者 yii\base\Behavior
$ruleConfig array 存取規則的預設組態。 yii\filters\AccessControl
$rules array 用於建立規則物件的存取規則物件或組態陣列的列表。 yii\filters\AccessControl
$user yii\web\User|array|string|false 表示驗證狀態的使用者物件,或是使用者應用程式元件的 ID。 yii\filters\AccessControl

公共方法

隱藏繼承的方法

方法 描述 定義於
__call() 呼叫未定義為類別方法的具名方法。 yii\base\BaseObject
__construct() 建構子。 yii\base\BaseObject
__get() 傳回物件屬性的值。 yii\base\BaseObject
__isset() 檢查屬性是否已設定,即已定義且非 null。 yii\base\BaseObject
__set() 設定物件屬性的值。 yii\base\BaseObject
__unset() 將物件屬性設定為 null。 yii\base\BaseObject
afterAction() 此方法在動作執行後立即調用。 yii\base\ActionFilter
afterFilter() yii\base\ActionFilter
attach() 將行為物件附加到元件。 yii\base\ActionFilter
beforeAction() 此方法在動作即將執行之前調用(在所有可能的篩選器之後)。您可以覆寫此方法,為動作進行最後一刻的準備。 yii\filters\AccessControl
beforeFilter() yii\base\ActionFilter
canGetProperty() 傳回一個值,指示是否可以讀取屬性。 yii\base\BaseObject
canSetProperty() 傳回一個值,指示是否可以設定屬性。 yii\base\BaseObject
className() 傳回此類別的完整限定名稱。 yii\base\BaseObject
detach() 從元件卸載行為物件。 yii\base\ActionFilter
events() $owner 的事件宣告事件處理器。 yii\base\Behavior
hasMethod() 傳回一個值,指示是否已定義方法。 yii\base\BaseObject
hasProperty() 傳回一個值,指示是否已定義屬性。 yii\base\BaseObject
init() 透過從組態實例化規則物件來初始化 $rules 陣列。 yii\filters\AccessControl

受保護的方法

隱藏繼承的方法

方法 描述 定義於
denyAccess() 拒絕使用者存取。 yii\filters\AccessControl
getActionId() 透過將 yii\base\Action::$uniqueId 轉換為相對於模組的 ID,傳回動作 ID。 yii\base\ActionFilter
isActive() 傳回一個值,指示篩選器是否對給定的動作處於活動狀態。 yii\base\ActionFilter

屬性詳細資訊

隱藏繼承的屬性

$denyCallback 公共屬性

當應拒絕當前使用者存取時,將呼叫的回呼函數。當沒有規則符合,或 $allow 設定為 false 的規則符合時,就會發生這種情況。如果未設定,將呼叫 denyAccess()

回呼的簽名應如下所示

function ($rule, $action)

其中 $rule 是拒絕使用者的規則,而 $action 是當前的 action 物件。如果因為沒有任何規則符合而拒絕存取,則 $rule 可以為 null

public callable|null $denyCallback null
$ruleConfig 公共屬性

存取規則的預設組態。透過 $rules 指定的個別規則組態,在規則的相同屬性被組態時,將優先採用。

public array $ruleConfig = [
    
'class' => 'yii\filters\AccessRule',
]
$rules 公共屬性

用於建立規則物件的存取規則物件或組態陣列的列表。如果規則是透過組態陣列指定的,則在用於建立規則物件之前,它將首先與 $ruleConfig 合併。

另請參閱 $ruleConfig

public array $rules = []
$user 公共屬性

表示驗證狀態的使用者物件,或是使用者應用程式元件的 ID。從 2.0.2 版本開始,這也可以是用於建立物件的組態陣列。從 2.0.12 版本開始,您可以將其設定為 false 以明確關閉此篩選器的元件支援。

方法詳細資訊

隱藏繼承的方法

__call() 公共方法

定義於: yii\base\BaseObject::__call()

呼叫未定義為類別方法的具名方法。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當調用未知方法時,它會被隱式呼叫。

public mixed __call ( $name, $params )
$name string

方法名稱

$params array

方法參數

return mixed

方法傳回值

throws yii\base\UnknownMethodException

當呼叫未知方法時

                public function __call($name, $params)
{
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__construct() 公共方法

定義於: yii\base\BaseObject::__construct()

建構子。

預設實作執行兩件事

  • 使用給定的組態 $config 初始化物件。
  • 呼叫 init()

如果子類別中覆寫了此方法,建議

  • 建構子的最後一個參數是組態陣列,例如此處的 $config
  • 在建構子的末尾呼叫父類別實作。
public void __construct ( $config = [] )
$config array

將用於初始化物件屬性的名稱-值對

                public function __construct($config = [])
{
    if (!empty($config)) {
        Yii::configure($this, $config);
    }
    $this->init();
}

            
__get() 公共方法

定義於: yii\base\BaseObject::__get()

傳回物件屬性的值。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $value = $object->property; 時,它會被隱式呼叫。

另請參閱 __set()

public mixed __get ( $name )
$name string

屬性名稱

return mixed

屬性值

throws yii\base\UnknownPropertyException

如果未定義屬性

throws yii\base\InvalidCallException

如果屬性是唯寫的

                public function __get($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter();
    } elseif (method_exists($this, 'set' . $name)) {
        throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
    }
    throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}

            
__isset() 公共方法

定義於: yii\base\BaseObject::__isset()

檢查屬性是否已設定,即已定義且非 null。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 isset($object->property) 時,它會被隱式呼叫。

請注意,如果未定義屬性,將傳回 false。

另請參閱 https://php.dev.org.tw/manual/en/function.isset.php

public boolean __isset ( $name )
$name string

屬性名稱或事件名稱

return boolean

具名屬性是否已設定(非 null)。

                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    return false;
}

            
__set() 公共方法

定義於: yii\base\BaseObject::__set()

設定物件屬性的值。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $object->property = $value; 時,它會被隱式呼叫。

另請參閱 __get()

public void __set ( $name, $value )
$name string

屬性名稱或事件名稱

$value mixed

屬性值

throws yii\base\UnknownPropertyException

如果未定義屬性

throws yii\base\InvalidCallException

如果屬性是唯讀的

                public function __set($name, $value)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter($value);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
    } else {
        throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
    }
}

            
__unset() 公共方法

定義於: yii\base\BaseObject::__unset()

將物件屬性設定為 null。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 unset($object->property) 時,它會被隱式呼叫。

請注意,如果未定義屬性,此方法將不執行任何操作。如果屬性是唯讀的,它將拋出例外。

另請參閱 https://php.dev.org.tw/manual/en/function.unset.php

public void __unset ( $name )
$name string

屬性名稱

throws yii\base\InvalidCallException

如果屬性是唯讀的。

                public function __unset($name)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter(null);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
    }
}

            
afterAction() 公共方法

定義於: yii\base\ActionFilter::afterAction()

此方法在動作執行後立即調用。

您可以覆寫此方法,為動作執行一些後處理。

public mixed afterAction ( $action, $result )
$action yii\base\Action

剛執行的動作。

$result mixed

動作執行結果

return mixed

已處理的動作結果。

                public function afterAction($action, $result)
{
    return $result;
}

            
afterFilter() 公共方法
public void afterFilter ( $event )
$event yii\base\ActionEvent

                public function afterFilter($event)
{
    $event->result = $this->afterAction($event->action, $event->result);
    $this->owner->off(Controller::EVENT_AFTER_ACTION, [$this, 'afterFilter']);
}

            
attach() 公共方法

定義於: yii\base\ActionFilter::attach()

將行為物件附加到元件。

預設實作將設定 $owner 屬性,並附加 events() 中宣告的事件處理器。如果您覆寫此方法,請確保呼叫父類別實作。

public void attach ( $owner )
$owner yii\base\Component

此行為要附加到的元件。

                public function attach($owner)
{
    $this->owner = $owner;
    $owner->on(Controller::EVENT_BEFORE_ACTION, [$this, 'beforeFilter']);
}

            
beforeAction() 公共方法

此方法在動作即將執行之前調用(在所有可能的篩選器之後)。您可以覆寫此方法,為動作進行最後一刻的準備。

public boolean beforeAction ( $action )
$action yii\base\Action

要執行的動作。

return boolean

動作是否應繼續執行。

                public function beforeAction($action)
{
    $user = $this->user;
    $request = Yii::$app->getRequest();
    /* @var $rule AccessRule */
    foreach ($this->rules as $rule) {
        if ($allow = $rule->allows($action, $user, $request)) {
            return true;
        } elseif ($allow === false) {
            if (isset($rule->denyCallback)) {
                call_user_func($rule->denyCallback, $rule, $action);
            } elseif ($this->denyCallback !== null) {
                call_user_func($this->denyCallback, $rule, $action);
            } else {
                $this->denyAccess($user);
            }
            return false;
        }
    }
    if ($this->denyCallback !== null) {
        call_user_func($this->denyCallback, null, $action);
    } else {
        $this->denyAccess($user);
    }
    return false;
}

            
beforeFilter() 公共方法
public void beforeFilter ( $event )
$event yii\base\ActionEvent

                public function beforeFilter($event)
{
    if (!$this->isActive($event->action)) {
        return;
    }
    $event->isValid = $this->beforeAction($event->action);
    if ($event->isValid) {
        // call afterFilter only if beforeFilter succeeds
        // beforeFilter and afterFilter should be properly nested
        $this->owner->on(Controller::EVENT_AFTER_ACTION, [$this, 'afterFilter'], null, false);
    } else {
        $event->handled = true;
    }
}

            
canGetProperty() 公共方法

定義於: yii\base\BaseObject::canGetProperty()

傳回一個值,指示是否可以讀取屬性。

如果屬性可讀取,則

  • 類別具有與指定名稱關聯的 getter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);

另請參閱 canSetProperty()

public boolean canGetProperty ( $name, $checkVars true )
$name string

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

屬性是否可以讀取

                public function canGetProperty($name, $checkVars = true)
{
    return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}

            
canSetProperty() 公共方法

定義於: yii\base\BaseObject::canSetProperty()

傳回一個值,指示是否可以設定屬性。

如果屬性可寫入,則

  • 類別具有與指定名稱關聯的 setter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);

另請參閱 canGetProperty()

public boolean canSetProperty ( $name, $checkVars true )
$name string

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

屬性是否可以寫入

                public function canSetProperty($name, $checkVars = true)
{
    return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}

            
className() 公共靜態方法
自 2.0.14 版本起已棄用。在 PHP >=5.5 上,請改用 ::class

定義於: yii\base\BaseObject::className()

傳回此類別的完整限定名稱。

public static string className ( )
return string

此類別的完整限定名稱。

                public static function className()
{
    return get_called_class();
}

            
denyAccess() 受保護的方法

拒絕使用者存取。

如果使用者是訪客,預設實作會將使用者重新導向到登入頁面;如果使用者已登入,則會拋出 403 HTTP 例外。

protected void denyAccess ( $user )
$user yii\web\User|false

當前使用者或布林值 false(如果 User 元件已卸載)

throws yii\web\ForbiddenHttpException

如果使用者已登入或在 User 元件已卸載的情況下。

                protected function denyAccess($user)
{
    if ($user !== false && $user->getIsGuest()) {
        $user->loginRequired();
    } else {
        throw new ForbiddenHttpException(Yii::t('yii', 'You are not allowed to perform this action.'));
    }
}

            
detach() 公共方法

定義於: yii\base\ActionFilter::detach()

從元件卸載行為物件。

預設實作將取消設定 $owner 屬性,並卸載 events() 中宣告的事件處理器。如果您覆寫此方法,請確保呼叫父類別實作。

public void detach ( )

                public function detach()
{
    if ($this->owner) {
        $this->owner->off(Controller::EVENT_BEFORE_ACTION, [$this, 'beforeFilter']);
        $this->owner->off(Controller::EVENT_AFTER_ACTION, [$this, 'afterFilter']);
        $this->owner = null;
    }
}

            
events() 公共方法

定義於: yii\base\Behavior::events()

$owner 的事件宣告事件處理器。

子類別可以覆寫此方法,以宣告應附加到 $owner 元件事件的 PHP 回呼。

當行為附加到擁有者時,回呼將附加到 $owner 的事件;當行為從元件卸載時,它們將從事件中卸載。

回呼可以是以下任何一種

  • 此行為中的方法:'handleClick',等效於 [$this, 'handleClick']
  • 物件方法:[$object, 'handleClick']
  • 靜態方法:['Page', 'handleClick']
  • 匿名函式:function ($event) { ... }

以下是一個範例

[
    Model::EVENT_BEFORE_VALIDATE => 'myBeforeValidate',
    Model::EVENT_AFTER_VALIDATE => 'myAfterValidate',
]
public array events ( )
return array

事件(陣列鍵)和對應的事件處理器方法(陣列值)。

                public function events()
{
    return [];
}

            
getActionId() 受保護的方法 (自 2.0.7 版本起可用)

定義於: yii\base\ActionFilter::getActionId()

透過將 yii\base\Action::$uniqueId 轉換為相對於模組的 ID,傳回動作 ID。

protected string getActionId ( $action )
$action yii\base\Action

                protected function getActionId($action)
{
    if ($this->owner instanceof Module) {
        $mid = $this->owner->getUniqueId();
        $id = $action->getUniqueId();
        if ($mid !== '' && strpos($id, $mid) === 0) {
            $id = substr($id, strlen($mid) + 1);
        }
    } else {
        $id = $action->id;
    }
    return $id;
}

            
hasMethod() 公有方法

定義於: yii\base\BaseObject::hasMethod()

傳回一個值,指示是否已定義方法。

預設實作是呼叫 PHP 函式 method_exists()。當您實作 PHP 魔術方法 __call() 時,您可以覆寫此方法。

public boolean hasMethod ( $name )
$name string

方法名稱

return boolean

方法是否已定義

                public function hasMethod($name)
{
    return method_exists($this, $name);
}

            
hasProperty() 公有方法

定義於: yii\base\BaseObject::hasProperty()

傳回一個值,指示是否已定義屬性。

屬性在以下情況被定義:

  • 類別具有與指定名稱相關聯的 getter 或 setter 方法(在此情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);

另請參閱

public boolean hasProperty ( $name, $checkVars true )
$name string

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

return boolean

屬性是否已定義

                public function hasProperty($name, $checkVars = true)
{
    return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}

            
init() 公有方法

透過從組態實例化規則物件來初始化 $rules 陣列。

public void init ( )

                public function init()
{
    parent::init();
    if ($this->user !== false) {
        $this->user = Instance::ensure($this->user, User::className());
    }
    foreach ($this->rules as $i => $rule) {
        if (is_array($rule)) {
            $this->rules[$i] = Yii::createObject(array_merge($this->ruleConfig, $rule));
        }
    }
}

            
isActive() 保護方法

定義於: yii\base\ActionFilter::isActive()

傳回一個值,指示篩選器是否對給定的動作處於活動狀態。

protected boolean isActive ( $action )
$action yii\base\Action

正在篩選的動作

return boolean

篩選器對於給定動作是否為啟用狀態。

                protected function isActive($action)
{
    $id = $this->getActionId($action);
    if (empty($this->only)) {
        $onlyMatch = true;
    } else {
        $onlyMatch = false;
        foreach ($this->only as $pattern) {
            if (StringHelper::matchWildcard($pattern, $id)) {
                $onlyMatch = true;
                break;
            }
        }
    }
    $exceptMatch = false;
    foreach ($this->except as $pattern) {
        if (StringHelper::matchWildcard($pattern, $id)) {
            $exceptMatch = true;
            break;
        }
    }
    return !$exceptMatch && $onlyMatch;
}