-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #188 from konecty/develop
Merge develop into main
- Loading branch information
Showing
35 changed files
with
1,456 additions
and
1,170 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { getUserSafe } from '@imports/auth/getUser'; | ||
import { MetaAccess } from '@imports/model/MetaAccess'; | ||
import { MetaObject } from '@imports/model/MetaObject'; | ||
import { KonectyResult } from '@imports/types/result'; | ||
import { checkMetaOperation, getAccessFor } from '@imports/utils/accessUtils'; | ||
import { errorReturn, successReturn } from '@imports/utils/return'; | ||
import { Span } from '@opentelemetry/api'; | ||
import filter from 'lodash/filter'; | ||
|
||
type GetAccessForParams = { | ||
document: string; | ||
authTokenId: string; | ||
|
||
tracingSpan?: Span; | ||
}; | ||
|
||
export default async function getAccessForDocument({ document, authTokenId, tracingSpan }: GetAccessForParams): Promise<KonectyResult<MetaAccess[]>> { | ||
tracingSpan?.setAttribute('document', document); | ||
tracingSpan?.addEvent('Get User', { authTokenId }); | ||
|
||
const userResponse = await getUserSafe(authTokenId); | ||
if (userResponse.success === false) { | ||
return errorReturn(userResponse.errors); | ||
} | ||
|
||
const user = userResponse.data; | ||
const access = getAccessFor(document, user); | ||
|
||
if (access === false || access.isReadable !== true) { | ||
return errorReturn(`[${document}] You don't have permission for this document`); | ||
} | ||
|
||
tracingSpan?.addEvent('Check Meta Operation'); | ||
|
||
const metaOperationAccess = checkMetaOperation({ user, operation: 'readAccess', document }); | ||
if (metaOperationAccess === false) { | ||
return errorReturn(`[${document}] You don't have permission to read access`); | ||
} | ||
|
||
tracingSpan?.addEvent('Filter Accesses'); | ||
const documentAccesses = filter(MetaObject.Access, { document }); | ||
|
||
return successReturn(documentAccesses); | ||
} |
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,101 @@ | ||
import { getUserSafe } from '@imports/auth/getUser'; | ||
import { Condition } from '@imports/model/Filter'; | ||
import { MetaAccess, MetaAccessSchema } from '@imports/model/MetaAccess'; | ||
import { MetaObject } from '@imports/model/MetaObject'; | ||
import { KonectyResult } from '@imports/types/result'; | ||
import { checkMetaOperation } from '@imports/utils/accessUtils'; | ||
import { errorReturn, successReturn } from '@imports/utils/return'; | ||
import { Span } from '@opentelemetry/api'; | ||
import find from 'lodash/find'; | ||
import { UpdateFilter } from 'mongodb'; | ||
import { z } from 'zod'; | ||
|
||
const AccessUpdateSchema = z.union([ | ||
z.object({ | ||
fields: z | ||
.object({ | ||
fieldNames: z.array(z.string()), | ||
allow: z.boolean(), | ||
operation: z.literal('READ').or(z.literal('UPDATE')).or(z.literal('DELETE')).or(z.literal('CREATE')), | ||
condition: Condition.optional(), | ||
}) | ||
.array(), | ||
}), | ||
MetaAccessSchema.pick({ readFilter: true }).required(), | ||
MetaAccessSchema.pick({ updateFilter: true }).required(), | ||
]); | ||
export type AccessUpdate = z.infer<typeof AccessUpdateSchema>; | ||
|
||
type UpdateAccessParams = { | ||
document: string; | ||
accessName: string; | ||
|
||
data: AccessUpdate; | ||
authTokenId: string; | ||
|
||
tracingSpan?: Span; | ||
}; | ||
|
||
export default async function updateAccess({ document, accessName, data, authTokenId, tracingSpan }: UpdateAccessParams): Promise<KonectyResult<MetaAccess>> { | ||
tracingSpan?.setAttribute('document', document); | ||
tracingSpan?.addEvent('Get User', { authTokenId }); | ||
|
||
const userResponse = await getUserSafe(authTokenId); | ||
if (userResponse.success === false) { | ||
return errorReturn(userResponse.errors); | ||
} | ||
|
||
const user = userResponse.data; | ||
|
||
tracingSpan?.addEvent('Check Meta Operation'); | ||
|
||
const metaOperationAccess = checkMetaOperation({ user, operation: 'updateAccess', document }); | ||
if (metaOperationAccess === false) { | ||
tracingSpan?.setAttribute('error', "You don't have permission to update access"); | ||
return errorReturn(`[${document}] You don't have permission to update access`); | ||
} | ||
|
||
tracingSpan?.addEvent('Find Access', { accessName }); | ||
const access = find(MetaObject.Access, { document, name: accessName }); | ||
if (!access) { | ||
tracingSpan?.setAttribute('error', 'Access not found'); | ||
return errorReturn(`[${document}] Access not found`); | ||
} | ||
|
||
tracingSpan?.addEvent('Parse update schema'); | ||
const parseResponse = AccessUpdateSchema.safeParse(data); | ||
if (parseResponse.success === false) { | ||
const errors = parseResponse.error.flatten(); | ||
const errorMessages = Object.values(errors.fieldErrors).concat(errors.formErrors).flat(); | ||
tracingSpan?.setAttribute('error', errorMessages.join(', ')); | ||
|
||
return errorReturn(errorMessages); | ||
} | ||
|
||
const updateObj: Required<Pick<UpdateFilter<MetaAccess>, '$set'>> = { $set: {} }; | ||
|
||
if ('fields' in data) { | ||
for (const { fieldNames, allow, condition, operation } of data.fields) { | ||
for (const fieldName of fieldNames) { | ||
updateObj.$set[`fields.${fieldName}.${operation}`] = { allow, condition }; | ||
} | ||
} | ||
} | ||
|
||
if ('readFilter' in data) { | ||
updateObj.$set = { ...updateObj.$set, readFilter: data.readFilter }; | ||
} | ||
|
||
if ('updateFilter' in data) { | ||
updateObj.$set = { ...updateObj.$set, updateFilter: data.updateFilter }; | ||
} | ||
|
||
if (Object.keys(updateObj.$set).length === 0) { | ||
tracingSpan?.addEvent('Nothing changed'); | ||
return successReturn(access); | ||
} | ||
|
||
tracingSpan?.addEvent('Update Access'); | ||
const result = await MetaObject.MetaObject.findOneAndUpdate({ _id: access._id }, updateObj, { returnDocument: 'after', ignoreUndefined: true }); | ||
return successReturn(result); | ||
} |
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
Oops, something went wrong.