Skip to content

Commit

Permalink
Correct User type definition
Browse files Browse the repository at this point in the history
Both the type and the openAPI documentation were inaccurate to the shape
of the data returned by the database when loading a `User`.  The actual
data being returned is what we wanted, so we just need to fix the
definitions and docs.
  • Loading branch information
slifty committed Nov 12, 2024
1 parent 6dcaf0b commit 17193d4
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 8 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Upgraded to use OpenAPI Specification 3.1.

## 0.17.0 2024-11-07
## 0.16.2 2024-11-12

### Fixed

- `User` type had an inaccurate specification regarding permission attributes.

## 0.16.1 2024-11-07

### Changed

Expand Down
5 changes: 5 additions & 0 deletions src/middleware/__tests__/requireAdministratorRole.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import type { User } from '../../types';
const getMockedUser = (): User => ({
keycloakUserId: getTestUserKeycloakUserId(),
createdAt: '',
permissions: {
changemaker: {},
dataProvider: {},
funder: {},
},
});

describe('requireAuthentication', () => {
Expand Down
103 changes: 98 additions & 5 deletions src/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"info": {
"title": "Philanthropy Data Commons API",
"description": "An API for a common data platform to make the process of submitting data requests to funders less burdensome for changemakers seeking grants.",
"version": "0.16.1",
"version": "0.16.2",
"license": {
"name": "GNU Affero General Public License v3.0 only",
"url": "https://spdx.org/licenses/AGPL-3.0-only.html"
Expand Down Expand Up @@ -1077,26 +1077,35 @@
"type": "string",
"example": "550e8400-e29b-41d4-a716-446655440000"
},
"roles": {
"permissions": {
"type": "object",
"readOnly": true,
"properties": {
"changemaker": {
"type": "object",
"additionalProperties": {
"$ref": "#/components/schemas/RoleMap"
"type": "array",
"items": {
"$ref": "#/components/schemas/Permission"
}
}
},
"dataProvider": {
"type": "object",
"additionalProperties": {
"$ref": "#/components/schemas/RoleMap"
"type": "array",
"items": {
"$ref": "#/components/schemas/Permission"
}
}
},
"funder": {
"type": "object",
"additionalProperties": {
"$ref": "#/components/schemas/RoleMap"
"type": "array",
"items": {
"$ref": "#/components/schemas/Permission"
}
}
}
},
Expand Down Expand Up @@ -1128,6 +1137,35 @@
"required": ["entries"]
}
]
},
"UserChangemakerPermission": {
"type": "object",
"properties": {
"permission": {
"$ref": "#/components/schemas/Permission",
"readOnly": true
},
"changemakerId": {
"type": "integer",
"readOnly": true
},
"userKeycloakUserId": {
"type": "string",
"format": "uuid",
"readOnly": true
},
"createdBy": {
"type": "string",
"format": "uuid",
"readOnly": true
},
"createdAt": {
"type": "string",
"format": "date-time",
"readOnly": true
}
},
"required": ["id", "", "createdAt"]
}
}
},
Expand Down Expand Up @@ -2621,6 +2659,61 @@
}
}
}
},
"/users/{userKeycloakUserId}/changemakers/{changemakerId}/permissions/{permission}": {
"put": {
"operationId": "createOrUpdateUserChangemakerPermission",
"summary": "Creates or updates a user-changemaker permission.",
"tags": ["Permissions"],
"security": [
{
"auth": []
}
],
"parameters": [
{
"name": "userKeycloakUserId",
"description": "The keycloak user id of a user.",
"in": "path",
"required": true,
"schema": {
"type": "string",
"format": "uuid"
}
},
{
"name": "changemakerId",
"description": "The id of a changemaker.",
"in": "path",
"required": true,
"schema": {
"type": "integer"
}
},
{
"name": "permission",
"description": "The permission to be granted.",
"in": "path",
"required": true,
"schema": {
"type": "string",
"enum": ["manage", "edit", "view"]
}
}
],
"responses": {
"201": {
"description": "The resulting permission.",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/UserChangemakerPermission"
}
}
}
}
}
}
}
}
}
12 changes: 11 additions & 1 deletion src/types/Permission.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { ajv } from '../ajv';
import type { JSONSchemaType } from 'ajv';

enum Permission {
MANAGE = 'manage',
EDIT = 'edit',
VIEW = 'view',
}

export { Permission };
const permissionSchema: JSONSchemaType<Permission> = {
type: 'string',
enum: Object.values(Permission),
};

const isPermission = ajv.compile(permissionSchema);

export { Permission, permissionSchema, isPermission };
39 changes: 38 additions & 1 deletion src/types/User.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,59 @@
import { keycloakUserIdSchema } from './KeycloakUserId';
import { permissionSchema } from './Permission';
import type { KeycloakUserId } from './KeycloakUserId';
import type { JSONSchemaType } from 'ajv';
import type { Writable } from './Writable';
import type { Permission } from './Permission';

interface User {
keycloakUserId: KeycloakUserId;
readonly permissions: {
changemaker: Record<string, Permission[]>;
dataProvider: Record<string, Permission[]>;
funder: Record<string, Permission[]>;
};
readonly createdAt: string;
}

const userSchema: JSONSchemaType<User> = {
type: 'object',
properties: {
keycloakUserId: keycloakUserIdSchema,
permissions: {
type: 'object',
properties: {
changemaker: {
type: 'object',
additionalProperties: {
type: 'array',
items: permissionSchema,
},
required: [],
},
dataProvider: {
type: 'object',
additionalProperties: {
type: 'array',
items: permissionSchema,
},
required: [],
},
funder: {
type: 'object',
additionalProperties: {
type: 'array',
items: permissionSchema,
},
required: [],
},
},
required: ['changemaker', 'dataProvider', 'funder'],
},
createdAt: {
type: 'string',
},
},
required: ['keycloakUserId', 'createdAt'],
required: ['keycloakUserId', 'permissions', 'createdAt'],
};

type WritableUser = Writable<User>;
Expand Down

0 comments on commit 17193d4

Please sign in to comment.