UNPKG

25.6 kBJavaScriptView Raw
1"use strict";
2var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6 return c > 3 && r && Object.defineProperty(target, key, r), r;
7};
8var __metadata = (this && this.__metadata) || function (k, v) {
9 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10};
11var __param = (this && this.__param) || function (paramIndex, decorator) {
12 return function (target, key) { decorator(target, key, paramIndex); }
13};
14var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
15 return new (P || (P = Promise))(function (resolve, reject) {
16 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
19 step((generator = generator.apply(thisArg, _arguments || [])).next());
20 });
21};
22Object.defineProperty(exports, "__esModule", { value: true });
23const common_1 = require("@nestjs/common");
24const typeorm_1 = require("@nestjs/typeorm");
25const crypto = require("crypto");
26const typeorm_2 = require("typeorm");
27const func_entity_1 = require("../model/func.entity");
28const info_group_entity_1 = require("../model/info.group.entity");
29const organization_entity_1 = require("../model/organization.entity");
30const permission_entity_1 = require("../model/permission.entity");
31const role_entity_1 = require("../model/role.entity");
32const user_entity_1 = require("../model/user.entity");
33const user_info_entity_1 = require("../model/user.info.entity");
34let UserService = class UserService {
35 constructor(storeComponent, funcRepository, roleRepository, userRepository, userInfoRepository, infoGroupRepository, permissionRepository, organizationRepository) {
36 this.storeComponent = storeComponent;
37 this.funcRepository = funcRepository;
38 this.roleRepository = roleRepository;
39 this.userRepository = userRepository;
40 this.userInfoRepository = userInfoRepository;
41 this.infoGroupRepository = infoGroupRepository;
42 this.permissionRepository = permissionRepository;
43 this.organizationRepository = organizationRepository;
44 }
45 getAll() {
46 return __awaiter(this, void 0, void 0, function* () {
47 return this.userRepository.find({ recycle: false });
48 });
49 }
50 getFreedomUsers() {
51 return __awaiter(this, void 0, void 0, function* () {
52 const users = yield this.userRepository.find({ relations: ["organizations"] });
53 return users.filter(user => {
54 return (user.organizations === null || user.organizations === undefined || user.organizations.length === 0) && !user.recycle;
55 });
56 });
57 }
58 getRecycleUsers() {
59 return __awaiter(this, void 0, void 0, function* () {
60 return this.userRepository.find({ recycle: true });
61 });
62 }
63 userInfos(id) {
64 return __awaiter(this, void 0, void 0, function* () {
65 const user = yield this.userRepository.findOneById(id, { relations: ["userInfos"] });
66 if (!user) {
67 throw new common_1.HttpException("指定用户不存在", 406);
68 }
69 const userInfos = yield this.userInfoRepository.createQueryBuilder("userInfo").leftJoinAndSelect("userInfo.infoItem", "infoItem", "userInfo.infoItemId=infoItem.id").where("userInfo.userId = :id", { id }).getMany();
70 return userInfos.map(userInfo => {
71 return { name: userInfo.infoItem.name, value: userInfo.value };
72 });
73 });
74 }
75 roles(id) {
76 return __awaiter(this, void 0, void 0, function* () {
77 const user = yield this.userRepository.findOneById(id, { relations: ["roles"] });
78 if (!user) {
79 throw new common_1.HttpException("指定用户不存在", 406);
80 }
81 return user.roles;
82 });
83 }
84 permissions(id) {
85 return __awaiter(this, void 0, void 0, function* () {
86 const user = yield this.userRepository.findOneById(id, { relations: ["roles", "adds", "reduces"] });
87 if (!user) {
88 throw new common_1.HttpException("指定id=" + id + "用户不存在", 406);
89 }
90 const result = [];
91 let temp = [];
92 const ids = new Set();
93 for (let i = 0; i < user.roles.length; i++) {
94 const role = yield this.roleRepository.findOneById(user.roles[i].id, { relations: ["funcs"] });
95 if (role && role.funcs && role.funcs.length > 0) {
96 for (let j = 0; j < role.funcs.length; j++) {
97 const func = yield this.funcRepository.findOneById(role.funcs[i].id, { relations: ["permissions"] });
98 if (func) {
99 temp = temp.concat(func.permissions);
100 }
101 }
102 }
103 }
104 temp.forEach(per => {
105 if (!ids.has(per.id)) {
106 ids.add(per.id);
107 result.push(per);
108 }
109 });
110 user.adds.forEach(per => {
111 if (!ids.has(per.id)) {
112 ids.add(per.id);
113 result.push(per);
114 }
115 });
116 user.reduces.forEach(per => {
117 if (ids.has(per.id)) {
118 ids.delete(per.id);
119 const index = result.findIndex(p => {
120 return p.id === per.id;
121 });
122 result.splice(index, 1);
123 }
124 });
125 result.sort((a, b) => {
126 return a.id - b.id;
127 });
128 return result;
129 });
130 }
131 createUser(organizationId, userName, password) {
132 return __awaiter(this, void 0, void 0, function* () {
133 const organizations = [];
134 if (organizationId) {
135 const organization = yield this.organizationRepository.findOneById(organizationId);
136 if (!organization) {
137 throw new common_1.HttpException("指定id=" + organizationId + "组织不存在", 402);
138 }
139 organizations.push(organization);
140 }
141 const exist = yield this.userRepository.findOne({ userName });
142 if (exist) {
143 throw new common_1.HttpException("指定userName=" + userName + "用户已存在", 406);
144 }
145 try {
146 const salt = crypto.createHash("md5").update(new Date().toString()).digest("hex").slice(0, 10);
147 const passwordWithSalt = crypto.createHash("md5").update(password + salt).digest("hex");
148 const user = this.userRepository.create({
149 userName,
150 password: passwordWithSalt,
151 salt,
152 status: true,
153 recycle: false,
154 organizations
155 });
156 yield this.userRepository.save(user);
157 }
158 catch (err) {
159 throw new common_1.HttpException("数据库错误" + err.toString(), 401);
160 }
161 });
162 }
163 createUserWithUserInfo(req, organizationId, userName, password, groups) {
164 return __awaiter(this, void 0, void 0, function* () {
165 const organizations = [];
166 if (organizationId) {
167 const organization = yield this.organizationRepository.findOneById(organizationId);
168 if (!organization) {
169 throw new common_1.HttpException("指定id=" + organizationId + "组织不存在", 402);
170 }
171 organizations.push(organization);
172 }
173 const exist = yield this.userRepository.findOne({ userName });
174 if (exist) {
175 throw new common_1.HttpException("指定userName=" + userName + "用户已存在", 406);
176 }
177 const salt = crypto.createHash("md5").update(new Date().toString()).digest("hex").slice(0, 10);
178 const passwordWithSalt = crypto.createHash("md5").update(password + salt).digest("hex");
179 const user = this.userRepository.create({
180 userName,
181 password: passwordWithSalt,
182 salt,
183 status: true,
184 recycle: false,
185 organizations,
186 userInfos: [],
187 infoItems: []
188 });
189 for (let i = 0; i < groups.length; i++) {
190 const { groupId, infos } = groups[i];
191 const group = yield this.infoGroupRepository.findOneById(groupId, { relations: ["items"] });
192 if (!group) {
193 throw new common_1.HttpException("指定信息组id=" + groupId + "不存在", 408);
194 }
195 yield this.addUserInfosAndInfoItems(req, user, group, infos);
196 }
197 try {
198 yield this.userRepository.save(user);
199 }
200 catch (err) {
201 throw new common_1.HttpException("数据库错误" + err.toString(), 401);
202 }
203 });
204 }
205 addUserInfoToUser(req, id, groups) {
206 return __awaiter(this, void 0, void 0, function* () {
207 const user = yield this.userRepository.findOneById(id, { relations: ["userInfos", "infoItems"] });
208 if (!user) {
209 throw new common_1.HttpException("指定id=" + id + "用户不存在", 406);
210 }
211 for (let i = 0; i < groups.length; i++) {
212 const { groupId, infos } = groups[i];
213 const group = yield this.infoGroupRepository.findOneById(groupId, { relations: ["items"] });
214 if (!group) {
215 throw new common_1.HttpException("指定信息组id=" + groupId + "不存在", 408);
216 }
217 yield this.addUserInfosAndInfoItems(req, user, group, infos);
218 }
219 try {
220 yield this.userRepository.save(user);
221 }
222 catch (err) {
223 throw new common_1.HttpException("数据库错误" + err.toString(), 401);
224 }
225 });
226 }
227 addUserInfosAndInfoItems(req, user, group, infos) {
228 return __awaiter(this, void 0, void 0, function* () {
229 const items = group.items || [];
230 const necessary = items.filter(item => {
231 return !!item.necessary;
232 });
233 for (let j = 0; j < infos.length; j++) {
234 const { name } = infos[j];
235 const match = items.find(item => {
236 return item.name === name;
237 });
238 if (!match) {
239 throw new common_1.HttpException("指定名称信息项:" + name + "不存在于信息组id=" + group.id + "中", 409);
240 }
241 const result = yield this.transfromInfoValue(req, match, infos[j]);
242 const userInfoIndex = user.userInfos.findIndex(userInfo => userInfo.infoItemId === match.id);
243 if (userInfoIndex >= 0) {
244 user.userInfos[userInfoIndex].value = result;
245 }
246 else {
247 user.userInfos.push(this.userInfoRepository.create({ infoItem: match, value: result }));
248 }
249 const index = necessary.findIndex(item => {
250 return item.id === match.id;
251 });
252 if (index >= 0) {
253 necessary.splice(index, 1);
254 }
255 user.infoItems.push(match);
256 }
257 if (necessary.length !== 0) {
258 const names = necessary.map(item => item.name);
259 throw new common_1.HttpException("指定信息项:" + names.join(",") + "为必填项", 410);
260 }
261 });
262 }
263 transfromInfoValue(req, match, info) {
264 return __awaiter(this, void 0, void 0, function* () {
265 let result;
266 if (match.type === "text" || match.type === "textarea" || match.type === "radio" || match.type === "date" || match.type === "number" || match.type === "pulldownmenu") {
267 if (!info.value) {
268 throw new common_1.HttpException("指定名称信息值:" + match.name + "不存在", 410);
269 }
270 if (!(typeof info.value === "string")) {
271 throw new common_1.HttpException("指定名称信息项name=" + match.name + "必须为字符串", 410);
272 }
273 result = info.value.trim();
274 }
275 else if (match.type === "checkbox") {
276 if (!info.array || info.array.length === 0) {
277 throw new common_1.HttpException("指定名称信息值:" + match.name + "不存在", 410);
278 }
279 if (!(info.array instanceof Array)) {
280 throw new common_1.HttpException("指定名称信息项name=" + match.name + "必须为数组", 410);
281 }
282 result = info.array.join(",");
283 }
284 else {
285 if (!info.base64) {
286 throw new common_1.HttpException("指定名称信息项name=" + match.name + "必须具有文件base64编码", 410);
287 }
288 if (!info.rawName) {
289 throw new common_1.HttpException("指定名称信息项name=" + match.name + "必须具有文件原名", 410);
290 }
291 if (!info.bucketName) {
292 throw new common_1.HttpException("指定名称信息项name=" + match.name + "必须具有文件存储空间名", 410);
293 }
294 const { bucketName, name, type } = yield this.storeComponent.upload(info.bucketName, info.rawName, info.base64, undefined);
295 result = yield this.storeComponent.getUrl(req, bucketName, name, type, undefined);
296 }
297 return result;
298 });
299 }
300 updateUser(id, userName, password) {
301 return __awaiter(this, void 0, void 0, function* () {
302 const exist = yield this.userRepository.findOneById(id);
303 if (!exist) {
304 throw new common_1.HttpException("指定id=" + id + "用户不存在", 406);
305 }
306 if (userName !== exist.userName) {
307 const sameUser = yield this.userRepository.findOne({ userName });
308 if (sameUser) {
309 throw new common_1.HttpException("指定userName=" + userName + "用户已存在", 406);
310 }
311 }
312 try {
313 exist.userName = userName;
314 const salt = crypto.createHash("md5").update(new Date().toString()).digest("hex").slice(0, 10);
315 exist.salt = salt;
316 exist.password = crypto.createHash("md5").update(password + salt).digest("hex");
317 yield this.userRepository.save(exist);
318 }
319 catch (err) {
320 throw new common_1.HttpException("数据库错误" + err.toString(), 401);
321 }
322 });
323 }
324 bannedUser(id) {
325 return __awaiter(this, void 0, void 0, function* () {
326 const exist = yield this.userRepository.findOneById(id);
327 if (!exist) {
328 throw new common_1.HttpException("指定id=" + id + "用户不存在", 406);
329 }
330 if (exist.recycle) {
331 throw new common_1.HttpException("指定id=" + id + "用户已存在回收站中", 406);
332 }
333 if (!exist.status) {
334 throw new common_1.HttpException("指定id=" + id + "用户已经封禁", 406);
335 }
336 try {
337 exist.status = false;
338 yield this.userRepository.save(exist);
339 }
340 catch (err) {
341 throw new common_1.HttpException("数据库错误" + err.toString(), 401);
342 }
343 });
344 }
345 unBannedUser(id) {
346 return __awaiter(this, void 0, void 0, function* () {
347 const exist = yield this.userRepository.findOneById(id);
348 if (!exist) {
349 throw new common_1.HttpException("指定id=" + id + "用户不存在", 406);
350 }
351 if (exist.recycle) {
352 throw new common_1.HttpException("指定id=" + id + "用户已存在回收站中", 406);
353 }
354 if (exist.status) {
355 throw new common_1.HttpException("指定id=" + id + "用户不需要解封", 406);
356 }
357 try {
358 exist.status = true;
359 yield this.userRepository.save(exist);
360 }
361 catch (err) {
362 throw new common_1.HttpException("数据库错误" + err.toString(), 401);
363 }
364 });
365 }
366 softDeleteUser(id) {
367 return __awaiter(this, void 0, void 0, function* () {
368 const exist = yield this.userRepository.findOneById(id);
369 if (!exist) {
370 throw new common_1.HttpException("指定id=" + id + "用户不存在", 406);
371 }
372 if (exist.recycle) {
373 throw new common_1.HttpException("指定id=" + id + "用户已存在回收站中", 406);
374 }
375 try {
376 exist.recycle = true;
377 yield this.userRepository.save(exist);
378 }
379 catch (err) {
380 throw new common_1.HttpException("数据库错误" + err.toString(), 401);
381 }
382 });
383 }
384 restoreUser(id) {
385 return __awaiter(this, void 0, void 0, function* () {
386 const exist = yield this.userRepository.findOneById(id);
387 if (!exist) {
388 throw new common_1.HttpException("指定id=" + id + "用户不存在", 406);
389 }
390 if (!exist.recycle) {
391 throw new common_1.HttpException("指定id=" + id + "用户不存在回收站中", 406);
392 }
393 try {
394 exist.recycle = false;
395 yield this.userRepository.save(exist);
396 }
397 catch (err) {
398 throw new common_1.HttpException("数据库错误" + err.toString(), 401);
399 }
400 });
401 }
402 restoreUsers(ids) {
403 return __awaiter(this, void 0, void 0, function* () {
404 const users = yield this.userRepository.findByIds(ids);
405 ids.forEach(id => {
406 const find = users.find(user => {
407 return user.id === id;
408 });
409 if (!find) {
410 throw new common_1.HttpException("指定id=" + id + "用户不存在", 406);
411 }
412 if (!find.recycle) {
413 throw new common_1.HttpException("指定用户id=" + id + "不在回收站中", 406);
414 }
415 find.recycle = false;
416 });
417 try {
418 yield this.userRepository.save(users);
419 }
420 catch (err) {
421 throw new common_1.HttpException("数据库错误" + err.toString(), 401);
422 }
423 });
424 }
425 deleteUser(id) {
426 return __awaiter(this, void 0, void 0, function* () {
427 const exist = yield this.userRepository.findOneById(id);
428 if (!exist) {
429 throw new common_1.HttpException("指定id=" + id + "用户不存在", 406);
430 }
431 if (!exist.recycle) {
432 throw new common_1.HttpException("指定id=" + id + "用户不存在回收站中", 406);
433 }
434 try {
435 yield this.userRepository.remove(exist);
436 }
437 catch (err) {
438 throw new common_1.HttpException("数据库错误" + err.toString(), 401);
439 }
440 });
441 }
442 deleteUsers(ids) {
443 return __awaiter(this, void 0, void 0, function* () {
444 const users = yield this.userRepository.findByIds(ids);
445 ids.forEach(id => {
446 const find = users.find(user => {
447 return user.id === id;
448 });
449 if (!find) {
450 throw new common_1.HttpException("指定id=" + id + "用户不存在", 406);
451 }
452 if (!find.recycle) {
453 throw new common_1.HttpException("指定id=" + id + "用户不存在于回收站中", 406);
454 }
455 });
456 try {
457 yield this.userRepository.remove(users);
458 }
459 catch (err) {
460 throw new common_1.HttpException("数据库错误" + err.toString(), 401);
461 }
462 });
463 }
464 setRoles(id, roleIds) {
465 return __awaiter(this, void 0, void 0, function* () {
466 const user = yield this.userRepository.findOneById(id, { relations: ["roles"] });
467 if (!user) {
468 throw new common_1.HttpException("指定id=" + id + "用户不存在", 406);
469 }
470 const roles = yield this.roleRepository.findByIds(roleIds);
471 roleIds.forEach(roleId => {
472 const find = roles.find(role => {
473 return role.id === roleId;
474 });
475 if (!find) {
476 throw new common_1.HttpException("指定id=" + roleId + "角色不存在", 406);
477 }
478 });
479 user.roles = roles;
480 try {
481 yield this.userRepository.save(user);
482 }
483 catch (err) {
484 throw new common_1.HttpException("数据库错误" + err.toString(), 401);
485 }
486 });
487 }
488 setPermissions(id, permissionIds) {
489 return __awaiter(this, void 0, void 0, function* () {
490 const user = yield this.userRepository.findOneById(id, { relations: ["roles", "adds", "reduces"] });
491 if (!user) {
492 throw new common_1.HttpException("指定id=" + id + "用户不存在", 406);
493 }
494 const result = [];
495 let temp = [];
496 const ids = new Set();
497 for (let i = 0; i < user.roles.length; i++) {
498 const role = yield this.roleRepository.findOneById(user.roles[i].id, { relations: ["funcs"] });
499 if (role && role.funcs && role.funcs.length > 0) {
500 for (let j = 0; j < role.funcs.length; j++) {
501 const func = yield this.funcRepository.findOneById(role.funcs[i].id, { relations: ["permissions"] });
502 if (func) {
503 temp = temp.concat(func.permissions);
504 }
505 }
506 }
507 }
508 temp.forEach(per => {
509 if (!ids.has(per.id)) {
510 ids.add(per.id);
511 result.push(per);
512 }
513 });
514 permissionIds = [].concat(...new Set(permissionIds));
515 const adds = [];
516 const reduces = [];
517 const permissions = yield this.permissionRepository.findByIds(permissionIds);
518 permissions.forEach(per => {
519 const find = result.find(p => {
520 return p.id === per.id;
521 });
522 if (!find) {
523 adds.push(per);
524 }
525 });
526 result.forEach(per => {
527 const find = permissions.find(p => {
528 return p.id === per.id;
529 });
530 if (!find) {
531 reduces.push(per);
532 }
533 });
534 try {
535 user.adds = adds;
536 user.reduces = reduces;
537 yield this.userRepository.save(user);
538 }
539 catch (err) {
540 throw new common_1.HttpException("数据库错误" + err.toString(), 401);
541 }
542 });
543 }
544};
545UserService = __decorate([
546 common_1.Component(),
547 __param(0, common_1.Inject("StoreComponentToken")),
548 __param(1, typeorm_1.InjectRepository(func_entity_1.Func)),
549 __param(2, typeorm_1.InjectRepository(role_entity_1.Role)),
550 __param(3, typeorm_1.InjectRepository(user_entity_1.User)),
551 __param(4, typeorm_1.InjectRepository(user_info_entity_1.UserInfo)),
552 __param(5, typeorm_1.InjectRepository(info_group_entity_1.InfoGroup)),
553 __param(6, typeorm_1.InjectRepository(permission_entity_1.Permission)),
554 __param(7, typeorm_1.InjectRepository(organization_entity_1.Organization)),
555 __metadata("design:paramtypes", [Object, typeorm_2.Repository,
556 typeorm_2.Repository,
557 typeorm_2.Repository,
558 typeorm_2.Repository,
559 typeorm_2.Repository,
560 typeorm_2.Repository,
561 typeorm_2.Repository])
562], UserService);
563exports.UserService = UserService;