@nocobase/plugin-departments 2.0.0-alpha.47 → 2.0.0-alpha.49

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.
@@ -35,7 +35,6 @@ var import_departments = require("./actions/departments");
35
35
  var import_users = require("./actions/users");
36
36
  var import_users2 = require("./collections/users");
37
37
  var import_middlewares = require("./middlewares");
38
- var import_set_departments_roles = require("./middlewares/set-departments-roles");
39
38
  var import_department = require("./models/department");
40
39
  var import_department_data_sync_resource = require("./department-data-sync-resource");
41
40
  class PluginDepartmentsServer extends import_server.Plugin {
@@ -52,6 +51,7 @@ class PluginDepartmentsServer extends import_server.Plugin {
52
51
  });
53
52
  }
54
53
  async load() {
54
+ this.registerErrorHandler();
55
55
  this.app.resourceManager.registerActionHandlers({
56
56
  "users:listExcludeDept": import_users.listExcludeDept,
57
57
  "users:setMainDepartment": import_users.setMainDepartment,
@@ -73,13 +73,13 @@ class PluginDepartmentsServer extends import_server.Plugin {
73
73
  "departments.members:*"
74
74
  ]
75
75
  });
76
- this.app.resourceManager.use(import_set_departments_roles.setDepartmentsInfo, {
76
+ this.app.resourceManager.use(import_middlewares.setDepartmentsInfo, {
77
77
  tag: "setDepartmentsInfo",
78
78
  before: "setCurrentRole",
79
79
  after: "auth"
80
80
  });
81
81
  this.app.dataSourceManager.afterAddDataSource((dataSource) => {
82
- dataSource.resourceManager.use(import_set_departments_roles.setDepartmentsInfo, {
82
+ dataSource.resourceManager.use(import_middlewares.setDepartmentsInfo, {
83
83
  tag: "setDepartmentsInfo",
84
84
  before: "setCurrentRole",
85
85
  after: "auth"
@@ -98,6 +98,38 @@ class PluginDepartmentsServer extends import_server.Plugin {
98
98
  const cache = this.app.cache;
99
99
  await cache.del(`departments:${model.get("userId")}`);
100
100
  });
101
+ this.app.db.on("users.beforeSave", async (model, options) => {
102
+ var _a;
103
+ const mainDepartmentId = model.get("mainDepartmentId");
104
+ if (!mainDepartmentId) {
105
+ return;
106
+ }
107
+ const userId = model.get("id");
108
+ const transaction = options == null ? void 0 : options.transaction;
109
+ if (userId) {
110
+ const userDepartment = await this.app.db.getRepository("departmentsUsers").findOne({
111
+ filter: {
112
+ userId,
113
+ departmentId: mainDepartmentId
114
+ },
115
+ transaction
116
+ });
117
+ if (userDepartment) {
118
+ return;
119
+ }
120
+ }
121
+ const submittedDepartments = (_a = options == null ? void 0 : options.values) == null ? void 0 : _a.departments;
122
+ if (Array.isArray(submittedDepartments)) {
123
+ const included = submittedDepartments.some((d) => {
124
+ const id = typeof d === "object" ? d && (d.id ?? d) : d;
125
+ return `${id}` === `${mainDepartmentId}`;
126
+ });
127
+ if (included) {
128
+ return;
129
+ }
130
+ }
131
+ throw new Error(`Invalid main department, it must be one of the user's departments`);
132
+ });
101
133
  this.app.on("beforeSignOut", ({ userId }) => {
102
134
  this.app.cache.del(`departments:${userId}`);
103
135
  });
@@ -146,6 +178,20 @@ class PluginDepartmentsServer extends import_server.Plugin {
146
178
  }
147
179
  async remove() {
148
180
  }
181
+ registerErrorHandler() {
182
+ const errorHandlerPlugin = this.app.pm.get("error-handler");
183
+ errorHandlerPlugin.errorHandler.register(
184
+ (err) => {
185
+ return err.message === "Invalid main department, it must be one of the user's departments";
186
+ },
187
+ (err, ctx) => {
188
+ return ctx.throw(
189
+ 400,
190
+ ctx.i18n.t("Invalid main department, it must be one of the user's departments", { ns: "departments" })
191
+ );
192
+ }
193
+ );
194
+ }
149
195
  }
150
196
  var plugin_default = PluginDepartmentsServer;
151
197
  // Annotate the CommonJS export names for ESM import in node:
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "description": "Organize users by departments, set hierarchical relationships, link roles to control permissions, and use departments as variables in workflows and expressions.",
7
7
  "description.ru-RU": "Организация пользователей по подразделениям, установление иерархических связей, привязка ролей для управления правами и использование подразделений в качестве переменных в рабочих процессах и выражениях.",
8
8
  "description.zh-CN": "以部门来组织用户,设定上下级关系,绑定角色控制权限,并支持作为变量用于工作流和表达式。",
9
- "version": "2.0.0-alpha.47",
9
+ "version": "2.0.0-alpha.49",
10
10
  "main": "dist/server/index.js",
11
11
  "peerDependencies": {
12
12
  "@nocobase/actions": "2.x",
@@ -18,5 +18,5 @@
18
18
  "keywords": [
19
19
  "Users & permissions"
20
20
  ],
21
- "gitHead": "50e5bfd0c5b6879535d20abcbbabc5541d749e12"
21
+ "gitHead": "99f71bc8961da62a761630b3eae11092a4372e3e"
22
22
  }