@fonoster/identity 0.7.4 → 0.7.7
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.
- package/README.md +163 -2
- package/dist/apikeys/createApiKey.d.ts +3 -3
- package/dist/apikeys/createApiKey.js +24 -36
- package/dist/apikeys/deleteApiKey.d.ts +2 -3
- package/dist/apikeys/deleteApiKey.js +11 -21
- package/dist/apikeys/listApiKeys.d.ts +2 -3
- package/dist/apikeys/listApiKeys.js +21 -1
- package/dist/apikeys/regenerateApiKey.d.ts +2 -3
- package/dist/apikeys/regenerateApiKey.js +18 -26
- package/dist/db.d.ts +1 -1
- package/dist/errors.d.ts +1 -1
- package/dist/errors.js +1 -1
- package/dist/exchanges/exchangeApiKey.d.ts +2 -19
- package/dist/exchanges/exchangeApiKey.js +12 -22
- package/dist/exchanges/exchangeCredentials.d.ts +2 -19
- package/dist/exchanges/exchangeCredentials.js +12 -22
- package/dist/exchanges/exchangeRefreshToken.d.ts +2 -16
- package/dist/exchanges/exchangeRefreshToken.js +11 -19
- package/dist/exchanges/index.d.ts +1 -1
- package/dist/exchanges/index.js +1 -1
- package/dist/exchanges/payloads/users/index.d.ts +1 -1
- package/dist/exchanges/payloads/users/index.js +1 -1
- package/dist/exchanges/types.d.ts +11 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +4 -4
- package/dist/invites/index.d.ts +1 -1
- package/dist/invites/index.js +1 -1
- package/dist/invites/sendInvite.d.ts +1 -1
- package/dist/roles.d.ts +1 -1
- package/dist/roles.js +1 -1
- package/dist/service.d.ts +37 -57
- package/dist/users/createUser.d.ts +2 -3
- package/dist/users/createUser.js +16 -28
- package/dist/users/deleteUser.d.ts +3 -3
- package/dist/users/deleteUser.js +14 -20
- package/dist/users/getUser.d.ts +2 -3
- package/dist/users/getUser.js +20 -23
- package/dist/users/index.d.ts +1 -1
- package/dist/users/index.js +1 -1
- package/dist/users/updateUser.d.ts +2 -3
- package/dist/users/updateUser.js +23 -34
- package/dist/users/upsertDefaultUser.d.ts +2 -2
- package/dist/users/upsertDefaultUser.js +2 -2
- package/dist/utils/generateAccessKeyId.d.ts +1 -1
- package/dist/utils/getApiKeyByAccessKeyId.d.ts +3 -3
- package/dist/utils/getUserByEmail.d.ts +2 -2
- package/dist/utils/index.d.ts +5 -5
- package/dist/utils/index.js +5 -5
- package/dist/utils/withAccess.js +10 -34
- package/dist/workspaces/createWorkspace.d.ts +2 -3
- package/dist/workspaces/createWorkspace.js +17 -25
- package/dist/workspaces/deleteWorkspace.d.ts +2 -3
- package/dist/workspaces/deleteWorkspace.js +14 -17
- package/dist/workspaces/getWorkspace.d.ts +2 -3
- package/dist/workspaces/getWorkspace.js +20 -23
- package/dist/workspaces/index.d.ts +2 -2
- package/dist/workspaces/index.js +2 -2
- package/dist/workspaces/inviteUserToWorkspace.d.ts +2 -3
- package/dist/workspaces/inviteUserToWorkspace.js +57 -70
- package/dist/workspaces/listWorkspaces.d.ts +1 -2
- package/dist/workspaces/listWorkspaces.js +21 -1
- package/dist/workspaces/removeUserFromWorkspace.d.ts +2 -3
- package/dist/workspaces/removeUserFromWorkspace.js +34 -37
- package/dist/workspaces/resendWorkspaceMembershipInvitation.d.ts +2 -3
- package/dist/workspaces/resendWorkspaceMembershipInvitation.js +47 -50
- package/dist/workspaces/updateWorkspace.d.ts +2 -3
- package/dist/workspaces/updateWorkspace.js +21 -30
- package/package.json +5 -5
|
@@ -3,10 +3,10 @@ declare function getApiKeyByAccessKeyId(prisma: Prisma): (accessKeyId: string) =
|
|
|
3
3
|
ref: string;
|
|
4
4
|
createdAt: Date;
|
|
5
5
|
updatedAt: Date;
|
|
6
|
-
accessKeyId: string;
|
|
7
6
|
role: "WORKSPACE_ADMIN";
|
|
8
|
-
workspaceRef: string;
|
|
9
|
-
accessKeySecret: string;
|
|
10
7
|
expiresAt: Date;
|
|
8
|
+
accessKeyId: string;
|
|
9
|
+
accessKeySecret: string;
|
|
10
|
+
workspaceRef: string;
|
|
11
11
|
}>;
|
|
12
12
|
export { getApiKeyByAccessKeyId };
|
|
@@ -6,10 +6,10 @@ declare function getUserByEmail(prisma: Prisma): (email: string) => Promise<{
|
|
|
6
6
|
createdAt: Date;
|
|
7
7
|
updatedAt: Date;
|
|
8
8
|
accessKeyId: string;
|
|
9
|
-
emailVerified: boolean;
|
|
10
9
|
password: string;
|
|
10
|
+
avatar: string;
|
|
11
|
+
emailVerified: boolean;
|
|
11
12
|
phoneNumber: string;
|
|
12
13
|
phoneNumberVerified: boolean;
|
|
13
|
-
avatar: string;
|
|
14
14
|
}>;
|
|
15
15
|
export { getUserByEmail };
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export * from "./
|
|
1
|
+
export * from "./createCallAccessToken";
|
|
2
2
|
export * from "./decodeToken";
|
|
3
|
-
export * from "./
|
|
3
|
+
export * from "./generateAccessKeyId";
|
|
4
4
|
export * from "./getAccessKeyIdFromCall";
|
|
5
|
+
export * from "./getAccessKeyIdFromToken";
|
|
6
|
+
export * from "./getTokenFromCall";
|
|
5
7
|
export * from "./hasAccess";
|
|
8
|
+
export * from "./hasAccessToResource";
|
|
6
9
|
export * from "./isValidToken";
|
|
7
|
-
export * from "./getTokenFromCall";
|
|
8
10
|
export * from "./tokenHasAccessKeyId";
|
|
9
|
-
export * from "./hasAccessToResource";
|
|
10
11
|
export * from "./withAccess";
|
|
11
|
-
export * from "./createCallAccessToken";
|
package/dist/utils/index.js
CHANGED
|
@@ -14,6 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./createCallAccessToken"), exports);
|
|
18
|
+
__exportStar(require("./decodeToken"), exports);
|
|
17
19
|
/*
|
|
18
20
|
* Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
|
|
19
21
|
* http://github.com/fonoster/fonoster
|
|
@@ -33,13 +35,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
33
35
|
* limitations under the License.
|
|
34
36
|
*/
|
|
35
37
|
__exportStar(require("./generateAccessKeyId"), exports);
|
|
36
|
-
__exportStar(require("./decodeToken"), exports);
|
|
37
|
-
__exportStar(require("./getAccessKeyIdFromToken"), exports);
|
|
38
38
|
__exportStar(require("./getAccessKeyIdFromCall"), exports);
|
|
39
|
+
__exportStar(require("./getAccessKeyIdFromToken"), exports);
|
|
40
|
+
__exportStar(require("./getTokenFromCall"), exports);
|
|
39
41
|
__exportStar(require("./hasAccess"), exports);
|
|
42
|
+
__exportStar(require("./hasAccessToResource"), exports);
|
|
40
43
|
__exportStar(require("./isValidToken"), exports);
|
|
41
|
-
__exportStar(require("./getTokenFromCall"), exports);
|
|
42
44
|
__exportStar(require("./tokenHasAccessKeyId"), exports);
|
|
43
|
-
__exportStar(require("./hasAccessToResource"), exports);
|
|
44
45
|
__exportStar(require("./withAccess"), exports);
|
|
45
|
-
__exportStar(require("./createCallAccessToken"), exports);
|
package/dist/utils/withAccess.js
CHANGED
|
@@ -33,44 +33,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
33
33
|
};
|
|
34
34
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
35
|
exports.withAccess = withAccess;
|
|
36
|
-
/*
|
|
37
|
-
* Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
|
|
38
|
-
* http://github.com/fonoster/fonoster
|
|
39
|
-
*
|
|
40
|
-
* This file is part of Fonoster
|
|
41
|
-
*
|
|
42
|
-
* Licensed under the MIT License (the "License");
|
|
43
|
-
* you may not use this file except in compliance with
|
|
44
|
-
* the License. You may obtain a copy of the License at
|
|
45
|
-
*
|
|
46
|
-
* https://opensource.org/licenses/MIT
|
|
47
|
-
*
|
|
48
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
49
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
50
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
51
|
-
* See the License for the specific language governing permissions and
|
|
52
|
-
* limitations under the License.
|
|
53
|
-
*/
|
|
54
|
-
const common_1 = require("@fonoster/common");
|
|
55
36
|
const grpc = __importStar(require("@grpc/grpc-js"));
|
|
56
37
|
const hasAccessToResource_1 = require("./hasAccessToResource");
|
|
57
38
|
function withAccess(handler, getFn) {
|
|
58
39
|
return (call, callback) => __awaiter(this, void 0, void 0, function* () {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
const response = yield handler(call);
|
|
70
|
-
callback(null, response);
|
|
71
|
-
}
|
|
72
|
-
catch (error) {
|
|
73
|
-
(0, common_1.handleError)(error, callback);
|
|
40
|
+
const typedCall = call;
|
|
41
|
+
const hasAccess = yield (0, hasAccessToResource_1.hasAccessToResource)(typedCall, getFn);
|
|
42
|
+
if (!hasAccess) {
|
|
43
|
+
callback({
|
|
44
|
+
code: grpc.status.PERMISSION_DENIED,
|
|
45
|
+
message: "You don't have permission to access this resource"
|
|
46
|
+
});
|
|
47
|
+
return;
|
|
74
48
|
}
|
|
49
|
+
const response = yield handler(call);
|
|
50
|
+
callback(null, response);
|
|
75
51
|
});
|
|
76
52
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { GrpcErrorMessage } from "@fonoster/common";
|
|
2
|
-
import { BaseApiObject, CreateWorkspaceRequest } from "@fonoster/types";
|
|
3
2
|
import { Prisma } from "../db";
|
|
4
3
|
declare function createWorkspace(prisma: Prisma): (call: {
|
|
5
|
-
request:
|
|
6
|
-
}, callback: (error
|
|
4
|
+
request: unknown;
|
|
5
|
+
}, callback: (error?: import("@fonoster/common").GrpcErrorMessage, response?: unknown) => void) => Promise<void>;
|
|
7
6
|
export { createWorkspace };
|
|
@@ -30,35 +30,27 @@ exports.createWorkspace = createWorkspace;
|
|
|
30
30
|
*/
|
|
31
31
|
const common_1 = require("@fonoster/common");
|
|
32
32
|
const logger_1 = require("@fonoster/logger");
|
|
33
|
-
const zod_1 = require("zod");
|
|
34
33
|
const generateAccessKeyId_1 = require("../utils/generateAccessKeyId");
|
|
35
34
|
const getTokenFromCall_1 = require("../utils/getTokenFromCall");
|
|
36
35
|
const getUserRefFromToken_1 = require("../utils/getUserRefFromToken");
|
|
37
36
|
const logger = (0, logger_1.getLogger)({ service: "identity", filePath: __filename });
|
|
38
|
-
const CreateWorkspaceRequestSchema = zod_1.z.object({
|
|
39
|
-
name: zod_1.z.string().min(3, "Name must contain at least 3 characters").max(50)
|
|
40
|
-
});
|
|
41
37
|
function createWorkspace(prisma) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
catch (error) {
|
|
61
|
-
(0, common_1.handleError)(error, callback);
|
|
62
|
-
}
|
|
38
|
+
const fn = (call, callback) => __awaiter(this, void 0, void 0, function* () {
|
|
39
|
+
const { request } = call;
|
|
40
|
+
const { name } = request;
|
|
41
|
+
const token = (0, getTokenFromCall_1.getTokenFromCall)(call);
|
|
42
|
+
const ownerRef = (0, getUserRefFromToken_1.getUserRefFromToken)(token);
|
|
43
|
+
const accessKeyId = (0, generateAccessKeyId_1.generateAccessKeyId)(generateAccessKeyId_1.AccessKeyIdType.WORKSPACE);
|
|
44
|
+
logger.verbose("call to createWorkspace", { name, ownerRef });
|
|
45
|
+
const workspace = yield prisma.workspace.create({
|
|
46
|
+
data: {
|
|
47
|
+
name,
|
|
48
|
+
accessKeyId,
|
|
49
|
+
ownerRef
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
const { ref } = workspace;
|
|
53
|
+
callback(null, { ref });
|
|
63
54
|
});
|
|
55
|
+
return (0, common_1.withErrorHandlingAndValidation)(fn, common_1.Validators.createWorkspaceRequestSchema);
|
|
64
56
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { GrpcErrorMessage } from "@fonoster/common";
|
|
2
|
-
import { BaseApiObject } from "@fonoster/types";
|
|
3
2
|
import { Prisma } from "../db";
|
|
4
3
|
declare function deleteWorkspace(prisma: Prisma): (call: {
|
|
5
|
-
request:
|
|
6
|
-
}, callback: (error
|
|
4
|
+
request: unknown;
|
|
5
|
+
}, callback: (error?: import("@fonoster/common").GrpcErrorMessage, response?: unknown) => void) => Promise<void>;
|
|
7
6
|
export { deleteWorkspace };
|
|
@@ -34,22 +34,19 @@ const getTokenFromCall_1 = require("../utils/getTokenFromCall");
|
|
|
34
34
|
const getUserRefFromToken_1 = require("../utils/getUserRefFromToken");
|
|
35
35
|
const logger = (0, logger_1.getLogger)({ service: "identity", filePath: __filename });
|
|
36
36
|
function deleteWorkspace(prisma) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
catch (error) {
|
|
52
|
-
(0, common_1.handleError)(error, callback);
|
|
53
|
-
}
|
|
37
|
+
const fn = (call, callback) => __awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
const { request } = call;
|
|
39
|
+
const { ref } = request;
|
|
40
|
+
const token = (0, getTokenFromCall_1.getTokenFromCall)(call);
|
|
41
|
+
const ownerRef = (0, getUserRefFromToken_1.getUserRefFromToken)(token);
|
|
42
|
+
logger.verbose("deleting workspace from the system", { ref, ownerRef });
|
|
43
|
+
yield prisma.workspace.delete({
|
|
44
|
+
where: {
|
|
45
|
+
ref,
|
|
46
|
+
ownerRef
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
callback(null, { ref });
|
|
54
50
|
});
|
|
51
|
+
return (0, common_1.withErrorHandlingAndValidation)(fn, common_1.Validators.baseApiObjectSchema);
|
|
55
52
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { GrpcErrorMessage } from "@fonoster/common";
|
|
2
|
-
import { BaseApiObject, Workspace } from "@fonoster/types";
|
|
3
2
|
import { Prisma } from "../db";
|
|
4
3
|
declare function getWorkspace(prisma: Prisma): (call: {
|
|
5
|
-
request:
|
|
6
|
-
}, callback: (error
|
|
4
|
+
request: unknown;
|
|
5
|
+
}, callback: (error?: import("@fonoster/common").GrpcErrorMessage, response?: unknown) => void) => Promise<void>;
|
|
7
6
|
export { getWorkspace };
|
|
@@ -35,30 +35,27 @@ const getTokenFromCall_1 = require("../utils/getTokenFromCall");
|
|
|
35
35
|
const getUserRefFromToken_1 = require("../utils/getUserRefFromToken");
|
|
36
36
|
const logger = (0, logger_1.getLogger)({ service: "identity", filePath: __filename });
|
|
37
37
|
function getWorkspace(prisma) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
if (!workspace) {
|
|
51
|
-
callback({
|
|
52
|
-
code: grpc_js_1.status.NOT_FOUND,
|
|
53
|
-
message: "Workspace not found"
|
|
54
|
-
});
|
|
55
|
-
return;
|
|
38
|
+
const fn = (call, callback) => __awaiter(this, void 0, void 0, function* () {
|
|
39
|
+
const { request } = call;
|
|
40
|
+
const { ref } = request;
|
|
41
|
+
const token = (0, getTokenFromCall_1.getTokenFromCall)(call);
|
|
42
|
+
const ownerRef = (0, getUserRefFromToken_1.getUserRefFromToken)(token);
|
|
43
|
+
logger.verbose("getting workspace by id", { ref, ownerRef });
|
|
44
|
+
const workspace = yield prisma.workspace.findUnique({
|
|
45
|
+
where: {
|
|
46
|
+
ref,
|
|
47
|
+
ownerRef
|
|
56
48
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
49
|
+
});
|
|
50
|
+
if (!workspace) {
|
|
51
|
+
callback({
|
|
52
|
+
code: grpc_js_1.status.NOT_FOUND,
|
|
53
|
+
message: "Workspace not found"
|
|
54
|
+
});
|
|
55
|
+
return;
|
|
62
56
|
}
|
|
57
|
+
const response = (0, common_1.datesMapper)(workspace);
|
|
58
|
+
callback(null, response);
|
|
63
59
|
});
|
|
60
|
+
return (0, common_1.withErrorHandlingAndValidation)(fn, common_1.Validators.baseApiObjectSchema);
|
|
64
61
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export * from "./createWorkspace";
|
|
2
2
|
export * from "./deleteWorkspace";
|
|
3
3
|
export * from "./getWorkspace";
|
|
4
|
-
export * from "./updateWorkspace";
|
|
5
|
-
export * from "./listWorkspaces";
|
|
6
4
|
export * from "./inviteUserToWorkspace";
|
|
5
|
+
export * from "./listWorkspaces";
|
|
7
6
|
export * from "./removeUserFromWorkspace";
|
|
8
7
|
export * from "./resendWorkspaceMembershipInvitation";
|
|
8
|
+
export * from "./updateWorkspace";
|
package/dist/workspaces/index.js
CHANGED
|
@@ -35,8 +35,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
35
35
|
__exportStar(require("./createWorkspace"), exports);
|
|
36
36
|
__exportStar(require("./deleteWorkspace"), exports);
|
|
37
37
|
__exportStar(require("./getWorkspace"), exports);
|
|
38
|
-
__exportStar(require("./updateWorkspace"), exports);
|
|
39
|
-
__exportStar(require("./listWorkspaces"), exports);
|
|
40
38
|
__exportStar(require("./inviteUserToWorkspace"), exports);
|
|
39
|
+
__exportStar(require("./listWorkspaces"), exports);
|
|
41
40
|
__exportStar(require("./removeUserFromWorkspace"), exports);
|
|
42
41
|
__exportStar(require("./resendWorkspaceMembershipInvitation"), exports);
|
|
42
|
+
__exportStar(require("./updateWorkspace"), exports);
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { GrpcErrorMessage } from "@fonoster/common";
|
|
2
|
-
import { InviteUserToWorkspaceRequest, InviteUserToWorkspaceResponse } from "@fonoster/types";
|
|
3
2
|
import { Prisma } from "../db";
|
|
4
3
|
import { IdentityConfig } from "../exchanges/types";
|
|
5
4
|
import { SendInvite } from "../invites/sendInvite";
|
|
6
5
|
declare function inviteUserToWorkspace(prisma: Prisma, identityConfig: IdentityConfig, sendInvite: SendInvite): (call: {
|
|
7
|
-
request:
|
|
8
|
-
}, callback: (error
|
|
6
|
+
request: unknown;
|
|
7
|
+
}, callback: (error?: import("@fonoster/common").GrpcErrorMessage, response?: unknown) => void) => Promise<void>;
|
|
9
8
|
export { inviteUserToWorkspace };
|
|
@@ -33,7 +33,6 @@ const logger_1 = require("@fonoster/logger");
|
|
|
33
33
|
const types_1 = require("@fonoster/types");
|
|
34
34
|
const grpc_js_1 = require("@grpc/grpc-js");
|
|
35
35
|
const nanoid_1 = require("nanoid");
|
|
36
|
-
const zod_1 = require("zod");
|
|
37
36
|
const createSendEmail_1 = require("./createSendEmail");
|
|
38
37
|
const isAdminMember_1 = require("./isAdminMember");
|
|
39
38
|
const isWorkspaceMember_1 = require("./isWorkspaceMember");
|
|
@@ -41,15 +40,6 @@ const utils_1 = require("../utils");
|
|
|
41
40
|
const getTokenFromCall_1 = require("../utils/getTokenFromCall");
|
|
42
41
|
const getUserRefFromToken_1 = require("../utils/getUserRefFromToken");
|
|
43
42
|
const logger = (0, logger_1.getLogger)({ service: "identity", filePath: __filename });
|
|
44
|
-
const InviteUserToWorkspaceRequestSchema = zod_1.z.object({
|
|
45
|
-
email: zod_1.z.string().email(),
|
|
46
|
-
name: zod_1.z.string().min(3, "Name must contain at least 3 characters").max(50),
|
|
47
|
-
role: zod_1.z.enum([types_1.WorkspaceRoleEnum.ADMIN, types_1.WorkspaceRoleEnum.USER]),
|
|
48
|
-
password: zod_1.z
|
|
49
|
-
.string()
|
|
50
|
-
.min(6, "Password must contain at least 8 characters")
|
|
51
|
-
.or(zod_1.z.undefined())
|
|
52
|
-
});
|
|
53
43
|
const userIsMemberError = {
|
|
54
44
|
code: grpc_js_1.status.ALREADY_EXISTS,
|
|
55
45
|
message: "User is already a member of this workspace"
|
|
@@ -79,69 +69,66 @@ const createUser = (prisma) => {
|
|
|
79
69
|
});
|
|
80
70
|
};
|
|
81
71
|
function inviteUserToWorkspace(prisma, identityConfig, sendInvite) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
72
|
+
const fn = (call, callback) => __awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
const token = (0, getTokenFromCall_1.getTokenFromCall)(call);
|
|
74
|
+
const adminRef = (0, getUserRefFromToken_1.getUserRefFromToken)(token);
|
|
75
|
+
const accessKeyId = (0, utils_1.getAccessKeyIdFromCall)(call);
|
|
76
|
+
const workspace = yield prisma.workspace.findUnique({
|
|
77
|
+
where: {
|
|
78
|
+
accessKeyId
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
const { ref: workspaceRef } = workspace;
|
|
82
|
+
const { request } = call;
|
|
83
|
+
const { email, name, role } = request;
|
|
84
|
+
logger.verbose("inviting user to workspace", {
|
|
85
|
+
workspaceRef,
|
|
86
|
+
email,
|
|
87
|
+
role
|
|
88
|
+
});
|
|
89
|
+
const isAdmin = yield (0, isAdminMember_1.isAdminMember)(prisma)(workspaceRef, adminRef);
|
|
90
|
+
if (!isAdmin) {
|
|
91
|
+
return callback(inviterIsNotAdminError);
|
|
92
|
+
}
|
|
93
|
+
let user = yield findUserByEmail(prisma, email);
|
|
94
|
+
const isMember = yield (0, isWorkspaceMember_1.isWorkspaceMember)(prisma)(workspaceRef, user === null || user === void 0 ? void 0 : user.ref);
|
|
95
|
+
if (isMember) {
|
|
96
|
+
return callback(userIsMemberError);
|
|
97
|
+
}
|
|
98
|
+
const oneTimePassword = (0, nanoid_1.customAlphabet)("1234567890abcdef", 10)();
|
|
99
|
+
let isExistingUser = true;
|
|
100
|
+
if (!user) {
|
|
101
|
+
isExistingUser = false;
|
|
102
|
+
user = yield createUser(prisma)({
|
|
103
|
+
name,
|
|
96
104
|
email,
|
|
105
|
+
password: oneTimePassword,
|
|
97
106
|
role
|
|
98
107
|
});
|
|
99
|
-
const isAdmin = yield (0, isAdminMember_1.isAdminMember)(prisma)(workspaceRef, adminRef);
|
|
100
|
-
if (!isAdmin) {
|
|
101
|
-
return callback(inviterIsNotAdminError);
|
|
102
|
-
}
|
|
103
|
-
let user = yield findUserByEmail(prisma, email);
|
|
104
|
-
const isMember = yield (0, isWorkspaceMember_1.isWorkspaceMember)(prisma)(workspaceRef, user === null || user === void 0 ? void 0 : user.ref);
|
|
105
|
-
if (isMember) {
|
|
106
|
-
return callback(userIsMemberError);
|
|
107
|
-
}
|
|
108
|
-
const oneTimePassword = (0, nanoid_1.customAlphabet)("1234567890abcdef", 10)();
|
|
109
|
-
let isExistingUser = true;
|
|
110
|
-
if (!user) {
|
|
111
|
-
isExistingUser = false;
|
|
112
|
-
user = yield createUser(prisma)({
|
|
113
|
-
name,
|
|
114
|
-
email,
|
|
115
|
-
password: oneTimePassword,
|
|
116
|
-
role
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
const newMember = yield prisma.workspaceMember.create({
|
|
120
|
-
data: {
|
|
121
|
-
userRef: user.ref,
|
|
122
|
-
workspaceRef,
|
|
123
|
-
role: role,
|
|
124
|
-
status: types_1.WorkspaceMemberStatus.PENDING
|
|
125
|
-
},
|
|
126
|
-
include: {
|
|
127
|
-
workspace: true
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
yield sendInvite((0, createSendEmail_1.createSendEmail)(identityConfig), {
|
|
131
|
-
recipient: email,
|
|
132
|
-
oneTimePassword,
|
|
133
|
-
workspaceName: newMember.workspace.name,
|
|
134
|
-
isExistingUser,
|
|
135
|
-
// TODO: Create inviteUrl with invite token
|
|
136
|
-
inviteUrl: "https://placehold.it?token=jwt"
|
|
137
|
-
});
|
|
138
|
-
callback(null, {
|
|
139
|
-
userRef: user === null || user === void 0 ? void 0 : user.ref,
|
|
140
|
-
workspaceRef
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
catch (error) {
|
|
144
|
-
(0, common_1.handleError)(error, callback);
|
|
145
108
|
}
|
|
109
|
+
const newMember = yield prisma.workspaceMember.create({
|
|
110
|
+
data: {
|
|
111
|
+
userRef: user.ref,
|
|
112
|
+
workspaceRef,
|
|
113
|
+
role: role,
|
|
114
|
+
status: types_1.WorkspaceMemberStatus.PENDING
|
|
115
|
+
},
|
|
116
|
+
include: {
|
|
117
|
+
workspace: true
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
yield sendInvite((0, createSendEmail_1.createSendEmail)(identityConfig), {
|
|
121
|
+
recipient: email,
|
|
122
|
+
oneTimePassword,
|
|
123
|
+
workspaceName: newMember.workspace.name,
|
|
124
|
+
isExistingUser,
|
|
125
|
+
// TODO: Create inviteUrl with invite token
|
|
126
|
+
inviteUrl: "https://placehold.it?token=jwt"
|
|
127
|
+
});
|
|
128
|
+
callback(null, {
|
|
129
|
+
userRef: user === null || user === void 0 ? void 0 : user.ref,
|
|
130
|
+
workspaceRef
|
|
131
|
+
});
|
|
146
132
|
});
|
|
133
|
+
return (0, common_1.withErrorHandlingAndValidation)(fn, common_1.Validators.inviteUserToWorkspaceRequestSchema);
|
|
147
134
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { GrpcErrorMessage } from "@fonoster/common";
|
|
2
|
-
import { ListWorkspacesResponse } from "@fonoster/types";
|
|
3
2
|
import { Prisma } from "../db";
|
|
4
3
|
declare function listWorkspaces(prisma: Prisma): (call: {
|
|
5
4
|
request: unknown;
|
|
6
|
-
}, callback: (error
|
|
5
|
+
}, callback: (error?: import("@fonoster/common").GrpcErrorMessage, response?: unknown) => void) => Promise<void>;
|
|
7
6
|
export { listWorkspaces };
|
|
@@ -10,13 +10,32 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.listWorkspaces = listWorkspaces;
|
|
13
|
+
/*
|
|
14
|
+
* Copyright (C) 2024 by Fonoster Inc (https://fonoster.com)
|
|
15
|
+
* http://github.com/fonoster/fonoster
|
|
16
|
+
*
|
|
17
|
+
* This file is part of Fonoster
|
|
18
|
+
*
|
|
19
|
+
* Licensed under the MIT License (the "License");
|
|
20
|
+
* you may not use this file except in compliance with
|
|
21
|
+
* the License. You may obtain a copy of the License at
|
|
22
|
+
*
|
|
23
|
+
* https://opensource.org/licenses/MIT
|
|
24
|
+
*
|
|
25
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
26
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
27
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
28
|
+
* See the License for the specific language governing permissions and
|
|
29
|
+
* limitations under the License.
|
|
30
|
+
*/
|
|
31
|
+
const common_1 = require("@fonoster/common");
|
|
13
32
|
const logger_1 = require("@fonoster/logger");
|
|
14
33
|
const utils_1 = require("../utils");
|
|
15
34
|
const getTokenFromCall_1 = require("../utils/getTokenFromCall");
|
|
16
35
|
const getUserRefFromToken_1 = require("../utils/getUserRefFromToken");
|
|
17
36
|
const logger = (0, logger_1.getLogger)({ service: "identity", filePath: __filename });
|
|
18
37
|
function listWorkspaces(prisma) {
|
|
19
|
-
|
|
38
|
+
const fn = (call, callback) => __awaiter(this, void 0, void 0, function* () {
|
|
20
39
|
var _a, _b;
|
|
21
40
|
const token = (0, getTokenFromCall_1.getTokenFromCall)(call);
|
|
22
41
|
const userRef = (0, getUserRefFromToken_1.getUserRefFromToken)(token);
|
|
@@ -52,4 +71,5 @@ function listWorkspaces(prisma) {
|
|
|
52
71
|
nextPageToken: (_b = items[items.length - 1]) === null || _b === void 0 ? void 0 : _b.ref
|
|
53
72
|
});
|
|
54
73
|
});
|
|
74
|
+
return (0, common_1.withErrorHandlingAndValidation)(fn, common_1.Validators.listRequestSchema);
|
|
55
75
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { GrpcErrorMessage } from "@fonoster/common";
|
|
2
|
-
import { RemoveUserFromWorkspaceRequest, RemoveUserFromWorkspaceResponse } from "@fonoster/types";
|
|
3
2
|
import { Prisma } from "../db";
|
|
4
3
|
declare function removeUserFromWorkspace(prisma: Prisma): (call: {
|
|
5
|
-
request:
|
|
6
|
-
}, callback: (error
|
|
4
|
+
request: unknown;
|
|
5
|
+
}, callback: (error?: import("@fonoster/common").GrpcErrorMessage, response?: unknown) => void) => Promise<void>;
|
|
7
6
|
export { removeUserFromWorkspace };
|
|
@@ -37,47 +37,44 @@ const getTokenFromCall_1 = require("../utils/getTokenFromCall");
|
|
|
37
37
|
const getUserRefFromToken_1 = require("../utils/getUserRefFromToken");
|
|
38
38
|
const logger = (0, logger_1.getLogger)({ service: "identity", filePath: __filename });
|
|
39
39
|
function removeUserFromWorkspace(prisma) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
const workspaceRef = workspace === null || workspace === void 0 ? void 0 : workspace.ref;
|
|
52
|
-
logger.verbose("removing user from workspace", { workspaceRef, userRef });
|
|
53
|
-
const isAdmin = yield (0, isAdminMember_1.isAdminMember)(prisma)(workspaceRef, adminRef);
|
|
54
|
-
if (!isAdmin && adminRef !== userRef) {
|
|
55
|
-
return callback({
|
|
56
|
-
code: grpc_js_1.status.PERMISSION_DENIED,
|
|
57
|
-
message: "Only admins or owners can remove users from a workspace"
|
|
58
|
-
});
|
|
40
|
+
const fn = (call, callback) => __awaiter(this, void 0, void 0, function* () {
|
|
41
|
+
const { request } = call;
|
|
42
|
+
const { userRef } = request;
|
|
43
|
+
const token = (0, getTokenFromCall_1.getTokenFromCall)(call);
|
|
44
|
+
const accessKeyId = (0, utils_1.getAccessKeyIdFromCall)(call);
|
|
45
|
+
const adminRef = (0, getUserRefFromToken_1.getUserRefFromToken)(token);
|
|
46
|
+
const workspace = yield prisma.workspace.findUnique({
|
|
47
|
+
where: {
|
|
48
|
+
accessKeyId
|
|
59
49
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
50
|
+
});
|
|
51
|
+
const { ref: workspaceRef } = workspace;
|
|
52
|
+
logger.verbose("removing user from workspace", { workspaceRef, userRef });
|
|
53
|
+
const isAdmin = yield (0, isAdminMember_1.isAdminMember)(prisma)(workspaceRef, adminRef);
|
|
54
|
+
if (!isAdmin && adminRef !== userRef) {
|
|
55
|
+
return callback({
|
|
56
|
+
code: grpc_js_1.status.PERMISSION_DENIED,
|
|
57
|
+
message: "Only admins or owners can remove users from a workspace"
|
|
65
58
|
});
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
59
|
+
}
|
|
60
|
+
const memberRef = yield prisma.workspaceMember.findFirst({
|
|
61
|
+
where: {
|
|
62
|
+
workspaceRef,
|
|
63
|
+
userRef
|
|
71
64
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
65
|
+
});
|
|
66
|
+
if (!memberRef) {
|
|
67
|
+
return callback({
|
|
68
|
+
code: grpc_js_1.status.NOT_FOUND,
|
|
69
|
+
message: "User not found in workspace"
|
|
76
70
|
});
|
|
77
|
-
callback(null, response);
|
|
78
|
-
}
|
|
79
|
-
catch (error) {
|
|
80
|
-
(0, common_1.handleError)(error, callback);
|
|
81
71
|
}
|
|
72
|
+
const response = yield prisma.workspaceMember.delete({
|
|
73
|
+
where: {
|
|
74
|
+
ref: memberRef === null || memberRef === void 0 ? void 0 : memberRef.ref
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
callback(null, response);
|
|
82
78
|
});
|
|
79
|
+
return (0, common_1.withErrorHandlingAndValidation)(fn, common_1.Validators.removeUserFromWorkspaceRequestSchema);
|
|
83
80
|
}
|