openloops 0.3.1 → 0.3.3

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.
@@ -44,7 +44,7 @@ export declare class BaseEntity {
44
44
  setDefaults(): void;
45
45
  setId(id: string | ObjectId): void;
46
46
  setOwner(currentUser?: CurrentUser): void;
47
- setCreatedBy(currentUser?: CurrentUser): void;
47
+ setCreatedBy(currentUser?: CurrentUser, sanitize?: boolean): void;
48
48
  setUpdatedBy(currentUser?: CurrentUser): void;
49
49
  setDeletedBy(currentUser?: CurrentUser): void;
50
50
  setArchivedBy(currentUser?: CurrentUser): void;
@@ -111,11 +111,10 @@ const _BaseEntity = class _BaseEntity {
111
111
  if (!currentUser) return;
112
112
  this.ownerId = _BaseEntity.toObjectId(currentUser.userId);
113
113
  }
114
- setCreatedBy(currentUser) {
115
- if (!currentUser) return;
116
- this.sanitize(currentUser);
114
+ setCreatedBy(currentUser, sanitize = true) {
115
+ if (sanitize) this.sanitize(currentUser);
117
116
  this.createdAt = /* @__PURE__ */ new Date();
118
- this.createdBy = _BaseEntity.toObjectId(currentUser.userId);
117
+ this.createdBy = _BaseEntity.toObjectId(currentUser?.userId);
119
118
  this.setDefaults();
120
119
  }
121
120
  setUpdatedBy(currentUser) {
@@ -83,11 +83,10 @@ const _BaseEntity = class _BaseEntity {
83
83
  if (!currentUser) return;
84
84
  this.ownerId = _BaseEntity.toObjectId(currentUser.userId);
85
85
  }
86
- setCreatedBy(currentUser) {
87
- if (!currentUser) return;
88
- this.sanitize(currentUser);
86
+ setCreatedBy(currentUser, sanitize = true) {
87
+ if (sanitize) this.sanitize(currentUser);
89
88
  this.createdAt = /* @__PURE__ */ new Date();
90
- this.createdBy = _BaseEntity.toObjectId(currentUser.userId);
89
+ this.createdBy = _BaseEntity.toObjectId(currentUser?.userId);
91
90
  this.setDefaults();
92
91
  }
93
92
  setUpdatedBy(currentUser) {
@@ -3,6 +3,7 @@ import { UserRepository } from '../repositories';
3
3
  import { UserProps } from '../entities';
4
4
  export interface Params extends BaseUseCaseParams {
5
5
  payload?: UserProps;
6
+ sanitize?: boolean;
6
7
  }
7
8
  export interface Output extends BaseUseCaseOutput {
8
9
  data: UserProps;
@@ -42,12 +42,12 @@ let CreateUserUseCase = class extends import_base.BaseUseCase {
42
42
  }
43
43
  userRepository;
44
44
  async execute(params) {
45
- const { payload, currentUser } = params;
45
+ const { payload, currentUser, sanitize } = params;
46
46
  if (!payload?.email) throw new Error("[Create User] email param is required");
47
47
  if (!payload?.password) throw new Error("[Create User] password param is required");
48
48
  const model = payload.preferences ? await (0, import_default_model.readDefaultModel)(payload.preferences) : void 0;
49
49
  const user = await import_entities.User.create({ ...payload, ...payload.preferences ? { preferences: model.preferences } : {} });
50
- user.setCreatedBy(currentUser);
50
+ user.setCreatedBy(currentUser, sanitize);
51
51
  await this.userRepository?.create(user);
52
52
  const { password, ...rest } = user.toJSON();
53
53
  return { data: rest };
@@ -14,12 +14,12 @@ let CreateUserUseCase = class extends BaseUseCase {
14
14
  }
15
15
  userRepository;
16
16
  async execute(params) {
17
- const { payload, currentUser } = params;
17
+ const { payload, currentUser, sanitize } = params;
18
18
  if (!payload?.email) throw new Error("[Create User] email param is required");
19
19
  if (!payload?.password) throw new Error("[Create User] password param is required");
20
20
  const model = payload.preferences ? await readDefaultModel(payload.preferences) : void 0;
21
21
  const user = await User.create({ ...payload, ...payload.preferences ? { preferences: model.preferences } : {} });
22
- user.setCreatedBy(currentUser);
22
+ user.setCreatedBy(currentUser, sanitize);
23
23
  await this.userRepository?.create(user);
24
24
  const { password, ...rest } = user.toJSON();
25
25
  return { data: rest };
package/docs/users.md CHANGED
@@ -3,7 +3,7 @@
3
3
  `openloops` includes its own user management, so you don't need to build a parallel system just to have an identity to hand the `Agent`.
4
4
 
5
5
  ```typescript
6
- import { createUser, getUserById, updateUser, deleteUser, ListUsersByProps } from 'openloops/users'
6
+ import { createUser, getUserById, updateUser, deleteUser, ListUsersByProps, UserRole } from 'openloops/users'
7
7
  import { CurrentUser } from 'openloops/base'
8
8
  ```
9
9
 
@@ -28,12 +28,20 @@ const result = await createUser({
28
28
  lastName: 'Barcos',
29
29
  email: 'jose@openloops.xyz',
30
30
  password: 'a-real-password',
31
+ role: UserRole.USER,
31
32
  },
33
+ sanitize: false,
32
34
  })
33
35
  ```
34
36
 
35
37
  `email` and `password` are required. Passwords are hashed before being stored and are never returned.
36
38
 
39
+ ### `sanitize`
40
+
41
+ By default `createUser` removes the fields only admins can set, such as `role`, unless the `currentUser` is an admin. This is what protects you from someone escalating privileges through the payload.
42
+
43
+ If you send `role`, pass `sanitize: false`, otherwise it is discarded. Do it only when your code builds the payload itself, with the fields you choose. Never pass the raw body of a request from an API.
44
+
37
45
  ## Getting a user
38
46
 
39
47
  ```typescript
@@ -1,4 +1,4 @@
1
- import { createUser } from 'openloops/users'
1
+ import { createUser, UserRole } from 'openloops/users'
2
2
  import { Logger } from 'openloops/common'
3
3
 
4
4
  async function createUserExample () {
@@ -9,8 +9,10 @@ async function createUserExample () {
9
9
  firstName: 'Jose',
10
10
  lastName: 'Barcos',
11
11
  email: 'jose@openloops.xyz',
12
- password: 'openloops!'
13
- }
12
+ password: 'openloops!',
13
+ role: UserRole.USER
14
+ },
15
+ sanitize: false
14
16
  })
15
17
 
16
18
  logger.info(result, '[Demo] Created user:')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openloops",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "description": "Open source harness for AI agents: a TypeScript runtime with persistence, tools, MCP trust, approvals and autonomous jobs, where the loop is pluggable",
5
5
  "author": {
6
6
  "name": "Jose Barcos",