@extrahorizon/exh-cli 1.9.0-dev-85-4c62b0a → 1.9.0-dev-86-1fadbca
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -5,7 +5,6 @@ const fs = require("fs/promises");
|
|
|
5
5
|
const chalk = require("chalk");
|
|
6
6
|
const constants_1 = require("../../constants");
|
|
7
7
|
const util_1 = require("../../helpers/util");
|
|
8
|
-
const authRepository = require("../../repositories/auth");
|
|
9
8
|
const functionRepository = require("../../repositories/functions");
|
|
10
9
|
const taskConfig_1 = require("./taskConfig");
|
|
11
10
|
const util_2 = require("./util");
|
|
@@ -108,7 +107,7 @@ async function syncSingleTask(sdk, config) {
|
|
|
108
107
|
});
|
|
109
108
|
config.environment = {
|
|
110
109
|
...config.environment,
|
|
111
|
-
API_HOST:
|
|
110
|
+
API_HOST: process.env.API_HOST,
|
|
112
111
|
API_OAUTH_CONSUMER_KEY: process.env.API_OAUTH_CONSUMER_KEY,
|
|
113
112
|
API_OAUTH_CONSUMER_SECRET: process.env.API_OAUTH_CONSUMER_SECRET,
|
|
114
113
|
API_OAUTH_TOKEN: credentials.token,
|
|
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.syncFunctionUser = exports.zipFileFromDirectory = void 0;
|
|
4
4
|
const fs_1 = require("fs");
|
|
5
5
|
const os_1 = require("os");
|
|
6
|
-
const javascript_sdk_1 = require("@extrahorizon/javascript-sdk");
|
|
7
6
|
const archiver = require("archiver");
|
|
8
7
|
const chalk = require("chalk");
|
|
9
8
|
const uuid_1 = require("uuid");
|
|
@@ -38,13 +37,10 @@ async function syncFunctionUser(sdk, data) {
|
|
|
38
37
|
const roleName = `exh.tasks.${taskName}`;
|
|
39
38
|
const role = await syncRoleWithPermissions(sdk, taskName, roleName, targetPermissions);
|
|
40
39
|
let user = await userRepository.findUserByEmail(sdk, email);
|
|
40
|
+
console.group(chalk.white(`🔄 Syncing user: ${email}`));
|
|
41
41
|
if (!user) {
|
|
42
|
-
console.log(chalk.white('⚙️ Creating
|
|
43
|
-
|
|
44
|
-
if (!emailAvailable) {
|
|
45
|
-
throw new Error('❌ The user could not be created as the email address is already in use');
|
|
46
|
-
}
|
|
47
|
-
user = await sdk.users.createAccount({
|
|
42
|
+
console.log(chalk.white('⚙️ Creating the user...'));
|
|
43
|
+
user = await userRepository.createUser(sdk, {
|
|
48
44
|
firstName: `${taskName}`,
|
|
49
45
|
lastName: 'exh.tasks',
|
|
50
46
|
email,
|
|
@@ -52,11 +48,13 @@ async function syncFunctionUser(sdk, data) {
|
|
|
52
48
|
phoneNumber: '0000000000',
|
|
53
49
|
language: 'EN',
|
|
54
50
|
});
|
|
55
|
-
console.log(chalk.green('✅ Successfully created a user for task'));
|
|
56
51
|
await assignRoleToUser(sdk, user.id, role.id);
|
|
57
|
-
|
|
52
|
+
const oAuth1Tokens = await createOAuth1Tokens(sdk, email, password);
|
|
53
|
+
console.groupEnd();
|
|
54
|
+
console.log(chalk.green('✅ Successfully synced user'));
|
|
55
|
+
console.log('');
|
|
56
|
+
return oAuth1Tokens;
|
|
58
57
|
}
|
|
59
|
-
console.log(chalk.white('⚙️ Checking for the existing users credentials'));
|
|
60
58
|
const currentFunction = await functionRepository.findByName(sdk, taskName);
|
|
61
59
|
const hasExistingCredentials = (currentFunction?.environmentVariables?.API_HOST?.value &&
|
|
62
60
|
currentFunction?.environmentVariables?.API_OAUTH_TOKEN_SECRET?.value &&
|
|
@@ -66,11 +64,14 @@ async function syncFunctionUser(sdk, data) {
|
|
|
66
64
|
if (!hasExistingCredentials) {
|
|
67
65
|
throw new Error('❌ No credentials were found for the existing user');
|
|
68
66
|
}
|
|
67
|
+
console.log(chalk.white('⚙️ Reusing existing user credentials...'));
|
|
69
68
|
const userRole = user.roles.find(({ name }) => name === roleName);
|
|
70
69
|
if (!userRole) {
|
|
71
70
|
await assignRoleToUser(sdk, user.id, role.id);
|
|
72
71
|
}
|
|
73
|
-
console.
|
|
72
|
+
console.groupEnd();
|
|
73
|
+
console.log(chalk.green('✅ Successfully synced user'));
|
|
74
|
+
console.log('');
|
|
74
75
|
return {
|
|
75
76
|
token: currentFunction.environmentVariables.API_OAUTH_TOKEN.value,
|
|
76
77
|
tokenSecret: currentFunction.environmentVariables.API_OAUTH_TOKEN_SECRET.value,
|
|
@@ -78,38 +79,50 @@ async function syncFunctionUser(sdk, data) {
|
|
|
78
79
|
}
|
|
79
80
|
exports.syncFunctionUser = syncFunctionUser;
|
|
80
81
|
async function syncRoleWithPermissions(sdk, taskName, roleName, targetPermissions) {
|
|
81
|
-
console.
|
|
82
|
-
|
|
82
|
+
console.group(chalk.white(`🔄 Syncing role: ${roleName}`));
|
|
83
|
+
if (targetPermissions.length === 0) {
|
|
84
|
+
console.log(chalk.yellow('⚠️ The executionCredentials.permissions field has no permissions defined'));
|
|
85
|
+
}
|
|
86
|
+
let role = await userRepository.findGlobalRoleByName(sdk, roleName);
|
|
83
87
|
if (!role) {
|
|
84
|
-
console.log(chalk.white('⚙️
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
console.
|
|
88
|
+
console.log(chalk.white('⚙️ Creating the role...'));
|
|
89
|
+
const roleDescription = `A role created by the CLI for the execution of the task ${taskName}`;
|
|
90
|
+
role = await userRepository.createGlobalRole(sdk, roleName, roleDescription);
|
|
91
|
+
if (targetPermissions.length !== 0) {
|
|
92
|
+
await userRepository.addPermissionsToGlobalRole(sdk, roleName, targetPermissions);
|
|
93
|
+
}
|
|
94
|
+
console.log(chalk.white(`🔐 Permissions added: [${targetPermissions.join(', ')}]`));
|
|
95
|
+
console.groupEnd();
|
|
96
|
+
console.log(chalk.green('✅ Successfully synced role'));
|
|
97
|
+
console.log('');
|
|
92
98
|
return role;
|
|
93
99
|
}
|
|
100
|
+
console.log(chalk.white('⚙️ Updating the role...'));
|
|
94
101
|
const currentPermissions = role.permissions?.flatMap(permission => permission.name) || [];
|
|
95
102
|
const permissionsToAdd = targetPermissions.filter(targetPermission => !currentPermissions.includes(targetPermission));
|
|
96
103
|
const permissionsToRemove = currentPermissions.filter(currentPermission => !targetPermissions.includes(currentPermission));
|
|
97
104
|
if (permissionsToAdd.length > 0) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
console.log(chalk.green('✅ Successfully added missing permissions to the role'));
|
|
105
|
+
await userRepository.addPermissionsToGlobalRole(sdk, roleName, permissionsToAdd);
|
|
106
|
+
console.log(chalk.white(`🔐 Permissions added: [${permissionsToAdd.join(',')}]`));
|
|
101
107
|
}
|
|
102
108
|
if (permissionsToRemove.length > 0) {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
console.log(chalk.green('✅ Successfully removed excess permissions from the role'));
|
|
109
|
+
await userRepository.removePermissionsFromGlobalRole(sdk, roleName, permissionsToRemove);
|
|
110
|
+
console.log(chalk.white(`🔐 Permissions removed: [${permissionsToRemove.join(',')}]`));
|
|
106
111
|
}
|
|
112
|
+
console.groupEnd();
|
|
113
|
+
console.log(chalk.green('✅ Successfully synced role'));
|
|
114
|
+
console.log('');
|
|
107
115
|
return role;
|
|
108
116
|
}
|
|
109
117
|
async function assignRoleToUser(sdk, userId, roleId) {
|
|
110
|
-
console.log(chalk.white('⚙️ Assigning the role to the user'));
|
|
111
|
-
await
|
|
112
|
-
|
|
118
|
+
console.log(chalk.white('⚙️ Assigning the role to the user...'));
|
|
119
|
+
await userRepository.addGlobalRoleToUser(sdk, userId, roleId);
|
|
120
|
+
}
|
|
121
|
+
async function createOAuth1Tokens(sdk, email, password) {
|
|
122
|
+
console.log(chalk.white('⚙️ Creating credentials...'));
|
|
123
|
+
const response = await authRepository.createOAuth1Tokens(sdk, email, password);
|
|
124
|
+
const { token, tokenSecret } = response;
|
|
125
|
+
return { token, tokenSecret };
|
|
113
126
|
}
|
|
114
127
|
function validateEmail(email) {
|
|
115
128
|
const emailRegex = /.+@.+\..+/;
|
|
@@ -117,10 +130,3 @@ function validateEmail(email) {
|
|
|
117
130
|
throw new Error('Invalid email address');
|
|
118
131
|
}
|
|
119
132
|
}
|
|
120
|
-
async function createOAuth1Tokens(sdk, email, password) {
|
|
121
|
-
console.log(chalk.white('⚙️ Creating OAuth1 tokens for the user', email));
|
|
122
|
-
const response = await authRepository.createOAuth1Tokens(sdk, email, password);
|
|
123
|
-
const { token, tokenSecret } = response.data;
|
|
124
|
-
console.log(chalk.green('✅ Successfully created OAuth1 tokens for the user', email));
|
|
125
|
-
return { token, tokenSecret };
|
|
126
|
-
}
|
|
@@ -7,7 +7,11 @@ async function find(sdk) {
|
|
|
7
7
|
}
|
|
8
8
|
exports.find = find;
|
|
9
9
|
async function findByName(sdk, name) {
|
|
10
|
-
const response = await sdk.raw.get(`/tasks/v1/functions/${name}`, { customResponseKeys: ['*'] })
|
|
10
|
+
const response = await sdk.raw.get(`/tasks/v1/functions/${name}`, { customResponseKeys: ['*'] })
|
|
11
|
+
.catch(e => e);
|
|
12
|
+
if (response.status === 404) {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
11
15
|
return response.data;
|
|
12
16
|
}
|
|
13
17
|
exports.findByName = findByName;
|
|
@@ -1,2 +1,9 @@
|
|
|
1
|
-
import { OAuth1Client } from '@extrahorizon/javascript-sdk';
|
|
1
|
+
import { OAuth1Client, RegisterUserData } from '@extrahorizon/javascript-sdk';
|
|
2
2
|
export declare function findUserByEmail(sdk: OAuth1Client, email: string): Promise<import("@extrahorizon/javascript-sdk").UserData>;
|
|
3
|
+
export declare function isEmailAvailable(sdk: OAuth1Client, email: string): Promise<boolean>;
|
|
4
|
+
export declare function createUser(sdk: OAuth1Client, data: RegisterUserData): Promise<import("@extrahorizon/javascript-sdk").UserData>;
|
|
5
|
+
export declare function findGlobalRoleByName(sdk: OAuth1Client, name: string): Promise<import("@extrahorizon/javascript-sdk").Role>;
|
|
6
|
+
export declare function createGlobalRole(sdk: OAuth1Client, name: string, description: string): Promise<import("@extrahorizon/javascript-sdk").Role>;
|
|
7
|
+
export declare function addPermissionsToGlobalRole(sdk: OAuth1Client, name: string, permissions: string[]): Promise<import("@extrahorizon/javascript-sdk").AffectedRecords>;
|
|
8
|
+
export declare function removePermissionsFromGlobalRole(sdk: OAuth1Client, name: string, permissions: string[]): Promise<import("@extrahorizon/javascript-sdk").AffectedRecords>;
|
|
9
|
+
export declare function addGlobalRoleToUser(sdk: OAuth1Client, userId: string, roleId: string): Promise<import("@extrahorizon/javascript-sdk").AffectedRecords>;
|
|
@@ -1,9 +1,38 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.findUserByEmail = void 0;
|
|
3
|
+
exports.addGlobalRoleToUser = exports.removePermissionsFromGlobalRole = exports.addPermissionsToGlobalRole = exports.createGlobalRole = exports.findGlobalRoleByName = exports.createUser = exports.isEmailAvailable = exports.findUserByEmail = void 0;
|
|
4
4
|
const javascript_sdk_1 = require("@extrahorizon/javascript-sdk");
|
|
5
5
|
async function findUserByEmail(sdk, email) {
|
|
6
6
|
const rql = (0, javascript_sdk_1.rqlBuilder)().eq('email', email).build();
|
|
7
7
|
return await sdk.users.findFirst({ rql });
|
|
8
8
|
}
|
|
9
9
|
exports.findUserByEmail = findUserByEmail;
|
|
10
|
+
async function isEmailAvailable(sdk, email) {
|
|
11
|
+
const { emailAvailable } = await sdk.users.isEmailAvailable(email);
|
|
12
|
+
return emailAvailable;
|
|
13
|
+
}
|
|
14
|
+
exports.isEmailAvailable = isEmailAvailable;
|
|
15
|
+
async function createUser(sdk, data) {
|
|
16
|
+
return await sdk.users.createAccount(data);
|
|
17
|
+
}
|
|
18
|
+
exports.createUser = createUser;
|
|
19
|
+
async function findGlobalRoleByName(sdk, name) {
|
|
20
|
+
return await sdk.users.globalRoles.findByName(name);
|
|
21
|
+
}
|
|
22
|
+
exports.findGlobalRoleByName = findGlobalRoleByName;
|
|
23
|
+
async function createGlobalRole(sdk, name, description) {
|
|
24
|
+
return await sdk.users.globalRoles.create({ name, description });
|
|
25
|
+
}
|
|
26
|
+
exports.createGlobalRole = createGlobalRole;
|
|
27
|
+
async function addPermissionsToGlobalRole(sdk, name, permissions) {
|
|
28
|
+
return await sdk.users.globalRoles.addPermissions((0, javascript_sdk_1.rqlBuilder)().eq('name', name).build(), { permissions });
|
|
29
|
+
}
|
|
30
|
+
exports.addPermissionsToGlobalRole = addPermissionsToGlobalRole;
|
|
31
|
+
async function removePermissionsFromGlobalRole(sdk, name, permissions) {
|
|
32
|
+
return await sdk.users.globalRoles.removePermissions((0, javascript_sdk_1.rqlBuilder)().eq('name', name).build(), { permissions });
|
|
33
|
+
}
|
|
34
|
+
exports.removePermissionsFromGlobalRole = removePermissionsFromGlobalRole;
|
|
35
|
+
async function addGlobalRoleToUser(sdk, userId, roleId) {
|
|
36
|
+
return await sdk.users.globalRoles.addToUsers((0, javascript_sdk_1.rqlBuilder)().eq('id', userId).build(), { roles: [roleId] });
|
|
37
|
+
}
|
|
38
|
+
exports.addGlobalRoleToUser = addGlobalRoleToUser;
|