-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherp-user.js
84 lines (77 loc) · 2.85 KB
/
erp-user.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
// Licensed under the Apache License. See footer for details.
var winston = require("winston");
var async = require("async");
var helper = require("./helper.js");
module.exports = function (ErpUser) {
// ERP roles - they get initialized in server/boot/create-roles.js
ErpUser.SUPPLY_CHAIN_MANAGER_ROLE = "supplychainmanager";
ErpUser.RETAIL_STORE_MANAGER_ROLE = "retailstoremanager";
// remove all remote API methods, leaving only login/logout and token management
helper.readOnly(ErpUser);
ErpUser.disableRemoteMethod("find", true);
ErpUser.disableRemoteMethod("findById", true);
ErpUser.disableRemoteMethod("confirm", true);
ErpUser.disableRemoteMethod("resetPassword", true);
ErpUser.disableRemoteMethod("login", true);
// hide the link back to the demo
helper.hideRelation(ErpUser, "demo");
helper.hideRelation(ErpUser, "accessTokens");
helper.readOnlyRelation(ErpUser, "roles");
// callback(err, principal)
ErpUser.assignRole = function (user, roleName, callback) {
ErpUser.app.models.Role.find({
where: {
name: roleName
}
}, function (err, roles) {
roles[0].principals.create({
principalType: ErpUser.app.models.RoleMapping.USER,
principalId: user.id
}, function (err, principal) {
callback(err, principal);
});
});
};
// Delete access tokens for the given user
ErpUser.observe("after delete", function (ctx, next) {
if (ctx.where.id) {
winston.info("Deleting access tokens and roles for user", ctx.where.id);
async.waterfall([
// delete its token
function (callback) {
ErpUser.app.models.AccessToken.destroyAll({
userId: ctx.where.id
}, function (err, info) {
callback();
});
},
// delete its roles
function (callback) {
ErpUser.app.models.RoleMapping.destroyAll({
principalType: ErpUser.app.models.RoleMapping.USER,
principalId: ctx.where.id
}, function (err, info) {
callback();
});
},
], function (err, result) {
next();
});
} else {
next();
}
});
};
//------------------------------------------------------------------------------
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//------------------------------------------------------------------------------