@brickert/kerio-connect-api 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -108,6 +108,7 @@ export class Kerio extends WebClient {
108
108
  Session: Session;
109
109
  Logs: Logs;
110
110
  Domains: Domains;
111
+ Users: Users;
111
112
 
112
113
  login(user: KerioUser): void;
113
114
 
@@ -348,4 +349,59 @@ export interface DKIMRecord {
348
349
  * @example 'abcdefg1234567'
349
350
  */
350
351
  raw_public_key: string;
352
+ }
353
+
354
+ export class Users extends KerioModules {
355
+ /**
356
+ * Get a Mapped Array of KerioUserItems indexed by their ID for the specified domain name
357
+ * @param {string} domainName Name of the domain
358
+ * @returns {Promise<Map<import('../index.d.ts').KerioUserID, import('../index.d.ts').KerioUserItem>>}
359
+ */
360
+ list(domainName: string): Promise<Map<KerioUserID, KerioUserItem>>;
361
+
362
+ /**
363
+ * Get statistics of the Kerio User. Such as mailbox size and last login timestamp in server's local time zone
364
+ * @param kerioUserID Kerio user UUID
365
+ */
366
+ getStatistics(kerioUserID: KerioUserID): Promise<KerioUserStatistic>;
367
+ }
368
+
369
+ /**
370
+ * @example "keriodb://user/1234-5678-901-2345/1234-5678-901"
371
+ */
372
+ export type KerioUserID = string;
373
+
374
+ export interface KerioUserItem {
375
+ id: string;
376
+ domainId: string;
377
+ loginName: string;
378
+ fullName: string;
379
+ description: string;
380
+ enabled: boolean;
381
+ primaryEmailAddress: string; //loginName + domainName;
382
+ }
383
+
384
+ export interface KerioUserStatistic {
385
+ storage: {
386
+ items: number;
387
+ value: number;
388
+ units: "Bytes" | string;
389
+ },
390
+ lastLogin: string; //YYYY-MM-DD HH:MM in local server time
391
+ loginCount: {
392
+ pop3: number;
393
+ pop3s: number;
394
+ imap: number;
395
+ imaps: number;
396
+ http: number;
397
+ https: number;
398
+ ldap: number;
399
+ ldaps: number;
400
+ nntp: number;
401
+ nntps: number;
402
+ activeSync: number;
403
+ secureActiveSync: number;
404
+ xmpp: number;
405
+ xmpps: number;
406
+ }
351
407
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brickert/kerio-connect-api",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "An unofficial API wrapper for Kerio Connect Mail Server jsonrpc Admin API.",
5
5
  "keywords": [
6
6
  "kerio",
package/src/kerio.js CHANGED
@@ -2,6 +2,7 @@ import { IPAddressGroup } from "./ipaddressgroups.js";
2
2
  import { Logs } from "./logs.js";
3
3
  import { Session } from "./session.js";
4
4
  import { Domains } from "./domains.js";
5
+ import { Users } from "./users.js";
5
6
  import { WebClient } from "./client.js";
6
7
  import { CronJob } from 'cron';
7
8
  import pino from 'pino';
@@ -25,6 +26,7 @@ export class Kerio extends WebClient {
25
26
  this.Logs = new Logs(this);
26
27
  this.Session = new Session(this);
27
28
  this.Domains = new Domains(this);
29
+ this.Users = new Users(this);
28
30
 
29
31
  this.logger = pino({
30
32
  level: "debug",
package/src/users.js ADDED
@@ -0,0 +1,208 @@
1
+ import { KerioModules } from './modules.js';
2
+
3
+ export class Users extends KerioModules {
4
+
5
+ /**
6
+ * Get a Mapped Array of KerioUserItems indexed by their ID for the specified domain name. Does not include aliases or user groups.
7
+ * @param {string} domainName Name of the domain
8
+ * @returns {Promise<Map<import('../index.d.ts').KerioUserID, import('../index.d.ts').KerioUserItem>>}
9
+ */
10
+ async list(domainName = null) {
11
+ try {
12
+ if (!this.instance.logged_in) {
13
+
14
+ this.reset();
15
+
16
+ throw {
17
+ name: "KerioClientSessionError",
18
+ message: `Kerio session invalid. Try logging in again.`,
19
+ type: 'Kerio',
20
+ from: "Kerio.Users.list"
21
+ }
22
+ }
23
+
24
+ if (!domainName) {
25
+ throw {
26
+ name: "KerioDomainNameUserError",
27
+ message: `Invalid Domain Name to find Domain ID while processing API method 'Users.get' for domain '${domainName}'`,
28
+ type: 'Kerio',
29
+ from: "Kerio.Users.list"
30
+ }
31
+ }
32
+
33
+ let domains = await this.instance.Domains.list();
34
+
35
+ if (domains.size != 0) {
36
+
37
+ let domainId = Array.from(domains.values()).find(d => d.name.toLowerCase() == domainName.toLowerCase()).id;
38
+
39
+ let users_response = await this.sessionedRequest({
40
+ http_method: 'POST',
41
+ api_method: 'Users.get',
42
+ auth: {
43
+ cookie: this.instance.session_cookie,
44
+ token: this.instance.x_token
45
+ },
46
+ body_params: {
47
+ query: {
48
+ },
49
+ domainId: domainId
50
+ }
51
+ });
52
+
53
+ let response_body = users_response._body;
54
+
55
+ if (!response_body.result?.errors && !response_body?.error) {
56
+
57
+ var userListResult = new Map();
58
+
59
+ if (response_body.result.list.length > 0) {
60
+
61
+ response_body.result.list.forEach(user => {
62
+ /**
63
+ * @type {import('../index.d.ts').KerioUserItem}
64
+ */
65
+ var obj = {
66
+ id: user.id,
67
+ domainId: user.domainId,
68
+ loginName: user.loginName,
69
+ fullName: user.fullName,
70
+ description: user.description,
71
+ enabled: user.isEnabled,
72
+ primaryEmailAddress: `${user.loginName}@${domainName}`
73
+ }
74
+
75
+ userListResult.set(user.id, obj);
76
+ });
77
+ }
78
+
79
+ return userListResult;
80
+
81
+ } else {
82
+ throw {
83
+ name: "KerioRequestError",
84
+ message: `Error occured while fetching results from API method 'Users.get' for domain ${domainName}`,
85
+ type: 'Kerio',
86
+ from: "Kerio.Users.list"
87
+ }
88
+ }
89
+
90
+ } else {
91
+ throw {
92
+ name: "KerioDomainError",
93
+ message: `Domain Name not found to find Domain ID while processing API method 'Users.get' for domain '${domainName}'`,
94
+ type: 'Kerio',
95
+ from: "Kerio.Users.list"
96
+ }
97
+ }
98
+
99
+ } catch (e) {
100
+ this.instance.logger.error(e);
101
+ throw e;
102
+ }
103
+ }
104
+
105
+ async getStatistics(kerioUserID = null) {
106
+ try {
107
+ if (!this.instance.logged_in) {
108
+
109
+ this.reset();
110
+
111
+ throw {
112
+ name: "KerioClientSessionError",
113
+ message: `Kerio session invalid. Try logging in again.`,
114
+ type: 'Kerio',
115
+ from: "Kerio.Users.getStatistics"
116
+ }
117
+ }
118
+
119
+ if (!kerioUserID || !kerioUserID.startsWith("keriodb://user/")) {
120
+ throw {
121
+ name: "KerioUserIDError",
122
+ message: `Invalid User ID while processing API method 'Users.getStatistics' for user ID '${kerioUserID}'`,
123
+ type: 'Kerio',
124
+ from: "Kerio.Users.getStatistics"
125
+ }
126
+ }
127
+
128
+ let user_stats_response = await this.sessionedRequest({
129
+ http_method: 'POST',
130
+ api_method: 'Users.getStatistics',
131
+ auth: {
132
+ cookie: this.instance.session_cookie,
133
+ token: this.instance.x_token
134
+ },
135
+ body_params: {
136
+ query: {
137
+
138
+ },
139
+ userIds: [
140
+ kerioUserID
141
+ ]
142
+ }
143
+ });
144
+
145
+ let response_body = user_stats_response._body;
146
+
147
+ if (!response_body.result?.errors && !response_body?.error) {
148
+
149
+ if (Array.isArray(response_body.result.list) && response_body.result.list.length > 0) {
150
+
151
+ let _stats = response_body.result.list[0];
152
+
153
+ let res = {
154
+ storage: {
155
+ items: _stats.occupiedSpace.items,
156
+ value: _stats.occupiedSpace.storage.value,
157
+ units: _stats.occupiedSpace.storage.units
158
+ },
159
+ lastLogin: "",
160
+ loginCount: {
161
+ pop3: _stats.pop3.count,
162
+ pop3s: _stats.securePop3.count,
163
+ imap: _stats.imap.count,
164
+ imaps: _stats.secureImap.count,
165
+ http: _stats.http.count,
166
+ https: _stats.secureHttp.count,
167
+ ldap: _stats.ldap.count,
168
+ ldaps: _stats.secureLdap.count,
169
+ nntp: _stats.nntp.count,
170
+ nntps: _stats.secureNntp.count,
171
+ activeSync: _stats.activeSync.count,
172
+ secureActiveSync: _stats.secureActiveSync.count,
173
+ xmpp: _stats.xmpp.count,
174
+ xmpps: _stats.secureXmpp.count
175
+ },
176
+ }
177
+
178
+ let login_timestamps_arr = Object.values(_stats).filter(services => services.lastLogin).map(t => t.lastLogin);
179
+
180
+ //most recent login timestamp YYYY-MM-DD HH:MM in server's local time zone
181
+ res.lastLogin = login_timestamps_arr.sort().at(-1);
182
+
183
+ return res;
184
+
185
+ } else {
186
+ throw {
187
+ name: "KerioUserStatsError",
188
+ message: `User may not exist. No statistics found for user ID ${kerioUserID}`,
189
+ type: 'Kerio',
190
+ from: "Kerio.Users.getStatistics"
191
+ }
192
+ }
193
+
194
+ } else {
195
+ throw {
196
+ name: "KerioRequestError",
197
+ message: `Error occured while fetching results from API method 'User.getStatistics'`,
198
+ type: 'Kerio',
199
+ from: "Kerio.Users.getStatistics"
200
+ }
201
+ }
202
+
203
+ } catch (e) {
204
+ this.instance.logger.error(e);
205
+ throw e;
206
+ }
207
+ }
208
+ }