-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathauth-user-api.ts
103 lines (97 loc) · 3.53 KB
/
auth-user-api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { AuthUser } from "src/domain/auth-user/auth-user-types";
import api, { API_PATHS } from "src/services/api";
import { KlawApiResponse } from "types/utils";
// user roles are added dynamically by admins, we can't make them enums
// but "SUPERADMIN" is the default role set by us. It's highly unlikely
// that users would change that string, so we can use it to identify
// the superadmin role, which has different view-access
const SUPERADMIN_USERROLE = "SUPERADMIN";
type SuperAdminUser = AuthUser & { userrole: typeof SUPERADMIN_USERROLE };
function isSuperAdmin(user: AuthUser): user is SuperAdminUser {
return user.userrole === SUPERADMIN_USERROLE;
}
function transformAuthResponse(response: KlawApiResponse<"getAuth">): AuthUser {
const {
username,
teamname,
teamId,
canSwitchTeams,
userrole,
myteamtopics,
myOrgTopics,
canShutdownKw,
canUpdatePermissions,
addEditRoles,
viewTopics,
requestItems,
viewKafkaConnect,
syncBackTopics,
syncBackSchemas,
syncBackAcls,
updateServerConfig,
showServerConfigEnvProperties,
addUser,
addTeams,
syncTopicsAcls,
syncConnectors,
manageConnectors,
syncSchemas,
approveAtleastOneRequest,
approveDeclineTopics,
approveDeclineOperationalReqs,
approveDeclineSubscriptions,
approveDeclineSchemas,
approveDeclineConnectors,
showAddDeleteTenants,
addDeleteEditClusters,
addDeleteEditEnvs,
} = response;
return {
username,
teamname,
teamId,
canSwitchTeams,
userrole,
totalTeamTopics: parseInt(myteamtopics),
totalOrgTopics: parseInt(myOrgTopics),
permissions: {
canShutdownKw: canShutdownKw === "Authorized",
canUpdatePermissions: canUpdatePermissions === "Authorized",
addEditRoles: addEditRoles === "Authorized",
viewTopics: viewTopics === "Authorized",
requestItems: requestItems === "Authorized",
viewKafkaConnect: viewKafkaConnect === "Authorized",
syncBackTopics: syncBackTopics === "Authorized",
syncBackSchemas: syncBackSchemas === "Authorized",
syncBackAcls: syncBackAcls === "Authorized",
updateServerConfig: updateServerConfig === "Authorized",
showServerConfigEnvProperties:
showServerConfigEnvProperties === "Authorized",
addUser: addUser === "Authorized",
addTeams: addTeams === "Authorized",
syncTopicsAcls: syncTopicsAcls === "Authorized",
syncConnectors: syncConnectors === "Authorized",
manageConnectors: manageConnectors === "Authorized",
syncSchemas: syncSchemas === "Authorized",
approveAtleastOneRequest: approveAtleastOneRequest === "Authorized",
approveDeclineTopics: approveDeclineTopics === "Authorized",
approveDeclineOperationalReqs:
approveDeclineOperationalReqs === "Authorized",
approveDeclineSubscriptions: approveDeclineSubscriptions === "Authorized",
approveDeclineSchemas: approveDeclineSchemas === "Authorized",
approveDeclineConnectors: approveDeclineConnectors === "Authorized",
showAddDeleteTenants: showAddDeleteTenants === "Authorized",
addDeleteEditClusters: addDeleteEditClusters === "Authorized",
addDeleteEditEnvs: addDeleteEditEnvs === "Authorized",
},
};
}
async function getAuth(): Promise<AuthUser> {
return api
.get<KlawApiResponse<"getAuth">>(API_PATHS.getAuth)
.then((response) => transformAuthResponse(response));
}
function logoutUser() {
return api.post<KlawApiResponse<"logout">, never>(API_PATHS.logout);
}
export { getAuth, logoutUser, isSuperAdmin };