/
member.js
152 lines (142 loc) · 4.67 KB
/
member.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
import { Reaction } from "/client/api";
import { Packages, Shops } from "/lib/collections";
import { Meteor } from "meteor/meteor";
import { Template } from "meteor/templating";
const getPermissionMap = (permissions) => {
const permissionMap = {};
_.each(permissions, function (existing) {
permissionMap[existing.permission] = existing.label;
});
return permissionMap;
};
/**
* shopMember helpers
* permissions / roles controls
* we use userInRole instead of Reaction intentionally
* to check each users permissions
*/
Template.member.events({
"click [data-event-action=showMemberSettings]": function () {
Reaction.setActionViewDetail({
label: "Permissions",
i18nKeyLabel: "admin.settings.permissionsSettingsLabel",
data: this,
template: "memberSettings"
});
}
});
Template.memberSettings.helpers({
isOwnerDisabled: function () {
if (Meteor.userId() === this.userId) {
if (Roles.userIsInRole(this.userId, "owner", this.shopId)) {
return true;
}
}
},
hasPermissionChecked: function (permission, userId) {
if (userId && Roles.userIsInRole(userId, permission, this.shopId || Roles.userIsInRole(userId, permission,
Roles.GLOBAL_GROUP))) {
return "checked";
}
},
groupsForUser: function (groupUserId) {
const userId = groupUserId || this.userId || Template.parentData(1).userId;
return Roles.getGroupsForUser(userId);
},
shopLabel: function (thisShopId) {
const shopId = thisShopId || Template.currentData();
const shop = Shops.findOne({
_id: shopId
});
if (shop && shop.name) {
return shop.name || "Default Shop";
}
},
permissionGroups: function (thisShopId) {
const permissionGroups = [];
const shopId = thisShopId || Template.currentData();
const packages = Packages.find({
shopId: shopId
});
packages.forEach(function (pkg) {
const permissions = [];
if (pkg.registry && pkg.enabled) {
for (const registryItem of pkg.registry) {
// Skip entires with missing routes
if (!registryItem.route) {
continue;
}
// Get all permissions, add them to an array
if (registryItem.permissions) {
for (const permission of registryItem.permissions) {
permission.shopId = shopId;
permissions.push(permission);
}
}
// Also create an object map of those same permissions as above
const permissionMap = getPermissionMap(permissions);
if (!permissionMap[registryItem.route]) {
permissions.push({
shopId: pkg.shopId,
permission: registryItem.name || pkg.name + "/" + registryItem.template, // launchdock-connect/connectDashboard
icon: registryItem.icon,
label: registryItem.label || registryItem.provides || registryItem.route
});
}
}
// TODO review this, hardcoded WIP
const label = pkg.name.replace("reaction", "").replace(/(-.)/g, function (x) {
return " " + x[1].toUpperCase();
});
return permissionGroups.push({
shopId: pkg.shopId,
icon: pkg.icon,
name: pkg.name,
label: label,
permissions: _.uniq(permissions)
});
}
});
return permissionGroups;
},
hasManyPermissions: function (permissions) {
return Boolean(permissions.length);
}
});
/**
* shopMember events
*
*/
Template.memberSettings.events({
"change [data-event-action=toggleMemberPermission]": function (event, template) {
const self = this;
const permissions = [];
const member = template.data;
if (!this.shopId) {
throw new Meteor.Error("Shop is required");
}
if (self.name) {
permissions.push(self.name);
for (const pkgPermissions of self.permissions) {
permissions.push(pkgPermissions.permission);
}
} else {
permissions.push(self.permission);
}
if (Template.instance().$(event.currentTarget).is(":checked")) {
Meteor.call("accounts/addUserPermissions", member.userId, permissions, this.shopId);
} else {
Meteor.call("accounts/removeUserPermissions", member.userId, permissions, this.shopId);
}
},
"click [data-event-action=resetMemberPermission]": function (event, template) {
const $icon = Template.instance().$(event.currentTarget);
if (confirm($icon.data("confirm"))) { // eslint-disable-line no-alert
const results = [];
for (const role of template.data.roles) {
results.push(Meteor.call("accounts/setUserPermissions", this.userId, ["guest", "account/profile"], role));
}
return results;
}
}
});