-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fi: support uuid -> username mapping (#290)
* fix: introduce user profile queries & mutation * fix: resolve username from db * test: add unit tests
- Loading branch information
Showing
27 changed files
with
836 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,35 @@ | ||
import { rule } from 'graphql-shield' | ||
import { rule, inputRule } from 'graphql-shield' | ||
import muuid from 'uuid-mongodb' | ||
|
||
export const isEditor = rule()(async (parent, args, ctx, info) => { | ||
return (ctx.user.uuid != null) && ctx.user.roles.includes('editor') | ||
return _hasUserUuid(ctx) && ctx.user.roles.includes('editor') | ||
}) | ||
|
||
export const isUserAdmin = rule()(async (parent, args, ctx, info) => { | ||
return (ctx.user.uuid != null) && ctx.user.roles.includes('user_admin') | ||
return _hasUserUuid(ctx) && ctx.user.roles.includes('user_admin') | ||
}) | ||
|
||
export const isOwner = rule()(async (parent, args, ctx, info) => { | ||
return _hasUserUuid(ctx) && ctx.user.uuid === muuid.from(args.userUuid) | ||
}) | ||
|
||
export const isBuilderServiceAccount = rule()(async (parent, args, ctx: Context, info) => { | ||
return _hasUserUuid(ctx) && ctx.user.isBuilder | ||
}) | ||
|
||
export const isValidEmail = inputRule()( | ||
(yup) => | ||
yup.object({ | ||
email: yup.string().email('Please provide a valid email') | ||
}), | ||
{ abortEarly: false } | ||
) | ||
|
||
interface Context { | ||
user: { | ||
uuid?: string | ||
isBuilder: boolean | ||
} | ||
} | ||
|
||
const _hasUserUuid = (ctx: Context): boolean => ctx.user.uuid != null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { glob } from 'glob' | ||
import { validate as uuidValidate } from 'uuid' | ||
import muuid from 'uuid-mongodb' | ||
import fs from 'fs' | ||
import { connectDB, gracefulExit, getUserModel } from '../../../index.js' | ||
import { logger } from '../../../../logger.js' | ||
import { User } from '../../../UserTypes.js' | ||
|
||
const LOCAL_MEDIA_DIR_UID = process.env.LOCAL_MEDIA_DIR_UID | ||
|
||
if (LOCAL_MEDIA_DIR_UID == null) { | ||
throw new Error('LOCAL_MEDIA_DIR_UID env not defined') | ||
} | ||
|
||
/** | ||
* Create a new Users collection from uid.json files in local media dir. | ||
*/ | ||
const onConnected = async (): Promise<void> => { | ||
logger.info('Creating users collection') | ||
const model = getUserModel() | ||
await model.ensureIndexes() | ||
const uidFIle = await glob(LOCAL_MEDIA_DIR_UID, { | ||
nodir: true, | ||
stat: false, | ||
withFileTypes: true | ||
}) | ||
|
||
let list: Array<Omit<User, 'createdAt' | 'updatedAt'>> = [] | ||
let count = 0 | ||
for (const file of uidFIle) { | ||
const folderUuidStr = file.parent?.name ?? '' | ||
if (!uuidValidate(folderUuidStr)) { | ||
logger.error({ file: file.name, parent: folderUuidStr }, 'Error: expect folder name to have uuid format. Found ') | ||
continue | ||
} | ||
const userUuid = muuid.from(folderUuidStr) | ||
const f = fs.readFileSync(file.fullpath(), 'utf-8') | ||
|
||
const { uid, ts } = JSON.parse(f) | ||
const newUser: Omit<User, 'createdAt' | 'updatedAt'> = { | ||
_id: userUuid, | ||
usernameInfo: { | ||
username: uid as string, | ||
updatedAt: new Date(ts) | ||
}, | ||
createdBy: userUuid | ||
} | ||
list.push(newUser) | ||
|
||
if (list.length === 40) { | ||
const rs = await model.insertMany(list) | ||
count = count + rs.length | ||
list = [] | ||
} | ||
} | ||
|
||
if (list.length > 0) { | ||
await model.insertMany(list) | ||
count = count + list.length | ||
} | ||
|
||
logger.info({ count }, 'Finish') | ||
|
||
await gracefulExit() | ||
} | ||
|
||
void connectDB(onConnected) |
Oops, something went wrong.