<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## 權限插件 ESD 默認已經安裝,無需手動安裝,該插件需要依賴redis,使用前請先配置redis連接。 ## 插件配置 該插件無需配置 ## Security 初始化 權限插件提供了,基于角色和權限的鑒權方式。 ~~~ use ESD\Plugins\Security\Beans\Principal; ~~~ 以下代碼創建了一個角色為`guest`,權限 `read` 的配置。最后調用 `setPrincipal` 保存配置對象。如果使用權限的類沒有繼承 `GoController`,需要`use trait GetSecurity`,來加入 `getPrincipal,setPrincipal` 方法。您也可可以根據項目選擇僅用`Role`,或僅用`Permissions`。 ~~~ $principal = new Principal(); $principal->addRole("guest"); $principal->addPermissions('read'); $principal->setUsername("guest"); $this->setPrincipal($principal); ~~~ 一般情況下,我們應該在用戶登錄設置完 uid 后再執行如下代碼,如。 ~~~ if($this->session->isAvailable()){ return $this->session->getId(); }else{ $this->session->create(); $this->session->setAttribute('uid',1); $principal = new Principal(); $principal->addRole("guest"); $principal->addPermissions('read'); $principal->setUsername("guest"); $this->setPrincipal($principal); return 'set guest'; } ~~~ ## 鑒權 鑒權部分可以使用注解的形式對需要鑒權的方法進行聲明,同樣的您也可以直接在需要鑒權的位置直接調用鑒權函數,下面具體說明。 ## @PreAuthorize 該注解將會先進行鑒權,如果鑒權返回 true,才會執行該方法。 PreAuthorize 輸入的是一個表達式如果最終返回 true,就會執行該方法,默認提供了幾種。 通過 $p可以獲取到入參數組 ### hasRole >[info]是否擁有某個角色 ~~~ /** * @PreAuthorize("hasRole('guest')") */ public function http_has_guest(){ return 'has role guest'; } ~~~ ### hasAnyRole >[info]是否擁有其中一個角色 ~~~ /** * @PreAuthorize("hasAnyRole(['guest','member'])") */ public function http_has_any(){ return 'has guest member'; } ~~~ ### hasPermission >[info]是否擁有某個權限 ~~~ /** * @PreAuthorize("hasPermission('read')") * @return string */ public function http_has_read(){ $auth = $this->getPrincipal(); $username = $auth->getPermissions(); return $username; } ~~~ ### isAuthenticated >[danger]是否已經登錄(注意是設置過 Principal,如需使用該方法,請自行保證登錄后再設置角色。未登錄就設置 Role 同樣會認為已授權) ~~~ /** * @PreAuthorize("isAuthenticated()") */ public function http_hello(){ $role = $this->getPrincipal(); return 'hello :'.$role->getUsername(); } ~~~ ### hasIpAddress >[info]是否符合某個IP地址規則 ~~~ /** * @PreAuthorize("hasIpAddress('10.0.0.0/16')") */ public function http_access(){ $ip = $this->request->getServer(Request::SERVER_REMOTE_ADDR); return 'hello ' . $ip; } ~~~ ## @PostAuthorize >[info]后置鑒權,通過返回值判斷是否應該含有權限,通過$p獲取入參,通過$returnObject獲取返回值 ~~~ /** * @PostAuthorize("$returnObject->group == 1") */ ~~~ ## 給整個控制器增加授權 >[info]需要在類的 initialization 方法上加入注解。 ~~~ /** * @PreAuthorize("hasRole('guest')") * @param string|null $controllerName * @param string|null $methodName * @return mixed|void * @throws AccessDeniedException */ public function initialization(?string $controllerName, ?string $methodName) { parent::initialization($controllerName, $methodName); $this->response->addHeader("Content-type", "text/html;charset=UTF-8"); } ~~~ ## 非注解使用 >[info]以上方法也可直接使用。如 ~~~ public function http_role(){ if(!hasRole('guest')){ return 'err'; } return 'succ'; } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看