-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontroller.js
244 lines (214 loc) · 6.65 KB
/
controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
const reduce = require('mout/object/reduce');
const asyncWrapper = require('../async_wrapper');
const {isMongoDB} = require('../helper');
const logger = require('../logger');
const errors = require('../errors');
const genAdminRole = (roleId, paths) => {
return reduce(paths, (ret, obj, k) => {
if (!obj.path) {
obj = {
path: k,
allow: obj,
};
}
if (!obj.allow) {
return ret;
}
const p = obj.path.split(':/');
ret.push({
role_id: roleId,
method: p[0].trim().toUpperCase(),
resource: p[1].trim(),
});
return ret;
}, []);
};
/**
* Controller : List Admin Role
* HTTP Method : GET
* PATH : /adminrole
*
* @param {Object} options
* @param {Sequelize.model} options.admin_roles
* @param {Object} pager
* @returns {function(*, *, *)}
*/
const registerList = (options, pager) => {
const AdminRoles = options.admin_roles;
return asyncWrapper(async (req, res) => {
const limit = Number(req.query.limit || pager.defaultLimit);
const offset = Number(req.query.offset || 0);
let list;
if (isMongoDB(AdminRoles)) { // MongoDB
list = await AdminRoles.find();
} else { //MySQL
list = await AdminRoles.findAll();
}
const data = reduce(reduce(list, (ret, role) => {
if (req.swagger.operation.responses[200].schema.items.properties.paths.type === 'array') {
// paths: [{"allow":true, "path":"GET:/users"}] パターン
ret[role.role_id] = ret[role.role_id] || [];
ret[role.role_id].push({allow: true, path: `${role.method}:/${role.resource}`});
} else {
// paths: {"GET:/users": true} パターン
ret[role.role_id] = ret[role.role_id] || {};
ret[role.role_id][`${role.method}:/${role.resource}`] = true;
}
return ret;
}, {}), (ret, paths, roleId) => {
ret.push({paths: paths, role_id: roleId});
return ret;
}, []);
const count = data.length;
const _data = data.slice(offset, offset + limit);
pager.setResHeader(res, limit, offset, count);
return res.json(_data);
});
};
/**
* Controller : Create Admin Role
* HTTP Method : POST
* PATH : /adminrole
*
* @param {Object} options
* @param {Sequelize.model} options.admin_roles
* @param {Sequelize} options.store
* @returns {function(*, *, *)}
*/
const registerCreate = options => {
const AdminRoles = options.admin_roles;
return asyncWrapper(async (req, res) => {
const roleId = req.body.role_id;
const paths = req.body.paths;
const list = genAdminRole(roleId, paths);
let data;
if (isMongoDB(AdminRoles)) { // MongoDB
data = await AdminRoles.find({role_id: roleId});
} else { //MySQL
data = await AdminRoles.findAll({where: {role_id: roleId}});
}
if (data.length !== 0) {
throw errors.frontend.AlreadyUsedRoleID();
}
if (isMongoDB(AdminRoles)) { // MongoDB
await AdminRoles.insertMany(list);
} else { //MySQL
await AdminRoles.bulkCreate(list);
}
return res.json({role_id: roleId, paths: paths});
});
};
/**
* Controller : Get Admin Role
* HTTP Method : GET
* PATH : /adminrole/:role_id
*
* @param {Object} options
* @param {Sequelize.model} options.admin_roles
* @returns {function(*, *, *)}
*/
const registerGet = options => {
const AdminRoles = options.admin_roles;
return asyncWrapper(async (req, res) => {
const roleId = req.swagger.params.role_id.value;
let list;
if (isMongoDB(AdminRoles)) { // MongoDB
list = await AdminRoles.find({role_id: roleId});
} else { //MySQL
list = await AdminRoles.findAll({where: {role_id: roleId}});
}
let paths;
if (req.swagger.operation.responses[200].schema.properties.paths.type === 'array') {
// paths: [{"allow":true, "path":"GET:/users"}] パターン
paths = list.map(role => {
return {allow: true, path: `${role.method}:/${role.resource}`};
});
} else {
// paths: {"GET:/users": true} パターン
paths = reduce(list, (obj, role) => {
obj[`${role.method}:/${role.resource}`] = true;
return obj;
}, {});
}
return res.json({paths: paths, role_id: roleId});
});
};
/**
* Controller : Remove Admin Role
* HTTP Method : DELETE
* PATH : /adminrole/:role_id
*
* @param {Object} options
* @param {Sequelize.model} options.admin_roles
* @param {Sequelize.model} options.admin_users
* @returns {function(*, *, *)}
*/
const registerRemove = options => {
const AdminRoles = options.admin_roles;
const AdminUsers = options.admin_users;
return asyncWrapper(async (req, res) => {
const roleId = req.swagger.params.role_id.value;
let list;
if (isMongoDB(AdminUsers)) { // MongoDB
list = await AdminUsers.find({role_id: roleId});
} else { //MySQL
list = await AdminUsers.findAll({where: {role_id: roleId}});
}
// 削除対象の権限を持っているユーザがいたら、エラーを返す。
if (list.length !== 0) {
throw errors.frontend.CurrentlyUsedAdminRole();
}
if (isMongoDB(AdminRoles)) { // MongoDB
await AdminRoles.deleteMany({role_id: roleId});
} else { //MySQL
await AdminRoles.destroy({where: {role_id: roleId}, force: true});
}
return res.status(204).end();
});
};
/**
* Controller : Update Admin Role
* HTTP Method : PUT
* PATH : /adminrole/:role_id
*
* @param {Object} options
* @param {Sequelize.model} options.admin_roles
* @param {Sequelize} options.store
* @returns {function(*, *, *)}
*/
const registerUpdate = options => {
const AdminRoles = options.admin_roles;
const store = options.store;
return asyncWrapper(async (req, res) => {
const roleId = req.swagger.params.role_id.value;
const paths = req.body.paths;
const list = genAdminRole(roleId, paths);
if (isMongoDB(AdminRoles)) { // MongoDB
// TODO: transaction not support!
await AdminRoles.deleteMany({role_id: roleId});
await AdminRoles.insertMany(list);
return res.json({role_id: roleId, paths: paths});
} else { // MySQL
const t = await store.transaction();
try {
await AdminRoles.destroy({where: {role_id: roleId}, force: true, transaction: t});
await AdminRoles.bulkCreate(list, {transaction: t});
await t.commit();
} catch (err) {
logger.error(err);
await t.rollback();
throw err;
}
return res.json({role_id: roleId, paths: paths});
}
});
};
module.exports = (options, pager, adminUserOption) => {
return {
list: registerList(options, pager),
create: registerCreate(options),
get: registerGet(options),
remove: registerRemove(options, adminUserOption),
update: registerUpdate(options),
};
};