Skip to content

Commit

Permalink
feat: [UIE-8129] - add new user permissions api with prefix (#11146)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaleksee-akamai authored Nov 14, 2024
1 parent 9209bc2 commit d52d108
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/api-v4/.changeset/pr-11146-added-1729681812578.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/api-v4": Added
---

New IAM endpoints and types ([#11146](https://github.com/linode/manager/pull/11146))
44 changes: 44 additions & 0 deletions packages/api-v4/src/iam/iam.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { BETA_API_ROOT } from '../constants';
import Request, { setData, setMethod, setURL } from '../request';
import { IamUserPermissions } from './types';

/**
* getUserPermissions
*
* Returns the full permissions structure for this User. This includes all entities on
* the Account alongside what level of access this User has to each of them.
*
* @param username { number } the username to look up.
*
*/
export const getUserPermissions = (username: string) =>
Request<IamUserPermissions>(
setURL(
`${BETA_API_ROOT}/iam/role-permissions/users/${encodeURIComponent(
username
)}`
),
setMethod('GET')
);
/**
* updateUserPermissions
*
* Update the permissions a User has.
*
* @param username { number } ID of the client to be viewed.
* @param data { object } the Permissions object to update.
*
*/
export const updateUserPermissions = (
username: string,
data: Partial<IamUserPermissions>
) =>
Request<IamUserPermissions>(
setURL(
`${BETA_API_ROOT}/iam/role-permissions/users/${encodeURIComponent(
username
)}`
),
setMethod('PUT'),
setData(data)
);
3 changes: 3 additions & 0 deletions packages/api-v4/src/iam/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './types';

export * from './iam';
30 changes: 30 additions & 0 deletions packages/api-v4/src/iam/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export interface IamUserPermissions {
account_access: AccountAccessType[];
resource_access: ResourceAccess[];
}

type ResourceType =
| 'linode'
| 'firewall'
| 'nodebalancer'
| 'longview'
| 'domain'
| 'stackscript'
| 'image'
| 'volume'
| 'database'
| 'account'
| 'vpc';

type AccountAccessType =
| 'account_linode_admin'
| 'linode_creator'
| 'firewall_creator';

type RoleType = 'linode_contributor' | 'firewall_admin';

export interface ResourceAccess {
resource_id: number;
resource_type: ResourceType;
roles: RoleType[];
}
2 changes: 2 additions & 0 deletions packages/api-v4/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export * from './vpcs';

export * from './betas';

export * from './iam';

export {
baseRequest,
setToken,
Expand Down
5 changes: 5 additions & 0 deletions packages/manager/.changeset/pr-11146-added-1729681889703.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/manager": Added
---

mock data and query for new permission api ([#11146](https://github.com/linode/manager/pull/11146))
24 changes: 24 additions & 0 deletions packages/manager/src/factories/userPermissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { IamUserPermissions } from '@linode/api-v4';
import Factory from 'src/factories/factoryProxy';

export const userPermissionsFactory = Factory.Sync.makeFactory<IamUserPermissions>(
{
account_access: [
'account_linode_admin',
'linode_creator',
'firewall_creator',
],
resource_access: [
{
resource_id: 12345678,
resource_type: 'linode',
roles: ['linode_contributor'],
},
{
resource_id: 45678901,
resource_type: 'firewall',
roles: ['firewall_admin'],
},
],
}
);
9 changes: 9 additions & 0 deletions packages/manager/src/queries/iam/iam.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { APIError, IamUserPermissions } from '@linode/api-v4';
import { iamQueries } from './queries';
import { useQuery } from '@tanstack/react-query';

export const useAccountUserPermissions = (username: string) => {
return useQuery<IamUserPermissions, APIError[]>(
iamQueries.user(username)._ctx.permissions
);
};
14 changes: 14 additions & 0 deletions packages/manager/src/queries/iam/queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getUserPermissions } from '@linode/api-v4';
import { createQueryKeys } from '@lukemorales/query-key-factory';

export const iamQueries = createQueryKeys('iam', {
user: (username: string) => ({
contextQueries: {
permissions: {
queryFn: () => getUserPermissions(username),
queryKey: null,
},
},
queryKey: [username],
}),
});

0 comments on commit d52d108

Please sign in to comment.