Bazaar-backend: Add user entity field to members table
A new field has been added to the members table. When a user has requested to be added to a project the "identityApi" is used to get the entity ref of the user. Signed-off-by: Niklas Aronsson <niklasar@axis.com>
This commit is contained in:
@@ -75,6 +75,13 @@ describe('DatabaseHandler', () => {
|
||||
responsible: bazaarProject.responsible,
|
||||
});
|
||||
|
||||
// Add a member to the project
|
||||
await knex('members').insert({
|
||||
item_id: 1,
|
||||
user_ref: 'user:default/thehulk',
|
||||
user_id: 'Bruce Banner',
|
||||
});
|
||||
|
||||
const res = await dbHandler.getMetadataByRef('ref1');
|
||||
|
||||
expect(res).toHaveLength(1);
|
||||
@@ -85,6 +92,9 @@ describe('DatabaseHandler', () => {
|
||||
expect(res[0].end_date).toEqual(null);
|
||||
expect(res[0].size).toEqual('small');
|
||||
expect(res[0].responsible).toEqual('r');
|
||||
expect(
|
||||
res[0].members_count === '1' || res[0].members_count === 1,
|
||||
).toBeTruthy();
|
||||
},
|
||||
60_000,
|
||||
);
|
||||
|
||||
@@ -67,11 +67,17 @@ export class DatabaseHandler {
|
||||
return await this.client.select('*').from('members').where({ item_id: id });
|
||||
}
|
||||
|
||||
async addMember(id: number, userId: string, picture?: string) {
|
||||
async addMember(
|
||||
id: number,
|
||||
userId: string,
|
||||
userRef?: string,
|
||||
picture?: string,
|
||||
) {
|
||||
await this.client
|
||||
.insert({
|
||||
item_id: id,
|
||||
user_id: userId,
|
||||
user_ref: userRef,
|
||||
picture: picture,
|
||||
})
|
||||
.into('members');
|
||||
|
||||
@@ -19,6 +19,7 @@ import express from 'express';
|
||||
import Router from 'express-promise-router';
|
||||
import { Logger } from 'winston';
|
||||
import { Config } from '@backstage/config';
|
||||
import { IdentityApi } from '@backstage/plugin-auth-node';
|
||||
import { DatabaseHandler } from './DatabaseHandler';
|
||||
|
||||
/** @public */
|
||||
@@ -26,13 +27,14 @@ export interface RouterOptions {
|
||||
logger: Logger;
|
||||
database: PluginDatabaseManager;
|
||||
config: Config;
|
||||
identity: IdentityApi;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export async function createRouter(
|
||||
options: RouterOptions,
|
||||
): Promise<express.Router> {
|
||||
const { logger, database } = options;
|
||||
const { logger, database, identity } = options;
|
||||
|
||||
const dbHandler = await DatabaseHandler.create({ database });
|
||||
|
||||
@@ -53,7 +55,14 @@ export async function createRouter(
|
||||
|
||||
router.put('/projects/:id/member/:userId', async (request, response) => {
|
||||
const { id, userId } = request.params;
|
||||
await dbHandler.addMember(parseInt(id, 10), userId, request.body?.picture);
|
||||
const user = await identity.getIdentity({ request: request });
|
||||
|
||||
await dbHandler.addMember(
|
||||
parseInt(id, 10),
|
||||
userId,
|
||||
user?.identity.userEntityRef,
|
||||
request.body?.picture,
|
||||
);
|
||||
|
||||
response.json({ status: 'ok' });
|
||||
});
|
||||
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
loadBackendConfig,
|
||||
useHotMemoize,
|
||||
} from '@backstage/backend-common';
|
||||
import { DefaultIdentityClient } from '@backstage/plugin-auth-node';
|
||||
import { Server } from 'http';
|
||||
import { Logger } from 'winston';
|
||||
import { createRouter } from './router';
|
||||
@@ -54,6 +55,7 @@ export async function startStandaloneServer(
|
||||
logger,
|
||||
database: { getClient: async () => db },
|
||||
config: config,
|
||||
identity: {} as DefaultIdentityClient,
|
||||
});
|
||||
|
||||
let service = createServiceBuilder(module)
|
||||
|
||||
Reference in New Issue
Block a user