PHP code example of ghost-cat / azoya-sso

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);
    }
}

    /**
     * sso filter
     *
     * @return array
     **/
    public function behaviors()
    {
        return [
            [
                'class' => SSOFilter::className(),
                'except' => ['sso/sync'],
            ],
        ];
    }



namespace app\controllers;

use Yii;
use AzoyaSso\Client as SSOClient;

class SsoController extends BaseController
{

    /**
     * 同步 sso token
     *
     * @return json
     **/
    public function actionSync()
    {
        try {
            $token = Yii::$app->request->get('sso_token');
            $to = Yii::$app->request->get('to');
            Yii::$app->session->set('sso_token', $token);

            $to = !empty($to) ? base64_decode($to) : '/site/index';
            
            return $this->redirect($to);
        } catch (\Exception $e) {

            return $this->returnJson($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 退出登录
     *
     * @return redirect
     **/
    public function actionLogout()
    {
        SSOClient::logout();

        return $this->redirect(SSOClient::loginUrl());
    }

    /**
     * 选择语言
     *
     * @return json
     **/
    public function actionSetLanguage()
    {
        try {
            $language = Yii::$app->request->post('language');
            SSOClient::setLanguage($language);

            return $this->returnJson(200, '设置成功');
        } catch (\Exception $e) {

            return $this->returnJson($e->getCode(), $e->getMessage());
        }
    }
}