Parcourir la source

認可にカスタム要素を追加

develop
sosuke.iwabuchi il y a 2 ans
Parent
révision
a6410d708a
4 fichiers modifiés avec 67 ajouts et 173 suppressions
  1. +2
    -158
      src/api/auth.ts
  2. +39
    -4
      src/contexts/AuthContext.tsx
  3. +1
    -1
      src/layouts/dashbord/navigator.tsx
  4. +25
    -10
      src/routes/auth.ts

+ 2
- 158
src/api/auth.ts Voir le fichier

@@ -9,7 +9,8 @@ type MeResponse = {
contract_id: string;
role: UserRole;
name: string;
custom: CustomCode[];
custom?: CustomCode[];
contract_name?: string;
};
} & APICommonResponse;

@@ -44,160 +45,3 @@ export const logout = async () => {
});
return res;
};
// export const getMe = async () => {
// const res = await request<MeResponse>({
// url: getUrl(ApiId.ME),
// method: HttpMethod.GET,
// });
// return res;
// };

// export const login = async (email: string, password: string) => {
// const data = new URLSearchParams({
// email,
// password,
// });
// const res = await request<MeResponse>({
// url: getUrl(ApiId.NORMAL_LOGIN),
// method: HttpMethod.POST,
// data,
// });
// return res;
// };

// export const loginAdmin = async (email: string, password: string) => {
// const data = new URLSearchParams({
// email,
// password,
// });
// const res = await request<MeResponse>({
// url: getUrl(ApiId.ADMIN_LOGIN),
// method: HttpMethod.POST,
// data,
// });
// return res;
// };

// export const logout = async () => {
// const res = await request({
// url: getUrl(ApiId.LOGOUT),
// method: HttpMethod.POST,
// });
// return res;
// };

// export type StartEmailVerifyParams = {
// email: string;
// for_entry?: boolean;
// customer_code?: string;
// parking_management_code?: string;
// };

// export const startEmailVerify = async (data: StartEmailVerifyParams) => {
// const sendData = new URLSearchParams(makeParam(data));
// const res = await request({
// url: getUrl(ApiId.EMAIL_VERIFY_START),
// method: HttpMethod.POST,
// data: sendData,
// });
// return res;
// };

// export const verifyEmail = async ({ token }: { token: string }) => {
// const sendData = new URLSearchParams(makeParam({ token }));
// const res = await request<EmailVerifyResponse>({
// url: getUrl(ApiId.EMAIL_VERIFY),
// method: HttpMethod.POST,
// data: sendData,
// });
// return res;
// };

// // パスワードリセット開始
// export type ResetPasswordStartParams = {
// email: string;
// customer_code?: string;
// parking_management_code?: string;
// };
// export const resetPasswordStart = async (data: ResetPasswordStartParams) => {
// const sendData = new URLSearchParams(makeParam(data));
// const res = await request({
// url: getUrl(ApiId.RESET_PASSWORD_START),
// method: HttpMethod.POST,
// data: sendData,
// });
// return res;
// };

// // パスワードリセットトークンチェック
// export type ResetPasswordVerifyParams = {
// token: string;
// };
// export const resetPasswordVerify = async (data: ResetPasswordVerifyParams) => {
// const sendData = new URLSearchParams(makeParam(data));
// const res = await request({
// url: getUrl(ApiId.RESET_PASSWORD_VERIFY),
// method: HttpMethod.POST,
// data: sendData,
// });
// return res;
// };

// // パスワードリセット
// export type ResetPasswordParams = {
// password: string;
// token: string;
// };
// export const resetPassword = async (data: ResetPasswordParams) => {
// const sendData = new URLSearchParams(makeParam(data));
// const res = await request({
// url: getUrl(ApiId.RESET_PASSWORD),
// method: HttpMethod.POST,
// data: sendData,
// });
// return res;
// };

// // 利用者登録
// export const RegisterUserParamKeyName = {
// TOKEN: 'token',
// PASSWORD: 'password',
// FIRST_NAME: 'first_name',
// LAST_NAME: 'last_name',
// FIRST_NAME_KANA: 'first_name_kana',
// LAST_NAME_KANA: 'last_name_kana',
// ZIP_CODE: 'zip_code',
// PREF_CODE: 'pref_code',
// ADDRESS1: 'address1',
// ADDRESS2: 'address2',
// ADDRESS3: 'address3',
// PHONE_NUMBER: 'phone_number',
// CONFIRM_PRIVACY_POLICY: 'confirm_privacy_policy',
// } as const;
// export type RegisterUserParamKeyName =
// typeof RegisterUserParamKeyName[keyof typeof RegisterUserParamKeyName];

// export type RegisterUserParam = {
// [RegisterUserParamKeyName.TOKEN]: string;
// [RegisterUserParamKeyName.PASSWORD]: string;
// [RegisterUserParamKeyName.FIRST_NAME]: string;
// [RegisterUserParamKeyName.LAST_NAME]: string;
// [RegisterUserParamKeyName.FIRST_NAME_KANA]: string;
// [RegisterUserParamKeyName.LAST_NAME_KANA]: string;
// [RegisterUserParamKeyName.ZIP_CODE]: string;
// [RegisterUserParamKeyName.PREF_CODE]: string;
// [RegisterUserParamKeyName.ADDRESS1]: string;
// [RegisterUserParamKeyName.ADDRESS2]: string;
// [RegisterUserParamKeyName.ADDRESS3]: string;
// [RegisterUserParamKeyName.PHONE_NUMBER]: string;
// [RegisterUserParamKeyName.CONFIRM_PRIVACY_POLICY]: boolean;
// };
// export const registerUser = async (data: RegisterUserParam) => {
// const sendData = new URLSearchParams(makeParam(data));
// const res = await request({
// url: getUrl(ApiId.REGISTER_USER),
// method: HttpMethod.POST,
// data: sendData,
// });
// return res;
// };

+ 39
- 4
src/contexts/AuthContext.tsx Voir le fichier

@@ -71,7 +71,7 @@ function AuthContextProvider({ children }: Props) {
setContractId(res.data.contract_id);
setRole(res.data.role);
setName(res.data.name);
setCustom(res.data.custom);
setCustom(res.data.custom ?? []);
setInitialized(true);
},
onFailed: () => {
@@ -85,6 +85,9 @@ function AuthContextProvider({ children }: Props) {
onSuccess: (res) => {
setContractId(res.data.contract_id);
setRole(res.data.role);
setName(res.data.name);
setCustom(res.data.custom ?? []);
setInitialized(true);
},
});

@@ -98,6 +101,8 @@ function AuthContextProvider({ children }: Props) {
const clear = () => {
setRole(UserRole.NONE);
setContractId(null);
setName("");
setCustom([]);
};

const login = async (email: string, password: string) => {
@@ -125,10 +130,40 @@ function AuthContextProvider({ children }: Props) {

const canAccess = useCallback(
(pageId: PageID): boolean => {
const roles = AUTH[pageId] ?? [];
return roles.includes(role);
const authorization = AUTH[pageId];

// デバッグ用
// console.log(
// "RET",
// pageId,
// role,
// custom,
// !!authorization &&
// authorization.role.includes(role) &&
// (authorization.custom.length === 0 ||
// !!custom.find((c) => {
// return authorization.custom.includes(c);
// })),
// [
// !!authorization,
// authorization.role.includes(role),
// authorization.custom.length === 0,
// !!custom.find((c) => {
// return authorization.custom.includes(c);
// }),
// ]
// );

return (
!!authorization &&
authorization.role.includes(role) &&
(authorization.custom.length === 0 ||
!!custom.find((c) => {
return authorization.custom.includes(c);
}))
);
},
[role]
[initialized, role, custom]
);

useEffect(() => {


+ 1
- 1
src/layouts/dashbord/navigator.tsx Voir le fichier

@@ -224,7 +224,7 @@ function useContents(children: Child[]) {
</ListItemButton>
);
});
}, [pageId, initialized]);
}, [pageId, initialized, canAccess]);

return {
elements,


+ 25
- 10
src/routes/auth.ts Voir le fichier

@@ -1,6 +1,11 @@
import { CustomCode as C } from "codes/custom";
import { PageID as P } from "codes/page";
import { UserRole as R } from "codes/user";

type AuthConfiguration = {
role: R[];
custom: C[];
};
export const AUTH = {
[P.NONE]: setAuth("all"),
[P.LOGIN]: setAuth("all"),
@@ -13,15 +18,18 @@ export const AUTH = {

[P.DASHBOARD_RECEIPT_ISSUING_ORDER_CREATE_CUSTOM_HELLO_TECHNO]: setAuth(
"ge",
R.NORMAL_ADMIN
R.NORMAL_ADMIN,
[C.HELLO_TECHNO]
),
[P.DASHBOARD_RECEIPT_ISSUING_ORDER_LIST_CUSTOM_HELLO_TECHNO]: setAuth(
"ge",
R.NORMAL_ADMIN
R.NORMAL_ADMIN,
[C.HELLO_TECHNO]
),
[P.DASHBOARD_RECEIPT_ISSUING_ORDER_DETAIL_CUSTOM_HELLO_TECHNO]: setAuth(
"ge",
R.NORMAL_ADMIN
R.NORMAL_ADMIN,
[C.HELLO_TECHNO]
),

[P.PAGE_403]: setAuth("all"),
@@ -30,14 +38,18 @@ export const AUTH = {

type Target = "ge" | "le" | "eq" | "all";
type UserRoleKey = keyof typeof R;
function setAuth(target: Target, targetRole?: R): R[] {
const ret: R[] = [];
function setAuth(
target: Target,
targetRole?: R,
custom: C[] = []
): AuthConfiguration {
const roles: R[] = [];

for (const key in R) {
const role = R[key as UserRoleKey];

if (target === "all") {
ret.push(role);
roles.push(role);
continue;
}

@@ -46,18 +58,21 @@ function setAuth(target: Target, targetRole?: R): R[] {
}

if (target === "ge" && role >= targetRole) {
ret.push(role);
roles.push(role);
continue;
}
if (target === "le" && role <= targetRole) {
ret.push(role);
roles.push(role);
continue;
}
if (target === "eq" && role === targetRole) {
ret.push(role);
roles.push(role);
continue;
}
}

return ret;
return {
role: roles,
custom: custom,
};
}

Chargement…
Annuler
Enregistrer