@extrahorizon/exh-cli 1.8.2-dev-75-af25fb7 → 1.8.2-feat-76-eba7a78
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.
|
@@ -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");
|
|
@@ -40,18 +39,19 @@ async function syncFunctionUser(sdk, data) {
|
|
|
40
39
|
let user = await userRepository.findUserByEmail(sdk, email);
|
|
41
40
|
if (!user) {
|
|
42
41
|
console.log(chalk.white('⚙️ Creating a user for the task'));
|
|
43
|
-
const
|
|
44
|
-
if (!
|
|
42
|
+
const isEmailAvailable = await userRepository.isEmailAvailable(sdk, email);
|
|
43
|
+
if (!isEmailAvailable) {
|
|
45
44
|
throw new Error('❌ The user could not be created as the email address is already in use');
|
|
46
45
|
}
|
|
47
|
-
|
|
46
|
+
const registerUserData = {
|
|
48
47
|
firstName: `${taskName}`,
|
|
49
48
|
lastName: 'exh.tasks',
|
|
50
49
|
email,
|
|
51
50
|
password,
|
|
52
51
|
phoneNumber: '0000000000',
|
|
53
52
|
language: 'EN',
|
|
54
|
-
}
|
|
53
|
+
};
|
|
54
|
+
user = await userRepository.createUser(sdk, registerUserData);
|
|
55
55
|
console.log(chalk.green('✅ Successfully created a user for task'));
|
|
56
56
|
await assignRoleToUser(sdk, user.id, role.id);
|
|
57
57
|
return await createOAuth1Tokens(sdk, email, password);
|
|
@@ -79,15 +79,13 @@ async function syncFunctionUser(sdk, data) {
|
|
|
79
79
|
exports.syncFunctionUser = syncFunctionUser;
|
|
80
80
|
async function syncRoleWithPermissions(sdk, taskName, roleName, targetPermissions) {
|
|
81
81
|
console.log(chalk.white('⚙️ Checking if the role exists'));
|
|
82
|
-
let role = await
|
|
82
|
+
let role = await userRepository.findGlobalRoleByName(sdk, roleName);
|
|
83
83
|
if (!role) {
|
|
84
84
|
console.log(chalk.white('⚙️ Role does not exist, creating a new role'));
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
description: `A role created by the CLI for the execution of the task ${taskName}`,
|
|
88
|
-
});
|
|
85
|
+
const roleDescription = `A role created by the CLI for the execution of the task ${taskName}`;
|
|
86
|
+
role = await userRepository.createGlobalRole(sdk, roleName, roleDescription);
|
|
89
87
|
console.log(chalk.white('⚙️ Assigning permissions to the role'));
|
|
90
|
-
await
|
|
88
|
+
await userRepository.addPermissionsToGlobalRole(sdk, roleName, targetPermissions);
|
|
91
89
|
console.log(chalk.green('✅ Successfully assigned permissions to the role'));
|
|
92
90
|
return role;
|
|
93
91
|
}
|
|
@@ -96,27 +94,21 @@ async function syncRoleWithPermissions(sdk, taskName, roleName, targetPermission
|
|
|
96
94
|
const permissionsToRemove = currentPermissions.filter(currentPermission => !targetPermissions.includes(currentPermission));
|
|
97
95
|
if (permissionsToAdd.length > 0) {
|
|
98
96
|
console.log(chalk.white('⚙️ Adding missing permissions to the role'));
|
|
99
|
-
await
|
|
97
|
+
await userRepository.addPermissionsToGlobalRole(sdk, roleName, permissionsToAdd);
|
|
100
98
|
console.log(chalk.green('✅ Successfully added missing permissions to the role'));
|
|
101
99
|
}
|
|
102
100
|
if (permissionsToRemove.length > 0) {
|
|
103
101
|
console.log(chalk.white('⚙️ Removing excess permissions from the role'));
|
|
104
|
-
await
|
|
102
|
+
await userRepository.removePermissionsFromGlobalRole(sdk, roleName, permissionsToRemove);
|
|
105
103
|
console.log(chalk.green('✅ Successfully removed excess permissions from the role'));
|
|
106
104
|
}
|
|
107
105
|
return role;
|
|
108
106
|
}
|
|
109
107
|
async function assignRoleToUser(sdk, userId, roleId) {
|
|
110
108
|
console.log(chalk.white('⚙️ Assigning the role to the user'));
|
|
111
|
-
await
|
|
109
|
+
await userRepository.addGlobalRoleToUser(sdk, userId, roleId);
|
|
112
110
|
console.log(chalk.green('✅ Successfully assigned the role to the user'));
|
|
113
111
|
}
|
|
114
|
-
function validateEmail(email) {
|
|
115
|
-
const emailRegex = /.+@.+\..+/;
|
|
116
|
-
if (email.length < 3 || email.length > 256 || !emailRegex.test(email)) {
|
|
117
|
-
throw new Error('Invalid email address');
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
112
|
async function createOAuth1Tokens(sdk, email, password) {
|
|
121
113
|
console.log(chalk.white('⚙️ Creating OAuth1 tokens for the user', email));
|
|
122
114
|
const response = await authRepository.createOAuth1Tokens(sdk, email, password);
|
|
@@ -124,3 +116,9 @@ async function createOAuth1Tokens(sdk, email, password) {
|
|
|
124
116
|
console.log(chalk.green('✅ Successfully created OAuth1 tokens for the user', email));
|
|
125
117
|
return { token, tokenSecret };
|
|
126
118
|
}
|
|
119
|
+
function validateEmail(email) {
|
|
120
|
+
const emailRegex = /.+@.+\..+/;
|
|
121
|
+
if (email.length < 3 || email.length > 256 || !emailRegex.test(email)) {
|
|
122
|
+
throw new Error('Invalid email address');
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -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;
|