02.套件設定 - t301000/laravel-ntpc-openid GitHub Wiki

設定檔位於 config/ntpcopenid.php

設定要取回的資料

設定 required

return [
    ....略
    'required' => [
        'namePerson/friendly',      //暱稱
        'contact/email',            //公務信箱
        'namePerson',               //姓名
        'birthDate',                //出生年月日
        'person/gender',            //性別
        'contact/postalCode/home',  //識別碼
        'contact/country/home',     //單位(學校名),如:xx國中
        'pref/language',            //年級班級座號 6 碼
        'pref/timezone'             // 授權資訊[學校別、身分別、職稱別、職務別]
    ],
    ....略
];

只要將 不要 的資料刪掉或註解起來即可

設定允許登入之規則

設定 canLoginRules

return [
    ....略
    'canLoginRules' => [
        // 規則定義在這....
    ],
    ....略
];

每條規則均為陣列,未設定規則代表不設限。

return [
    ....略
    'canLoginRules' => [], // 不設限
    ....略
];
return [
    ....略
    'canLoginRules' => [
        ['unitCode' => '014569'], // 單位代碼為 014569 的所有人可登入
        ['group' => '資訊組長'], // 資訊組長可登入
    ],
    ....略
];
return [
    ....略
    'canLoginRules' => [
        ['role' => ['教師', '學生']] // 教師與學生可登入
    ],
    ....略
];

可用欄位:

  • unitCode:單位代碼
  • role:身份
  • title:職務
  • group:職稱
  • openID:OpedID 帳號

除 unitCode 為字串之外,其餘可為字串(單一值)或陣列。

規則設定範例:

單位代碼為 014569

['unitCode' => '014569'],

單位代碼為 014569 且 身份為 教師

['unitCode' => '014569', 'role' => '教師'],

單位代碼為 014569 且 身份為 教師或學生

['unitCode' => '014569', 'role' => ['教師', '學生']],

身份為 教師

['role' => '教師'],

單位代碼為 014569 且 職務為 主任或組長

['unitCode' => '014569', 'title' => ['主任', '組長']],

職稱為 資訊組長

['group' => '資訊組長'],

OpenID 帳號為 somebody

['openID' => ['somebody']],

比對時是從第一個開始,一旦符合某條規則即允許登入,之後的規則就不再比對。

設定路由前綴

設定 prefix

return [
    ....略
    'prefix' => 'ntpcopenid',
    ....略
];

設定暫存 OpenID 取回之資料之 session key

設定 sessionKey

return [
    ....略
    'sessionKey' => 'ntpcopenid',
    ....略
];

設定重導向路徑

設定 redirectToUrls,預設均為網站根目錄

return [
    ....略
    'redirectToUrls' => [
        'user_cancel' => '/', // user 取消認證 時導向至哪裡
        'validate_fail' => '/', // user 驗證未過 時導向至哪裡
        'login_allow' => '/', // user 登入後 導向至哪裡
        'login_deny' => '/', // user 被拒絕登入 時導向至哪裡
        'other' => '/', // 其他狀況未經正常程序,如直接輸入網址瀏覽,導向至哪裡
    ]
    ....略
];

開發時略過 OpenID 流程,使用假資料進行測試

設定 skipRealOpenID 為 true, fakeUserIndex 設為 fakeUsers 陣列中某筆假資料的索引值, fakeUsers 之內容可自行增刪修改

return [
    ....略
    
    /* 以下只有開發時測試用 */
    'skipRealOpenID' => false, // 略過真正的 OpenID 流程,測試時使用
    'fakeUserIndex' => 0, // 使用哪一筆假資料,index 由 0 開始
    'fakeUsers' => [ // 測試用假資料,每筆假資料為陣列,可自行增加
        // index = 0
        [
            "namePerson/friendly" => "林小明",
            "contact/email" => "[email protected]",
            "namePerson" => "林小明",
            "birthDate" => "2000-08-19",
            "person/gender" => "M",
            "contact/postalCode/home" => "5EE2EFC0E0722558C2B47AA5461F60FE69F811651068678F6F7F264BAF1234DA",
            "contact/country/home" => "育林國中",
            "pref/language" => "000000",
            "pref/timezone" => [
                [
                    "id" => "014569",
                    "name" => "新北市立育林國民中學",
                    "role" => "家長",
                    "title" => "其他",
                    "groups" => ["其他"]
                ],
                [
                    "id" => "014569",
                    "name" => "新北市立育林國民中學",
                    "role" => "教師",
                    "title" => "教師兼組長",
                    "groups" => ["資訊組長"]
                ],
            ],
            "openid" => "t001"
        ],
        // index = 1
        [
            "namePerson/friendly" => "黃小保",
            "contact/email" => "[email protected]",
            "namePerson" => "黃小保",
            "birthDate" => "2003-10-09",
            "person/gender" => "M",
            "contact/postalCode/home" => "5FF2GGC0E0722558C2B47GG5461F60FE69F811651068678F6F7F264BAF5678GG",
            "contact/country/home" => "育林國中",
            "pref/language" => "070215",
            "pref/timezone" => [
                [
                    "id" => "014569",
                    "name" => "新北市立育林國民中學",
                    "role" => "學生",
                    "title" => "其他",
                    "groups" => ["其他"]
                ],
            ],
            "openid" => "s001"
        ],
        // index = 2
        [
            "namePerson/friendly" => "許小誠",
            "contact/email" => "[email protected]",
            "namePerson" => "許小誠",
            "birthDate" => "2003-12-09",
            "person/gender" => "M",
            "contact/postalCode/home" => "5FF2GGC0E0722558C2B47GG5461F60GG69F811651012345F6F7F264BAF5678GG",
            "contact/country/home" => "育林國中",
            "pref/language" => "081005",
            "pref/timezone" => [
                [
                    "id" => "014569",
                    "name" => "新北市立育林國民中學",
                    "role" => "學生",
                    "title" => "其他",
                    "groups" => ["其他"]
                ],
            ],
            "openid" => "s002"
        ],
    ],
];