Skip to content

Commit

Permalink
Merge branch 'feature/admin-views' into feature/beta-v1
Browse files Browse the repository at this point in the history
  • Loading branch information
maxtechera committed Sep 3, 2024
2 parents d74d385 + 6f118f9 commit ad4a3b7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/server/src/controllers/predictions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const createPrediction = async (req: Request, res: Response, next: NextFunction)
`Error: predictionsController.createPrediction - body not provided!`
)
}
const chatflow = await chatflowsService.getChatflowById(req.params.id)
const chatflow = await chatflowsService.getChatflowById(req.params.id, req.user)
if (!chatflow) {
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Chatflow ${req.params.id} not found`)
}
Expand Down
12 changes: 7 additions & 5 deletions packages/server/src/routes/credentials/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import express from 'express'
import credentialsController from '../../controllers/credentials'
import enforceAbility from '../../middlewares/authentication/enforceAbility'

const router = express.Router()

// CREATE
router.post('/', credentialsController.createCredential)
router.post('/', enforceAbility('Credential'), credentialsController.createCredential)

// READ
router.get('/', credentialsController.getAllCredentials)
router.get(['/', '/:id'], credentialsController.getCredentialById)
router.get('/', enforceAbility('Credential'), credentialsController.getAllCredentials)
router.get(['/', '/:id'], enforceAbility('Credential'), credentialsController.getCredentialById)

// UPDATE
router.put(['/', '/:id'], credentialsController.updateCredential)
router.put(['/', '/:id'], enforceAbility('Credential'), credentialsController.updateCredential)

// DELETE
router.delete(['/', '/:id'], credentialsController.deleteCredentials)
router.delete(['/', '/:id'], enforceAbility('Credential'), credentialsController.deleteCredentials)

export default router
14 changes: 10 additions & 4 deletions packages/server/src/services/chatflows/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,21 @@ const getChatflowById = async (chatflowId: string, user?: IUser): Promise<any> =
const appServer = getRunningExpressApp()
const dbResponse = await appServer.AppDataSource.getRepository(ChatFlow)
.createQueryBuilder('chatFlow')
.where(user?.permissions?.includes('org:manage') ? 'chatFlow.id = :id' : 'chatFlow.id = :id AND chatFlow.userId = :userId', {
id: chatflowId,
userId: user?.id
})
.where('chatFlow.id = :id', { id: chatflowId })
.getOne()

if (!dbResponse) {
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Chatflow ${chatflowId} not found in the database!`)
}

// Check if the chatflow is not public and the user is not an org manager
if (!dbResponse.isPublic && !user?.permissions?.includes('org:manage')) {
// Perform the check against userId
if (dbResponse.userId !== user?.id) {
throw new InternalFlowiseError(StatusCodes.UNAUTHORIZED, `Unauthorized to access this chatflow`)
}
}

return dbResponse
} catch (error) {
throw new InternalFlowiseError(
Expand Down
8 changes: 4 additions & 4 deletions packages/server/src/services/credentials/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ const getAllCredentials = async (paramCredentialName: any, user: IUser) => {
const fetchCredentials = async (name?: string) => {
let baseConditions = []

if (isAdmin) {
// If name is provided, only fetch owned credentials
if (!name && isAdmin) {
// Admin can see all organization credentials
baseConditions = [{ organizationId: user.organizationId }]
baseConditions = [{ organizationId: user.organizationId }, { userId: IsNull() }]
} else {
baseConditions = [
{ userId: user.id },
Expand Down Expand Up @@ -148,8 +149,7 @@ const updateCredential = async (credentialId: string, requestBody: any, userId?:
try {
const appServer = getRunningExpressApp()
const credential = await appServer.AppDataSource.getRepository(Credential).findOneBy({
id: credentialId,
userId
id: credentialId
})
if (!credential) {
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${credentialId} not found`)
Expand Down
8 changes: 6 additions & 2 deletions packages/ui/src/views/chatflows/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ const Chatflows = () => {

const myChatflowsData = getAllChatflowsApi.data
const { processedImages: myImages, processedNodeTypes: myNodeTypes } = processFlowData(myChatflowsData)
console.log('User', { myChatflowsData, user, flags })
setMyChatflows(myChatflowsData?.filter((flow) => flow.isOwner))
setOrganizationChatflows(myChatflowsData?.filter((flow) => !flow.isOwner))
const marketplaceChatflows = getMarketplaceChatflowsApi.data
Expand Down Expand Up @@ -185,6 +184,11 @@ const Chatflows = () => {
[communityChatflows, search, categoryFilter]
)

const filteredOrganizationChatflows = useMemo(
() => filterChatflows(organizationChatflows, search, categoryFilter),
[organizationChatflows, search, categoryFilter]
)

return (
<MainCard>
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 3 }}>
Expand Down Expand Up @@ -255,7 +259,7 @@ const Chatflows = () => {
</TabPanel>
<TabPanel value={tabValue} index={3}>
<FlowListView
data={organizationChatflows}
data={filteredOrganizationChatflows}
images={images}
nodeTypes={nodeTypes}
isLoading={isLoading}
Expand Down

0 comments on commit ad4a3b7

Please sign in to comment.