1. Go to this page and download the library: Download ghost-cat/azoya-sso library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ghost-cat / azoya-sso example snippets
use AzoyaSso\Client as SSOClient;
// 获取用户ID/用户名称
$userId = SSOClient::userId();
$userName = SSOClient::userName();
SSOClient::hasToken();
$url = SSOClient::syncUrl();
$loginUrl = SSOClient::loginUrl();
$url = SSOClient::homeUrl();
SSOClient::hasAccess($route);
$menu = SSOClient::menu();
SSOClient::menuHasAccess($menuRoute);
SSOClient::siteHasAccess($siteId);
$sites = SSOClient::sites();
$users = SSOClient::users($userIds);
$user = SSOClient::userByName($name);
SSOClient::logout();
SSOClient::language();
SSOClient::setLanguage($language);
namespace app\components;
use Yii;
use yii\web\Response;
use yii\base\ActionFilter;
use AzoyaSso\Client as SSOClient;
class SSOFilter extends ActionFilter
{
public function beforeAction($action)
{
// 同步 sso_token
if (!SSOClient::hasToken()) {
return Yii::$app->controller->redirect(SSOClient::syncUrl());
}
// token 状态判断
$tokenStatus = SSOClient::tokenStatus();
if ($tokenStatus == SSOClient::SSO_TOKEN_INVALID) {
// token 无效,同步token
return Yii::$app->controller->redirect(SSOClient::syncUrl());
} elseif ($tokenStatus == SSOClient::SSO_TOKEN_NOTLOGIN) {
// token 未登录
return Yii::$app->controller->redirect(SSOClient::loginUrl());
}
if (!SSOClient::hasAccess('/' . Yii::$app->request->pathInfo)) {
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
Yii::$app->response->data = ['status' => 403, 'message' => '没有权限'];
return false;
}
// 设置语言
Yii::$app->language = SSOClient::language();
return parent::beforeAction($action);
}
public function afterAction($action, $result)
{
return parent::afterAction($action, $result);
}
}