@nocobase/database 2.1.0-beta.47 → 2.1.0-beta.48
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.
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
9
|
import { DataTypes } from 'sequelize';
|
|
10
|
+
import type { UpdateOptions } from 'sequelize';
|
|
11
|
+
import { Model } from '../model';
|
|
10
12
|
import { BaseColumnFieldOptions, Field } from './field';
|
|
11
13
|
export interface PasswordFieldOptions extends BaseColumnFieldOptions {
|
|
12
14
|
type: 'password';
|
|
@@ -20,11 +22,15 @@ export interface PasswordFieldOptions extends BaseColumnFieldOptions {
|
|
|
20
22
|
randomBytesSize?: number;
|
|
21
23
|
}
|
|
22
24
|
type PasswordValue = string | number;
|
|
25
|
+
type BulkUpdateOptions = UpdateOptions<Record<string, unknown>> & {
|
|
26
|
+
attributes?: Record<string, unknown>;
|
|
27
|
+
};
|
|
23
28
|
export declare class PasswordField extends Field {
|
|
24
29
|
get dataType(): DataTypes.StringDataTypeConstructor;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
30
|
+
listener: (instances: Model | Model[]) => Promise<void>;
|
|
31
|
+
bulkUpdateListener: (options: BulkUpdateOptions) => Promise<void>;
|
|
32
|
+
verify(password: PasswordValue, hash: string): Promise<boolean>;
|
|
33
|
+
hash(password: PasswordValue): Promise<string>;
|
|
28
34
|
bind(): void;
|
|
29
35
|
unbind(): void;
|
|
30
36
|
}
|
|
@@ -47,6 +47,41 @@ const _PasswordField = class _PasswordField extends import_field.Field {
|
|
|
47
47
|
get dataType() {
|
|
48
48
|
return import_sequelize.DataTypes.STRING;
|
|
49
49
|
}
|
|
50
|
+
listener = /* @__PURE__ */ __name(async (instances) => {
|
|
51
|
+
const { name } = this.options;
|
|
52
|
+
const fieldName = name;
|
|
53
|
+
instances = Array.isArray(instances) ? instances : [instances];
|
|
54
|
+
for (const instance of instances) {
|
|
55
|
+
if (!instance.changed(fieldName)) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
const value = instance.get(fieldName);
|
|
59
|
+
if (value !== null && value !== void 0 && value !== "") {
|
|
60
|
+
const password = typeof value === "number" ? value : String(value);
|
|
61
|
+
const hash = await this.hash(password);
|
|
62
|
+
instance.set(fieldName, hash);
|
|
63
|
+
} else {
|
|
64
|
+
instance.set(fieldName, instance.previous(fieldName));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}, "listener");
|
|
68
|
+
bulkUpdateListener = /* @__PURE__ */ __name(async (options) => {
|
|
69
|
+
var _a;
|
|
70
|
+
const { name } = this.options;
|
|
71
|
+
const fieldName = name;
|
|
72
|
+
const { attributes } = options;
|
|
73
|
+
if (!attributes || !Object.prototype.hasOwnProperty.call(attributes, fieldName)) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const value = attributes[fieldName];
|
|
77
|
+
if (value !== null && value !== void 0 && value !== "") {
|
|
78
|
+
const password = typeof value === "number" ? value : String(value);
|
|
79
|
+
attributes[fieldName] = await this.hash(password);
|
|
80
|
+
} else {
|
|
81
|
+
delete attributes[fieldName];
|
|
82
|
+
options.fields = (_a = options.fields) == null ? void 0 : _a.filter((field) => field !== fieldName);
|
|
83
|
+
}
|
|
84
|
+
}, "bulkUpdateListener");
|
|
50
85
|
async verify(password, hash) {
|
|
51
86
|
const passwordString = password === null || password === void 0 ? "" : String(password);
|
|
52
87
|
hash = hash || "";
|
|
@@ -71,37 +106,19 @@ const _PasswordField = class _PasswordField extends import_field.Field {
|
|
|
71
106
|
});
|
|
72
107
|
});
|
|
73
108
|
}
|
|
74
|
-
init() {
|
|
75
|
-
const { name } = this.options;
|
|
76
|
-
const fieldName = name;
|
|
77
|
-
this.listener = async (instances) => {
|
|
78
|
-
instances = Array.isArray(instances) ? instances : [instances];
|
|
79
|
-
for (const instance of instances) {
|
|
80
|
-
if (!instance.changed(fieldName)) {
|
|
81
|
-
continue;
|
|
82
|
-
}
|
|
83
|
-
const value = instance.get(fieldName);
|
|
84
|
-
if (value !== null && value !== void 0 && value !== "") {
|
|
85
|
-
const password = typeof value === "number" ? value : String(value);
|
|
86
|
-
const hash = await this.hash(password);
|
|
87
|
-
instance.set(fieldName, hash);
|
|
88
|
-
} else {
|
|
89
|
-
instance.set(fieldName, instance.previous(fieldName));
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
109
|
bind() {
|
|
95
110
|
super.bind();
|
|
96
111
|
this.on("beforeCreate", this.listener);
|
|
97
112
|
this.on("beforeBulkCreate", this.listener);
|
|
98
113
|
this.on("beforeUpdate", this.listener);
|
|
114
|
+
this.on("beforeBulkUpdate", this.bulkUpdateListener);
|
|
99
115
|
}
|
|
100
116
|
unbind() {
|
|
101
117
|
super.unbind();
|
|
102
118
|
this.off("beforeCreate", this.listener);
|
|
103
119
|
this.off("beforeBulkCreate", this.listener);
|
|
104
120
|
this.off("beforeUpdate", this.listener);
|
|
121
|
+
this.off("beforeBulkUpdate", this.bulkUpdateListener);
|
|
105
122
|
}
|
|
106
123
|
};
|
|
107
124
|
__name(_PasswordField, "PasswordField");
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/database",
|
|
3
|
-
"version": "2.1.0-beta.
|
|
3
|
+
"version": "2.1.0-beta.48",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
7
7
|
"license": "Apache-2.0",
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@nocobase/logger": "2.1.0-beta.
|
|
10
|
-
"@nocobase/utils": "2.1.0-beta.
|
|
9
|
+
"@nocobase/logger": "2.1.0-beta.48",
|
|
10
|
+
"@nocobase/utils": "2.1.0-beta.48",
|
|
11
11
|
"async-mutex": "^0.3.2",
|
|
12
12
|
"chalk": "^4.1.1",
|
|
13
13
|
"cron-parser": "4.4.0",
|
|
@@ -38,5 +38,5 @@
|
|
|
38
38
|
"url": "git+https://github.com/nocobase/nocobase.git",
|
|
39
39
|
"directory": "packages/database"
|
|
40
40
|
},
|
|
41
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "f8c27a286db015c5e433b48241f14c0412e50530"
|
|
42
42
|
}
|