1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.isUserInRoles = exports.isUserInRole = exports.isAdmin = exports.Users = undefined;
|
7 |
|
8 | var _typeof2 = require("babel-runtime/helpers/typeof");
|
9 |
|
10 | var _typeof3 = _interopRequireDefault(_typeof2);
|
11 |
|
12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
13 |
|
14 | var Users = exports.Users = Meteor.users;
|
15 |
|
16 | Users.isInRole = function (userId, role) {
|
17 | var user = Users.findOne({ _id: userId });
|
18 | return !!(user && user.roles && user.roles.indexOf(role) != -1);
|
19 | };
|
20 |
|
21 | Users.isInRoles = function (userId, roleList) {
|
22 | var user = Users.findOne({ _id: userId });
|
23 | if (!user || !user.roles) {
|
24 | return false;
|
25 | }
|
26 |
|
27 |
|
28 | var granted = roleList.filter(function (value) {
|
29 | return user.roles.indexOf(value) > -1;
|
30 | });
|
31 |
|
32 | if (!granted || granted.length == 0) {
|
33 | return false;
|
34 | }
|
35 | return true;
|
36 | };
|
37 |
|
38 | Users.isAdmin = function (userId) {
|
39 | return Users.isInRole(userId, "admin");
|
40 | };
|
41 |
|
42 | Users.isAdminOrInRole = function (userId, role) {
|
43 | return Users.isInRole(userId, "admin") || Users.isInRole(userId, role);
|
44 | };
|
45 |
|
46 | var isAdmin = exports.isAdmin = function isAdmin() {
|
47 | return Users.isAdmin(Meteor.userId());
|
48 | };
|
49 |
|
50 | var isUserInRole = exports.isUserInRole = function isUserInRole(role, options) {
|
51 | return Users.isInRole(Meteor.userId(), role);
|
52 | };
|
53 |
|
54 | var isUserInRoles = exports.isUserInRoles = function isUserInRoles(roleList, options) {
|
55 | return Users.isInRoles(Meteor.userId(), roleList);
|
56 | };
|
57 |
|
58 | if (Meteor.isServer) {
|
59 | (function () {
|
60 | var _extendFilter = function _extendFilter(originalFilter, extraOptions) {
|
61 | originalFilter = originalFilter || {};
|
62 | extraOptions = extraOptions || {};
|
63 |
|
64 | var searchText = extraOptions.searchText || "";
|
65 | var searchFields = extraOptions.searchFields || [];
|
66 |
|
67 | var addFilter = {};
|
68 |
|
69 |
|
70 | if (searchText && searchFields && searchFields.length) {
|
71 | (function () {
|
72 | var searchList = [];
|
73 | var searchRegExp = new RegExp(searchText, "i");
|
74 | searchFields.map(function (fieldName) {
|
75 | var searchItem = {};
|
76 | searchItem[fieldName] = searchRegExp;
|
77 | searchList.push(searchItem);
|
78 | });
|
79 | addFilter["$or"] = searchList;
|
80 | })();
|
81 | }
|
82 |
|
83 | var filter = originalFilter;
|
84 | if (!_.isEmpty(addFilter) && !_.isEmpty(originalFilter)) {
|
85 | filter = { "$and": [originalFilter, addFilter] };
|
86 | } else {
|
87 | if (!_.isEmpty(addFilter)) {
|
88 | filter = addFilter;
|
89 | } else {
|
90 | filter = originalFilter;
|
91 | }
|
92 | }
|
93 |
|
94 | return filter;
|
95 | };
|
96 |
|
97 | var _extendOptions = function _extendOptions(originalOptions, extraOptions) {
|
98 | originalOptions = originalOptions || {};
|
99 | extraOptions = extraOptions || {};
|
100 |
|
101 | var sortBy = extraOptions.sortBy || "";
|
102 | var pageNo = typeof extraOptions.pageNo == "undefined" ? -1 : extraOptions.pageNo;
|
103 | var pageSize = extraOptions.pageSize || 0;
|
104 | var doSkip = extraOptions.doSkip || false;
|
105 |
|
106 | var addOptions = {};
|
107 |
|
108 |
|
109 | if (sortBy) {
|
110 | addOptions.sort = {};
|
111 | addOptions.sort[sortBy] = typeof extraOptions.sortAscending == "undefined" || exraOptions.sortAscending ? 1 : -1;
|
112 | }
|
113 |
|
114 |
|
115 | if (!extraOptions.noPaging && pageNo >= 0 && pageSize > 0) {
|
116 | if (doSkip) {
|
117 | addOptions.skip = pageNo * pageSize;
|
118 | }
|
119 | addOptions.limit = pageSize;
|
120 | }
|
121 |
|
122 | var options = originalOptions;
|
123 |
|
124 | if (!_.isEmpty(addOptions)) {
|
125 | _mergeObjects(options, addOptions);
|
126 | }
|
127 |
|
128 | return options;
|
129 | };
|
130 |
|
131 | var _mergeObjects = function _mergeObjects(target, source) {
|
132 |
|
133 | |
134 |
|
135 |
|
136 | if ((typeof target === "undefined" ? "undefined" : (0, _typeof3.default)(target)) !== "object") {
|
137 | target = {};
|
138 | }
|
139 |
|
140 | for (var property in source) {
|
141 |
|
142 | if (source.hasOwnProperty(property)) {
|
143 |
|
144 | var sourceProperty = source[property];
|
145 |
|
146 | if ((typeof sourceProperty === "undefined" ? "undefined" : (0, _typeof3.default)(sourceProperty)) === 'object') {
|
147 | target[property] = _mergeObjects(target[property], sourceProperty);
|
148 | continue;
|
149 | }
|
150 |
|
151 | target[property] = sourceProperty;
|
152 | }
|
153 | }
|
154 |
|
155 | for (var a = 2, l = arguments.length; a < l; a++) {
|
156 | _mergeObjects(target, arguments[a]);
|
157 | }
|
158 |
|
159 | return target;
|
160 | };
|
161 |
|
162 | Users.allow({
|
163 |
|
164 | update: function update(userId, doc, fieldNames, modifier) {
|
165 |
|
166 | return Users.isAdmin(userId) || doc._id === userId && fieldNames.indexOf("roles") < 0;
|
167 | }
|
168 | });
|
169 |
|
170 |
|
171 | Users.before.insert(function (userId, doc) {
|
172 | if (!doc.createdAt) doc.createdAt = new Date();
|
173 | if (!doc.modifiedAt) doc.modifiedAt = doc.createdAt;
|
174 | if (!doc.roles) doc.roles = [];
|
175 | });
|
176 |
|
177 | Users.before.update(function (userId, doc, fieldNames, modifier, options) {
|
178 | modifier.$set = modifier.$set || {};
|
179 | modifier.$set.modifiedAt = Date.now();
|
180 | });
|
181 |
|
182 | Meteor.publish("admin_user", function (_id) {
|
183 | if (!Users.isAdmin(this.userId)) {
|
184 | return this.ready();
|
185 | }
|
186 | var user = Meteor.users.find({ _id: _id });
|
187 | if (Users.publishJoinedCursors) {
|
188 | return Users.publishJoinedCursors(user);
|
189 | }
|
190 | return user;
|
191 | });
|
192 |
|
193 | Meteor.publish("admin_users", function () {
|
194 | if (!Users.isAdmin(this.userId)) {
|
195 | return this.ready();
|
196 | }
|
197 | var users = Meteor.users.find({});
|
198 | if (Users.publishJoinedCursors) {
|
199 | return Users.publishJoinedCursors(users);
|
200 | }
|
201 | return users;
|
202 | });
|
203 |
|
204 | Meteor.publish("admin_users_paged", function (extraOptions) {
|
205 | extraOptions.doSkip = true;
|
206 | if (!Users.isAdmin(this.userId)) {
|
207 | return this.ready();
|
208 | }
|
209 | var users = Meteor.users.find(_extendFilter({}, extraOptions), _extendOptions({}, extraOptions));
|
210 | if (Users.publishJoinedCursors) {
|
211 | return Users.publishJoinedCursors(users);
|
212 | }
|
213 | return users;
|
214 | });
|
215 |
|
216 | Meteor.publish("admin_users_paged_count", function (extraOptions) {
|
217 | if (typeof Counts == "undefined" || !Users.isAdmin(this.userId)) {
|
218 | return this.ready();
|
219 | }
|
220 | Counts.publish(this, "admin_users_paged_count", Meteor.users.find(_extendFilter({}, extraOptions), { fields: { _id: 1 } }));
|
221 | });
|
222 |
|
223 | Meteor.publish("current_user_data", function () {
|
224 | var user = Meteor.users.find({ _id: this.userId }, { fields: { username: 1, profile: 1, private: 1, public: 1, roles: 1, emails: 1 } });
|
225 | if (Users.publishJoinedCursors) {
|
226 | return Users.publishJoinedCursors(user);
|
227 | }
|
228 | return user;
|
229 | });
|
230 | })();
|
231 | } |
\ | No newline at end of file |