@enonic-types/lib-auth 7.11.0-B1 → 7.11.0-B3

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.
Files changed (3) hide show
  1. package/README.md +4 -4
  2. package/auth.d.ts +54 -59
  3. package/package.json +4 -1
package/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  ## Install
6
6
 
7
7
  ```bash
8
- npm i --save-dev @enonic/lib-auth
8
+ npm i --save-dev @enonic-types/lib-auth
9
9
  ```
10
10
 
11
11
  ## Use
@@ -26,8 +26,8 @@ Add the corresponding types to your `tsconfig.json` file that is used for applic
26
26
 
27
27
  ### Require and custom imports
28
28
 
29
- To make `require` work out of box, you must install and add the `@enonic-types/global` types. Aside from providing definitions for XP global
30
- objects, e.g. `log`, `app`, `__`, etc, requiring library by the default path will return typed object.
29
+ To make `require` work out of the box, you must install and add the `@enonic-types/global` types. Aside from providing definitions for XP
30
+ global objects, e.g. `log`, `app`, `__`, etc, requiring a library by the default path will return typed object.
31
31
 
32
32
  `tsconfig.json`
33
33
 
@@ -49,7 +49,7 @@ const {login, logout, getUser, generatePassword} = require('/lib/xp/auth');
49
49
  ```
50
50
 
51
51
  More detailed explanation on how it works and how to type custom import function can be
52
- found [here](https://github.com/enonic/xp/tree/master/modules/lib/typescript/README.md).
52
+ found [here](https://developer.enonic.com/docs/xp/stable/api).
53
53
 
54
54
  ### ES6-style import
55
55
 
package/auth.d.ts CHANGED
@@ -11,8 +11,12 @@ declare global {
11
11
  '/lib/xp/auth': typeof import('./auth');
12
12
  }
13
13
  }
14
- export declare type EditorFn<T> = (value: T) => T;
14
+ import type { Group, GroupKey, Principal, PrincipalKey, Role, RoleKey, User, UserKey } from '@enonic-types/core';
15
+ export type { PrincipalKey, UserKey, GroupKey, RoleKey, Principal, User, Group, Role } from '@enonic-types/core';
15
16
  export declare type LoginScopeType = 'SESSION' | 'REQUEST' | 'NONE';
17
+ export interface UserWithProfile<Profile extends Record<string, unknown> = Record<string, unknown>> extends User {
18
+ profile?: Profile;
19
+ }
16
20
  export interface LoginParams {
17
21
  user: string;
18
22
  skipAuth?: boolean;
@@ -21,29 +25,10 @@ export interface LoginParams {
21
25
  scope?: LoginScopeType;
22
26
  sessionTimeout?: number;
23
27
  }
24
- export interface Principal {
25
- type: string;
26
- key: string;
27
- displayName: string;
28
- modifiedTime: string;
29
- }
30
- export interface User extends Principal {
31
- disabled?: boolean;
32
- email?: string;
33
- login?: string;
34
- idProvider: string;
35
- profile?: Record<string, unknown>;
36
- }
37
- export interface Role extends Principal {
38
- description?: string;
39
- }
40
- export interface Group extends Principal {
41
- description?: string;
42
- }
43
28
  export interface LoginResult {
44
29
  authenticated: boolean;
45
30
  message: string;
46
- users?: Principal;
31
+ user?: User;
47
32
  }
48
33
  /**
49
34
  * Login a user with the specified idProvider, userName, password and scope.
@@ -79,14 +64,19 @@ export interface GetUserParams {
79
64
  *
80
65
  * @returns {User} Information for logged-in user.
81
66
  */
82
- export declare function getUser(params?: GetUserParams): User | null;
67
+ export declare function getUser(params?: {
68
+ includeProfile?: false;
69
+ }): User | null;
70
+ export declare function getUser<Profile extends Record<string, unknown> = Record<string, unknown>>(params: {
71
+ includeProfile: true;
72
+ }): UserWithProfile<Profile> | null;
83
73
  /**
84
74
  * Checks if the logged-in user has the specified role.
85
75
  *
86
76
  * @example-ref examples/auth/hasRole.js
87
77
  *
88
- * @param {string} role Role to check for.
89
- * @returns {boolean} True if the user has specfied role, false otherwise.
78
+ * @param {string} role Role name to check for.
79
+ * @returns {boolean} True if the user has specified role, false otherwise.
90
80
  */
91
81
  export declare function hasRole(role: string): boolean;
92
82
  /**
@@ -117,31 +107,31 @@ export declare function changePassword(params: ChangePasswordParams): void;
117
107
  * @example-ref examples/auth/getPrincipal.js
118
108
  *
119
109
  * @param {string} principalKey Principal key to look for.
120
- * @returns {Principal} the principal specified, or null if it doesn't exist.
110
+ * @returns {User | Group | Role} the principal specified, or null if it doesn't exist.
121
111
  */
122
- export declare function getPrincipal(principalKey: string): User | Group | Role | null;
112
+ export declare function getPrincipal(principalKey: PrincipalKey): Principal | null;
123
113
  /**
124
114
  * Returns a list of principals the specified principal is a member of.
125
115
  *
126
116
  * @example-ref examples/auth/getMemberships.js
127
117
  *
128
- * @param {string} principalKey Principal key to retrieve memberships for.
118
+ * @param {string} principalKey Principal key of user or group to retrieve memberships for.
129
119
  * @param {boolean} [transitive=false] Retrieve transitive memberships.
130
- * @returns {Array.<User | Group | Role>} Returns the list of principals.
120
+ * @returns {Array<Group | Role>} Returns the list of groups and roles.
131
121
  */
132
- export declare function getMemberships(principalKey: string, transitive: boolean): (User | Group | Role)[];
122
+ export declare function getMemberships(principalKey: UserKey | GroupKey, transitive?: boolean): (Group | Role)[];
133
123
  /**
134
124
  * Returns a list of principals that are members of the specified principal.
135
125
  *
136
126
  * @example-ref examples/auth/getMembers.js
137
127
  *
138
128
  * @param {string} principalKey Principal key to retrieve members for.
139
- * @returns {Array.<User | Group | Role>} Returns the list of principals.
129
+ * @returns {Array<User | Group>} Returns the list of users and groups.
140
130
  */
141
- export declare function getMembers(principalKey: string): (User | Group | Role)[];
131
+ export declare function getMembers(principalKey: GroupKey | RoleKey): (User | Group)[];
142
132
  export interface CreateUserParams {
143
133
  name: string;
144
- displayName: string;
134
+ displayName?: string;
145
135
  idProvider: string;
146
136
  email?: string;
147
137
  }
@@ -153,13 +143,13 @@ export interface CreateUserParams {
153
143
  * @param {object} params JSON parameters.
154
144
  * @param {string} params.idProvider Key for id provider where user has to be created.
155
145
  * @param {string} params.name User login name to set.
156
- * @param {string} params.displayName User display name.
146
+ * @param {string} [params.displayName] User display name.
157
147
  * @param {string} [params.email] User email.
158
148
  */
159
149
  export declare function createUser(params: CreateUserParams): User;
160
150
  export interface ModifyUserParams {
161
- key: string;
162
- editor: EditorFn<User>;
151
+ key: UserKey;
152
+ editor: (user: User) => User;
163
153
  }
164
154
  /**
165
155
  * Retrieves the user specified and updates it with the changes applied.
@@ -175,8 +165,8 @@ export declare function modifyUser(params: ModifyUserParams): User | null;
175
165
  export interface CreateGroupParams {
176
166
  idProvider: string;
177
167
  name: string;
178
- displayName: string;
179
- description: string;
168
+ displayName?: string;
169
+ description?: string;
180
170
  }
181
171
  /**
182
172
  * Creates a group.
@@ -186,13 +176,13 @@ export interface CreateGroupParams {
186
176
  * @param {object} params JSON parameters.
187
177
  * @param {string} params.idProvider Key for id provider where group has to be created.
188
178
  * @param {string} params.name Group name.
189
- * @param {string} params.displayName Group display name.
190
- * @param {string} params.description as principal description .
179
+ * @param {string} [params.displayName] Group display name.
180
+ * @param {string} [params.description] as principal description .
191
181
  */
192
182
  export declare function createGroup(params: CreateGroupParams): Group;
193
183
  export interface ModifyGroupParams {
194
- key: string;
195
- editor: EditorFn<Group>;
184
+ key: GroupKey;
185
+ editor: (group: Group) => Group;
196
186
  }
197
187
  /**
198
188
  * Retrieves the group specified and updates it with the changes applied.
@@ -211,9 +201,9 @@ export declare function modifyGroup(params: ModifyGroupParams): Group | null;
211
201
  * @example-ref examples/auth/addMembers.js
212
202
  *
213
203
  * @param {string} principalKey Key of the principal to add members to.
214
- * @param {string} members Keys of the principals to add.
204
+ * @param {Array<string>} members Keys of users and groups to add.
215
205
  */
216
- export declare function addMembers(principalKey: string, members: string): void;
206
+ export declare function addMembers(principalKey: GroupKey | RoleKey, members: (UserKey | GroupKey)[]): void;
217
207
  /**
218
208
  * Removes members from a principal (user or role).
219
209
  *
@@ -222,19 +212,19 @@ export declare function addMembers(principalKey: string, members: string): void;
222
212
  * @param {string} principalKey Key of the principal to remove members from.
223
213
  * @param {string} members Keys of the principals to remove.
224
214
  */
225
- export declare function removeMembers(principalKey: string, members: string): void;
215
+ export declare function removeMembers(principalKey: GroupKey | RoleKey, members: (UserKey | GroupKey)[]): void;
226
216
  export interface FindPrincipalsParams {
227
- type?: string;
217
+ type?: Principal['type'];
228
218
  idProvider?: string;
229
219
  start?: number;
230
220
  count?: number;
231
221
  name?: string;
232
222
  searchText?: string;
233
223
  }
234
- export interface FindPrincipalsResult {
224
+ export interface FindPrincipalsResult<P extends Principal = Principal> {
235
225
  total: number;
236
226
  count: number;
237
- hits: (User | Group | Role)[];
227
+ hits: P[];
238
228
  }
239
229
  /**
240
230
  * Search for principals matching the specified criteria.
@@ -259,7 +249,7 @@ export declare function findPrincipals(params: FindPrincipalsParams): FindPrinci
259
249
  * @param {string} principalKey Principal key to delete.
260
250
  * @returns {boolean} True if deleted, false otherwise.
261
251
  */
262
- export declare function deletePrincipal(principalKey: string): boolean;
252
+ export declare function deletePrincipal(principalKey: PrincipalKey): boolean;
263
253
  /**
264
254
  * This function returns the ID provider configuration.
265
255
  * It is meant to be called from an ID provider controller.
@@ -268,9 +258,9 @@ export declare function deletePrincipal(principalKey: string): boolean;
268
258
  *
269
259
  * @returns {object} The ID provider configuration as JSON.
270
260
  */
271
- export declare function getIdProviderConfig(): Record<string, unknown> | null;
261
+ export declare function getIdProviderConfig<IdProviderConfig extends Record<string, unknown>>(): IdProviderConfig | null;
272
262
  export interface GetProfileParams {
273
- key: string;
263
+ key: UserKey;
274
264
  scope?: string;
275
265
  }
276
266
  /**
@@ -283,11 +273,11 @@ export interface GetProfileParams {
283
273
  * @param {string} [params.scope] Scope of the data to retrieve.
284
274
  * @returns {object} The extra data as JSON
285
275
  */
286
- export declare function getProfile(params: GetProfileParams): Record<string, unknown> | null;
276
+ export declare function getProfile<Profile extends Record<string, unknown> = Record<string, unknown>>(params: GetProfileParams): Profile | null;
287
277
  export interface ModifyProfileParams {
288
- key: string;
278
+ key: UserKey;
289
279
  scope?: string | null;
290
- editor: EditorFn<User>;
280
+ editor: (user: User) => User;
291
281
  }
292
282
  /**
293
283
  * This function retrieves the profile of a user and updates it.
@@ -300,7 +290,7 @@ export interface ModifyProfileParams {
300
290
  * @param {function} params.editor Profile editor function to apply.
301
291
  * @returns {object} The extra data as JSON
302
292
  */
303
- export declare function modifyProfile(params: ModifyProfileParams): Record<string, unknown> | null;
293
+ export declare function modifyProfile<Profile extends Record<string, unknown> = Record<string, unknown>>(params: ModifyProfileParams): Profile | null;
304
294
  export interface FindUsersParams {
305
295
  start?: number;
306
296
  count?: number;
@@ -322,11 +312,16 @@ export interface FindUsersParams {
322
312
  *
323
313
  * @returns {FindPrincipalsResult} Result of query.
324
314
  */
325
- export declare function findUsers(params: FindUsersParams): FindPrincipalsResult;
315
+ export declare function findUsers(params: FindUsersParams & {
316
+ includeProfile?: false;
317
+ }): FindPrincipalsResult<User>;
318
+ export declare function findUsers<Profile extends Record<string, unknown> = Record<string, unknown>>(params: FindUsersParams & {
319
+ includeProfile: true;
320
+ }): FindPrincipalsResult<UserWithProfile<Profile>>;
326
321
  export interface CreateRoleParams {
327
322
  name: string;
328
- displayName: string;
329
- description: string;
323
+ displayName?: string;
324
+ description?: string;
330
325
  }
331
326
  /**
332
327
  * Creates a role.
@@ -339,8 +334,8 @@ export interface CreateRoleParams {
339
334
  */
340
335
  export declare function createRole(params: CreateRoleParams): Role;
341
336
  export interface ModifyRoleParams {
342
- key: string;
343
- editor: EditorFn<Role>;
337
+ key: RoleKey;
338
+ editor: (role: Role) => Role;
344
339
  }
345
340
  /**
346
341
  * Retrieves the role specified and updates it with the changes applied.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@enonic-types/lib-auth",
3
- "version": "7.11.0-B1",
3
+ "version": "7.11.0-B3",
4
4
  "description": "Type definitions for lib-auth.",
5
5
  "types": "auth.d.ts",
6
6
  "files": [
@@ -26,6 +26,9 @@
26
26
  "url": "https://github.com/enonic/xp/issues"
27
27
  },
28
28
  "homepage": "https://github.com/enonic/xp/tree/master#readme",
29
+ "dependencies": {
30
+ "@enonic-types/core": "7.11.0-B3"
31
+ },
29
32
  "publishConfig": {
30
33
  "access": "public"
31
34
  }