Skip to content

Commit d52d108

Browse files
feat: [UIE-8129] - add new user permissions api with prefix (#11146)
1 parent 9209bc2 commit d52d108

File tree

9 files changed

+136
-0
lines changed

9 files changed

+136
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@linode/api-v4": Added
3+
---
4+
5+
New IAM endpoints and types ([#11146](https://github.com/linode/manager/pull/11146))

packages/api-v4/src/iam/iam.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { BETA_API_ROOT } from '../constants';
2+
import Request, { setData, setMethod, setURL } from '../request';
3+
import { IamUserPermissions } from './types';
4+
5+
/**
6+
* getUserPermissions
7+
*
8+
* Returns the full permissions structure for this User. This includes all entities on
9+
* the Account alongside what level of access this User has to each of them.
10+
*
11+
* @param username { number } the username to look up.
12+
*
13+
*/
14+
export const getUserPermissions = (username: string) =>
15+
Request<IamUserPermissions>(
16+
setURL(
17+
`${BETA_API_ROOT}/iam/role-permissions/users/${encodeURIComponent(
18+
username
19+
)}`
20+
),
21+
setMethod('GET')
22+
);
23+
/**
24+
* updateUserPermissions
25+
*
26+
* Update the permissions a User has.
27+
*
28+
* @param username { number } ID of the client to be viewed.
29+
* @param data { object } the Permissions object to update.
30+
*
31+
*/
32+
export const updateUserPermissions = (
33+
username: string,
34+
data: Partial<IamUserPermissions>
35+
) =>
36+
Request<IamUserPermissions>(
37+
setURL(
38+
`${BETA_API_ROOT}/iam/role-permissions/users/${encodeURIComponent(
39+
username
40+
)}`
41+
),
42+
setMethod('PUT'),
43+
setData(data)
44+
);

packages/api-v4/src/iam/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './types';
2+
3+
export * from './iam';

packages/api-v4/src/iam/types.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export interface IamUserPermissions {
2+
account_access: AccountAccessType[];
3+
resource_access: ResourceAccess[];
4+
}
5+
6+
type ResourceType =
7+
| 'linode'
8+
| 'firewall'
9+
| 'nodebalancer'
10+
| 'longview'
11+
| 'domain'
12+
| 'stackscript'
13+
| 'image'
14+
| 'volume'
15+
| 'database'
16+
| 'account'
17+
| 'vpc';
18+
19+
type AccountAccessType =
20+
| 'account_linode_admin'
21+
| 'linode_creator'
22+
| 'firewall_creator';
23+
24+
type RoleType = 'linode_contributor' | 'firewall_admin';
25+
26+
export interface ResourceAccess {
27+
resource_id: number;
28+
resource_type: ResourceType;
29+
roles: RoleType[];
30+
}

packages/api-v4/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ export * from './vpcs';
5050

5151
export * from './betas';
5252

53+
export * from './iam';
54+
5355
export {
5456
baseRequest,
5557
setToken,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@linode/manager": Added
3+
---
4+
5+
mock data and query for new permission api ([#11146](https://github.com/linode/manager/pull/11146))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { IamUserPermissions } from '@linode/api-v4';
2+
import Factory from 'src/factories/factoryProxy';
3+
4+
export const userPermissionsFactory = Factory.Sync.makeFactory<IamUserPermissions>(
5+
{
6+
account_access: [
7+
'account_linode_admin',
8+
'linode_creator',
9+
'firewall_creator',
10+
],
11+
resource_access: [
12+
{
13+
resource_id: 12345678,
14+
resource_type: 'linode',
15+
roles: ['linode_contributor'],
16+
},
17+
{
18+
resource_id: 45678901,
19+
resource_type: 'firewall',
20+
roles: ['firewall_admin'],
21+
},
22+
],
23+
}
24+
);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { APIError, IamUserPermissions } from '@linode/api-v4';
2+
import { iamQueries } from './queries';
3+
import { useQuery } from '@tanstack/react-query';
4+
5+
export const useAccountUserPermissions = (username: string) => {
6+
return useQuery<IamUserPermissions, APIError[]>(
7+
iamQueries.user(username)._ctx.permissions
8+
);
9+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { getUserPermissions } from '@linode/api-v4';
2+
import { createQueryKeys } from '@lukemorales/query-key-factory';
3+
4+
export const iamQueries = createQueryKeys('iam', {
5+
user: (username: string) => ({
6+
contextQueries: {
7+
permissions: {
8+
queryFn: () => getUserPermissions(username),
9+
queryKey: null,
10+
},
11+
},
12+
queryKey: [username],
13+
}),
14+
});

0 commit comments

Comments
 (0)