@webiny/api-core 6.5.0 → 6.5.1-beta.0
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/exports/api/security.d.ts +2 -0
- package/exports/api/security.js +2 -0
- package/features/security/ResolveIdentityTenants/ResolveIdentityTenantsUseCase.d.ts +12 -0
- package/features/security/ResolveIdentityTenants/ResolveIdentityTenantsUseCase.js +46 -0
- package/features/security/ResolveIdentityTenants/ResolveIdentityTenantsUseCase.js.map +1 -0
- package/features/security/ResolveIdentityTenants/abstractions.d.ts +22 -0
- package/features/security/ResolveIdentityTenants/abstractions.js +5 -0
- package/features/security/ResolveIdentityTenants/abstractions.js.map +1 -0
- package/features/security/ResolveIdentityTenants/feature.d.ts +4 -0
- package/features/security/ResolveIdentityTenants/feature.js +11 -0
- package/features/security/ResolveIdentityTenants/feature.js.map +1 -0
- package/features/security/ResolveIdentityTenants/index.d.ts +1 -0
- package/features/security/ResolveIdentityTenants/index.js +1 -0
- package/features/security/SecurityFeature.js +4 -0
- package/features/security/SecurityFeature.js.map +1 -1
- package/features/security/login/LogInUseCase.d.ts +20 -0
- package/features/security/login/LogInUseCase.js +75 -0
- package/features/security/login/LogInUseCase.js.map +1 -0
- package/features/security/login/abstractions.d.ts +45 -0
- package/features/security/login/abstractions.js +2 -1
- package/features/security/login/abstractions.js.map +1 -1
- package/features/security/login/errors.d.ts +13 -0
- package/features/security/login/errors.js +25 -0
- package/features/security/login/errors.js.map +1 -0
- package/features/security/login/feature.d.ts +4 -0
- package/features/security/login/feature.js +11 -0
- package/features/security/login/feature.js.map +1 -0
- package/features/tenancy/TenancyFeature.js +1 -1
- package/features/tenancy/TenancyFeature.js.map +1 -1
- package/features/users/AdminUsersFeature.js +2 -0
- package/features/users/AdminUsersFeature.js.map +1 -1
- package/features/users/FindUserRecords/FindUserRecordsUseCase.d.ts +12 -0
- package/features/users/FindUserRecords/FindUserRecordsUseCase.js +54 -0
- package/features/users/FindUserRecords/FindUserRecordsUseCase.js.map +1 -0
- package/features/users/FindUserRecords/abstractions.d.ts +29 -0
- package/features/users/FindUserRecords/abstractions.js +5 -0
- package/features/users/FindUserRecords/abstractions.js.map +1 -0
- package/features/users/FindUserRecords/feature.d.ts +4 -0
- package/features/users/FindUserRecords/feature.js +11 -0
- package/features/users/FindUserRecords/feature.js.map +1 -0
- package/features/users/FindUserRecords/index.d.ts +1 -0
- package/features/users/FindUserRecords/index.js +1 -0
- package/features/users/shared/AdminUsersRepository.d.ts +3 -1
- package/features/users/shared/AdminUsersRepository.js +22 -7
- package/features/users/shared/AdminUsersRepository.js.map +1 -1
- package/features/users/shared/abstractions.d.ts +4 -1
- package/features/users/shared/abstractions.js.map +1 -1
- package/features/users/shared/types.d.ts +9 -0
- package/graphql/security/identity.gql.js +27 -22
- package/graphql/security/identity.gql.js.map +1 -1
- package/legacy/tenancy/createTenancyContext.js +8 -1
- package/legacy/tenancy/createTenancyContext.js.map +1 -1
- package/package.json +14 -14
- package/types/users.d.ts +17 -0
|
@@ -9,4 +9,6 @@ export { TeamFactory } from "../../features/security/teams/shared/abstractions.j
|
|
|
9
9
|
export { ApiToken } from "../../domain/security/ApiToken.js";
|
|
10
10
|
export { IdentityProvider, OidcIdentityProvider, JwtIdentityProvider } from "../../idp/index.js";
|
|
11
11
|
export { Authenticator } from "../../features/security/authentication/Authenticator/abstractions.js";
|
|
12
|
+
export { ResolveIdentityTenantsUseCase } from "../../features/security/ResolveIdentityTenants/index.js";
|
|
13
|
+
export { LogInUseCase } from "../../features/security/login/index.js";
|
|
12
14
|
export { Authorizer } from "../../features/security/authorization/Authorizer/index.js";
|
package/exports/api/security.js
CHANGED
|
@@ -6,4 +6,6 @@ export { TeamFactory } from "../../features/security/teams/shared/abstractions.j
|
|
|
6
6
|
export { ApiToken } from "../../domain/security/ApiToken.js";
|
|
7
7
|
export { IdentityProvider, JwtIdentityProvider, OidcIdentityProvider } from "../../idp/index.js";
|
|
8
8
|
export { Authenticator } from "../../features/security/authentication/Authenticator/abstractions.js";
|
|
9
|
+
export { ResolveIdentityTenantsUseCase } from "../../features/security/ResolveIdentityTenants/index.js";
|
|
10
|
+
export { LogInUseCase } from "../../features/security/login/index.js";
|
|
9
11
|
export { Authorizer } from "../../features/security/authorization/Authorizer/index.js";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ResolveIdentityTenantsUseCase as Abstraction } from "./abstractions.js";
|
|
2
|
+
import { GetTenantByIdUseCase } from "../../../features/tenancy/GetTenantById/index.js";
|
|
3
|
+
import { FindUserRecordsUseCase } from "../../../features/users/FindUserRecords/index.js";
|
|
4
|
+
declare class ResolveIdentityTenantsUseCaseImpl implements Abstraction.Interface {
|
|
5
|
+
private findUserRecords;
|
|
6
|
+
private getTenantById;
|
|
7
|
+
constructor(findUserRecords: FindUserRecordsUseCase.Interface, getTenantById: GetTenantByIdUseCase.Interface);
|
|
8
|
+
execute(identityId: string): Abstraction.Return;
|
|
9
|
+
private loadTenant;
|
|
10
|
+
}
|
|
11
|
+
export declare const ResolveIdentityTenantsUseCase: import("@webiny/di").Implementation<typeof ResolveIdentityTenantsUseCaseImpl>;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Result, createImplementation } from "@webiny/feature/api";
|
|
2
|
+
import { ResolveIdentityTenantsUseCase } from "./abstractions.js";
|
|
3
|
+
import { GetTenantByIdUseCase } from "../../tenancy/GetTenantById/index.js";
|
|
4
|
+
import { FindUserRecordsUseCase } from "../../users/FindUserRecords/index.js";
|
|
5
|
+
import { UserStorageError } from "../../users/shared/errors.js";
|
|
6
|
+
class ResolveIdentityTenantsUseCaseImpl {
|
|
7
|
+
constructor(findUserRecords, getTenantById){
|
|
8
|
+
this.findUserRecords = findUserRecords;
|
|
9
|
+
this.getTenantById = getTenantById;
|
|
10
|
+
}
|
|
11
|
+
async execute(identityId) {
|
|
12
|
+
const usersResult = await this.findUserRecords.execute(identityId);
|
|
13
|
+
if (usersResult.isFail()) return Result.fail(usersResult.error);
|
|
14
|
+
const tenantIds = [
|
|
15
|
+
...new Set(usersResult.value.map((user)=>user.tenant))
|
|
16
|
+
];
|
|
17
|
+
let tenants;
|
|
18
|
+
try {
|
|
19
|
+
tenants = await Promise.all(tenantIds.map((id)=>this.loadTenant(id)));
|
|
20
|
+
} catch (error) {
|
|
21
|
+
return Result.fail(new UserStorageError(error));
|
|
22
|
+
}
|
|
23
|
+
const identityTenants = tenants.filter((tenant)=>!!tenant && tenant.isInstalled && "disabled" !== tenant.status).map((tenant)=>({
|
|
24
|
+
id: tenant.id,
|
|
25
|
+
name: tenant.name
|
|
26
|
+
})).sort((a, b)=>a.name.localeCompare(b.name));
|
|
27
|
+
return Result.ok(identityTenants);
|
|
28
|
+
}
|
|
29
|
+
async loadTenant(id) {
|
|
30
|
+
const result = await this.getTenantById.execute(id);
|
|
31
|
+
if (result.isOk()) return result.value;
|
|
32
|
+
if ("NOT_FOUND" === result.error.type) return null;
|
|
33
|
+
throw result.error.cause;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const ResolveIdentityTenantsUseCase_ResolveIdentityTenantsUseCase = createImplementation({
|
|
37
|
+
abstraction: ResolveIdentityTenantsUseCase,
|
|
38
|
+
implementation: ResolveIdentityTenantsUseCaseImpl,
|
|
39
|
+
dependencies: [
|
|
40
|
+
FindUserRecordsUseCase,
|
|
41
|
+
GetTenantByIdUseCase
|
|
42
|
+
]
|
|
43
|
+
});
|
|
44
|
+
export { ResolveIdentityTenantsUseCase_ResolveIdentityTenantsUseCase as ResolveIdentityTenantsUseCase };
|
|
45
|
+
|
|
46
|
+
//# sourceMappingURL=ResolveIdentityTenantsUseCase.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"features/security/ResolveIdentityTenants/ResolveIdentityTenantsUseCase.js","sources":["../../../../src/features/security/ResolveIdentityTenants/ResolveIdentityTenantsUseCase.ts"],"sourcesContent":["import { createImplementation } from \"@webiny/feature/api\";\nimport { Result } from \"@webiny/feature/api\";\nimport { ResolveIdentityTenantsUseCase as Abstraction } from \"./abstractions.js\";\nimport { GetTenantByIdUseCase } from \"~/features/tenancy/GetTenantById/index.js\";\nimport { FindUserRecordsUseCase } from \"~/features/users/FindUserRecords/index.js\";\nimport { UserStorageError } from \"~/features/users/shared/errors.js\";\nimport type { Tenant } from \"~/types/tenancy.js\";\n\nclass ResolveIdentityTenantsUseCaseImpl implements Abstraction.Interface {\n constructor(\n private findUserRecords: FindUserRecordsUseCase.Interface,\n private getTenantById: GetTenantByIdUseCase.Interface\n ) {}\n\n async execute(identityId: string): Abstraction.Return {\n const usersResult = await this.findUserRecords.execute(identityId);\n if (usersResult.isFail()) {\n return Result.fail(usersResult.error);\n }\n\n const tenantIds = [...new Set(usersResult.value.map(user => user.tenant))];\n\n let tenants: Array<Tenant | null>;\n try {\n tenants = await Promise.all(tenantIds.map(id => this.loadTenant(id)));\n } catch (error) {\n return Result.fail(new UserStorageError(error as Error));\n }\n\n const identityTenants = tenants\n // Disabled tenants can't be logged into, and uninstalled tenants have no users yet.\n .filter((tenant): tenant is Tenant => {\n return !!tenant && tenant.isInstalled && tenant.status !== \"disabled\";\n })\n .map(tenant => ({ id: tenant.id, name: tenant.name }))\n .sort((a, b) => a.name.localeCompare(b.name));\n\n return Result.ok(identityTenants);\n }\n\n private async loadTenant(id: string): Promise<Tenant | null> {\n const result = await this.getTenantById.execute(id);\n if (result.isOk()) {\n return result.value;\n }\n\n // A record left behind by a deleted tenant.\n if (result.error.type === \"NOT_FOUND\") {\n return null;\n }\n\n throw result.error.cause;\n }\n}\n\nexport const ResolveIdentityTenantsUseCase = createImplementation({\n abstraction: Abstraction,\n implementation: ResolveIdentityTenantsUseCaseImpl,\n dependencies: [FindUserRecordsUseCase, GetTenantByIdUseCase]\n});\n"],"names":["ResolveIdentityTenantsUseCaseImpl","findUserRecords","getTenantById","identityId","usersResult","Result","tenantIds","Set","user","tenants","Promise","id","error","UserStorageError","identityTenants","tenant","a","b","result","ResolveIdentityTenantsUseCase","createImplementation","Abstraction","FindUserRecordsUseCase","GetTenantByIdUseCase"],"mappings":";;;;;AAQA,MAAMA;IACF,YACYC,eAAiD,EACjDC,aAA6C,CACvD;aAFUD,eAAe,GAAfA;aACAC,aAAa,GAAbA;IACT;IAEH,MAAM,QAAQC,UAAkB,EAAsB;QAClD,MAAMC,cAAc,MAAM,IAAI,CAAC,eAAe,CAAC,OAAO,CAACD;QACvD,IAAIC,YAAY,MAAM,IAClB,OAAOC,OAAO,IAAI,CAACD,YAAY,KAAK;QAGxC,MAAME,YAAY;eAAI,IAAIC,IAAIH,YAAY,KAAK,CAAC,GAAG,CAACI,CAAAA,OAAQA,KAAK,MAAM;SAAG;QAE1E,IAAIC;QACJ,IAAI;YACAA,UAAU,MAAMC,QAAQ,GAAG,CAACJ,UAAU,GAAG,CAACK,CAAAA,KAAM,IAAI,CAAC,UAAU,CAACA;QACpE,EAAE,OAAOC,OAAO;YACZ,OAAOP,OAAO,IAAI,CAAC,IAAIQ,iBAAiBD;QAC5C;QAEA,MAAME,kBAAkBL,QAEnB,MAAM,CAAC,CAACM,SACE,CAAC,CAACA,UAAUA,OAAO,WAAW,IAAIA,AAAkB,eAAlBA,OAAO,MAAM,EAEzD,GAAG,CAACA,CAAAA,SAAW;gBAAE,IAAIA,OAAO,EAAE;gBAAE,MAAMA,OAAO,IAAI;YAAC,IAClD,IAAI,CAAC,CAACC,GAAGC,IAAMD,EAAE,IAAI,CAAC,aAAa,CAACC,EAAE,IAAI;QAE/C,OAAOZ,OAAO,EAAE,CAACS;IACrB;IAEA,MAAc,WAAWH,EAAU,EAA0B;QACzD,MAAMO,SAAS,MAAM,IAAI,CAAC,aAAa,CAAC,OAAO,CAACP;QAChD,IAAIO,OAAO,IAAI,IACX,OAAOA,OAAO,KAAK;QAIvB,IAAIA,AAAsB,gBAAtBA,OAAO,KAAK,CAAC,IAAI,EACjB,OAAO;QAGX,MAAMA,OAAO,KAAK,CAAC,KAAK;IAC5B;AACJ;AAEO,MAAMC,8DAAgCC,qBAAqB;IAC9D,aAAaC;IACb,gBAAgBrB;IAChB,cAAc;QAACsB;QAAwBC;KAAqB;AAChE"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Result } from "@webiny/feature/api";
|
|
2
|
+
import { AdminUsersRepository } from "../../../features/users/shared/abstractions.js";
|
|
3
|
+
export interface IdentityTenant {
|
|
4
|
+
id: string;
|
|
5
|
+
name: string;
|
|
6
|
+
}
|
|
7
|
+
type ResolveIdentityTenantsError = AdminUsersRepository.Error;
|
|
8
|
+
export interface IResolveIdentityTenantsUseCase {
|
|
9
|
+
/**
|
|
10
|
+
* List the enabled, installed tenants in which the identity has a user record.
|
|
11
|
+
*/
|
|
12
|
+
execute(identityId: string): Promise<Result<IdentityTenant[], ResolveIdentityTenantsError>>;
|
|
13
|
+
}
|
|
14
|
+
/** Find the tenants an identity can log into. */
|
|
15
|
+
export declare const ResolveIdentityTenantsUseCase: import("@webiny/di").Abstraction<IResolveIdentityTenantsUseCase>;
|
|
16
|
+
export declare namespace ResolveIdentityTenantsUseCase {
|
|
17
|
+
type Interface = IResolveIdentityTenantsUseCase;
|
|
18
|
+
type Tenant = IdentityTenant;
|
|
19
|
+
type Error = ResolveIdentityTenantsError;
|
|
20
|
+
type Return = Promise<Result<IdentityTenant[], ResolveIdentityTenantsError>>;
|
|
21
|
+
}
|
|
22
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"features/security/ResolveIdentityTenants/abstractions.js","sources":["../../../../src/features/security/ResolveIdentityTenants/abstractions.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport { Result } from \"@webiny/feature/api\";\nimport { AdminUsersRepository } from \"~/features/users/shared/abstractions.js\";\n\nexport interface IdentityTenant {\n id: string;\n name: string;\n}\n\ntype ResolveIdentityTenantsError = AdminUsersRepository.Error;\n\nexport interface IResolveIdentityTenantsUseCase {\n /**\n * List the enabled, installed tenants in which the identity has a user record.\n */\n execute(identityId: string): Promise<Result<IdentityTenant[], ResolveIdentityTenantsError>>;\n}\n\n/** Find the tenants an identity can log into. */\nexport const ResolveIdentityTenantsUseCase = createAbstraction<IResolveIdentityTenantsUseCase>(\n \"ResolveIdentityTenantsUseCase\"\n);\n\nexport namespace ResolveIdentityTenantsUseCase {\n export type Interface = IResolveIdentityTenantsUseCase;\n export type Tenant = IdentityTenant;\n export type Error = ResolveIdentityTenantsError;\n export type Return = Promise<Result<IdentityTenant[], ResolveIdentityTenantsError>>;\n}\n"],"names":["ResolveIdentityTenantsUseCase","createAbstraction"],"mappings":";AAmBO,MAAMA,gCAAgCC,kBACzC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createFeature } from "@webiny/feature/api";
|
|
2
|
+
import { ResolveIdentityTenantsUseCase } from "./ResolveIdentityTenantsUseCase.js";
|
|
3
|
+
const ResolveIdentityTenantsFeature = createFeature({
|
|
4
|
+
name: "ResolveIdentityTenants",
|
|
5
|
+
register (container) {
|
|
6
|
+
container.register(ResolveIdentityTenantsUseCase);
|
|
7
|
+
}
|
|
8
|
+
});
|
|
9
|
+
export { ResolveIdentityTenantsFeature };
|
|
10
|
+
|
|
11
|
+
//# sourceMappingURL=feature.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"features/security/ResolveIdentityTenants/feature.js","sources":["../../../../src/features/security/ResolveIdentityTenants/feature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport { ResolveIdentityTenantsUseCase } from \"./ResolveIdentityTenantsUseCase.js\";\n\nexport const ResolveIdentityTenantsFeature = createFeature({\n name: \"ResolveIdentityTenants\",\n register(container) {\n container.register(ResolveIdentityTenantsUseCase);\n }\n});\n"],"names":["ResolveIdentityTenantsFeature","createFeature","container","ResolveIdentityTenantsUseCase"],"mappings":";;AAGO,MAAMA,gCAAgCC,cAAc;IACvD,MAAM;IACN,UAASC,SAAS;QACdA,UAAU,QAAQ,CAACC;IACvB;AACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ResolveIdentityTenantsUseCase } from "./abstractions.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ResolveIdentityTenantsUseCase } from "./abstractions.js";
|
|
@@ -7,6 +7,8 @@ import { IdentityContextFeature } from "./IdentityContext/index.js";
|
|
|
7
7
|
import { ApiKeysFeature } from "./apiKeys/feature.js";
|
|
8
8
|
import { TeamsFeature } from "./teams/feature.js";
|
|
9
9
|
import { RolesFeature } from "./roles/feature.js";
|
|
10
|
+
import { ResolveIdentityTenantsFeature } from "./ResolveIdentityTenants/feature.js";
|
|
11
|
+
import { LogInFeature } from "./login/feature.js";
|
|
10
12
|
const SecurityFeature = createFeature({
|
|
11
13
|
name: "SecurityFeature",
|
|
12
14
|
register (container, storageOperations) {
|
|
@@ -18,6 +20,8 @@ const SecurityFeature = createFeature({
|
|
|
18
20
|
RolesFeature.register(container);
|
|
19
21
|
TeamsFeature.register(container);
|
|
20
22
|
GroupsTeamsAuthorizerFeature.register(container);
|
|
23
|
+
ResolveIdentityTenantsFeature.register(container);
|
|
24
|
+
LogInFeature.register(container);
|
|
21
25
|
}
|
|
22
26
|
});
|
|
23
27
|
export { SecurityFeature };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"features/security/SecurityFeature.js","sources":["../../../src/features/security/SecurityFeature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport type { Container } from \"@webiny/di\";\nimport type { SecurityStorageOperations as ISecurityStorageOperations } from \"~/types/security.js\";\nimport { SecurityStorageOperations } from \"./shared/abstractions.js\";\nimport { AuthenticationContextFeature } from \"./authentication/AuthenticationContext/index.js\";\nimport { AuthorizationContextFeature } from \"./authorization/AuthorizationContext/index.js\";\nimport { GroupsTeamsAuthorizerFeature } from \"./authorization/GroupsTeamsAuthorizer/feature.js\";\nimport { IdentityContextFeature } from \"./IdentityContext/index.js\";\nimport { ApiKeysFeature } from \"./apiKeys/feature.js\";\nimport { TeamsFeature } from \"./teams/feature.js\";\nimport { RolesFeature } from \"~/features/security/roles/feature.js\";\n\n/**\n * Setup all security features in the DI container.\n * This is the main entry point for registering all security-related features.\n *\n * @param container - DI container instance\n * @param storageOperations - Storage operations implementation\n */\nexport const SecurityFeature = createFeature({\n name: \"SecurityFeature\",\n register(container: Container, storageOperations: ISecurityStorageOperations) {\n // Register storage operations abstraction (singleton)\n container.registerInstance(SecurityStorageOperations, storageOperations);\n\n AuthorizationContextFeature.register(container);\n AuthenticationContextFeature.register(container);\n IdentityContextFeature.register(container);\n ApiKeysFeature.register(container);\n RolesFeature.register(container);\n TeamsFeature.register(container);\n GroupsTeamsAuthorizerFeature.register(container);\n }\n});\n"],"names":["SecurityFeature","createFeature","container","storageOperations","SecurityStorageOperations","AuthorizationContextFeature","AuthenticationContextFeature","IdentityContextFeature","ApiKeysFeature","RolesFeature","TeamsFeature","GroupsTeamsAuthorizerFeature"],"mappings":"
|
|
1
|
+
{"version":3,"file":"features/security/SecurityFeature.js","sources":["../../../src/features/security/SecurityFeature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport type { Container } from \"@webiny/di\";\nimport type { SecurityStorageOperations as ISecurityStorageOperations } from \"~/types/security.js\";\nimport { SecurityStorageOperations } from \"./shared/abstractions.js\";\nimport { AuthenticationContextFeature } from \"./authentication/AuthenticationContext/index.js\";\nimport { AuthorizationContextFeature } from \"./authorization/AuthorizationContext/index.js\";\nimport { GroupsTeamsAuthorizerFeature } from \"./authorization/GroupsTeamsAuthorizer/feature.js\";\nimport { IdentityContextFeature } from \"./IdentityContext/index.js\";\nimport { ApiKeysFeature } from \"./apiKeys/feature.js\";\nimport { TeamsFeature } from \"./teams/feature.js\";\nimport { RolesFeature } from \"~/features/security/roles/feature.js\";\nimport { ResolveIdentityTenantsFeature } from \"./ResolveIdentityTenants/feature.js\";\nimport { LogInFeature } from \"./login/feature.js\";\n\n/**\n * Setup all security features in the DI container.\n * This is the main entry point for registering all security-related features.\n *\n * @param container - DI container instance\n * @param storageOperations - Storage operations implementation\n */\nexport const SecurityFeature = createFeature({\n name: \"SecurityFeature\",\n register(container: Container, storageOperations: ISecurityStorageOperations) {\n // Register storage operations abstraction (singleton)\n container.registerInstance(SecurityStorageOperations, storageOperations);\n\n AuthorizationContextFeature.register(container);\n AuthenticationContextFeature.register(container);\n IdentityContextFeature.register(container);\n ApiKeysFeature.register(container);\n RolesFeature.register(container);\n TeamsFeature.register(container);\n GroupsTeamsAuthorizerFeature.register(container);\n ResolveIdentityTenantsFeature.register(container);\n LogInFeature.register(container);\n }\n});\n"],"names":["SecurityFeature","createFeature","container","storageOperations","SecurityStorageOperations","AuthorizationContextFeature","AuthenticationContextFeature","IdentityContextFeature","ApiKeysFeature","RolesFeature","TeamsFeature","GroupsTeamsAuthorizerFeature","ResolveIdentityTenantsFeature","LogInFeature"],"mappings":";;;;;;;;;;;AAqBO,MAAMA,kBAAkBC,cAAc;IACzC,MAAM;IACN,UAASC,SAAoB,EAAEC,iBAA6C;QAExED,UAAU,gBAAgB,CAACE,2BAA2BD;QAEtDE,4BAA4B,QAAQ,CAACH;QACrCI,6BAA6B,QAAQ,CAACJ;QACtCK,uBAAuB,QAAQ,CAACL;QAChCM,eAAe,QAAQ,CAACN;QACxBO,aAAa,QAAQ,CAACP;QACtBQ,aAAa,QAAQ,CAACR;QACtBS,6BAA6B,QAAQ,CAACT;QACtCU,8BAA8B,QAAQ,CAACV;QACvCW,aAAa,QAAQ,CAACX;IAC1B;AACJ"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { EventPublisher } from "../../../features/eventPublisher/index.js";
|
|
2
|
+
import { IdentityContext } from "../../../features/security/IdentityContext/index.js";
|
|
3
|
+
import { TenantContext } from "../../../features/tenancy/TenantContext/index.js";
|
|
4
|
+
import { GetIdentityProfileUseCase } from "../../../features/users/GetIdentityProfile/index.js";
|
|
5
|
+
import { ResolveIdentityTenantsUseCase } from "../../../features/security/ResolveIdentityTenants/abstractions.js";
|
|
6
|
+
import { LogInUseCase as UseCaseAbstraction } from "./abstractions.js";
|
|
7
|
+
declare class LogInUseCaseImpl implements UseCaseAbstraction.Interface {
|
|
8
|
+
private identityContext;
|
|
9
|
+
private tenantContext;
|
|
10
|
+
private eventPublisher;
|
|
11
|
+
private getIdentityProfile;
|
|
12
|
+
private resolveIdentityTenants;
|
|
13
|
+
constructor(identityContext: IdentityContext.Interface, tenantContext: TenantContext.Interface, eventPublisher: EventPublisher.Interface, getIdentityProfile: GetIdentityProfileUseCase.Interface, resolveIdentityTenants: ResolveIdentityTenantsUseCase.Interface);
|
|
14
|
+
execute(): UseCaseAbstraction.Return;
|
|
15
|
+
private listIdentityTenants;
|
|
16
|
+
}
|
|
17
|
+
export declare const LogInUseCase: typeof LogInUseCaseImpl & {
|
|
18
|
+
__abstraction: import("@webiny/di").Abstraction<import("./abstractions.js").ILogInUseCase>;
|
|
19
|
+
};
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { Result } from "@webiny/feature/api";
|
|
2
|
+
import { EventPublisher } from "../../eventPublisher/index.js";
|
|
3
|
+
import { IdentityContext } from "../IdentityContext/index.js";
|
|
4
|
+
import { TenantContext } from "../../tenancy/TenantContext/index.js";
|
|
5
|
+
import { GetIdentityProfileUseCase } from "../../users/GetIdentityProfile/index.js";
|
|
6
|
+
import { UserNotFoundError, UserStorageError } from "../../users/shared/errors.js";
|
|
7
|
+
import { ResolveIdentityTenantsUseCase } from "../ResolveIdentityTenants/abstractions.js";
|
|
8
|
+
import { AfterLoginEvent, LogInUseCase } from "./abstractions.js";
|
|
9
|
+
import { LogInMissingProfileError, LogInNotAuthenticatedError, LogInTenantAccessDeniedError } from "./errors.js";
|
|
10
|
+
class LogInUseCaseImpl {
|
|
11
|
+
constructor(identityContext, tenantContext, eventPublisher, getIdentityProfile, resolveIdentityTenants){
|
|
12
|
+
this.identityContext = identityContext;
|
|
13
|
+
this.tenantContext = tenantContext;
|
|
14
|
+
this.eventPublisher = eventPublisher;
|
|
15
|
+
this.getIdentityProfile = getIdentityProfile;
|
|
16
|
+
this.resolveIdentityTenants = resolveIdentityTenants;
|
|
17
|
+
}
|
|
18
|
+
async execute() {
|
|
19
|
+
const identity = this.identityContext.getIdentity();
|
|
20
|
+
if (identity.isAnonymous()) return Result.fail(new LogInNotAuthenticatedError());
|
|
21
|
+
const isExternal = identity.profile.external;
|
|
22
|
+
if (isExternal) {
|
|
23
|
+
if (false === identity.context.canAccessTenant) return Result.fail(new LogInTenantAccessDeniedError());
|
|
24
|
+
await this.eventPublisher.publish(new AfterLoginEvent({
|
|
25
|
+
identity
|
|
26
|
+
}));
|
|
27
|
+
}
|
|
28
|
+
const profileResult = await this.identityContext.withoutAuthorization(async ()=>this.getIdentityProfile.execute(identity.id));
|
|
29
|
+
if (profileResult.isFail()) {
|
|
30
|
+
if (!isExternal) {
|
|
31
|
+
if (profileResult.error instanceof UserStorageError) return Result.fail(profileResult.error);
|
|
32
|
+
if (profileResult.error instanceof UserNotFoundError) {
|
|
33
|
+
const tenantsResult = await this.listIdentityTenants(identity.id);
|
|
34
|
+
if (tenantsResult.isFail()) return Result.fail(tenantsResult.error);
|
|
35
|
+
const tenants = tenantsResult.value;
|
|
36
|
+
if (tenants.length > 0) return Result.ok({
|
|
37
|
+
type: "tenantSelectionRequired",
|
|
38
|
+
tenants
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return Result.fail(new LogInMissingProfileError());
|
|
43
|
+
}
|
|
44
|
+
if (!isExternal) await this.eventPublisher.publish(new AfterLoginEvent({
|
|
45
|
+
identity
|
|
46
|
+
}));
|
|
47
|
+
return Result.ok({
|
|
48
|
+
type: "loggedIn",
|
|
49
|
+
identity,
|
|
50
|
+
user: profileResult.value
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
async listIdentityTenants(identityId) {
|
|
54
|
+
const result = await this.identityContext.withoutAuthorization(async ()=>this.resolveIdentityTenants.execute(identityId));
|
|
55
|
+
if (result.isFail()) {
|
|
56
|
+
if (result.error instanceof UserStorageError) return Result.fail(result.error);
|
|
57
|
+
return Result.ok([]);
|
|
58
|
+
}
|
|
59
|
+
const currentTenantId = this.tenantContext.getTenant().id;
|
|
60
|
+
return Result.ok(result.value.filter((tenant)=>tenant.id !== currentTenantId));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const LogInUseCase_LogInUseCase = LogInUseCase.createImplementation({
|
|
64
|
+
implementation: LogInUseCaseImpl,
|
|
65
|
+
dependencies: [
|
|
66
|
+
IdentityContext,
|
|
67
|
+
TenantContext,
|
|
68
|
+
EventPublisher,
|
|
69
|
+
GetIdentityProfileUseCase,
|
|
70
|
+
ResolveIdentityTenantsUseCase
|
|
71
|
+
]
|
|
72
|
+
});
|
|
73
|
+
export { LogInUseCase_LogInUseCase as LogInUseCase };
|
|
74
|
+
|
|
75
|
+
//# sourceMappingURL=LogInUseCase.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"features/security/login/LogInUseCase.js","sources":["../../../../src/features/security/login/LogInUseCase.ts"],"sourcesContent":["import { Result } from \"@webiny/feature/api\";\nimport { EventPublisher } from \"~/features/eventPublisher/index.js\";\nimport { IdentityContext } from \"~/features/security/IdentityContext/index.js\";\nimport { TenantContext } from \"~/features/tenancy/TenantContext/index.js\";\nimport { GetIdentityProfileUseCase } from \"~/features/users/GetIdentityProfile/index.js\";\nimport { UserNotFoundError, UserStorageError } from \"~/features/users/shared/errors.js\";\nimport { ResolveIdentityTenantsUseCase } from \"~/features/security/ResolveIdentityTenants/abstractions.js\";\nimport { AfterLoginEvent, LogInUseCase as UseCaseAbstraction } from \"./abstractions.js\";\nimport {\n LogInMissingProfileError,\n LogInNotAuthenticatedError,\n LogInTenantAccessDeniedError\n} from \"./errors.js\";\n\nclass LogInUseCaseImpl implements UseCaseAbstraction.Interface {\n constructor(\n private identityContext: IdentityContext.Interface,\n private tenantContext: TenantContext.Interface,\n private eventPublisher: EventPublisher.Interface,\n private getIdentityProfile: GetIdentityProfileUseCase.Interface,\n private resolveIdentityTenants: ResolveIdentityTenantsUseCase.Interface\n ) {}\n\n async execute(): UseCaseAbstraction.Return {\n const identity = this.identityContext.getIdentity();\n if (identity.isAnonymous()) {\n return Result.fail(new LogInNotAuthenticatedError());\n }\n\n /**\n * External identities are provisioned by the AfterLogin handlers, so the profile is\n * loaded after them. Native identities must have a profile before AfterLogin runs:\n * without one, they get the list of their tenants instead.\n */\n const isExternal = identity.profile.external;\n\n if (isExternal) {\n // The IdP decides tenant access for its identities. Without access, AfterLogin must\n // not run, or the user sync would create a record in a tenant they can't use.\n if (identity.context.canAccessTenant === false) {\n return Result.fail(new LogInTenantAccessDeniedError());\n }\n\n await this.eventPublisher.publish(new AfterLoginEvent({ identity }));\n }\n\n const profileResult = await this.identityContext.withoutAuthorization(async () => {\n return this.getIdentityProfile.execute(identity.id);\n });\n\n if (profileResult.isFail()) {\n if (!isExternal) {\n // A failed read says nothing about where the user belongs, so it fails the login.\n if (profileResult.error instanceof UserStorageError) {\n return Result.fail(profileResult.error);\n }\n\n if (profileResult.error instanceof UserNotFoundError) {\n const tenantsResult = await this.listIdentityTenants(identity.id);\n if (tenantsResult.isFail()) {\n return Result.fail(tenantsResult.error);\n }\n\n const tenants = tenantsResult.value;\n if (tenants.length > 0) {\n return Result.ok({ type: \"tenantSelectionRequired\", tenants });\n }\n }\n }\n\n return Result.fail(new LogInMissingProfileError());\n }\n\n if (!isExternal) {\n await this.eventPublisher.publish(new AfterLoginEvent({ identity }));\n }\n\n return Result.ok({ type: \"loggedIn\", identity, user: profileResult.value });\n }\n\n private async listIdentityTenants(\n identityId: string\n ): Promise<Result<ResolveIdentityTenantsUseCase.Tenant[], UserStorageError>> {\n const result = await this.identityContext.withoutAuthorization(async () => {\n return this.resolveIdentityTenants.execute(identityId);\n });\n\n if (result.isFail()) {\n // Like the profile read, a failed lookup fails the login instead of saying \"no access\".\n if (result.error instanceof UserStorageError) {\n return Result.fail(result.error);\n }\n return Result.ok([]);\n }\n\n // The profile lookup already covered the current tenant, so offering it would loop.\n const currentTenantId = this.tenantContext.getTenant().id;\n return Result.ok(result.value.filter(tenant => tenant.id !== currentTenantId));\n }\n}\n\nexport const LogInUseCase = UseCaseAbstraction.createImplementation({\n implementation: LogInUseCaseImpl,\n dependencies: [\n IdentityContext,\n TenantContext,\n EventPublisher,\n GetIdentityProfileUseCase,\n ResolveIdentityTenantsUseCase\n ]\n});\n"],"names":["LogInUseCaseImpl","identityContext","tenantContext","eventPublisher","getIdentityProfile","resolveIdentityTenants","identity","Result","LogInNotAuthenticatedError","isExternal","LogInTenantAccessDeniedError","AfterLoginEvent","profileResult","UserStorageError","UserNotFoundError","tenantsResult","tenants","LogInMissingProfileError","identityId","result","currentTenantId","tenant","LogInUseCase","UseCaseAbstraction","IdentityContext","TenantContext","EventPublisher","GetIdentityProfileUseCase","ResolveIdentityTenantsUseCase"],"mappings":";;;;;;;;;AAcA,MAAMA;IACF,YACYC,eAA0C,EAC1CC,aAAsC,EACtCC,cAAwC,EACxCC,kBAAuD,EACvDC,sBAA+D,CACzE;aALUJ,eAAe,GAAfA;aACAC,aAAa,GAAbA;aACAC,cAAc,GAAdA;aACAC,kBAAkB,GAAlBA;aACAC,sBAAsB,GAAtBA;IACT;IAEH,MAAM,UAAqC;QACvC,MAAMC,WAAW,IAAI,CAAC,eAAe,CAAC,WAAW;QACjD,IAAIA,SAAS,WAAW,IACpB,OAAOC,OAAO,IAAI,CAAC,IAAIC;QAQ3B,MAAMC,aAAaH,SAAS,OAAO,CAAC,QAAQ;QAE5C,IAAIG,YAAY;YAGZ,IAAIH,AAAqC,UAArCA,SAAS,OAAO,CAAC,eAAe,EAChC,OAAOC,OAAO,IAAI,CAAC,IAAIG;YAG3B,MAAM,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,IAAIC,gBAAgB;gBAAEL;YAAS;QACrE;QAEA,MAAMM,gBAAgB,MAAM,IAAI,CAAC,eAAe,CAAC,oBAAoB,CAAC,UAC3D,IAAI,CAAC,kBAAkB,CAAC,OAAO,CAACN,SAAS,EAAE;QAGtD,IAAIM,cAAc,MAAM,IAAI;YACxB,IAAI,CAACH,YAAY;gBAEb,IAAIG,cAAc,KAAK,YAAYC,kBAC/B,OAAON,OAAO,IAAI,CAACK,cAAc,KAAK;gBAG1C,IAAIA,cAAc,KAAK,YAAYE,mBAAmB;oBAClD,MAAMC,gBAAgB,MAAM,IAAI,CAAC,mBAAmB,CAACT,SAAS,EAAE;oBAChE,IAAIS,cAAc,MAAM,IACpB,OAAOR,OAAO,IAAI,CAACQ,cAAc,KAAK;oBAG1C,MAAMC,UAAUD,cAAc,KAAK;oBACnC,IAAIC,QAAQ,MAAM,GAAG,GACjB,OAAOT,OAAO,EAAE,CAAC;wBAAE,MAAM;wBAA2BS;oBAAQ;gBAEpE;YACJ;YAEA,OAAOT,OAAO,IAAI,CAAC,IAAIU;QAC3B;QAEA,IAAI,CAACR,YACD,MAAM,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,IAAIE,gBAAgB;YAAEL;QAAS;QAGrE,OAAOC,OAAO,EAAE,CAAC;YAAE,MAAM;YAAYD;YAAU,MAAMM,cAAc,KAAK;QAAC;IAC7E;IAEA,MAAc,oBACVM,UAAkB,EACuD;QACzE,MAAMC,SAAS,MAAM,IAAI,CAAC,eAAe,CAAC,oBAAoB,CAAC,UACpD,IAAI,CAAC,sBAAsB,CAAC,OAAO,CAACD;QAG/C,IAAIC,OAAO,MAAM,IAAI;YAEjB,IAAIA,OAAO,KAAK,YAAYN,kBACxB,OAAON,OAAO,IAAI,CAACY,OAAO,KAAK;YAEnC,OAAOZ,OAAO,EAAE,CAAC,EAAE;QACvB;QAGA,MAAMa,kBAAkB,IAAI,CAAC,aAAa,CAAC,SAAS,GAAG,EAAE;QACzD,OAAOb,OAAO,EAAE,CAACY,OAAO,KAAK,CAAC,MAAM,CAACE,CAAAA,SAAUA,OAAO,EAAE,KAAKD;IACjE;AACJ;AAEO,MAAME,4BAAeC,aAAAA,oBAAuC,CAAC;IAChE,gBAAgBvB;IAChB,cAAc;QACVwB;QACAC;QACAC;QACAC;QACAC;KACH;AACL"}
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { DomainEvent } from "../../../features/eventPublisher/index.js";
|
|
2
2
|
import type { IEventHandler } from "../../../features/eventPublisher/index.js";
|
|
3
|
+
import { Result } from "@webiny/feature/api";
|
|
3
4
|
import { Identity } from "../../../features/security/IdentityContext/index.js";
|
|
5
|
+
import type { AdminUser } from "../../../features/users/shared/types.js";
|
|
6
|
+
import type { UserStorageError } from "../../../features/users/shared/errors.js";
|
|
7
|
+
import type { IdentityTenant } from "../../../features/security/ResolveIdentityTenants/abstractions.js";
|
|
8
|
+
import type { LogInNotAuthenticatedError, LogInMissingProfileError, LogInTenantAccessDeniedError } from "./errors.js";
|
|
4
9
|
export interface AfterLoginPayload {
|
|
5
10
|
identity: Identity;
|
|
6
11
|
}
|
|
@@ -14,3 +19,43 @@ export declare namespace AfterLoginEventHandler {
|
|
|
14
19
|
type Interface = IEventHandler<AfterLoginEvent>;
|
|
15
20
|
type Event = AfterLoginEvent;
|
|
16
21
|
}
|
|
22
|
+
/** The identity has a profile in the current tenant (or its parent), and is logged in. */
|
|
23
|
+
export interface LogInLoggedIn {
|
|
24
|
+
type: "loggedIn";
|
|
25
|
+
identity: Identity;
|
|
26
|
+
user: AdminUser;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* A native identity has no profile in the current tenant, but has one in these tenants.
|
|
30
|
+
* The client picks one and logs in there.
|
|
31
|
+
*/
|
|
32
|
+
export interface LogInTenantSelectionRequired {
|
|
33
|
+
type: "tenantSelectionRequired";
|
|
34
|
+
tenants: IdentityTenant[];
|
|
35
|
+
}
|
|
36
|
+
export type LogInOutput = LogInLoggedIn | LogInTenantSelectionRequired;
|
|
37
|
+
export interface ILogInUseCaseErrors {
|
|
38
|
+
notAuthenticated: LogInNotAuthenticatedError;
|
|
39
|
+
tenantAccessDenied: LogInTenantAccessDeniedError;
|
|
40
|
+
missingProfile: LogInMissingProfileError;
|
|
41
|
+
storage: UserStorageError;
|
|
42
|
+
}
|
|
43
|
+
type LogInError = ILogInUseCaseErrors[keyof ILogInUseCaseErrors];
|
|
44
|
+
export interface ILogInUseCase {
|
|
45
|
+
/**
|
|
46
|
+
* Log in the current identity: load its user profile and publish `AfterLoginEvent`.
|
|
47
|
+
* A native identity without a profile in the current tenant gets the tenants it can log into.
|
|
48
|
+
*/
|
|
49
|
+
execute(): Promise<Result<LogInOutput, LogInError>>;
|
|
50
|
+
}
|
|
51
|
+
/** Log in the current identity. */
|
|
52
|
+
export declare const LogInUseCase: import("@webiny/di").Abstraction<ILogInUseCase>;
|
|
53
|
+
export declare namespace LogInUseCase {
|
|
54
|
+
type Interface = ILogInUseCase;
|
|
55
|
+
type Output = LogInOutput;
|
|
56
|
+
type LoggedIn = LogInLoggedIn;
|
|
57
|
+
type TenantSelectionRequired = LogInTenantSelectionRequired;
|
|
58
|
+
type Error = LogInError;
|
|
59
|
+
type Return = Promise<Result<LogInOutput, LogInError>>;
|
|
60
|
+
}
|
|
61
|
+
export {};
|
|
@@ -9,6 +9,7 @@ class AfterLoginEvent extends DomainEvent {
|
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
11
|
const AfterLoginEventHandler = createAbstraction("AfterLoginEventHandler");
|
|
12
|
-
|
|
12
|
+
const LogInUseCase = createAbstraction("LogInUseCase");
|
|
13
|
+
export { AfterLoginEvent, AfterLoginEventHandler, LogInUseCase };
|
|
13
14
|
|
|
14
15
|
//# sourceMappingURL=abstractions.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"features/security/login/abstractions.js","sources":["../../../../src/features/security/login/abstractions.ts"],"sourcesContent":["import { DomainEvent } from \"~/features/eventPublisher/index.js\";\nimport type { IEventHandler } from \"~/features/eventPublisher/index.js\";\nimport { createAbstraction } from \"@webiny/feature/api\";\nimport { Identity } from \"~/features/security/IdentityContext/index.js\";\n\nexport interface AfterLoginPayload {\n identity: Identity;\n}\n\nexport class AfterLoginEvent extends DomainEvent<AfterLoginPayload> {\n eventType = \"login.afterLogin\" as const;\n\n getHandlerAbstraction() {\n return AfterLoginEventHandler;\n }\n}\n\n/** Hook into login lifecycle after a user logs in. */\nexport const AfterLoginEventHandler =\n createAbstraction<IEventHandler<AfterLoginEvent>>(\"AfterLoginEventHandler\");\n\nexport namespace AfterLoginEventHandler {\n export type Interface = IEventHandler<AfterLoginEvent>;\n export type Event = AfterLoginEvent;\n}\n"],"names":["AfterLoginEvent","DomainEvent","AfterLoginEventHandler","createAbstraction"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"features/security/login/abstractions.js","sources":["../../../../src/features/security/login/abstractions.ts"],"sourcesContent":["import { DomainEvent } from \"~/features/eventPublisher/index.js\";\nimport type { IEventHandler } from \"~/features/eventPublisher/index.js\";\nimport { createAbstraction } from \"@webiny/feature/api\";\nimport { Result } from \"@webiny/feature/api\";\nimport { Identity } from \"~/features/security/IdentityContext/index.js\";\nimport type { AdminUser } from \"~/features/users/shared/types.js\";\nimport type { UserStorageError } from \"~/features/users/shared/errors.js\";\nimport type { IdentityTenant } from \"~/features/security/ResolveIdentityTenants/abstractions.js\";\nimport type {\n LogInNotAuthenticatedError,\n LogInMissingProfileError,\n LogInTenantAccessDeniedError\n} from \"./errors.js\";\n\nexport interface AfterLoginPayload {\n identity: Identity;\n}\n\nexport class AfterLoginEvent extends DomainEvent<AfterLoginPayload> {\n eventType = \"login.afterLogin\" as const;\n\n getHandlerAbstraction() {\n return AfterLoginEventHandler;\n }\n}\n\n/** Hook into login lifecycle after a user logs in. */\nexport const AfterLoginEventHandler =\n createAbstraction<IEventHandler<AfterLoginEvent>>(\"AfterLoginEventHandler\");\n\nexport namespace AfterLoginEventHandler {\n export type Interface = IEventHandler<AfterLoginEvent>;\n export type Event = AfterLoginEvent;\n}\n\n/** The identity has a profile in the current tenant (or its parent), and is logged in. */\nexport interface LogInLoggedIn {\n type: \"loggedIn\";\n identity: Identity;\n user: AdminUser;\n}\n\n/**\n * A native identity has no profile in the current tenant, but has one in these tenants.\n * The client picks one and logs in there.\n */\nexport interface LogInTenantSelectionRequired {\n type: \"tenantSelectionRequired\";\n tenants: IdentityTenant[];\n}\n\nexport type LogInOutput = LogInLoggedIn | LogInTenantSelectionRequired;\n\nexport interface ILogInUseCaseErrors {\n notAuthenticated: LogInNotAuthenticatedError;\n tenantAccessDenied: LogInTenantAccessDeniedError;\n missingProfile: LogInMissingProfileError;\n storage: UserStorageError;\n}\n\ntype LogInError = ILogInUseCaseErrors[keyof ILogInUseCaseErrors];\n\nexport interface ILogInUseCase {\n /**\n * Log in the current identity: load its user profile and publish `AfterLoginEvent`.\n * A native identity without a profile in the current tenant gets the tenants it can log into.\n */\n execute(): Promise<Result<LogInOutput, LogInError>>;\n}\n\n/** Log in the current identity. */\nexport const LogInUseCase = createAbstraction<ILogInUseCase>(\"LogInUseCase\");\n\nexport namespace LogInUseCase {\n export type Interface = ILogInUseCase;\n export type Output = LogInOutput;\n export type LoggedIn = LogInLoggedIn;\n export type TenantSelectionRequired = LogInTenantSelectionRequired;\n export type Error = LogInError;\n export type Return = Promise<Result<LogInOutput, LogInError>>;\n}\n"],"names":["AfterLoginEvent","DomainEvent","AfterLoginEventHandler","createAbstraction","LogInUseCase"],"mappings":";;AAkBO,MAAMA,wBAAwBC;IAGjC,wBAAwB;QACpB,OAAOC;IACX;;QALG,qBACH,SAAS,GAAG;;AAKhB;AAGO,MAAMA,yBACTC,kBAAkD;AA2C/C,MAAMC,eAAeD,kBAAiC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BaseError } from "@webiny/feature/api";
|
|
2
|
+
export declare class LogInNotAuthenticatedError extends BaseError {
|
|
3
|
+
readonly code: "Security/Identity/NotAuthenticated";
|
|
4
|
+
constructor();
|
|
5
|
+
}
|
|
6
|
+
export declare class LogInTenantAccessDeniedError extends BaseError {
|
|
7
|
+
readonly code: "Security/Identity/NotAuthorized";
|
|
8
|
+
constructor();
|
|
9
|
+
}
|
|
10
|
+
export declare class LogInMissingProfileError extends BaseError {
|
|
11
|
+
readonly code: "Security/Identity/NotAuthorized";
|
|
12
|
+
constructor();
|
|
13
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { BaseError } from "@webiny/feature/api";
|
|
2
|
+
class LogInNotAuthenticatedError extends BaseError {
|
|
3
|
+
constructor(){
|
|
4
|
+
super({
|
|
5
|
+
message: "Unauthenticated!"
|
|
6
|
+
}), this.code = "Security/Identity/NotAuthenticated";
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
class LogInTenantAccessDeniedError extends BaseError {
|
|
10
|
+
constructor(){
|
|
11
|
+
super({
|
|
12
|
+
message: "You can't access this tenant."
|
|
13
|
+
}), this.code = "Security/Identity/NotAuthorized";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
class LogInMissingProfileError extends BaseError {
|
|
17
|
+
constructor(){
|
|
18
|
+
super({
|
|
19
|
+
message: "Missing user profile!"
|
|
20
|
+
}), this.code = "Security/Identity/NotAuthorized";
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export { LogInMissingProfileError, LogInNotAuthenticatedError, LogInTenantAccessDeniedError };
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"features/security/login/errors.js","sources":["../../../../src/features/security/login/errors.ts"],"sourcesContent":["import { BaseError } from \"@webiny/feature/api\";\n\nexport class LogInNotAuthenticatedError extends BaseError {\n override readonly code = \"Security/Identity/NotAuthenticated\" as const;\n\n constructor() {\n super({\n message: \"Unauthenticated!\"\n });\n }\n}\n\nexport class LogInTenantAccessDeniedError extends BaseError {\n override readonly code = \"Security/Identity/NotAuthorized\" as const;\n\n constructor() {\n super({\n message: \"You can't access this tenant.\"\n });\n }\n}\n\nexport class LogInMissingProfileError extends BaseError {\n override readonly code = \"Security/Identity/NotAuthorized\" as const;\n\n constructor() {\n super({\n message: \"Missing user profile!\"\n });\n }\n}\n"],"names":["LogInNotAuthenticatedError","BaseError","LogInTenantAccessDeniedError","LogInMissingProfileError"],"mappings":";AAEO,MAAMA,mCAAmCC;IAG5C,aAAc;QACV,KAAK,CAAC;YACF,SAAS;QACb,SALc,IAAI,GAAG;IAMzB;AACJ;AAEO,MAAMC,qCAAqCD;IAG9C,aAAc;QACV,KAAK,CAAC;YACF,SAAS;QACb,SALc,IAAI,GAAG;IAMzB;AACJ;AAEO,MAAME,iCAAiCF;IAG1C,aAAc;QACV,KAAK,CAAC;YACF,SAAS;QACb,SALc,IAAI,GAAG;IAMzB;AACJ"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createFeature } from "@webiny/feature/api";
|
|
2
|
+
import { LogInUseCase } from "./LogInUseCase.js";
|
|
3
|
+
const LogInFeature = createFeature({
|
|
4
|
+
name: "LogIn",
|
|
5
|
+
register (container) {
|
|
6
|
+
container.register(LogInUseCase);
|
|
7
|
+
}
|
|
8
|
+
});
|
|
9
|
+
export { LogInFeature };
|
|
10
|
+
|
|
11
|
+
//# sourceMappingURL=feature.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"features/security/login/feature.js","sources":["../../../../src/features/security/login/feature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport { LogInUseCase } from \"./LogInUseCase.js\";\n\nexport const LogInFeature = createFeature({\n name: \"LogIn\",\n register(container) {\n container.register(LogInUseCase);\n }\n});\n"],"names":["LogInFeature","createFeature","container","LogInUseCase"],"mappings":";;AAGO,MAAMA,eAAeC,cAAc;IACtC,MAAM;IACN,UAASC,SAAS;QACdA,UAAU,QAAQ,CAACC;IACvB;AACJ"}
|
|
@@ -17,7 +17,7 @@ const TenancyFeature = createFeature({
|
|
|
17
17
|
container.registerInstance(TenantCache, new TenantCache_js_TenantCache(async (ids)=>{
|
|
18
18
|
if (0 === ids.length) return [];
|
|
19
19
|
const tenants = await storageOperations.getTenantsByIds(ids);
|
|
20
|
-
return ids.map((
|
|
20
|
+
return ids.map((id)=>tenants.find((tenant)=>tenant.id === id));
|
|
21
21
|
}));
|
|
22
22
|
CreateTenantFeature.register(container);
|
|
23
23
|
DeleteTenantFeature.register(container);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"features/tenancy/TenancyFeature.js","sources":["../../../src/features/tenancy/TenancyFeature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport type {
|
|
1
|
+
{"version":3,"file":"features/tenancy/TenancyFeature.js","sources":["../../../src/features/tenancy/TenancyFeature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport type {\n Tenant,\n TenancyStorageOperations as ITenancyStorageOperations\n} from \"~/types/tenancy.js\";\nimport { TenantCache } from \"./shared/abstractions.js\";\nimport { TenantCache as TenantCacheImpl } from \"./shared/TenantCache.js\";\nimport { TenancyStorageOperations } from \"./shared/storageOperations.js\";\nimport { GetRootTenantFeature } from \"./GetRootTenant/feature.js\";\nimport { ListTenantsFeature } from \"./ListTenants/feature.js\";\nimport { CreateTenantFeature } from \"./CreateTenant/feature.js\";\nimport { UpdateTenantFeature } from \"./UpdateTenant/feature.js\";\nimport { DeleteTenantFeature } from \"./DeleteTenant/feature.js\";\nimport { GetTenantByIdFeature } from \"./GetTenantById/feature.js\";\nimport { TenantContextFeature } from \"./TenantContext/feature.js\";\nimport { InstallTenantFeature } from \"./InstallTenant/index.js\";\n\nexport const TenancyFeature = createFeature<ITenancyStorageOperations>({\n name: \"TenancyFeature\",\n register: (container, storageOperations) => {\n // Register storage operations abstraction (singleton)\n container.registerInstance(TenancyStorageOperations, storageOperations);\n\n // Register shared cache (singleton) - batch loader provided here\n container.registerInstance(\n TenantCache,\n new TenantCacheImpl(async (ids: readonly string[]) => {\n if (ids.length === 0) {\n return [];\n }\n const tenants = await storageOperations.getTenantsByIds(ids);\n // Storage skips missing tenants and doesn't keep the order, so match by ID.\n return ids.map(id => tenants.find(tenant => tenant.id === id) as Tenant);\n })\n );\n\n // Register all features - they will auto-wire their dependencies from the container\n CreateTenantFeature.register(container);\n DeleteTenantFeature.register(container);\n GetRootTenantFeature.register(container);\n GetTenantByIdFeature.register(container);\n ListTenantsFeature.register(container);\n UpdateTenantFeature.register(container);\n TenantContextFeature.register(container);\n InstallTenantFeature.register(container);\n }\n});\n"],"names":["TenancyFeature","createFeature","container","storageOperations","TenancyStorageOperations","TenantCache","TenantCacheImpl","ids","tenants","id","tenant","CreateTenantFeature","DeleteTenantFeature","GetRootTenantFeature","GetTenantByIdFeature","ListTenantsFeature","UpdateTenantFeature","TenantContextFeature","InstallTenantFeature"],"mappings":";;;;;;;;;;;;AAiBO,MAAMA,iBAAiBC,cAAyC;IACnE,MAAM;IACN,UAAU,CAACC,WAAWC;QAElBD,UAAU,gBAAgB,CAACE,0BAA0BD;QAGrDD,UAAU,gBAAgB,CACtBG,aACA,IAAIC,2BAAgB,OAAOC;YACvB,IAAIA,AAAe,MAAfA,IAAI,MAAM,EACV,OAAO,EAAE;YAEb,MAAMC,UAAU,MAAML,kBAAkB,eAAe,CAACI;YAExD,OAAOA,IAAI,GAAG,CAACE,CAAAA,KAAMD,QAAQ,IAAI,CAACE,CAAAA,SAAUA,OAAO,EAAE,KAAKD;QAC9D;QAIJE,oBAAoB,QAAQ,CAACT;QAC7BU,oBAAoB,QAAQ,CAACV;QAC7BW,qBAAqB,QAAQ,CAACX;QAC9BY,qBAAqB,QAAQ,CAACZ;QAC9Ba,mBAAmB,QAAQ,CAACb;QAC5Bc,oBAAoB,QAAQ,CAACd;QAC7Be,qBAAqB,QAAQ,CAACf;QAC9BgB,qBAAqB,QAAQ,CAAChB;IAClC;AACJ"}
|
|
@@ -9,6 +9,7 @@ import { DeleteUserFeature } from "./DeleteUser/feature.js";
|
|
|
9
9
|
import { ListUserTeamsFeature } from "./ListUserTeams/feature.js";
|
|
10
10
|
import { AdminUsersStorageOperations } from "./shared/storageAbstractions.js";
|
|
11
11
|
import { ExternalIdpUserSyncFeature } from "./ExternalIdpUserSync/index.js";
|
|
12
|
+
import { FindUserRecordsFeature } from "./FindUserRecords/feature.js";
|
|
12
13
|
const AdminUsersFeature = createFeature({
|
|
13
14
|
name: "AdminUsers",
|
|
14
15
|
register (container, storageOperations) {
|
|
@@ -21,6 +22,7 @@ const AdminUsersFeature = createFeature({
|
|
|
21
22
|
UpdateUserFeature.register(container);
|
|
22
23
|
DeleteUserFeature.register(container);
|
|
23
24
|
ExternalIdpUserSyncFeature.register(container);
|
|
25
|
+
FindUserRecordsFeature.register(container);
|
|
24
26
|
container.registerInstance(AdminUsersStorageOperations, storageOperations);
|
|
25
27
|
}
|
|
26
28
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"features/users/AdminUsersFeature.js","sources":["../../../src/features/users/AdminUsersFeature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport { AdminUsersRepository } from \"./shared/AdminUsersRepository.js\";\nimport { GetUserFeature } from \"./GetUser/feature.js\";\nimport { GetIdentityProfileFeature } from \"./GetIdentityProfile/feature.js\";\nimport { ListUsersFeature } from \"./ListUsers/feature.js\";\nimport { CreateUserFeature } from \"./CreateUser/feature.js\";\nimport { UpdateUserFeature } from \"./UpdateUser/feature.js\";\nimport { DeleteUserFeature } from \"./DeleteUser/feature.js\";\nimport { ListUserTeamsFeature } from \"./ListUserTeams/feature.js\";\nimport { AdminUsersStorageOperations } from \"./shared/storageAbstractions.js\";\nimport { ExternalIdpUserSyncFeature } from \"./ExternalIdpUserSync/index.js\";\n\nexport const AdminUsersFeature = createFeature<AdminUsersStorageOperations.Interface>({\n name: \"AdminUsers\",\n register(container, storageOperations) {\n // Register repository in singleton scope\n container.register(AdminUsersRepository).inSingletonScope();\n\n // Register all use cases\n GetUserFeature.register(container);\n GetIdentityProfileFeature.register(container);\n ListUsersFeature.register(container);\n ListUserTeamsFeature.register(container);\n CreateUserFeature.register(container);\n UpdateUserFeature.register(container);\n DeleteUserFeature.register(container);\n ExternalIdpUserSyncFeature.register(container);\n\n // Legacy storage ops\n container.registerInstance(AdminUsersStorageOperations, storageOperations);\n }\n});\n"],"names":["AdminUsersFeature","createFeature","container","storageOperations","AdminUsersRepository","GetUserFeature","GetIdentityProfileFeature","ListUsersFeature","ListUserTeamsFeature","CreateUserFeature","UpdateUserFeature","DeleteUserFeature","ExternalIdpUserSyncFeature","AdminUsersStorageOperations"],"mappings":"
|
|
1
|
+
{"version":3,"file":"features/users/AdminUsersFeature.js","sources":["../../../src/features/users/AdminUsersFeature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport { AdminUsersRepository } from \"./shared/AdminUsersRepository.js\";\nimport { GetUserFeature } from \"./GetUser/feature.js\";\nimport { GetIdentityProfileFeature } from \"./GetIdentityProfile/feature.js\";\nimport { ListUsersFeature } from \"./ListUsers/feature.js\";\nimport { CreateUserFeature } from \"./CreateUser/feature.js\";\nimport { UpdateUserFeature } from \"./UpdateUser/feature.js\";\nimport { DeleteUserFeature } from \"./DeleteUser/feature.js\";\nimport { ListUserTeamsFeature } from \"./ListUserTeams/feature.js\";\nimport { AdminUsersStorageOperations } from \"./shared/storageAbstractions.js\";\nimport { ExternalIdpUserSyncFeature } from \"./ExternalIdpUserSync/index.js\";\nimport { FindUserRecordsFeature } from \"./FindUserRecords/feature.js\";\n\nexport const AdminUsersFeature = createFeature<AdminUsersStorageOperations.Interface>({\n name: \"AdminUsers\",\n register(container, storageOperations) {\n // Register repository in singleton scope\n container.register(AdminUsersRepository).inSingletonScope();\n\n // Register all use cases\n GetUserFeature.register(container);\n GetIdentityProfileFeature.register(container);\n ListUsersFeature.register(container);\n ListUserTeamsFeature.register(container);\n CreateUserFeature.register(container);\n UpdateUserFeature.register(container);\n DeleteUserFeature.register(container);\n ExternalIdpUserSyncFeature.register(container);\n FindUserRecordsFeature.register(container);\n\n // Legacy storage ops\n container.registerInstance(AdminUsersStorageOperations, storageOperations);\n }\n});\n"],"names":["AdminUsersFeature","createFeature","container","storageOperations","AdminUsersRepository","GetUserFeature","GetIdentityProfileFeature","ListUsersFeature","ListUserTeamsFeature","CreateUserFeature","UpdateUserFeature","DeleteUserFeature","ExternalIdpUserSyncFeature","FindUserRecordsFeature","AdminUsersStorageOperations"],"mappings":";;;;;;;;;;;;AAaO,MAAMA,oBAAoBC,cAAqD;IAClF,MAAM;IACN,UAASC,SAAS,EAAEC,iBAAiB;QAEjCD,UAAU,QAAQ,CAACE,sBAAsB,gBAAgB;QAGzDC,eAAe,QAAQ,CAACH;QACxBI,0BAA0B,QAAQ,CAACJ;QACnCK,iBAAiB,QAAQ,CAACL;QAC1BM,qBAAqB,QAAQ,CAACN;QAC9BO,kBAAkB,QAAQ,CAACP;QAC3BQ,kBAAkB,QAAQ,CAACR;QAC3BS,kBAAkB,QAAQ,CAACT;QAC3BU,2BAA2B,QAAQ,CAACV;QACpCW,uBAAuB,QAAQ,CAACX;QAGhCA,UAAU,gBAAgB,CAACY,6BAA6BX;IAC5D;AACJ"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { FindUserRecordsUseCase as Abstraction } from "./abstractions.js";
|
|
2
|
+
import { ListTenantsUseCase } from "../../../features/tenancy/ListTenants/index.js";
|
|
3
|
+
import { AdminUsersRepository } from "../../../features/users/shared/abstractions.js";
|
|
4
|
+
declare class FindUserRecordsUseCaseImpl implements Abstraction.Interface {
|
|
5
|
+
private listTenants;
|
|
6
|
+
private repository;
|
|
7
|
+
constructor(listTenants: ListTenantsUseCase.Interface, repository: AdminUsersRepository.Interface);
|
|
8
|
+
execute(id: string, options?: Abstraction.Options): Abstraction.Return;
|
|
9
|
+
private probeAllTenants;
|
|
10
|
+
}
|
|
11
|
+
export declare const FindUserRecordsUseCase: import("@webiny/di").Implementation<typeof FindUserRecordsUseCaseImpl>;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Result, createImplementation } from "@webiny/feature/api";
|
|
2
|
+
import { FindUserRecordsUseCase } from "./abstractions.js";
|
|
3
|
+
import { ListTenantsUseCase } from "../../tenancy/ListTenants/index.js";
|
|
4
|
+
import { AdminUsersRepository } from "../shared/abstractions.js";
|
|
5
|
+
import { UserStorageError } from "../shared/errors.js";
|
|
6
|
+
class FindUserRecordsUseCaseImpl {
|
|
7
|
+
constructor(listTenants, repository){
|
|
8
|
+
this.listTenants = listTenants;
|
|
9
|
+
this.repository = repository;
|
|
10
|
+
}
|
|
11
|
+
async execute(id, options = {}) {
|
|
12
|
+
const indexedResult = await this.repository.listUserRecords({
|
|
13
|
+
id
|
|
14
|
+
});
|
|
15
|
+
if (indexedResult.isFail()) return Result.fail(indexedResult.error);
|
|
16
|
+
const indexed = indexedResult.value;
|
|
17
|
+
if (indexed.length > 0 && !options.exhaustive) return Result.ok(indexed);
|
|
18
|
+
const probedResult = await this.probeAllTenants(id);
|
|
19
|
+
if (probedResult.isFail()) return Result.fail(probedResult.error);
|
|
20
|
+
const indexedTenants = new Set(indexed.map((user)=>user.tenant));
|
|
21
|
+
const unindexed = probedResult.value.filter((user)=>!indexedTenants.has(user.tenant));
|
|
22
|
+
await Promise.all(unindexed.map((user)=>this.repository.update(user)));
|
|
23
|
+
return Result.ok([
|
|
24
|
+
...indexed,
|
|
25
|
+
...unindexed
|
|
26
|
+
]);
|
|
27
|
+
}
|
|
28
|
+
async probeAllTenants(id) {
|
|
29
|
+
let tenantsResult;
|
|
30
|
+
try {
|
|
31
|
+
tenantsResult = await this.listTenants.execute();
|
|
32
|
+
} catch (error) {
|
|
33
|
+
return Result.fail(new UserStorageError(error));
|
|
34
|
+
}
|
|
35
|
+
if (tenantsResult.isFail()) return Result.fail(new UserStorageError(tenantsResult.error));
|
|
36
|
+
const tenants = tenantsResult.value.filter((tenant)=>tenant.isInstalled);
|
|
37
|
+
if (0 === tenants.length) return Result.ok([]);
|
|
38
|
+
return this.repository.getUserAcrossTenants({
|
|
39
|
+
id,
|
|
40
|
+
tenants: tenants.map((tenant)=>tenant.id)
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const FindUserRecordsUseCase_FindUserRecordsUseCase = createImplementation({
|
|
45
|
+
abstraction: FindUserRecordsUseCase,
|
|
46
|
+
implementation: FindUserRecordsUseCaseImpl,
|
|
47
|
+
dependencies: [
|
|
48
|
+
ListTenantsUseCase,
|
|
49
|
+
AdminUsersRepository
|
|
50
|
+
]
|
|
51
|
+
});
|
|
52
|
+
export { FindUserRecordsUseCase_FindUserRecordsUseCase as FindUserRecordsUseCase };
|
|
53
|
+
|
|
54
|
+
//# sourceMappingURL=FindUserRecordsUseCase.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"features/users/FindUserRecords/FindUserRecordsUseCase.js","sources":["../../../../src/features/users/FindUserRecords/FindUserRecordsUseCase.ts"],"sourcesContent":["import { createImplementation } from \"@webiny/feature/api\";\nimport { Result } from \"@webiny/feature/api\";\nimport { FindUserRecordsUseCase as Abstraction } from \"./abstractions.js\";\nimport { ListTenantsUseCase } from \"~/features/tenancy/ListTenants/index.js\";\nimport { AdminUsersRepository } from \"~/features/users/shared/abstractions.js\";\nimport { UserStorageError } from \"~/features/users/shared/errors.js\";\nimport type { AdminUser } from \"~/features/users/shared/types.js\";\n\nclass FindUserRecordsUseCaseImpl implements Abstraction.Interface {\n constructor(\n private listTenants: ListTenantsUseCase.Interface,\n private repository: AdminUsersRepository.Interface\n ) {}\n\n async execute(id: string, options: Abstraction.Options = {}): Abstraction.Return {\n const indexedResult = await this.repository.listUserRecords({ id });\n if (indexedResult.isFail()) {\n return Result.fail(indexedResult.error);\n }\n\n const indexed = indexedResult.value;\n if (indexed.length > 0 && !options.exhaustive) {\n return Result.ok(indexed);\n }\n\n const probedResult = await this.probeAllTenants(id);\n if (probedResult.isFail()) {\n return Result.fail(probedResult.error);\n }\n\n const indexedTenants = new Set(indexed.map(user => user.tenant));\n const unindexed = probedResult.value.filter(user => !indexedTenants.has(user.tenant));\n\n // Rewriting a record adds the index keys. A failed write only means the next lookup probes again.\n await Promise.all(unindexed.map(user => this.repository.update(user)));\n\n return Result.ok([...indexed, ...unindexed]);\n }\n\n private async probeAllTenants(id: string): Promise<Result<AdminUser[], Abstraction.Error>> {\n let tenantsResult: Awaited<ReturnType<ListTenantsUseCase.Interface[\"execute\"]>>;\n try {\n tenantsResult = await this.listTenants.execute();\n } catch (error) {\n return Result.fail(new UserStorageError(error as Error));\n }\n\n if (tenantsResult.isFail()) {\n return Result.fail(new UserStorageError(tenantsResult.error as Error));\n }\n\n // Uninstalled tenants have no users yet.\n const tenants = tenantsResult.value.filter(tenant => tenant.isInstalled);\n if (tenants.length === 0) {\n return Result.ok([]);\n }\n\n return this.repository.getUserAcrossTenants({\n id,\n tenants: tenants.map(tenant => tenant.id)\n });\n }\n}\n\nexport const FindUserRecordsUseCase = createImplementation({\n abstraction: Abstraction,\n implementation: FindUserRecordsUseCaseImpl,\n dependencies: [ListTenantsUseCase, AdminUsersRepository]\n});\n"],"names":["FindUserRecordsUseCaseImpl","listTenants","repository","id","options","indexedResult","Result","indexed","probedResult","indexedTenants","Set","user","unindexed","Promise","tenantsResult","error","UserStorageError","tenants","tenant","FindUserRecordsUseCase","createImplementation","Abstraction","ListTenantsUseCase","AdminUsersRepository"],"mappings":";;;;;AAQA,MAAMA;IACF,YACYC,WAAyC,EACzCC,UAA0C,CACpD;aAFUD,WAAW,GAAXA;aACAC,UAAU,GAAVA;IACT;IAEH,MAAM,QAAQC,EAAU,EAAEC,UAA+B,CAAC,CAAC,EAAsB;QAC7E,MAAMC,gBAAgB,MAAM,IAAI,CAAC,UAAU,CAAC,eAAe,CAAC;YAAEF;QAAG;QACjE,IAAIE,cAAc,MAAM,IACpB,OAAOC,OAAO,IAAI,CAACD,cAAc,KAAK;QAG1C,MAAME,UAAUF,cAAc,KAAK;QACnC,IAAIE,QAAQ,MAAM,GAAG,KAAK,CAACH,QAAQ,UAAU,EACzC,OAAOE,OAAO,EAAE,CAACC;QAGrB,MAAMC,eAAe,MAAM,IAAI,CAAC,eAAe,CAACL;QAChD,IAAIK,aAAa,MAAM,IACnB,OAAOF,OAAO,IAAI,CAACE,aAAa,KAAK;QAGzC,MAAMC,iBAAiB,IAAIC,IAAIH,QAAQ,GAAG,CAACI,CAAAA,OAAQA,KAAK,MAAM;QAC9D,MAAMC,YAAYJ,aAAa,KAAK,CAAC,MAAM,CAACG,CAAAA,OAAQ,CAACF,eAAe,GAAG,CAACE,KAAK,MAAM;QAGnF,MAAME,QAAQ,GAAG,CAACD,UAAU,GAAG,CAACD,CAAAA,OAAQ,IAAI,CAAC,UAAU,CAAC,MAAM,CAACA;QAE/D,OAAOL,OAAO,EAAE,CAAC;eAAIC;eAAYK;SAAU;IAC/C;IAEA,MAAc,gBAAgBT,EAAU,EAAmD;QACvF,IAAIW;QACJ,IAAI;YACAA,gBAAgB,MAAM,IAAI,CAAC,WAAW,CAAC,OAAO;QAClD,EAAE,OAAOC,OAAO;YACZ,OAAOT,OAAO,IAAI,CAAC,IAAIU,iBAAiBD;QAC5C;QAEA,IAAID,cAAc,MAAM,IACpB,OAAOR,OAAO,IAAI,CAAC,IAAIU,iBAAiBF,cAAc,KAAK;QAI/D,MAAMG,UAAUH,cAAc,KAAK,CAAC,MAAM,CAACI,CAAAA,SAAUA,OAAO,WAAW;QACvE,IAAID,AAAmB,MAAnBA,QAAQ,MAAM,EACd,OAAOX,OAAO,EAAE,CAAC,EAAE;QAGvB,OAAO,IAAI,CAAC,UAAU,CAAC,oBAAoB,CAAC;YACxCH;YACA,SAASc,QAAQ,GAAG,CAACC,CAAAA,SAAUA,OAAO,EAAE;QAC5C;IACJ;AACJ;AAEO,MAAMC,gDAAyBC,qBAAqB;IACvD,aAAaC;IACb,gBAAgBrB;IAChB,cAAc;QAACsB;QAAoBC;KAAqB;AAC5D"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Result } from "@webiny/feature/api";
|
|
2
|
+
import type { AdminUser } from "../../../features/users/shared/types.js";
|
|
3
|
+
import { AdminUsersRepository } from "../../../features/users/shared/abstractions.js";
|
|
4
|
+
export interface FindUserRecordsOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Also probe every installed tenant, instead of trusting a non-empty index result.
|
|
7
|
+
* Records written before the user→tenant index existed are only found this way, so
|
|
8
|
+
* anything that acts on "the user has no other tenant" (deleting the sign-in account,
|
|
9
|
+
* changing its password) must set it.
|
|
10
|
+
*/
|
|
11
|
+
exhaustive?: boolean;
|
|
12
|
+
}
|
|
13
|
+
type FindUserRecordsError = AdminUsersRepository.Error;
|
|
14
|
+
export interface IFindUserRecordsUseCase {
|
|
15
|
+
/**
|
|
16
|
+
* Find the user's records in every tenant (disabled tenants included).
|
|
17
|
+
* Records found outside the user→tenant index are added to it.
|
|
18
|
+
*/
|
|
19
|
+
execute(id: string, options?: FindUserRecordsOptions): Promise<Result<AdminUser[], FindUserRecordsError>>;
|
|
20
|
+
}
|
|
21
|
+
/** Find every tenant's record of one user. */
|
|
22
|
+
export declare const FindUserRecordsUseCase: import("@webiny/di").Abstraction<IFindUserRecordsUseCase>;
|
|
23
|
+
export declare namespace FindUserRecordsUseCase {
|
|
24
|
+
type Interface = IFindUserRecordsUseCase;
|
|
25
|
+
type Options = FindUserRecordsOptions;
|
|
26
|
+
type Error = FindUserRecordsError;
|
|
27
|
+
type Return = Promise<Result<AdminUser[], FindUserRecordsError>>;
|
|
28
|
+
}
|
|
29
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"features/users/FindUserRecords/abstractions.js","sources":["../../../../src/features/users/FindUserRecords/abstractions.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport { Result } from \"@webiny/feature/api\";\nimport type { AdminUser } from \"~/features/users/shared/types.js\";\nimport { AdminUsersRepository } from \"~/features/users/shared/abstractions.js\";\n\nexport interface FindUserRecordsOptions {\n /**\n * Also probe every installed tenant, instead of trusting a non-empty index result.\n * Records written before the user→tenant index existed are only found this way, so\n * anything that acts on \"the user has no other tenant\" (deleting the sign-in account,\n * changing its password) must set it.\n */\n exhaustive?: boolean;\n}\n\ntype FindUserRecordsError = AdminUsersRepository.Error;\n\nexport interface IFindUserRecordsUseCase {\n /**\n * Find the user's records in every tenant (disabled tenants included).\n * Records found outside the user→tenant index are added to it.\n */\n execute(\n id: string,\n options?: FindUserRecordsOptions\n ): Promise<Result<AdminUser[], FindUserRecordsError>>;\n}\n\n/** Find every tenant's record of one user. */\nexport const FindUserRecordsUseCase =\n createAbstraction<IFindUserRecordsUseCase>(\"FindUserRecordsUseCase\");\n\nexport namespace FindUserRecordsUseCase {\n export type Interface = IFindUserRecordsUseCase;\n export type Options = FindUserRecordsOptions;\n export type Error = FindUserRecordsError;\n export type Return = Promise<Result<AdminUser[], FindUserRecordsError>>;\n}\n"],"names":["FindUserRecordsUseCase","createAbstraction"],"mappings":";AA6BO,MAAMA,yBACTC,kBAA2C"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createFeature } from "@webiny/feature/api";
|
|
2
|
+
import { FindUserRecordsUseCase } from "./FindUserRecordsUseCase.js";
|
|
3
|
+
const FindUserRecordsFeature = createFeature({
|
|
4
|
+
name: "FindUserRecords",
|
|
5
|
+
register (container) {
|
|
6
|
+
container.register(FindUserRecordsUseCase);
|
|
7
|
+
}
|
|
8
|
+
});
|
|
9
|
+
export { FindUserRecordsFeature };
|
|
10
|
+
|
|
11
|
+
//# sourceMappingURL=feature.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"features/users/FindUserRecords/feature.js","sources":["../../../../src/features/users/FindUserRecords/feature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport { FindUserRecordsUseCase } from \"./FindUserRecordsUseCase.js\";\n\nexport const FindUserRecordsFeature = createFeature({\n name: \"FindUserRecords\",\n register(container) {\n container.register(FindUserRecordsUseCase);\n }\n});\n"],"names":["FindUserRecordsFeature","createFeature","container","FindUserRecordsUseCase"],"mappings":";;AAGO,MAAMA,yBAAyBC,cAAc;IAChD,MAAM;IACN,UAASC,SAAS;QACdA,UAAU,QAAQ,CAACC;IACvB;AACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { FindUserRecordsUseCase } from "./abstractions.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { FindUserRecordsUseCase } from "./abstractions.js";
|
|
@@ -3,7 +3,7 @@ import { TenantContext } from "../../../features/tenancy/TenantContext/index.js"
|
|
|
3
3
|
import { AdminUsersRepository as RepositoryAbstraction } from "./abstractions.js";
|
|
4
4
|
import { AdminUsersStorageOperations } from "./storageAbstractions.js";
|
|
5
5
|
import type { AdminUser } from "./types.js";
|
|
6
|
-
import type { GetUserInput, ListUsersInput } from "./types.js";
|
|
6
|
+
import type { GetUserAcrossTenantsInput, GetUserInput, ListUserRecordsInput, ListUsersInput } from "./types.js";
|
|
7
7
|
declare class AdminUsersRepositoryImpl implements RepositoryAbstraction.Interface {
|
|
8
8
|
private tenantContext;
|
|
9
9
|
private storageOperations;
|
|
@@ -11,6 +11,8 @@ declare class AdminUsersRepositoryImpl implements RepositoryAbstraction.Interfac
|
|
|
11
11
|
constructor(tenantContext: TenantContext.Interface, storageOperations: AdminUsersStorageOperations.Interface);
|
|
12
12
|
get(params: GetUserInput): Promise<Result<AdminUser, RepositoryAbstraction.Error>>;
|
|
13
13
|
list(params: ListUsersInput): Promise<Result<AdminUser[], RepositoryAbstraction.Error>>;
|
|
14
|
+
getUserAcrossTenants(params: GetUserAcrossTenantsInput): Promise<Result<AdminUser[], RepositoryAbstraction.Error>>;
|
|
15
|
+
listUserRecords(params: ListUserRecordsInput): Promise<Result<AdminUser[], RepositoryAbstraction.Error>>;
|
|
14
16
|
create(user: AdminUser): Promise<Result<AdminUser, RepositoryAbstraction.Error>>;
|
|
15
17
|
update(user: AdminUser): Promise<Result<AdminUser, RepositoryAbstraction.Error>>;
|
|
16
18
|
delete(user: AdminUser): Promise<Result<void, RepositoryAbstraction.Error>>;
|
|
@@ -27,7 +27,7 @@ class AdminUsersRepositoryImpl {
|
|
|
27
27
|
if (params.email) {
|
|
28
28
|
const user = await this.storageOperations.getUser({
|
|
29
29
|
where: {
|
|
30
|
-
tenant,
|
|
30
|
+
tenant: params.tenant ?? tenant,
|
|
31
31
|
email: params.email
|
|
32
32
|
}
|
|
33
33
|
});
|
|
@@ -56,6 +56,22 @@ class AdminUsersRepositoryImpl {
|
|
|
56
56
|
return Result.fail(new UserStorageError(error));
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
|
+
async getUserAcrossTenants(params) {
|
|
60
|
+
try {
|
|
61
|
+
const users = await this.storageOperations.getUserAcrossTenants(params);
|
|
62
|
+
return Result.ok(users);
|
|
63
|
+
} catch (error) {
|
|
64
|
+
return Result.fail(new UserStorageError(error));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async listUserRecords(params) {
|
|
68
|
+
try {
|
|
69
|
+
const users = await this.storageOperations.listUserRecords(params);
|
|
70
|
+
return Result.ok(users);
|
|
71
|
+
} catch (error) {
|
|
72
|
+
return Result.fail(new UserStorageError(error));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
59
75
|
async create(user) {
|
|
60
76
|
try {
|
|
61
77
|
const result = await this.storageOperations.createUser({
|
|
@@ -77,11 +93,11 @@ class AdminUsersRepositoryImpl {
|
|
|
77
93
|
await this.storageOperations.updateUser({
|
|
78
94
|
user
|
|
79
95
|
});
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
tenant,
|
|
96
|
+
const key = {
|
|
97
|
+
tenant: user.tenant,
|
|
83
98
|
id: user.id
|
|
84
|
-
}
|
|
99
|
+
};
|
|
100
|
+
this.loaders.getUser.clear(key).prime(key, user);
|
|
85
101
|
return Result.ok(user);
|
|
86
102
|
} catch (error) {
|
|
87
103
|
return Result.fail(new UserStorageError(error));
|
|
@@ -92,10 +108,9 @@ class AdminUsersRepositoryImpl {
|
|
|
92
108
|
await this.storageOperations.deleteUser({
|
|
93
109
|
user
|
|
94
110
|
});
|
|
95
|
-
const tenant = this.tenantContext.getTenant().id;
|
|
96
111
|
this.clearCache([
|
|
97
112
|
{
|
|
98
|
-
tenant,
|
|
113
|
+
tenant: user.tenant,
|
|
99
114
|
id: user.id
|
|
100
115
|
}
|
|
101
116
|
]);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"features/users/shared/AdminUsersRepository.js","sources":["../../../../src/features/users/shared/AdminUsersRepository.ts"],"sourcesContent":["import { createImplementation } from \"@webiny/feature/api\";\nimport { Result } from \"@webiny/feature/api\";\nimport { TenantContext } from \"~/features/tenancy/TenantContext/index.js\";\nimport { AdminUsersRepository as RepositoryAbstraction } from \"./abstractions.js\";\nimport { AdminUsersStorageOperations } from \"./storageAbstractions.js\";\nimport { createUserLoaders } from \"./loaders.js\";\nimport { UserNotFoundError, UserStorageError } from \"./errors.js\";\nimport type { AdminUser } from \"./types.js\";\nimport type {
|
|
1
|
+
{"version":3,"file":"features/users/shared/AdminUsersRepository.js","sources":["../../../../src/features/users/shared/AdminUsersRepository.ts"],"sourcesContent":["import { createImplementation } from \"@webiny/feature/api\";\nimport { Result } from \"@webiny/feature/api\";\nimport { TenantContext } from \"~/features/tenancy/TenantContext/index.js\";\nimport { AdminUsersRepository as RepositoryAbstraction } from \"./abstractions.js\";\nimport { AdminUsersStorageOperations } from \"./storageAbstractions.js\";\nimport { createUserLoaders } from \"./loaders.js\";\nimport { UserNotFoundError, UserStorageError } from \"./errors.js\";\nimport type { AdminUser } from \"./types.js\";\nimport type {\n GetUserAcrossTenantsInput,\n GetUserInput,\n ListUserRecordsInput,\n ListUsersInput\n} from \"./types.js\";\n\nclass AdminUsersRepositoryImpl implements RepositoryAbstraction.Interface {\n private loaders: ReturnType<typeof createUserLoaders>;\n\n constructor(\n private tenantContext: TenantContext.Interface,\n private storageOperations: AdminUsersStorageOperations.Interface\n ) {\n // Initialize DataLoaders using existing implementation\n this.loaders = createUserLoaders({\n getTenant: () => this.tenantContext.getTenant().id,\n storageOperations: this.storageOperations\n });\n }\n\n async get(params: GetUserInput): Promise<Result<AdminUser, RepositoryAbstraction.Error>> {\n try {\n const tenant = this.tenantContext.getTenant().id;\n\n // Use DataLoader for ID-based queries (majority of queries)\n if (params.id) {\n const user = await this.loaders.getUser.load({\n tenant,\n id: params.id\n });\n\n if (!user) {\n return Result.fail(new UserNotFoundError(params.id));\n }\n\n return Result.ok(user);\n }\n\n // Direct query for email-based lookups (rare)\n if (params.email) {\n const user = await this.storageOperations.getUser({\n where: { tenant: params.tenant ?? tenant, email: params.email }\n });\n\n if (!user) {\n return Result.fail(new UserNotFoundError(params.email));\n }\n\n return Result.ok(user);\n }\n\n return Result.fail(new UserNotFoundError(\"unknown\"));\n } catch (error) {\n return Result.fail(new UserStorageError(error as Error));\n }\n }\n\n async list(params: ListUsersInput): Promise<Result<AdminUser[], RepositoryAbstraction.Error>> {\n try {\n const tenant = this.tenantContext.getTenant().id;\n const users = await this.storageOperations.listUsers({\n where: { tenant, ...params.where },\n sort: params.sort || [\"createdOn_ASC\"]\n });\n\n return Result.ok(users);\n } catch (error) {\n return Result.fail(new UserStorageError(error as Error));\n }\n }\n\n async getUserAcrossTenants(\n params: GetUserAcrossTenantsInput\n ): Promise<Result<AdminUser[], RepositoryAbstraction.Error>> {\n try {\n const users = await this.storageOperations.getUserAcrossTenants(params);\n\n return Result.ok(users);\n } catch (error) {\n return Result.fail(new UserStorageError(error as Error));\n }\n }\n\n async listUserRecords(\n params: ListUserRecordsInput\n ): Promise<Result<AdminUser[], RepositoryAbstraction.Error>> {\n try {\n const users = await this.storageOperations.listUserRecords(params);\n\n return Result.ok(users);\n } catch (error) {\n return Result.fail(new UserStorageError(error as Error));\n }\n }\n\n async create(user: AdminUser): Promise<Result<AdminUser, RepositoryAbstraction.Error>> {\n try {\n const result = await this.storageOperations.createUser({ user });\n\n // Prime the cache with the new user\n const tenant = this.tenantContext.getTenant().id;\n const key = { tenant, id: result.id };\n this.loaders.getUser.clear(key).prime(key, result);\n\n return Result.ok(result);\n } catch (error) {\n return Result.fail(new UserStorageError(error as Error));\n }\n }\n\n async update(user: AdminUser): Promise<Result<AdminUser, RepositoryAbstraction.Error>> {\n try {\n await this.storageOperations.updateUser({ user });\n\n // Update the cache. The record can belong to another tenant than the current one.\n const key = { tenant: user.tenant, id: user.id };\n this.loaders.getUser.clear(key).prime(key, user);\n\n return Result.ok(user);\n } catch (error) {\n return Result.fail(new UserStorageError(error as Error));\n }\n }\n\n async delete(user: AdminUser): Promise<Result<void, RepositoryAbstraction.Error>> {\n try {\n await this.storageOperations.deleteUser({ user });\n\n // Clear from cache\n this.clearCache([{ tenant: user.tenant, id: user.id }]);\n\n return Result.ok();\n } catch (error) {\n return Result.fail(new UserStorageError(error as Error));\n }\n }\n\n clearCache(keys: Array<{ tenant: string; id: string }>): void {\n this.loaders.clearLoadersCache(keys);\n }\n}\n\nexport const AdminUsersRepository = createImplementation({\n abstraction: RepositoryAbstraction,\n implementation: AdminUsersRepositoryImpl,\n dependencies: [TenantContext, AdminUsersStorageOperations]\n});\n"],"names":["AdminUsersRepositoryImpl","tenantContext","storageOperations","createUserLoaders","params","tenant","user","Result","UserNotFoundError","error","UserStorageError","users","result","key","keys","AdminUsersRepository","createImplementation","RepositoryAbstraction","TenantContext","AdminUsersStorageOperations"],"mappings":";;;;;;AAeA,MAAMA;IAGF,YACYC,aAAsC,EACtCC,iBAAwD,CAClE;aAFUD,aAAa,GAAbA;aACAC,iBAAiB,GAAjBA;QAGR,IAAI,CAAC,OAAO,GAAGC,kBAAkB;YAC7B,WAAW,IAAM,IAAI,CAAC,aAAa,CAAC,SAAS,GAAG,EAAE;YAClD,mBAAmB,IAAI,CAAC,iBAAiB;QAC7C;IACJ;IAEA,MAAM,IAAIC,MAAoB,EAA2D;QACrF,IAAI;YACA,MAAMC,SAAS,IAAI,CAAC,aAAa,CAAC,SAAS,GAAG,EAAE;YAGhD,IAAID,OAAO,EAAE,EAAE;gBACX,MAAME,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC;oBACzCD;oBACA,IAAID,OAAO,EAAE;gBACjB;gBAEA,IAAI,CAACE,MACD,OAAOC,OAAO,IAAI,CAAC,IAAIC,kBAAkBJ,OAAO,EAAE;gBAGtD,OAAOG,OAAO,EAAE,CAACD;YACrB;YAGA,IAAIF,OAAO,KAAK,EAAE;gBACd,MAAME,OAAO,MAAM,IAAI,CAAC,iBAAiB,CAAC,OAAO,CAAC;oBAC9C,OAAO;wBAAE,QAAQF,OAAO,MAAM,IAAIC;wBAAQ,OAAOD,OAAO,KAAK;oBAAC;gBAClE;gBAEA,IAAI,CAACE,MACD,OAAOC,OAAO,IAAI,CAAC,IAAIC,kBAAkBJ,OAAO,KAAK;gBAGzD,OAAOG,OAAO,EAAE,CAACD;YACrB;YAEA,OAAOC,OAAO,IAAI,CAAC,IAAIC,kBAAkB;QAC7C,EAAE,OAAOC,OAAO;YACZ,OAAOF,OAAO,IAAI,CAAC,IAAIG,iBAAiBD;QAC5C;IACJ;IAEA,MAAM,KAAKL,MAAsB,EAA6D;QAC1F,IAAI;YACA,MAAMC,SAAS,IAAI,CAAC,aAAa,CAAC,SAAS,GAAG,EAAE;YAChD,MAAMM,QAAQ,MAAM,IAAI,CAAC,iBAAiB,CAAC,SAAS,CAAC;gBACjD,OAAO;oBAAEN;oBAAQ,GAAGD,OAAO,KAAK;gBAAC;gBACjC,MAAMA,OAAO,IAAI,IAAI;oBAAC;iBAAgB;YAC1C;YAEA,OAAOG,OAAO,EAAE,CAACI;QACrB,EAAE,OAAOF,OAAO;YACZ,OAAOF,OAAO,IAAI,CAAC,IAAIG,iBAAiBD;QAC5C;IACJ;IAEA,MAAM,qBACFL,MAAiC,EACwB;QACzD,IAAI;YACA,MAAMO,QAAQ,MAAM,IAAI,CAAC,iBAAiB,CAAC,oBAAoB,CAACP;YAEhE,OAAOG,OAAO,EAAE,CAACI;QACrB,EAAE,OAAOF,OAAO;YACZ,OAAOF,OAAO,IAAI,CAAC,IAAIG,iBAAiBD;QAC5C;IACJ;IAEA,MAAM,gBACFL,MAA4B,EAC6B;QACzD,IAAI;YACA,MAAMO,QAAQ,MAAM,IAAI,CAAC,iBAAiB,CAAC,eAAe,CAACP;YAE3D,OAAOG,OAAO,EAAE,CAACI;QACrB,EAAE,OAAOF,OAAO;YACZ,OAAOF,OAAO,IAAI,CAAC,IAAIG,iBAAiBD;QAC5C;IACJ;IAEA,MAAM,OAAOH,IAAe,EAA2D;QACnF,IAAI;YACA,MAAMM,SAAS,MAAM,IAAI,CAAC,iBAAiB,CAAC,UAAU,CAAC;gBAAEN;YAAK;YAG9D,MAAMD,SAAS,IAAI,CAAC,aAAa,CAAC,SAAS,GAAG,EAAE;YAChD,MAAMQ,MAAM;gBAAER;gBAAQ,IAAIO,OAAO,EAAE;YAAC;YACpC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAACC,KAAK,KAAK,CAACA,KAAKD;YAE3C,OAAOL,OAAO,EAAE,CAACK;QACrB,EAAE,OAAOH,OAAO;YACZ,OAAOF,OAAO,IAAI,CAAC,IAAIG,iBAAiBD;QAC5C;IACJ;IAEA,MAAM,OAAOH,IAAe,EAA2D;QACnF,IAAI;YACA,MAAM,IAAI,CAAC,iBAAiB,CAAC,UAAU,CAAC;gBAAEA;YAAK;YAG/C,MAAMO,MAAM;gBAAE,QAAQP,KAAK,MAAM;gBAAE,IAAIA,KAAK,EAAE;YAAC;YAC/C,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAACO,KAAK,KAAK,CAACA,KAAKP;YAE3C,OAAOC,OAAO,EAAE,CAACD;QACrB,EAAE,OAAOG,OAAO;YACZ,OAAOF,OAAO,IAAI,CAAC,IAAIG,iBAAiBD;QAC5C;IACJ;IAEA,MAAM,OAAOH,IAAe,EAAsD;QAC9E,IAAI;YACA,MAAM,IAAI,CAAC,iBAAiB,CAAC,UAAU,CAAC;gBAAEA;YAAK;YAG/C,IAAI,CAAC,UAAU,CAAC;gBAAC;oBAAE,QAAQA,KAAK,MAAM;oBAAE,IAAIA,KAAK,EAAE;gBAAC;aAAE;YAEtD,OAAOC,OAAO,EAAE;QACpB,EAAE,OAAOE,OAAO;YACZ,OAAOF,OAAO,IAAI,CAAC,IAAIG,iBAAiBD;QAC5C;IACJ;IAEA,WAAWK,IAA2C,EAAQ;QAC1D,IAAI,CAAC,OAAO,CAAC,iBAAiB,CAACA;IACnC;AACJ;AAEO,MAAMC,4CAAuBC,qBAAqB;IACrD,aAAaC;IACb,gBAAgBjB;IAChB,cAAc;QAACkB;QAAeC;KAA4B;AAC9D"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Result } from "@webiny/feature/api";
|
|
2
2
|
import type { AdminUser } from "../../../types/users.js";
|
|
3
|
-
import type { GetUserInput, ListUsersInput } from "./types.js";
|
|
3
|
+
import type { GetUserAcrossTenantsInput, GetUserInput, ListUserRecordsInput, ListUsersInput } from "./types.js";
|
|
4
4
|
import { UserNotFoundError, UserStorageError } from "./errors.js";
|
|
5
5
|
export interface IAdminUsersRepositoryErrors {
|
|
6
6
|
base: UserNotFoundError | UserStorageError;
|
|
@@ -9,6 +9,9 @@ export type RepositoryError = IAdminUsersRepositoryErrors[keyof IAdminUsersRepos
|
|
|
9
9
|
export interface IAdminUsersRepository {
|
|
10
10
|
get(params: GetUserInput): Promise<Result<AdminUser, RepositoryError>>;
|
|
11
11
|
list(params: ListUsersInput): Promise<Result<AdminUser[], RepositoryError>>;
|
|
12
|
+
getUserAcrossTenants(params: GetUserAcrossTenantsInput): Promise<Result<AdminUser[], RepositoryError>>;
|
|
13
|
+
/** Every tenant's record of the user, through the user→tenant index. */
|
|
14
|
+
listUserRecords(params: ListUserRecordsInput): Promise<Result<AdminUser[], RepositoryError>>;
|
|
12
15
|
create(user: AdminUser): Promise<Result<AdminUser, RepositoryError>>;
|
|
13
16
|
update(user: AdminUser): Promise<Result<AdminUser, RepositoryError>>;
|
|
14
17
|
delete(user: AdminUser): Promise<Result<void, RepositoryError>>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"features/users/shared/abstractions.js","sources":["../../../../src/features/users/shared/abstractions.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport { Result } from \"@webiny/feature/api\";\nimport type { AdminUser } from \"~/types/users.js\";\nimport type {
|
|
1
|
+
{"version":3,"file":"features/users/shared/abstractions.js","sources":["../../../../src/features/users/shared/abstractions.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport { Result } from \"@webiny/feature/api\";\nimport type { AdminUser } from \"~/types/users.js\";\nimport type {\n GetUserAcrossTenantsInput,\n GetUserInput,\n ListUserRecordsInput,\n ListUsersInput\n} from \"./types.js\";\nimport { UserNotFoundError, UserStorageError } from \"./errors.js\";\n\n// Repository error interface\nexport interface IAdminUsersRepositoryErrors {\n base: UserNotFoundError | UserStorageError;\n}\n\nexport type RepositoryError = IAdminUsersRepositoryErrors[keyof IAdminUsersRepositoryErrors];\n\n// Repository interface\nexport interface IAdminUsersRepository {\n get(params: GetUserInput): Promise<Result<AdminUser, RepositoryError>>;\n list(params: ListUsersInput): Promise<Result<AdminUser[], RepositoryError>>;\n getUserAcrossTenants(\n params: GetUserAcrossTenantsInput\n ): Promise<Result<AdminUser[], RepositoryError>>;\n /** Every tenant's record of the user, through the user→tenant index. */\n listUserRecords(params: ListUserRecordsInput): Promise<Result<AdminUser[], RepositoryError>>;\n create(user: AdminUser): Promise<Result<AdminUser, RepositoryError>>;\n update(user: AdminUser): Promise<Result<AdminUser, RepositoryError>>;\n delete(user: AdminUser): Promise<Result<void, RepositoryError>>;\n clearCache(keys: Array<{ tenant: string; id: string }>): void;\n}\n\n/** Persist and retrieve admin users. */\nexport const AdminUsersRepository =\n createAbstraction<IAdminUsersRepository>(\"AdminUsersRepository\");\n\n// Namespace exports\nexport namespace AdminUsersRepository {\n export type Interface = IAdminUsersRepository;\n export type Error = RepositoryError;\n}\n"],"names":["AdminUsersRepository","createAbstraction"],"mappings":";AAkCO,MAAMA,uBACTC,kBAAyC"}
|
|
@@ -26,7 +26,16 @@ export type GetUserInput = {
|
|
|
26
26
|
} | {
|
|
27
27
|
id?: never;
|
|
28
28
|
email: string;
|
|
29
|
+
/** Look the email up in this tenant instead of the current one. */
|
|
30
|
+
tenant?: string;
|
|
29
31
|
};
|
|
32
|
+
export interface GetUserAcrossTenantsInput {
|
|
33
|
+
id: string;
|
|
34
|
+
tenants: string[];
|
|
35
|
+
}
|
|
36
|
+
export interface ListUserRecordsInput {
|
|
37
|
+
id: string;
|
|
38
|
+
}
|
|
30
39
|
export interface ListUsersInput {
|
|
31
40
|
where?: {
|
|
32
41
|
id_in?: string[];
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import { GraphQLSchemaPlugin } from "@webiny/handler-graphql/plugins/GraphQLSchemaPlugin.js";
|
|
2
2
|
import { ErrorResponse, Response } from "@webiny/handler-graphql";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { GetIdentityProfileUseCase } from "../../features/users/GetIdentityProfile/index.js";
|
|
3
|
+
import { LogInUseCase } from "../../features/security/login/index.js";
|
|
4
|
+
import { ResolveIdentityTenantsUseCase } from "../../features/security/ResolveIdentityTenants/index.js";
|
|
6
5
|
import { TeamsRepository } from "../../features/security/teams/shared/abstractions.js";
|
|
7
6
|
import { RolesRepository } from "../../features/security/roles/shared/abstractions.js";
|
|
8
|
-
import { IdentityContext } from "../../features/security/IdentityContext/index.js";
|
|
9
7
|
import { ProfileMapper } from "./ProfileMapper.js";
|
|
10
8
|
const getDefaultTenant = async (context)=>{
|
|
11
9
|
const identity = context.security.getIdentity();
|
|
@@ -51,10 +49,19 @@ const identity_gql = new GraphQLSchemaPlugin({
|
|
|
51
49
|
profile: SecurityIdentityProfile!
|
|
52
50
|
currentTenant: SecurityIdentityTenant!
|
|
53
51
|
defaultTenant: SecurityIdentityTenant!
|
|
52
|
+
"The enabled tenants in which the identity has a user record, sorted by name."
|
|
53
|
+
tenants: [SecurityIdentityTenant!]!
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
"The identity has no user record in this tenant, but has one in these tenants."
|
|
57
|
+
type SecurityLoginTenantSelection {
|
|
58
|
+
tenants: [SecurityIdentityTenant!]!
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
union SecurityLoginResult = SecurityIdentity | SecurityLoginTenantSelection
|
|
62
|
+
|
|
56
63
|
type SecurityIdentityLoginResponse {
|
|
57
|
-
data:
|
|
64
|
+
data: SecurityLoginResult
|
|
58
65
|
error: SecurityError
|
|
59
66
|
}
|
|
60
67
|
|
|
@@ -66,29 +73,21 @@ const identity_gql = new GraphQLSchemaPlugin({
|
|
|
66
73
|
resolvers: {
|
|
67
74
|
SecurityMutation: {
|
|
68
75
|
login: async (_, __, context)=>{
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
if (identity.isAnonymous()) return new ErrorResponse({
|
|
72
|
-
code: "Security/Identity/NotAuthenticated",
|
|
73
|
-
message: "Unauthenticated!"
|
|
74
|
-
});
|
|
76
|
+
const logIn = context.container.resolve(LogInUseCase);
|
|
77
|
+
let result;
|
|
75
78
|
try {
|
|
76
|
-
|
|
77
|
-
await eventPublisher.publish(new AfterLoginEvent({
|
|
78
|
-
identity
|
|
79
|
-
}));
|
|
79
|
+
result = await logIn.execute();
|
|
80
80
|
} catch (err) {
|
|
81
81
|
return new ErrorResponse(err);
|
|
82
82
|
}
|
|
83
|
+
if (result.isFail()) return new ErrorResponse(result.error);
|
|
84
|
+
if ("tenantSelectionRequired" === result.value.type) return new Response({
|
|
85
|
+
__typename: "SecurityLoginTenantSelection",
|
|
86
|
+
tenants: result.value.tenants
|
|
87
|
+
});
|
|
88
|
+
const { identity, user } = result.value;
|
|
83
89
|
const rolesRepo = context.container.resolve(RolesRepository);
|
|
84
90
|
const teamsRepo = context.container.resolve(TeamsRepository);
|
|
85
|
-
const getProfile = context.container.resolve(GetIdentityProfileUseCase);
|
|
86
|
-
const result = await context.security.withoutAuthorization(async ()=>getProfile.execute(identity.id));
|
|
87
|
-
if (result.isFail()) return new ErrorResponse({
|
|
88
|
-
code: "Security/Identity/NotAuthorized",
|
|
89
|
-
message: "Missing user profile!"
|
|
90
|
-
});
|
|
91
|
-
const user = result.value;
|
|
92
91
|
const [roles, teams] = await Promise.all([
|
|
93
92
|
getRoles(user, rolesRepo),
|
|
94
93
|
getTeams(user, teamsRepo)
|
|
@@ -96,6 +95,7 @@ const identity_gql = new GraphQLSchemaPlugin({
|
|
|
96
95
|
const profileMapper = new ProfileMapper();
|
|
97
96
|
const profile = await profileMapper.toDTO(user);
|
|
98
97
|
return new Response({
|
|
98
|
+
__typename: "SecurityIdentity",
|
|
99
99
|
...identity.toJson(),
|
|
100
100
|
profile,
|
|
101
101
|
roles,
|
|
@@ -112,6 +112,11 @@ const identity_gql = new GraphQLSchemaPlugin({
|
|
|
112
112
|
},
|
|
113
113
|
permissions (_, __, context) {
|
|
114
114
|
return context.security.listPermissions();
|
|
115
|
+
},
|
|
116
|
+
async tenants (identity, __, context) {
|
|
117
|
+
const resolveIdentityTenants = context.container.resolve(ResolveIdentityTenantsUseCase);
|
|
118
|
+
const result = await context.security.withoutAuthorization(()=>resolveIdentityTenants.execute(identity.id));
|
|
119
|
+
return result.isOk() ? result.value : [];
|
|
115
120
|
}
|
|
116
121
|
}
|
|
117
122
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graphql/security/identity.gql.js","sources":["../../../src/graphql/security/identity.gql.ts"],"sourcesContent":["import { GraphQLSchemaPlugin } from \"@webiny/handler-graphql/plugins/GraphQLSchemaPlugin.js\";\nimport { ErrorResponse, Response } from \"@webiny/handler-graphql\";\nimport
|
|
1
|
+
{"version":3,"file":"graphql/security/identity.gql.js","sources":["../../../src/graphql/security/identity.gql.ts"],"sourcesContent":["import { GraphQLSchemaPlugin } from \"@webiny/handler-graphql/plugins/GraphQLSchemaPlugin.js\";\nimport { ErrorResponse, Response } from \"@webiny/handler-graphql\";\nimport type { ApiCoreContext } from \"~/types/core.js\";\nimport { LogInUseCase } from \"~/features/security/login/index.js\";\nimport { ResolveIdentityTenantsUseCase } from \"~/features/security/ResolveIdentityTenants/index.js\";\nimport type { SecurityContext } from \"~/types/security.js\";\nimport type { AdminUser } from \"~/types/users.js\";\nimport { TeamsRepository } from \"~/features/security/teams/shared/abstractions.js\";\nimport { RolesRepository } from \"~/features/security/roles/shared/abstractions.js\";\nimport { ProfileMapper } from \"./ProfileMapper.js\";\n\nconst getDefaultTenant = async (context: SecurityContext) => {\n const identity = context.security.getIdentity();\n const defaultTenantId = identity.context.defaultTenantId ?? \"root\";\n\n const defaultTenant = await context.tenancy.getTenantById(defaultTenantId);\n if (defaultTenant) {\n return defaultTenant;\n }\n\n return context.tenancy.getRootTenant();\n};\n\nexport default new GraphQLSchemaPlugin<ApiCoreContext>({\n typeDefs: /* GraphQL */ `\n type SecurityIdentityTenant {\n id: ID!\n name: String!\n }\n type SecurityIdentityProfileRole {\n id: String!\n slug: String!\n name: String!\n }\n\n type SecurityIdentityProfileTeam {\n id: String!\n slug: String!\n name: String!\n }\n\n type SecurityIdentityProfile {\n firstName: String\n lastName: String\n email: String\n avatar: JSON\n external: Boolean!\n createdOn: DateTime!\n }\n\n type SecurityIdentity {\n id: ID!\n type: String!\n displayName: String!\n roles: [SecurityIdentityProfileRole!]!\n teams: [SecurityIdentityProfileTeam!]!\n permissions: [JSON!]!\n profile: SecurityIdentityProfile!\n currentTenant: SecurityIdentityTenant!\n defaultTenant: SecurityIdentityTenant!\n \"The enabled tenants in which the identity has a user record, sorted by name.\"\n tenants: [SecurityIdentityTenant!]!\n }\n\n \"The identity has no user record in this tenant, but has one in these tenants.\"\n type SecurityLoginTenantSelection {\n tenants: [SecurityIdentityTenant!]!\n }\n\n union SecurityLoginResult = SecurityIdentity | SecurityLoginTenantSelection\n\n type SecurityIdentityLoginResponse {\n data: SecurityLoginResult\n error: SecurityError\n }\n\n extend type SecurityMutation {\n \"Login using idToken obtained from a 3rd party identity provider\"\n login: SecurityIdentityLoginResponse\n }\n `,\n resolvers: {\n SecurityMutation: {\n login: async (_, __, context) => {\n const logIn = context.container.resolve(LogInUseCase);\n\n let result: Awaited<LogInUseCase.Return>;\n try {\n result = await logIn.execute();\n } catch (err) {\n // AfterLogin handlers report failures by throwing.\n return new ErrorResponse(err);\n }\n\n if (result.isFail()) {\n return new ErrorResponse(result.error);\n }\n\n if (result.value.type === \"tenantSelectionRequired\") {\n return new Response({\n __typename: \"SecurityLoginTenantSelection\",\n tenants: result.value.tenants\n });\n }\n\n const { identity, user } = result.value;\n\n // Roles and teams are stored in the user record.\n const rolesRepo = context.container.resolve(RolesRepository);\n const teamsRepo = context.container.resolve(TeamsRepository);\n\n const [roles, teams] = await Promise.all([\n getRoles(user, rolesRepo),\n getTeams(user, teamsRepo)\n ]);\n\n const profileMapper = new ProfileMapper();\n const profile = await profileMapper.toDTO(user);\n\n return new Response({\n __typename: \"SecurityIdentity\",\n ...identity.toJson(),\n profile,\n roles,\n teams\n });\n }\n },\n SecurityIdentity: {\n defaultTenant(_, __, context) {\n return getDefaultTenant(context);\n },\n currentTenant(_, __, context) {\n return context.tenancy.getCurrentTenant();\n },\n permissions(_, __, context) {\n return context.security.listPermissions();\n },\n async tenants(identity: { id: string }, __, context) {\n const resolveIdentityTenants = context.container.resolve(\n ResolveIdentityTenantsUseCase\n );\n const result = await context.security.withoutAuthorization(() => {\n return resolveIdentityTenants.execute(identity.id);\n });\n\n // The list only drives tenant switching, so a failed lookup doesn't fail the login.\n return result.isOk() ? result.value : [];\n }\n }\n }\n});\n\nconst getRoles = async (user: AdminUser, rolesRepo: RolesRepository.Interface) => {\n const roleIds = user.roles ?? [];\n if (roleIds.length > 0) {\n const result = await rolesRepo.list({\n where: { id_in: roleIds }\n });\n\n if (result.isOk()) {\n return result.value.map(role => ({\n id: role.id,\n slug: role.slug,\n name: role.name\n }));\n }\n }\n\n return [];\n};\n\nconst getTeams = async (user: AdminUser, teamsRepo: TeamsRepository.Interface) => {\n const teamIds = user.teams ?? [];\n if (teamIds.length > 0) {\n const result = await teamsRepo.list({\n where: { id_in: teamIds }\n });\n if (result.isOk()) {\n return result.value.map(team => ({\n id: team.id,\n slug: team.slug,\n name: team.name\n }));\n }\n }\n return [];\n};\n"],"names":["getDefaultTenant","context","identity","defaultTenantId","defaultTenant","GraphQLSchemaPlugin","_","__","logIn","LogInUseCase","result","err","ErrorResponse","Response","user","rolesRepo","RolesRepository","teamsRepo","TeamsRepository","roles","teams","Promise","getRoles","getTeams","profileMapper","ProfileMapper","profile","resolveIdentityTenants","ResolveIdentityTenantsUseCase","roleIds","role","teamIds","team"],"mappings":"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"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ContextPlugin } from "@webiny/api";
|
|
2
|
+
import error from "@webiny/error";
|
|
2
3
|
import { Request } from "@webiny/handler";
|
|
3
4
|
import { GetTenantByIdUseCase } from "../../features/tenancy/GetTenantById/index.js";
|
|
4
5
|
import { TenantContext } from "../../features/tenancy/TenantContext/index.js";
|
|
@@ -21,7 +22,13 @@ const createTenancyContext = ()=>new ContextPlugin(async (context)=>{
|
|
|
21
22
|
const tenantResult = await getTenantById.execute(tenantId);
|
|
22
23
|
if (tenantResult.isOk()) tenantContext.setTenant(tenantResult.value);
|
|
23
24
|
else {
|
|
24
|
-
if ("root" !== tenantId) throw new
|
|
25
|
+
if ("root" !== tenantId) throw new error({
|
|
26
|
+
message: "Unable to load tenant!",
|
|
27
|
+
code: "Tenancy/TenantNotFound",
|
|
28
|
+
data: {
|
|
29
|
+
tenantId
|
|
30
|
+
}
|
|
31
|
+
});
|
|
25
32
|
tenantContext.setTenant(RootTenantValue.create());
|
|
26
33
|
}
|
|
27
34
|
context.tenancy = new LegacyContext(context.container);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"legacy/tenancy/createTenancyContext.js","sources":["../../../src/legacy/tenancy/createTenancyContext.ts"],"sourcesContent":["import { ContextPlugin } from \"@webiny/api\";\nimport { Request } from \"@webiny/handler\";\nimport type { ApiCoreContext } from \"~/types/core.js\";\nimport { GetTenantByIdUseCase } from \"~/features/tenancy/GetTenantById/index.js\";\nimport { TenantContext } from \"~/features/tenancy/TenantContext/index.js\";\nimport { LegacyContext } from \"~/legacy/tenancy/LegacyContext.js\";\nimport { RootTenantValue } from \"~/domain/tenancy/RootTenantValue.js\";\n\nexport const createTenancyContext = () => {\n return new ContextPlugin<ApiCoreContext>(async context => {\n let tenantId = \"root\";\n\n const request = context.container.resolve(Request);\n\n const multiTenancy = context.wcp.canUseFeature(\"multiTenancy\");\n if (!request) {\n throw new Error(\"MISSING CONTEXT REQUEST\");\n }\n\n if (multiTenancy) {\n const { headers = {} } = request;\n\n tenantId = (headers[\"x-tenant\"] as string) ?? \"root\";\n }\n\n // Add WCP telemetry identifier\n // This tenancy package is used by GraphQL, Headless CMS, and PB import/export functions\n context.plugins.register({ type: \"wcp-telemetry-tracker\" });\n\n // We need to load tenant, and set the current tenant context.\n const getTenantById = context.container.resolve(GetTenantByIdUseCase);\n const tenantContext = context.container.resolve(TenantContext);\n\n const tenantResult = await getTenantById.execute(tenantId);\n if (tenantResult.isOk()) {\n tenantContext.setTenant(tenantResult.value);\n } else {\n // If there's no `root` tenant, it means system installation wasn't finished yet.\n // But all other tenants should throw an error.\n if (tenantId !== \"root\") {\n throw new
|
|
1
|
+
{"version":3,"file":"legacy/tenancy/createTenancyContext.js","sources":["../../../src/legacy/tenancy/createTenancyContext.ts"],"sourcesContent":["import { ContextPlugin } from \"@webiny/api\";\nimport WebinyError from \"@webiny/error\";\nimport { Request } from \"@webiny/handler\";\nimport type { ApiCoreContext } from \"~/types/core.js\";\nimport { GetTenantByIdUseCase } from \"~/features/tenancy/GetTenantById/index.js\";\nimport { TenantContext } from \"~/features/tenancy/TenantContext/index.js\";\nimport { LegacyContext } from \"~/legacy/tenancy/LegacyContext.js\";\nimport { RootTenantValue } from \"~/domain/tenancy/RootTenantValue.js\";\n\nexport const createTenancyContext = () => {\n return new ContextPlugin<ApiCoreContext>(async context => {\n let tenantId = \"root\";\n\n const request = context.container.resolve(Request);\n\n const multiTenancy = context.wcp.canUseFeature(\"multiTenancy\");\n if (!request) {\n throw new Error(\"MISSING CONTEXT REQUEST\");\n }\n\n if (multiTenancy) {\n const { headers = {} } = request;\n\n tenantId = (headers[\"x-tenant\"] as string) ?? \"root\";\n }\n\n // Add WCP telemetry identifier\n // This tenancy package is used by GraphQL, Headless CMS, and PB import/export functions\n context.plugins.register({ type: \"wcp-telemetry-tracker\" });\n\n // We need to load tenant, and set the current tenant context.\n const getTenantById = context.container.resolve(GetTenantByIdUseCase);\n const tenantContext = context.container.resolve(TenantContext);\n\n const tenantResult = await getTenantById.execute(tenantId);\n if (tenantResult.isOk()) {\n tenantContext.setTenant(tenantResult.value);\n } else {\n // If there's no `root` tenant, it means system installation wasn't finished yet.\n // But all other tenants should throw an error.\n if (tenantId !== \"root\") {\n throw new WebinyError({\n message: \"Unable to load tenant!\",\n code: \"Tenancy/TenantNotFound\",\n data: { tenantId }\n });\n }\n\n // Mock root tenant so the system can bootstrap\n tenantContext.setTenant(RootTenantValue.create());\n }\n\n // Set up legacy context. We use this API in many places across our codebase, and this will provide\n // a working bridge until everything is migrated to use DI container.\n context.tenancy = new LegacyContext(context.container);\n });\n};\n"],"names":["createTenancyContext","ContextPlugin","context","tenantId","request","Request","multiTenancy","Error","headers","getTenantById","GetTenantByIdUseCase","tenantContext","TenantContext","tenantResult","WebinyError","RootTenantValue","LegacyContext"],"mappings":";;;;;;;AASO,MAAMA,uBAAuB,IACzB,IAAIC,cAA8B,OAAMC;QAC3C,IAAIC,WAAW;QAEf,MAAMC,UAAUF,QAAQ,SAAS,CAAC,OAAO,CAACG;QAE1C,MAAMC,eAAeJ,QAAQ,GAAG,CAAC,aAAa,CAAC;QAC/C,IAAI,CAACE,SACD,MAAM,IAAIG,MAAM;QAGpB,IAAID,cAAc;YACd,MAAM,EAAEE,UAAU,CAAC,CAAC,EAAE,GAAGJ;YAEzBD,WAAYK,OAAO,CAAC,WAAW,IAAe;QAClD;QAIAN,QAAQ,OAAO,CAAC,QAAQ,CAAC;YAAE,MAAM;QAAwB;QAGzD,MAAMO,gBAAgBP,QAAQ,SAAS,CAAC,OAAO,CAACQ;QAChD,MAAMC,gBAAgBT,QAAQ,SAAS,CAAC,OAAO,CAACU;QAEhD,MAAMC,eAAe,MAAMJ,cAAc,OAAO,CAACN;QACjD,IAAIU,aAAa,IAAI,IACjBF,cAAc,SAAS,CAACE,aAAa,KAAK;aACvC;YAGH,IAAIV,AAAa,WAAbA,UACA,MAAM,IAAIW,MAAY;gBAClB,SAAS;gBACT,MAAM;gBACN,MAAM;oBAAEX;gBAAS;YACrB;YAIJQ,cAAc,SAAS,CAACI,gBAAgB,MAAM;QAClD;QAIAb,QAAQ,OAAO,GAAG,IAAIc,cAAcd,QAAQ,SAAS;IACzD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webiny/api-core",
|
|
3
|
-
"version": "6.5.0",
|
|
3
|
+
"version": "6.5.1-beta.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -18,17 +18,17 @@
|
|
|
18
18
|
"@ai-sdk/google": "4.0.98",
|
|
19
19
|
"@ai-sdk/openai": "4.0.95",
|
|
20
20
|
"@openrouter/ai-sdk-provider": "3.1.0",
|
|
21
|
-
"@webiny/api": "6.5.0",
|
|
21
|
+
"@webiny/api": "6.5.1-beta.0",
|
|
22
22
|
"@webiny/di": "1.1.0",
|
|
23
|
-
"@webiny/error": "6.5.0",
|
|
24
|
-
"@webiny/feature": "6.5.0",
|
|
25
|
-
"@webiny/feature-flags": "6.5.0",
|
|
26
|
-
"@webiny/handler": "6.5.0",
|
|
27
|
-
"@webiny/handler-db": "6.5.0",
|
|
28
|
-
"@webiny/handler-graphql": "6.5.0",
|
|
29
|
-
"@webiny/plugins": "6.5.0",
|
|
30
|
-
"@webiny/utils": "6.5.0",
|
|
31
|
-
"@webiny/wcp": "6.5.0",
|
|
23
|
+
"@webiny/error": "6.5.1-beta.0",
|
|
24
|
+
"@webiny/feature": "6.5.1-beta.0",
|
|
25
|
+
"@webiny/feature-flags": "6.5.1-beta.0",
|
|
26
|
+
"@webiny/handler": "6.5.1-beta.0",
|
|
27
|
+
"@webiny/handler-db": "6.5.1-beta.0",
|
|
28
|
+
"@webiny/handler-graphql": "6.5.1-beta.0",
|
|
29
|
+
"@webiny/plugins": "6.5.1-beta.0",
|
|
30
|
+
"@webiny/utils": "6.5.1-beta.0",
|
|
31
|
+
"@webiny/wcp": "6.5.1-beta.0",
|
|
32
32
|
"ai": "7.0.142",
|
|
33
33
|
"dataloader": "2.2.3",
|
|
34
34
|
"jose": "6.2.12",
|
|
@@ -41,9 +41,9 @@
|
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@types/jsonwebtoken": "9.0.10",
|
|
44
|
-
"@webiny/build-tools": "6.5.0",
|
|
45
|
-
"@webiny/handler-aws": "6.5.0",
|
|
46
|
-
"@webiny/project-utils": "6.5.0",
|
|
44
|
+
"@webiny/build-tools": "6.5.1-beta.0",
|
|
45
|
+
"@webiny/handler-aws": "6.5.1-beta.0",
|
|
46
|
+
"@webiny/project-utils": "6.5.1-beta.0",
|
|
47
47
|
"rimraf": "6.1.3",
|
|
48
48
|
"typescript": "7.0.2",
|
|
49
49
|
"vitest": "5.0.5"
|
package/types/users.d.ts
CHANGED
|
@@ -63,6 +63,13 @@ export interface StorageOperationsGetUserParams extends GetUserParams {
|
|
|
63
63
|
tenant: string;
|
|
64
64
|
};
|
|
65
65
|
}
|
|
66
|
+
export interface StorageOperationsGetUserAcrossTenantsParams {
|
|
67
|
+
id: string;
|
|
68
|
+
tenants: string[];
|
|
69
|
+
}
|
|
70
|
+
export interface StorageOperationsListUserRecordsParams {
|
|
71
|
+
id: string;
|
|
72
|
+
}
|
|
66
73
|
export interface StorageOperationsCreateUserParams<TUser> {
|
|
67
74
|
user: TUser;
|
|
68
75
|
}
|
|
@@ -78,6 +85,16 @@ export interface System {
|
|
|
78
85
|
}
|
|
79
86
|
export interface AdminUsersStorageOperations {
|
|
80
87
|
getUser<TUser extends AdminUser = AdminUser>(params: StorageOperationsGetUserParams): Promise<TUser | null>;
|
|
88
|
+
/**
|
|
89
|
+
* Load the user records with the given ID from each of the given tenants.
|
|
90
|
+
* Tenants that don't have a record for this ID are skipped.
|
|
91
|
+
*/
|
|
92
|
+
getUserAcrossTenants<TUser extends AdminUser = AdminUser>(params: StorageOperationsGetUserAcrossTenantsParams): Promise<TUser[]>;
|
|
93
|
+
/**
|
|
94
|
+
* Load the user records with the given ID from every tenant, through the user→tenant index.
|
|
95
|
+
* Records written before the index existed may be missing: `getUserAcrossTenants` finds those.
|
|
96
|
+
*/
|
|
97
|
+
listUserRecords<TUser extends AdminUser = AdminUser>(params: StorageOperationsListUserRecordsParams): Promise<TUser[]>;
|
|
81
98
|
listUsers<TUser extends AdminUser = AdminUser>(params: StorageOperationsListUsersParams): Promise<TUser[]>;
|
|
82
99
|
createUser<TUser extends AdminUser = AdminUser>(params: StorageOperationsCreateUserParams<TUser>): Promise<TUser>;
|
|
83
100
|
updateUser<TUser extends AdminUser = AdminUser>(params: StorageOperationsUpdateUserParams<TUser>): Promise<TUser>;
|