n8n/packages/editor-ui/src/modules/userHelpers.ts
Jan Oberhauser c85faff4f1
feat: Make it possible to dynamically load community nodes (#2849)
*  Make it possible to dynamically load node packages

*  Fix comment

*  Make possible to dynamically install nodes from npm

* Created migration for sqlite regarding community nodes

* Saving to db whenever a package with nodes is installed

* Created endpoint to fetch installed packages

* WIP - uninstall package with nodes

* Fix lint issues

* Updating nodes via API

* Lint and improvement fixes

* Created community node helpers and removed packages taht do not contain nodes

* Check for package updates when fetching installed packages

* Blocked access to non-owner and preventing incorrect install of packages

* Added auto healing process

* Unit tests for helpers

* Finishing tests for helpers

* Improved unit tests, refactored more helpers and created integration tests for GET

* Implemented detection of missing packages on init and added warning to frontend settings

* Add check for banned packages and fix broken tests

* Create migrations for other db systems

* Updated with latest changes from master

* Fixed conflict errors

* Improved unit tests, refactored more helpers and created integration tests for GET

* Implemented detection of missing packages on init and added warning to frontend settings

* 🔥 Removing access check for the Settings sidebar item

*  Added inital community nodes settings screen

* Added executionMode flag to settings

*  Implemented N8N-callout component

* 💄Updating Callout component template propery names

* 💄 Updating Callout component styling.

* 💄Updating Callout component sizing and colors.

* ✔️ Updating Callout component test snapshots after styling changes

*  Updating the `ActionBox` component so it supports callouts and conditional button rendering

* 💄 Removing duplicate callout theme validation in the `ActionBox` component. Adding a selection control for it in the storybook.

*  Added warning message if instance is in the queue mode. Updated colors based on the new design.

*  Added a custom permission support to router

* 🔨 Implemented UM detection as a custom permission.

* 👌Updating route permission logic.

*  Implemented installed community packages list in the settings view

* 👌 Updating settings routes rules and community nodes setting view.

* Allow installation of packages that failed to load

* 👌 Updating `ActionBox`, `CommuntyPackageCard` components and settings loading logic.

* 👌 Fixing community nodes loading state and sidebar icon spacing.

*  Implemented loading skeletons for community package cards

* 👌 Handling errrors while loading installed package list. Updating spacing.

* 👌 Updating community nodes error messages.

* Added disable flag

* 🐛 Fixing a community nodes update detection bug when there are missing packages. (#3497)

*  Added front-end support for community nodes feature flag

*  Implemented community package installation modal dialog

* 💄 Community nodes installation modal updates: Moved links to constants and used them in translations, disabling inputs in loading state.

*  Implemented community packages install flow

* Standardize error codes (#3501)

* Standardize error: 400 for request issues such as invalid package name and 500 for installation problems

* Fix http status code for when package is not found

*  Implemented community package installation modal dialog

* 💄 Community nodes installation modal updates: Moved links to constants and used them in translations, disabling inputs in loading state.

*  Implemented community packages install flow

*  Updated error handling based on the response codes

*  Implemented community package installation modal dialog

*  Implemented community package uninstall flow.

*  Finished update confirm modal UI

* 💄 Replaced community nodes tooltip image with the one exported from figma.

*  Implemented community package update process

*  Updating community nodes list after successful package update

* 🔒 Updating public API setting route to use new access rules. Updating express app definition in community nodes tests

*  Implemented community package installation modal dialog

* 💄 Community nodes installation modal updates: Moved links to constants and used them in translations, disabling inputs in loading state.

*  Implemented community packages install flow

*  Updated error handling based on the response codes

* Change output for installation request

* Improve payload for update requests

* 👌 Updating community nodes install modal UI

* 👌 Updating community nodes confirm modal logic

* 👌 Refactoring community nodes confirm modal dialog

* 👌 Separating community nodes components loading states

* 💄 Updating community nodes install modal spacing.

* Fix behavior for installing already installed packages

* 💡 Commenting community nodes install process

* 🔥 Removing leftover commits of deleted Vue mutations

*  Updated node list to identify community nodes and handle node name clash

*  Implemented missing community node dialog.

* 💄 Updating n8n-tabs component to support tooltips

*  Updating node details with community node details.

* 🔨 Using back-end response when updating community packages

* 👌 Updating tabs component and refactoring community nodes store mutations

* 👌 Adding community node flag to node type descriptions and using it to identify community nodes

* 👌 Hiding unnecessary elements from missing node details panel.

* 👌 Updating missing node type descriptions for custom and community nodes

* 👌 Updating community node package name detection logic

* 👌 Removing communityNode flag from node description

*  Adding `force` flag to credentials fetching (#3527)

*  Adding `force` flag to credentials fetching which can be used to skip check when loading credentials

*  Forcing credentials loading when opening nodeView

* 👌 Minor updates to community nodes details panel

* tests for post endpoint

* duplicate comments

* Add Patch and Delete enpoints tests

* 🔒 Using `pageCategory`prop to assemble the list of settings routes instead of hard-coded array (#3562)

* 📈 Added front-end telemetry events for community nodes

* 📈 Updating community nodes telemetry events

* 💄 Updating community nodes settings UI elements based on product/design review

* 💄 Updating node view & node details view for community nodes based on product/design feedback

* 💄 Fixing community node text capitalisation

*  Adding community node install error message under the package name input field

* Fixed and improved tests

* Fix lint issue

* feat: Migrated to npm release of riot-tmpl fork.

* 📈 Updating community nodes telemetry events based on the product review

* 💄 Updating community nodes UI based on the design feedback

* 🔀 Merging recent node draggable panels changes

* Implement self healing process

* Improve error messages for package name requirement and disk space

* 💄 Removing front-end error message override since appropriate response is available from the back-end

* Fix lint issues

* Fix installed node name

* 💄 Removed additional node name parsing

* 📈 Updating community nodes telemetry events

* Fix postgres migration for cascading nodes when package is removed

* Remove postman mock for banned packages

* 📈 Adding missing telemetry event for community node documentation click

* 🐛 Fixing community nodes UI bugs reported during the bug bash

* Fix issue with uninstalling packages not reflecting UI

* 🐛 Fixing a missing node type bug when trying to run a workflow.

* Improve error detection for installing packages

* 💄 Updating community nodes components styling and wording based on the product feedback

* Implement telemetry be events

* Add author name and email to packages

* Fix telemetry be events for community packages

* 📈 Updating front-end telemetry events with community nodes author data

* 💄 Updating credentials documentation link logic to handle community nodes credentials

* 🐛 Fixing draggable panels logic

* Fix duplicate wrong import

* 💄 Hiding community nodes credentials documentation links when they don't contain an absolute URL

* Fix issue with detection of missing packages

* 💄 Adding the `Docs` tab to community nodes

* 💄 Adding a failed loading indicator to community nodes list

* Prevent n8n from crashing on startup

* Refactor and improve code quality

*  Remove not needed depenedency

Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: Milorad Filipović <milorad@n8n.io>
Co-authored-by: Milorad FIlipović <miloradfilipovic19@gmail.com>
Co-authored-by: agobrech <ael.gobrecht@gmail.com>
Co-authored-by: Alex Grozav <alex@grozav.com>
2022-07-20 16:24:03 +02:00

320 lines
11 KiB
TypeScript

import { CALENDLY_TRIGGER_NODE_TYPE, CLEARBIT_NODE_TYPE, COMPANY_SIZE_1000_OR_MORE, COMPANY_SIZE_500_999, CRON_NODE_TYPE, ELASTIC_SECURITY_NODE_TYPE, EMAIL_SEND_NODE_TYPE, EXECUTE_COMMAND_NODE_TYPE, FINANCE_WORK_AREA, FUNCTION_NODE_TYPE, GITHUB_TRIGGER_NODE_TYPE, HTTP_REQUEST_NODE_TYPE, IF_NODE_TYPE, ITEM_LISTS_NODE_TYPE, IT_ENGINEERING_WORK_AREA, JIRA_TRIGGER_NODE_TYPE, MICROSOFT_EXCEL_NODE_TYPE, MICROSOFT_TEAMS_NODE_TYPE, PAGERDUTY_NODE_TYPE, PRODUCT_WORK_AREA, QUICKBOOKS_NODE_TYPE, SALESFORCE_NODE_TYPE, SALES_BUSINESSDEV_WORK_AREA, SECURITY_WORK_AREA, SEGMENT_NODE_TYPE, SET_NODE_TYPE, SLACK_NODE_TYPE, SPREADSHEET_FILE_NODE_TYPE, SWITCH_NODE_TYPE, WEBHOOK_NODE_TYPE, XERO_NODE_TYPE, COMPANY_SIZE_KEY, WORK_AREA_KEY, CODING_SKILL_KEY, COMPANY_TYPE_KEY, ECOMMERCE_COMPANY_TYPE, MSP_COMPANY_TYPE, PERSONAL_COMPANY_TYPE, AUTOMATION_GOAL_KEY, OTHER_AUTOMATION_GOAL, NOT_SURE_YET_GOAL, CUSTOMER_INTEGRATIONS_GOAL, CUSTOMER_SUPPORT_GOAL, FINANCE_ACCOUNTING_GOAL, ZENDESK_TRIGGER_NODE_TYPE, WOOCOMMERCE_TRIGGER_NODE_TYPE, SALES_MARKETING_GOAL, HUBSPOT_TRIGGER_NODE_TYPE, HR_GOAL, WORKABLE_TRIGGER_NODE_TYPE, OPERATIONS_GOAL, PRODUCT_GOAL, NOTION_TRIGGER_NODE_TYPE, SECURITY_GOAL, THE_HIVE_TRIGGER_NODE_TYPE, ZENDESK_NODE_TYPE, SERVICENOW_NODE_TYPE, JIRA_NODE_TYPE, BAMBOO_HR_NODE_TYPE, GOOGLE_SHEETS_NODE_TYPE } from '@/constants';
import { IPermissions, IPersonalizationSurveyAnswersV1, IPersonalizationSurveyAnswersV2, IRootState, IUser } from '@/Interface';
import { ILogInStatus, IRole, IUserPermissions } from "@/Interface";
export const ROLE: {Owner: IRole, Member: IRole, Default: IRole} = {
Owner: 'owner',
Member: 'member',
Default: 'default', // default user with no email when setting up instance
};
export const LOGIN_STATUS: {LoggedIn: ILogInStatus, LoggedOut: ILogInStatus} = {
LoggedIn: 'LoggedIn', // Can be owner or member or default user
LoggedOut: 'LoggedOut', // Can only be logged out if UM has been setup
};
export const PERMISSIONS: IUserPermissions = {
TAGS: {
CAN_DELETE_TAGS: {
allow: {
role: [ROLE.Owner, ROLE.Default],
},
},
},
PRIMARY_MENU: {
CAN_ACCESS_USER_INFO: {
allow: {
loginStatus: [LOGIN_STATUS.LoggedIn],
},
deny: {
role: [ROLE.Default],
},
},
},
USER_SETTINGS: {
VIEW_UM_SETUP_WARNING: {
allow: {
role: [ROLE.Default],
},
},
},
};
/**
* To be authorized, user must pass all deny rules and pass any of the allow rules.
*
* @param permissions
* @param currentUser
* @returns
*/
export const isAuthorized = (permissions: IPermissions, currentUser: IUser | null): boolean => {
const loginStatus = currentUser ? LOGIN_STATUS.LoggedIn : LOGIN_STATUS.LoggedOut;
// big AND block
// if any of these are false, block user
if (permissions.deny) {
if (permissions.deny.shouldDeny && permissions.deny.shouldDeny()) {
return false;
}
if (permissions.deny.loginStatus && permissions.deny.loginStatus.includes(loginStatus)) {
return false;
}
if (currentUser && currentUser.globalRole) {
const role = currentUser.isDefaultUser ? ROLE.Default : currentUser.globalRole.name;
if (permissions.deny.role && permissions.deny.role.includes(role)) {
return false;
}
}
else if (permissions.deny.role) {
return false;
}
}
// big OR block
// if any of these are true, allow user
if (permissions.allow) {
if (permissions.allow.shouldAllow && permissions.allow.shouldAllow()) {
return true;
}
if (permissions.allow.loginStatus && permissions.allow.loginStatus.includes(loginStatus)) {
return true;
}
if (currentUser && currentUser.globalRole) {
const role = currentUser.isDefaultUser ? ROLE.Default : currentUser.globalRole.name;
if (permissions.allow.role && permissions.allow.role.includes(role)) {
return true;
}
}
}
return false;
};
export function getPersonalizedNodeTypes(answers: IPersonalizationSurveyAnswersV1 | IPersonalizationSurveyAnswersV2 | null): string[] {
if (!answers) {
return [];
}
// @ts-ignore
if (answers.version === 'v2') {
return getPersonalizationV2(answers as IPersonalizationSurveyAnswersV2);
}
return getPersonalizationV1(answers as IPersonalizationSurveyAnswersV1);
}
function getPersonalizationV2(answers: IPersonalizationSurveyAnswersV2) {
let nodeTypes: string[] = [];
const {version, ...data} = answers;
if (Object.keys(data).length === 0) {
return [];
}
const companySize = answers[COMPANY_SIZE_KEY];
const companyType = answers[COMPANY_TYPE_KEY];
const automationGoal = answers[AUTOMATION_GOAL_KEY];
let codingSkill = null;
if (answers[CODING_SKILL_KEY]) {
codingSkill = parseInt(answers[CODING_SKILL_KEY] as string, 10);
codingSkill = isNaN(codingSkill)? 0 : codingSkill;
}
// slot 1 trigger
if (companyType === ECOMMERCE_COMPANY_TYPE) {
nodeTypes = nodeTypes.concat(WOOCOMMERCE_TRIGGER_NODE_TYPE);
} else if (companyType === MSP_COMPANY_TYPE) {
nodeTypes = nodeTypes.concat(JIRA_TRIGGER_NODE_TYPE);
} else if((companyType === PERSONAL_COMPANY_TYPE || automationGoal === OTHER_AUTOMATION_GOAL || automationGoal === NOT_SURE_YET_GOAL) && codingSkill !== null && codingSkill >= 4) {
nodeTypes = nodeTypes.concat(WEBHOOK_NODE_TYPE);
} else if((companyType === PERSONAL_COMPANY_TYPE || automationGoal === OTHER_AUTOMATION_GOAL || automationGoal === NOT_SURE_YET_GOAL) && codingSkill !== null && codingSkill < 3) {
nodeTypes = nodeTypes.concat(CRON_NODE_TYPE);
} else if (automationGoal === CUSTOMER_INTEGRATIONS_GOAL) {
nodeTypes = nodeTypes.concat(WEBHOOK_NODE_TYPE);
} else if (automationGoal === CUSTOMER_SUPPORT_GOAL || automationGoal === FINANCE_ACCOUNTING_GOAL) {
nodeTypes = nodeTypes.concat(ZENDESK_TRIGGER_NODE_TYPE);
} else if (automationGoal === SALES_MARKETING_GOAL) {
nodeTypes = nodeTypes.concat(HUBSPOT_TRIGGER_NODE_TYPE);
} else if (automationGoal === HR_GOAL) {
nodeTypes = nodeTypes.concat(WORKABLE_TRIGGER_NODE_TYPE);
} else if (automationGoal === OPERATIONS_GOAL) {
nodeTypes = nodeTypes.concat(CRON_NODE_TYPE);
} else if (automationGoal === PRODUCT_GOAL) {
nodeTypes = nodeTypes.concat(NOTION_TRIGGER_NODE_TYPE);
} else if (automationGoal === SECURITY_GOAL) {
nodeTypes = nodeTypes.concat(THE_HIVE_TRIGGER_NODE_TYPE);
} else {
nodeTypes = nodeTypes.concat(WEBHOOK_NODE_TYPE);
}
// slot 2 data transformation
if (codingSkill !== null && codingSkill >= 4) {
nodeTypes = nodeTypes.concat(FUNCTION_NODE_TYPE);
} else {
nodeTypes = nodeTypes.concat(ITEM_LISTS_NODE_TYPE);
}
// slot 3 logic node
if (codingSkill !== null && codingSkill < 3) {
nodeTypes = nodeTypes.concat(IF_NODE_TYPE);
}
else {
nodeTypes = nodeTypes.concat(SWITCH_NODE_TYPE);
}
// slot 4 usecase #1
if (companySize === COMPANY_SIZE_500_999 || companySize === COMPANY_SIZE_1000_OR_MORE) {
switch (automationGoal) {
case CUSTOMER_INTEGRATIONS_GOAL:
nodeTypes = nodeTypes.concat(HTTP_REQUEST_NODE_TYPE);
break;
case CUSTOMER_SUPPORT_GOAL:
nodeTypes = nodeTypes.concat(ZENDESK_NODE_TYPE);
break;
case SALES_MARKETING_GOAL:
nodeTypes = nodeTypes.concat(SALESFORCE_NODE_TYPE);
break;
case HR_GOAL:
nodeTypes = nodeTypes.concat(SERVICENOW_NODE_TYPE);
break;
case PRODUCT_GOAL:
nodeTypes = nodeTypes.concat(JIRA_NODE_TYPE);
break;
case FINANCE_ACCOUNTING_GOAL:
nodeTypes = nodeTypes.concat(SPREADSHEET_FILE_NODE_TYPE);
break;
case SECURITY_GOAL:
nodeTypes = nodeTypes.concat(ELASTIC_SECURITY_NODE_TYPE);
break;
default:
nodeTypes = nodeTypes.concat(SLACK_NODE_TYPE);
}
} else {
switch (automationGoal) {
case CUSTOMER_INTEGRATIONS_GOAL:
nodeTypes = nodeTypes.concat(HTTP_REQUEST_NODE_TYPE);
break;
case CUSTOMER_SUPPORT_GOAL:
nodeTypes = nodeTypes.concat(ZENDESK_NODE_TYPE);
break;
case FINANCE_ACCOUNTING_GOAL:
nodeTypes = nodeTypes.concat(QUICKBOOKS_NODE_TYPE);
break;
case HR_GOAL:
nodeTypes = nodeTypes.concat(BAMBOO_HR_NODE_TYPE);
break;
case PRODUCT_GOAL:
nodeTypes = nodeTypes.concat(JIRA_NODE_TYPE);
break;
case SALES_MARKETING_GOAL:
nodeTypes = nodeTypes.concat(GOOGLE_SHEETS_NODE_TYPE);
break;
case SECURITY_GOAL:
nodeTypes = nodeTypes.concat(ELASTIC_SECURITY_NODE_TYPE);
break;
default:
nodeTypes = nodeTypes.concat(SLACK_NODE_TYPE);
}
}
// slot 4
nodeTypes = nodeTypes.concat(SET_NODE_TYPE);
return nodeTypes;
}
function getPersonalizationV1(answers: IPersonalizationSurveyAnswersV1) {
const companySize = answers[COMPANY_SIZE_KEY];
const workArea = answers[WORK_AREA_KEY];
function isWorkAreaAnswer(name: string) {
if (Array.isArray(workArea)) {
return workArea.includes(name);
} else {
return workArea === name;
}
}
const workAreaIsEmpty = !workArea|| workArea.length === 0;
if (companySize === null && workAreaIsEmpty && answers[CODING_SKILL_KEY] === null) {
return [];
}
let codingSkill = null;
if (answers[CODING_SKILL_KEY]) {
codingSkill = parseInt(answers[CODING_SKILL_KEY] as string, 10);
codingSkill = isNaN(codingSkill)? 0 : codingSkill;
}
let nodeTypes = [] as string[];
if (isWorkAreaAnswer(IT_ENGINEERING_WORK_AREA)) {
nodeTypes = nodeTypes.concat(WEBHOOK_NODE_TYPE);
}
else {
nodeTypes = nodeTypes.concat(CRON_NODE_TYPE);
}
if (codingSkill !== null && codingSkill >= 4) {
nodeTypes = nodeTypes.concat(FUNCTION_NODE_TYPE);
}
else {
nodeTypes = nodeTypes.concat(ITEM_LISTS_NODE_TYPE);
}
if (codingSkill !== null && codingSkill < 3) {
nodeTypes = nodeTypes.concat(IF_NODE_TYPE);
}
else {
nodeTypes = nodeTypes.concat(SWITCH_NODE_TYPE);
}
if (companySize === COMPANY_SIZE_500_999 || companySize === COMPANY_SIZE_1000_OR_MORE) {
if (isWorkAreaAnswer(SALES_BUSINESSDEV_WORK_AREA)) {
nodeTypes = nodeTypes.concat(SALESFORCE_NODE_TYPE);
}
else if (isWorkAreaAnswer(SECURITY_WORK_AREA)) {
nodeTypes = nodeTypes.concat([ELASTIC_SECURITY_NODE_TYPE, HTTP_REQUEST_NODE_TYPE]);
}
else if (isWorkAreaAnswer(PRODUCT_WORK_AREA)) {
nodeTypes = nodeTypes.concat([JIRA_TRIGGER_NODE_TYPE, SEGMENT_NODE_TYPE]);
}
else if (isWorkAreaAnswer(IT_ENGINEERING_WORK_AREA)) {
nodeTypes = nodeTypes.concat([GITHUB_TRIGGER_NODE_TYPE, HTTP_REQUEST_NODE_TYPE]);
}
else {
nodeTypes = nodeTypes.concat([MICROSOFT_EXCEL_NODE_TYPE, MICROSOFT_TEAMS_NODE_TYPE]);
}
}
else {
if (isWorkAreaAnswer(SALES_BUSINESSDEV_WORK_AREA)) {
nodeTypes = nodeTypes.concat(CLEARBIT_NODE_TYPE);
}
else if (isWorkAreaAnswer(SECURITY_WORK_AREA)) {
nodeTypes = nodeTypes.concat([PAGERDUTY_NODE_TYPE, HTTP_REQUEST_NODE_TYPE]);
}
else if (isWorkAreaAnswer(PRODUCT_WORK_AREA)) {
nodeTypes = nodeTypes.concat([JIRA_TRIGGER_NODE_TYPE, CALENDLY_TRIGGER_NODE_TYPE]);
}
else if (isWorkAreaAnswer(IT_ENGINEERING_WORK_AREA)) {
nodeTypes = nodeTypes.concat([EXECUTE_COMMAND_NODE_TYPE, HTTP_REQUEST_NODE_TYPE]);
}
else if (isWorkAreaAnswer(FINANCE_WORK_AREA)) {
nodeTypes = nodeTypes.concat([XERO_NODE_TYPE, QUICKBOOKS_NODE_TYPE, SPREADSHEET_FILE_NODE_TYPE]);
}
else {
nodeTypes = nodeTypes.concat([EMAIL_SEND_NODE_TYPE, SLACK_NODE_TYPE]);
}
}
nodeTypes = nodeTypes.concat(SET_NODE_TYPE);
return nodeTypes;
}