1 | "use strict";
|
2 | var __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 | };
|
8 | var __metadata = (this && this.__metadata) || function (k, v) {
|
9 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
10 | };
|
11 | var __param = (this && this.__param) || function (paramIndex, decorator) {
|
12 | return function (target, key) { decorator(target, key, paramIndex); }
|
13 | };
|
14 | var __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 | };
|
22 | Object.defineProperty(exports, "__esModule", { value: true });
|
23 | const typeorm_1 = require("@nestjs/typeorm");
|
24 | const crypto = require("crypto");
|
25 | const typeorm_2 = require("typeorm");
|
26 | const func_entity_1 = require("../model/func.entity");
|
27 | const role_entity_1 = require("../model/role.entity");
|
28 | const user_entity_1 = require("../model/user.entity");
|
29 | let UserComponent = class UserComponent {
|
30 | constructor(funcRepository, roleRepository, userRepository) {
|
31 | this.funcRepository = funcRepository;
|
32 | this.roleRepository = roleRepository;
|
33 | this.userRepository = userRepository;
|
34 | }
|
35 | permissions(id) {
|
36 | return __awaiter(this, void 0, void 0, function* () {
|
37 | const user = yield this.userRepository.findOneById(id, { relations: ["roles", "adds", "reduces"] });
|
38 | if (!user) {
|
39 | return [];
|
40 | }
|
41 | const result = [];
|
42 | let temp = [];
|
43 | const ids = new Set();
|
44 | for (let i = 0; i < user.roles.length; i++) {
|
45 | const role = yield this.roleRepository.findOneById(user.roles[i].id, { relations: ["funcs"] });
|
46 | if (role && role.funcs && role.funcs.length > 0) {
|
47 | for (let j = 0; j < role.funcs.length; j++) {
|
48 | const func = yield this.funcRepository.findOneById(role.funcs[i].id, { relations: ["permissions"] });
|
49 | if (func) {
|
50 | temp = temp.concat(func.permissions);
|
51 | }
|
52 | }
|
53 | }
|
54 | }
|
55 | temp.forEach(per => {
|
56 | if (!ids.has(per.id)) {
|
57 | ids.add(per.id);
|
58 | result.push(per);
|
59 | }
|
60 | });
|
61 | user.adds.forEach(per => {
|
62 | if (!ids.has(per.id)) {
|
63 | ids.add(per.id);
|
64 | result.push(per);
|
65 | }
|
66 | });
|
67 | user.reduces.forEach(per => {
|
68 | if (ids.has(per.id)) {
|
69 | ids.delete(per.id);
|
70 | const index = result.findIndex(p => {
|
71 | return p.id === per.id;
|
72 | });
|
73 | result.splice(index, 1);
|
74 | }
|
75 | });
|
76 | result.sort((a, b) => {
|
77 | return a.id - b.id;
|
78 | });
|
79 | return result;
|
80 | });
|
81 | }
|
82 | login(userName, password) {
|
83 | return __awaiter(this, void 0, void 0, function* () {
|
84 | const user = yield this.userRepository.findOne({ userName });
|
85 | if (!user) {
|
86 | return false;
|
87 | }
|
88 | if (user.recycle) {
|
89 | return false;
|
90 | }
|
91 | const passwordWithSalt = crypto.createHash("md5").update(password + user.salt).digest("hex");
|
92 | if (passwordWithSalt !== user.password) {
|
93 | return false;
|
94 | }
|
95 | return this.userRepository.findOneById(user.id, { select: ["id", "userName", "status", "recycle"] });
|
96 | });
|
97 | }
|
98 | getUser(id) {
|
99 | return __awaiter(this, void 0, void 0, function* () {
|
100 | return this.userRepository.findOneById(id, { select: ["id", "userName", "status", "recycle"] });
|
101 | });
|
102 | }
|
103 | isExist(user) {
|
104 | return __awaiter(this, void 0, void 0, function* () {
|
105 | const exist = yield this.userRepository.findOne(user);
|
106 | return !!exist;
|
107 | });
|
108 | }
|
109 | };
|
110 | UserComponent = __decorate([
|
111 | __param(0, typeorm_1.InjectRepository(func_entity_1.Func)),
|
112 | __param(1, typeorm_1.InjectRepository(role_entity_1.Role)),
|
113 | __param(2, typeorm_1.InjectRepository(user_entity_1.User)),
|
114 | __metadata("design:paramtypes", [typeorm_2.Repository,
|
115 | typeorm_2.Repository,
|
116 | typeorm_2.Repository])
|
117 | ], UserComponent);
|
118 | exports.UserComponent = UserComponent;
|
119 | exports.UserComponentProvider = {
|
120 | provide: "UserComponentToken",
|
121 | useFactory: (funcRepository, roleRepository, userRepository) => {
|
122 | return new UserComponent(funcRepository, roleRepository, userRepository);
|
123 | },
|
124 | inject: ["FuncRepository", "RoleRepository", "UserRepository"]
|
125 | };
|