users.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. // Sandstorm context is detected using the METEOR_SETTINGS environment variable
  2. // in the package definition.
  3. const isSandstorm = Meteor.settings && Meteor.settings.public &&
  4. Meteor.settings.public.sandstorm;
  5. Users = Meteor.users;
  6. Users.attachSchema(new SimpleSchema({
  7. username: {
  8. type: String,
  9. optional: true,
  10. autoValue() { // eslint-disable-line consistent-return
  11. if (this.isInsert && !this.isSet) {
  12. const name = this.field('profile.fullname');
  13. if (name.isSet) {
  14. return name.value.toLowerCase().replace(/\s/g, '');
  15. }
  16. }
  17. },
  18. },
  19. emails: {
  20. type: [Object],
  21. optional: true,
  22. },
  23. 'emails.$.address': {
  24. type: String,
  25. regEx: SimpleSchema.RegEx.Email,
  26. },
  27. 'emails.$.verified': {
  28. type: Boolean,
  29. },
  30. createdAt: {
  31. type: Date,
  32. autoValue() { // eslint-disable-line consistent-return
  33. if (this.isInsert) {
  34. return new Date();
  35. } else {
  36. this.unset();
  37. }
  38. },
  39. },
  40. profile: {
  41. type: Object,
  42. optional: true,
  43. autoValue() { // eslint-disable-line consistent-return
  44. if (this.isInsert && !this.isSet) {
  45. return {
  46. boardView: 'board-view-lists',
  47. };
  48. }
  49. },
  50. },
  51. 'profile.avatarUrl': {
  52. type: String,
  53. optional: true,
  54. },
  55. 'profile.emailBuffer': {
  56. type: [String],
  57. optional: true,
  58. },
  59. 'profile.fullname': {
  60. type: String,
  61. optional: true,
  62. },
  63. 'profile.hiddenSystemMessages': {
  64. type: Boolean,
  65. optional: true,
  66. },
  67. 'profile.initials': {
  68. type: String,
  69. optional: true,
  70. },
  71. 'profile.invitedBoards': {
  72. type: [String],
  73. optional: true,
  74. },
  75. 'profile.language': {
  76. type: String,
  77. optional: true,
  78. },
  79. 'profile.notifications': {
  80. type: [String],
  81. optional: true,
  82. },
  83. 'profile.showCardsCountAt': {
  84. type: Number,
  85. optional: true,
  86. },
  87. 'profile.starredBoards': {
  88. type: [String],
  89. optional: true,
  90. },
  91. 'profile.icode': {
  92. type: String,
  93. optional: true,
  94. },
  95. 'profile.boardView': {
  96. type: String,
  97. optional: true,
  98. allowedValues: [
  99. 'board-view-lists',
  100. 'board-view-swimlanes',
  101. 'board-view-cal',
  102. ],
  103. },
  104. services: {
  105. type: Object,
  106. optional: true,
  107. blackbox: true,
  108. },
  109. heartbeat: {
  110. type: Date,
  111. optional: true,
  112. },
  113. isAdmin: {
  114. type: Boolean,
  115. optional: true,
  116. },
  117. createdThroughApi: {
  118. type: Boolean,
  119. optional: true,
  120. },
  121. loginDisabled: {
  122. type: Boolean,
  123. optional: true,
  124. },
  125. }));
  126. Users.allow({
  127. update(userId) {
  128. const user = Users.findOne(userId);
  129. return user && Meteor.user().isAdmin;
  130. },
  131. });
  132. // Search a user in the complete server database by its name or username. This
  133. // is used for instance to add a new user to a board.
  134. const searchInFields = ['username', 'profile.fullname'];
  135. Users.initEasySearch(searchInFields, {
  136. use: 'mongo-db',
  137. returnFields: [...searchInFields, 'profile.avatarUrl'],
  138. });
  139. if (Meteor.isClient) {
  140. Users.helpers({
  141. isBoardMember() {
  142. const board = Boards.findOne(Session.get('currentBoard'));
  143. return board && board.hasMember(this._id);
  144. },
  145. isNotNoComments() {
  146. const board = Boards.findOne(Session.get('currentBoard'));
  147. return board && board.hasMember(this._id) && !board.hasNoComments(this._id);
  148. },
  149. isNoComments() {
  150. const board = Boards.findOne(Session.get('currentBoard'));
  151. return board && board.hasNoComments(this._id);
  152. },
  153. isNotCommentOnly() {
  154. const board = Boards.findOne(Session.get('currentBoard'));
  155. return board && board.hasMember(this._id) && !board.hasCommentOnly(this._id);
  156. },
  157. isCommentOnly() {
  158. const board = Boards.findOne(Session.get('currentBoard'));
  159. return board && board.hasCommentOnly(this._id);
  160. },
  161. isBoardAdmin() {
  162. const board = Boards.findOne(Session.get('currentBoard'));
  163. return board && board.hasAdmin(this._id);
  164. },
  165. });
  166. }
  167. Users.helpers({
  168. boards() {
  169. return Boards.find({ 'members.userId': this._id });
  170. },
  171. starredBoards() {
  172. const {starredBoards = []} = this.profile;
  173. return Boards.find({archived: false, _id: {$in: starredBoards}});
  174. },
  175. hasStarred(boardId) {
  176. const {starredBoards = []} = this.profile;
  177. return _.contains(starredBoards, boardId);
  178. },
  179. invitedBoards() {
  180. const {invitedBoards = []} = this.profile;
  181. return Boards.find({archived: false, _id: {$in: invitedBoards}});
  182. },
  183. isInvitedTo(boardId) {
  184. const {invitedBoards = []} = this.profile;
  185. return _.contains(invitedBoards, boardId);
  186. },
  187. hasTag(tag) {
  188. const {tags = []} = this.profile;
  189. return _.contains(tags, tag);
  190. },
  191. hasNotification(activityId) {
  192. const {notifications = []} = this.profile;
  193. return _.contains(notifications, activityId);
  194. },
  195. hasHiddenSystemMessages() {
  196. const profile = this.profile || {};
  197. return profile.hiddenSystemMessages || false;
  198. },
  199. getEmailBuffer() {
  200. const {emailBuffer = []} = this.profile;
  201. return emailBuffer;
  202. },
  203. getInitials() {
  204. const profile = this.profile || {};
  205. if (profile.initials)
  206. return profile.initials;
  207. else if (profile.fullname) {
  208. return profile.fullname.split(/\s+/).reduce((memo, word) => {
  209. return memo + word[0];
  210. }, '').toUpperCase();
  211. } else {
  212. return this.username[0].toUpperCase();
  213. }
  214. },
  215. getLimitToShowCardsCount() {
  216. const profile = this.profile || {};
  217. return profile.showCardsCountAt;
  218. },
  219. getName() {
  220. const profile = this.profile || {};
  221. return profile.fullname || this.username;
  222. },
  223. getLanguage() {
  224. const profile = this.profile || {};
  225. return profile.language || 'en';
  226. },
  227. });
  228. Users.mutations({
  229. toggleBoardStar(boardId) {
  230. const queryKind = this.hasStarred(boardId) ? '$pull' : '$addToSet';
  231. return {
  232. [queryKind]: {
  233. 'profile.starredBoards': boardId,
  234. },
  235. };
  236. },
  237. addInvite(boardId) {
  238. return {
  239. $addToSet: {
  240. 'profile.invitedBoards': boardId,
  241. },
  242. };
  243. },
  244. removeInvite(boardId) {
  245. return {
  246. $pull: {
  247. 'profile.invitedBoards': boardId,
  248. },
  249. };
  250. },
  251. addTag(tag) {
  252. return {
  253. $addToSet: {
  254. 'profile.tags': tag,
  255. },
  256. };
  257. },
  258. removeTag(tag) {
  259. return {
  260. $pull: {
  261. 'profile.tags': tag,
  262. },
  263. };
  264. },
  265. toggleTag(tag) {
  266. if (this.hasTag(tag))
  267. this.removeTag(tag);
  268. else
  269. this.addTag(tag);
  270. },
  271. toggleSystem(value = false) {
  272. return {
  273. $set: {
  274. 'profile.hiddenSystemMessages': !value,
  275. },
  276. };
  277. },
  278. addNotification(activityId) {
  279. return {
  280. $addToSet: {
  281. 'profile.notifications': activityId,
  282. },
  283. };
  284. },
  285. removeNotification(activityId) {
  286. return {
  287. $pull: {
  288. 'profile.notifications': activityId,
  289. },
  290. };
  291. },
  292. addEmailBuffer(text) {
  293. return {
  294. $addToSet: {
  295. 'profile.emailBuffer': text,
  296. },
  297. };
  298. },
  299. clearEmailBuffer() {
  300. return {
  301. $set: {
  302. 'profile.emailBuffer': [],
  303. },
  304. };
  305. },
  306. setAvatarUrl(avatarUrl) {
  307. return {$set: {'profile.avatarUrl': avatarUrl}};
  308. },
  309. setShowCardsCountAt(limit) {
  310. return {$set: {'profile.showCardsCountAt': limit}};
  311. },
  312. setBoardView(view) {
  313. return {
  314. $set : {
  315. 'profile.boardView': view,
  316. },
  317. };
  318. },
  319. });
  320. Meteor.methods({
  321. setUsername(username, userId) {
  322. check(username, String);
  323. const nUsersWithUsername = Users.find({username}).count();
  324. if (nUsersWithUsername > 0) {
  325. throw new Meteor.Error('username-already-taken');
  326. } else {
  327. Users.update(userId, {$set: {username}});
  328. }
  329. },
  330. toggleSystemMessages() {
  331. const user = Meteor.user();
  332. user.toggleSystem(user.hasHiddenSystemMessages());
  333. },
  334. changeLimitToShowCardsCount(limit) {
  335. check(limit, Number);
  336. Meteor.user().setShowCardsCountAt(limit);
  337. },
  338. setEmail(email, userId) {
  339. check(email, String);
  340. const existingUser = Users.findOne({'emails.address': email}, {fields: {_id: 1}});
  341. if (existingUser) {
  342. throw new Meteor.Error('email-already-taken');
  343. } else {
  344. Users.update(userId, {
  345. $set: {
  346. emails: [{
  347. address: email,
  348. verified: false,
  349. }],
  350. },
  351. });
  352. }
  353. },
  354. setUsernameAndEmail(username, email, userId) {
  355. check(username, String);
  356. check(email, String);
  357. check(userId, String);
  358. Meteor.call('setUsername', username, userId);
  359. Meteor.call('setEmail', email, userId);
  360. },
  361. setPassword(newPassword, userId) {
  362. check(userId, String);
  363. check(newPassword, String);
  364. if(Meteor.user().isAdmin){
  365. Accounts.setPassword(userId, newPassword);
  366. }
  367. },
  368. });
  369. if (Meteor.isServer) {
  370. Meteor.methods({
  371. // we accept userId, username, email
  372. inviteUserToBoard(username, boardId) {
  373. check(username, String);
  374. check(boardId, String);
  375. const inviter = Meteor.user();
  376. const board = Boards.findOne(boardId);
  377. const allowInvite = inviter &&
  378. board &&
  379. board.members &&
  380. _.contains(_.pluck(board.members, 'userId'), inviter._id) &&
  381. _.where(board.members, {userId: inviter._id})[0].isActive &&
  382. _.where(board.members, {userId: inviter._id})[0].isAdmin;
  383. if (!allowInvite) throw new Meteor.Error('error-board-notAMember');
  384. this.unblock();
  385. const posAt = username.indexOf('@');
  386. let user = null;
  387. if (posAt >= 0) {
  388. user = Users.findOne({emails: {$elemMatch: {address: username}}});
  389. } else {
  390. user = Users.findOne(username) || Users.findOne({username});
  391. }
  392. if (user) {
  393. if (user._id === inviter._id) throw new Meteor.Error('error-user-notAllowSelf');
  394. } else {
  395. if (posAt <= 0) throw new Meteor.Error('error-user-doesNotExist');
  396. if (Settings.findOne().disableRegistration) throw new Meteor.Error('error-user-notCreated');
  397. // Set in lowercase email before creating account
  398. const email = username.toLowerCase();
  399. username = email.substring(0, posAt);
  400. const newUserId = Accounts.createUser({username, email});
  401. if (!newUserId) throw new Meteor.Error('error-user-notCreated');
  402. // assume new user speak same language with inviter
  403. if (inviter.profile && inviter.profile.language) {
  404. Users.update(newUserId, {
  405. $set: {
  406. 'profile.language': inviter.profile.language,
  407. },
  408. });
  409. }
  410. Accounts.sendEnrollmentEmail(newUserId);
  411. user = Users.findOne(newUserId);
  412. }
  413. board.addMember(user._id);
  414. user.addInvite(boardId);
  415. try {
  416. const params = {
  417. user: user.username,
  418. inviter: inviter.username,
  419. board: board.title,
  420. url: board.absoluteUrl(),
  421. };
  422. const lang = user.getLanguage();
  423. Email.send({
  424. to: user.emails[0].address.toLowerCase(),
  425. from: Accounts.emailTemplates.from,
  426. subject: TAPi18n.__('email-invite-subject', params, lang),
  427. text: TAPi18n.__('email-invite-text', params, lang),
  428. });
  429. } catch (e) {
  430. throw new Meteor.Error('email-fail', e.message);
  431. }
  432. return {username: user.username, email: user.emails[0].address};
  433. },
  434. });
  435. Accounts.onCreateUser((options, user) => {
  436. const userCount = Users.find().count();
  437. if (!isSandstorm && userCount === 0) {
  438. user.isAdmin = true;
  439. return user;
  440. }
  441. // if (user.services.oidc) {
  442. // const email = user.services.oidc.email.toLowerCase();
  443. //
  444. // user.username = user.services.oidc.username;
  445. // user.emails = [{ address: email, verified: true }];
  446. // const initials = user.services.oidc.fullname.match(/\b[a-zA-Z]/g).join('').toUpperCase();
  447. // user.profile = { initials, fullname: user.services.oidc.fullname };
  448. //
  449. // // see if any existing user has this email address or username, otherwise create new
  450. // const existingUser = Meteor.users.findOne({$or: [{'emails.address': email}, {'username':user.username}]});
  451. // if (!existingUser)
  452. // return user;
  453. //
  454. // // copy across new service info
  455. // const service = _.keys(user.services)[0];
  456. // existingUser.services[service] = user.services[service];
  457. // existingUser.emails = user.emails;
  458. // existingUser.username = user.username;
  459. // existingUser.profile = user.profile;
  460. //
  461. // Meteor.users.remove({_id: existingUser._id}); // remove existing record
  462. // return existingUser;
  463. // }
  464. if (options.from === 'admin') {
  465. user.createdThroughApi = true;
  466. return user;
  467. }
  468. const disableRegistration = Settings.findOne().disableRegistration;
  469. if (!disableRegistration) {
  470. return user;
  471. }
  472. if (!options || !options.profile) {
  473. throw new Meteor.Error('error-invitation-code-blank', 'The invitation code is required');
  474. }
  475. const invitationCode = InvitationCodes.findOne({
  476. code: options.profile.invitationcode,
  477. email: options.email,
  478. valid: true,
  479. });
  480. if (!invitationCode) {
  481. throw new Meteor.Error('error-invitation-code-not-exist', 'The invitation code doesn\'t exist');
  482. } else {
  483. user.profile = {icode: options.profile.invitationcode};
  484. user.profile.boardView = 'board-view-lists';
  485. // Deletes the invitation code after the user was created successfully.
  486. setTimeout(Meteor.bindEnvironment(() => {
  487. InvitationCodes.remove({'_id': invitationCode._id});
  488. }), 200);
  489. return user;
  490. }
  491. });
  492. }
  493. if (Meteor.isServer) {
  494. // Let mongoDB ensure username unicity
  495. Meteor.startup(() => {
  496. Users._collection._ensureIndex({
  497. username: 1,
  498. }, {unique: true});
  499. });
  500. // Each board document contains the de-normalized number of users that have
  501. // starred it. If the user star or unstar a board, we need to update this
  502. // counter.
  503. // We need to run this code on the server only, otherwise the incrementation
  504. // will be done twice.
  505. Users.after.update(function (userId, user, fieldNames) {
  506. // The `starredBoards` list is hosted on the `profile` field. If this
  507. // field hasn't been modificated we don't need to run this hook.
  508. if (!_.contains(fieldNames, 'profile'))
  509. return;
  510. // To calculate a diff of board starred ids, we get both the previous
  511. // and the newly board ids list
  512. function getStarredBoardsIds(doc) {
  513. return doc.profile && doc.profile.starredBoards;
  514. }
  515. const oldIds = getStarredBoardsIds(this.previous);
  516. const newIds = getStarredBoardsIds(user);
  517. // The _.difference(a, b) method returns the values from a that are not in
  518. // b. We use it to find deleted and newly inserted ids by using it in one
  519. // direction and then in the other.
  520. function incrementBoards(boardsIds, inc) {
  521. boardsIds.forEach((boardId) => {
  522. Boards.update(boardId, {$inc: {stars: inc}});
  523. });
  524. }
  525. incrementBoards(_.difference(oldIds, newIds), -1);
  526. incrementBoards(_.difference(newIds, oldIds), +1);
  527. });
  528. const fakeUserId = new Meteor.EnvironmentVariable();
  529. const getUserId = CollectionHooks.getUserId;
  530. CollectionHooks.getUserId = () => {
  531. return fakeUserId.get() || getUserId();
  532. };
  533. if (!isSandstorm) {
  534. Users.after.insert((userId, doc) => {
  535. const fakeUser = {
  536. extendAutoValueContext: {
  537. userId: doc._id,
  538. },
  539. };
  540. fakeUserId.withValue(doc._id, () => {
  541. // Insert the Welcome Board
  542. Boards.insert({
  543. title: TAPi18n.__('welcome-board'),
  544. permission: 'private',
  545. }, fakeUser, (err, boardId) => {
  546. Swimlanes.insert({
  547. title: TAPi18n.__('welcome-swimlane'),
  548. boardId,
  549. sort: 1,
  550. }, fakeUser);
  551. ['welcome-list1', 'welcome-list2'].forEach((title, titleIndex) => {
  552. Lists.insert({title: TAPi18n.__(title), boardId, sort: titleIndex}, fakeUser);
  553. });
  554. });
  555. });
  556. });
  557. }
  558. Users.after.insert((userId, doc) => {
  559. if (doc.createdThroughApi) {
  560. // The admin user should be able to create a user despite disabling registration because
  561. // it is two different things (registration and creation).
  562. // So, when a new user is created via the api (only admin user can do that) one must avoid
  563. // the disableRegistration check.
  564. // Issue : https://github.com/wekan/wekan/issues/1232
  565. // PR : https://github.com/wekan/wekan/pull/1251
  566. Users.update(doc._id, {$set: {createdThroughApi: ''}});
  567. return;
  568. }
  569. //invite user to corresponding boards
  570. const disableRegistration = Settings.findOne().disableRegistration;
  571. if (disableRegistration) {
  572. const invitationCode = InvitationCodes.findOne({code: doc.profile.icode, valid: true});
  573. if (!invitationCode) {
  574. throw new Meteor.Error('error-invitation-code-not-exist');
  575. } else {
  576. invitationCode.boardsToBeInvited.forEach((boardId) => {
  577. const board = Boards.findOne(boardId);
  578. board.addMember(doc._id);
  579. });
  580. if (!doc.profile) {
  581. doc.profile = {};
  582. }
  583. doc.profile.invitedBoards = invitationCode.boardsToBeInvited;
  584. Users.update(doc._id, {$set: {profile: doc.profile}});
  585. InvitationCodes.update(invitationCode._id, {$set: {valid: false}});
  586. }
  587. }
  588. });
  589. }
  590. // USERS REST API
  591. if (Meteor.isServer) {
  592. // Middleware which checks that API is enabled.
  593. JsonRoutes.Middleware.use(function (req, res, next) {
  594. const api = req.url.search('api');
  595. if (api === 1 && process.env.WITH_API === 'true' || api === -1){
  596. return next();
  597. }
  598. else {
  599. res.writeHead(301, {Location: '/'});
  600. return res.end();
  601. }
  602. });
  603. JsonRoutes.add('GET', '/api/user', function(req, res) {
  604. try {
  605. Authentication.checkLoggedIn(req.userId);
  606. const data = Meteor.users.findOne({ _id: req.userId});
  607. delete data.services;
  608. JsonRoutes.sendResult(res, {
  609. code: 200,
  610. data,
  611. });
  612. }
  613. catch (error) {
  614. JsonRoutes.sendResult(res, {
  615. code: 200,
  616. data: error,
  617. });
  618. }
  619. });
  620. JsonRoutes.add('GET', '/api/users', function (req, res) {
  621. try {
  622. Authentication.checkUserId(req.userId);
  623. JsonRoutes.sendResult(res, {
  624. code: 200,
  625. data: Meteor.users.find({}).map(function (doc) {
  626. return { _id: doc._id, username: doc.username };
  627. }),
  628. });
  629. }
  630. catch (error) {
  631. JsonRoutes.sendResult(res, {
  632. code: 200,
  633. data: error,
  634. });
  635. }
  636. });
  637. JsonRoutes.add('GET', '/api/users/:id', function (req, res) {
  638. try {
  639. Authentication.checkUserId(req.userId);
  640. const id = req.params.id;
  641. JsonRoutes.sendResult(res, {
  642. code: 200,
  643. data: Meteor.users.findOne({ _id: id }),
  644. });
  645. }
  646. catch (error) {
  647. JsonRoutes.sendResult(res, {
  648. code: 200,
  649. data: error,
  650. });
  651. }
  652. });
  653. JsonRoutes.add('PUT', '/api/users/:id', function (req, res) {
  654. try {
  655. Authentication.checkUserId(req.userId);
  656. const id = req.params.id;
  657. const action = req.body.action;
  658. let data = Meteor.users.findOne({ _id: id });
  659. if (data !== undefined) {
  660. if (action === 'takeOwnership') {
  661. data = Boards.find({
  662. 'members.userId': id,
  663. 'members.isAdmin': true,
  664. }).map(function(board) {
  665. if (board.hasMember(req.userId)) {
  666. board.removeMember(req.userId);
  667. }
  668. board.changeOwnership(id, req.userId);
  669. return {
  670. _id: board._id,
  671. title: board.title,
  672. };
  673. });
  674. } else {
  675. if ((action === 'disableLogin') && (id !== req.userId)) {
  676. Users.update({ _id: id }, { $set: { loginDisabled: true, 'services.resume.loginTokens': '' } });
  677. } else if (action === 'enableLogin') {
  678. Users.update({ _id: id }, { $set: { loginDisabled: '' } });
  679. }
  680. data = Meteor.users.findOne({ _id: id });
  681. }
  682. }
  683. JsonRoutes.sendResult(res, {
  684. code: 200,
  685. data,
  686. });
  687. }
  688. catch (error) {
  689. JsonRoutes.sendResult(res, {
  690. code: 200,
  691. data: error,
  692. });
  693. }
  694. });
  695. JsonRoutes.add('POST', '/api/users/', function (req, res) {
  696. try {
  697. Authentication.checkUserId(req.userId);
  698. const id = Accounts.createUser({
  699. username: req.body.username,
  700. email: req.body.email,
  701. password: req.body.password,
  702. from: 'admin',
  703. });
  704. JsonRoutes.sendResult(res, {
  705. code: 200,
  706. data: {
  707. _id: id,
  708. },
  709. });
  710. }
  711. catch (error) {
  712. JsonRoutes.sendResult(res, {
  713. code: 200,
  714. data: error,
  715. });
  716. }
  717. });
  718. JsonRoutes.add('DELETE', '/api/users/:id', function (req, res) {
  719. try {
  720. Authentication.checkUserId(req.userId);
  721. const id = req.params.id;
  722. Meteor.users.remove({ _id: id });
  723. JsonRoutes.sendResult(res, {
  724. code: 200,
  725. data: {
  726. _id: id,
  727. },
  728. });
  729. }
  730. catch (error) {
  731. JsonRoutes.sendResult(res, {
  732. code: 200,
  733. data: error,
  734. });
  735. }
  736. });
  737. }