users.js 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394
  1. //var nodemailer = require('nodemailer');
  2. import { SyncedCron } from 'meteor/percolate:synced-cron';
  3. import ImpersonatedUsers from './impersonatedUsers';
  4. // Sandstorm context is detected using the METEOR_SETTINGS environment variable
  5. // in the package definition.
  6. const isSandstorm =
  7. Meteor.settings && Meteor.settings.public && Meteor.settings.public.sandstorm;
  8. Users = Meteor.users;
  9. const allowedSortValues = [
  10. '-modifiedAt',
  11. 'modifiedAt',
  12. '-title',
  13. 'title',
  14. '-sort',
  15. 'sort',
  16. ];
  17. const defaultSortBy = allowedSortValues[0];
  18. /**
  19. * A User in wekan
  20. */
  21. Users.attachSchema(
  22. new SimpleSchema({
  23. username: {
  24. /**
  25. * the username of the user
  26. */
  27. type: String,
  28. optional: true,
  29. // eslint-disable-next-line consistent-return
  30. autoValue() {
  31. if (this.isInsert && !this.isSet) {
  32. const name = this.field('profile.fullname');
  33. if (name.isSet) {
  34. return name.value.toLowerCase().replace(/\s/g, '');
  35. }
  36. }
  37. },
  38. },
  39. orgs: {
  40. /**
  41. * the list of organizations that a user belongs to
  42. */
  43. type: [Object],
  44. optional: true,
  45. },
  46. 'orgs.$.orgId':{
  47. /**
  48. * The uniq ID of the organization
  49. */
  50. type: String,
  51. },
  52. 'orgs.$.orgDisplayName':{
  53. /**
  54. * The display name of the organization
  55. */
  56. type: String,
  57. },
  58. teams: {
  59. /**
  60. * the list of teams that a user belongs to
  61. */
  62. type: [Object],
  63. optional: true,
  64. },
  65. 'teams.$.teamId':{
  66. /**
  67. * The uniq ID of the team
  68. */
  69. type: String,
  70. },
  71. 'teams.$.teamDisplayName':{
  72. /**
  73. * The display name of the team
  74. */
  75. type: String,
  76. },
  77. emails: {
  78. /**
  79. * the list of emails attached to a user
  80. */
  81. type: [Object],
  82. optional: true,
  83. },
  84. 'emails.$.address': {
  85. /**
  86. * The email address
  87. */
  88. type: String,
  89. regEx: SimpleSchema.RegEx.Email,
  90. },
  91. 'emails.$.verified': {
  92. /**
  93. * Has the email been verified
  94. */
  95. type: Boolean,
  96. },
  97. createdAt: {
  98. /**
  99. * creation date of the user
  100. */
  101. type: Date,
  102. // eslint-disable-next-line consistent-return
  103. autoValue() {
  104. if (this.isInsert) {
  105. return new Date();
  106. } else if (this.isUpsert) {
  107. return {
  108. $setOnInsert: new Date(),
  109. };
  110. } else {
  111. this.unset();
  112. }
  113. },
  114. },
  115. modifiedAt: {
  116. type: Date,
  117. denyUpdate: false,
  118. // eslint-disable-next-line consistent-return
  119. autoValue() {
  120. if (this.isInsert || this.isUpsert || this.isUpdate) {
  121. return new Date();
  122. } else {
  123. this.unset();
  124. }
  125. },
  126. },
  127. profile: {
  128. /**
  129. * profile settings
  130. */
  131. type: Object,
  132. optional: true,
  133. // eslint-disable-next-line consistent-return
  134. autoValue() {
  135. if (this.isInsert && !this.isSet) {
  136. return {
  137. boardView: 'board-view-swimlanes',
  138. };
  139. }
  140. },
  141. },
  142. 'profile.avatarUrl': {
  143. /**
  144. * URL of the avatar of the user
  145. */
  146. type: String,
  147. optional: true,
  148. },
  149. 'profile.emailBuffer': {
  150. /**
  151. * list of email buffers of the user
  152. */
  153. type: [String],
  154. optional: true,
  155. },
  156. 'profile.fullname': {
  157. /**
  158. * full name of the user
  159. */
  160. type: String,
  161. optional: true,
  162. },
  163. 'profile.showDesktopDragHandles': {
  164. /**
  165. * does the user want to show desktop drag handles?
  166. */
  167. type: Boolean,
  168. optional: true,
  169. },
  170. 'profile.hideCheckedItems': {
  171. /**
  172. * does the user want to hide checked checklist items?
  173. */
  174. type: Boolean,
  175. optional: true,
  176. },
  177. 'profile.cardMaximized': {
  178. /**
  179. * has user clicked maximize card?
  180. */
  181. type: Boolean,
  182. optional: true,
  183. },
  184. 'profile.customFieldsGrid': {
  185. /**
  186. * has user at card Custom Fields have Grid (false) or one per row (true) layout?
  187. */
  188. type: Boolean,
  189. optional: true,
  190. },
  191. 'profile.hiddenSystemMessages': {
  192. /**
  193. * does the user want to hide system messages?
  194. */
  195. type: Boolean,
  196. optional: true,
  197. },
  198. 'profile.hiddenMinicardLabelText': {
  199. /**
  200. * does the user want to hide minicard label texts?
  201. */
  202. type: Boolean,
  203. optional: true,
  204. },
  205. 'profile.initials': {
  206. /**
  207. * initials of the user
  208. */
  209. type: String,
  210. optional: true,
  211. },
  212. 'profile.invitedBoards': {
  213. /**
  214. * board IDs the user has been invited to
  215. */
  216. type: [String],
  217. optional: true,
  218. },
  219. 'profile.language': {
  220. /**
  221. * language of the user
  222. */
  223. type: String,
  224. optional: true,
  225. },
  226. 'profile.moveAndCopyDialog' : {
  227. /**
  228. * move and copy card dialog
  229. */
  230. type: Object,
  231. optional: true,
  232. blackbox: true,
  233. },
  234. 'profile.moveAndCopyDialog.$.boardId': {
  235. /**
  236. * last selected board id
  237. */
  238. type: String,
  239. },
  240. 'profile.moveAndCopyDialog.$.swimlaneId': {
  241. /**
  242. * last selected swimlane id
  243. */
  244. type: String,
  245. },
  246. 'profile.moveAndCopyDialog.$.listId': {
  247. /**
  248. * last selected list id
  249. */
  250. type: String,
  251. },
  252. 'profile.moveChecklistDialog' : {
  253. /**
  254. * move checklist dialog
  255. */
  256. type: Object,
  257. optional: true,
  258. blackbox: true,
  259. },
  260. 'profile.moveChecklistDialog.$.boardId': {
  261. /**
  262. * last selected board id
  263. */
  264. type: String,
  265. },
  266. 'profile.moveChecklistDialog.$.swimlaneId': {
  267. /**
  268. * last selected swimlane id
  269. */
  270. type: String,
  271. },
  272. 'profile.moveChecklistDialog.$.listId': {
  273. /**
  274. * last selected list id
  275. */
  276. type: String,
  277. },
  278. 'profile.moveChecklistDialog.$.cardId': {
  279. /**
  280. * last selected card id
  281. */
  282. type: String,
  283. },
  284. 'profile.copyChecklistDialog' : {
  285. /**
  286. * copy checklist dialog
  287. */
  288. type: Object,
  289. optional: true,
  290. blackbox: true,
  291. },
  292. 'profile.copyChecklistDialog.$.boardId': {
  293. /**
  294. * last selected board id
  295. */
  296. type: String,
  297. },
  298. 'profile.copyChecklistDialog.$.swimlaneId': {
  299. /**
  300. * last selected swimlane id
  301. */
  302. type: String,
  303. },
  304. 'profile.copyChecklistDialog.$.listId': {
  305. /**
  306. * last selected list id
  307. */
  308. type: String,
  309. },
  310. 'profile.copyChecklistDialog.$.cardId': {
  311. /**
  312. * last selected card id
  313. */
  314. type: String,
  315. },
  316. 'profile.notifications': {
  317. /**
  318. * enabled notifications for the user
  319. */
  320. type: [Object],
  321. optional: true,
  322. },
  323. 'profile.notifications.$.activity': {
  324. /**
  325. * The id of the activity this notification references
  326. */
  327. type: String,
  328. },
  329. 'profile.notifications.$.read': {
  330. /**
  331. * the date on which this notification was read
  332. */
  333. type: Date,
  334. optional: true,
  335. },
  336. 'profile.showCardsCountAt': {
  337. /**
  338. * showCardCountAt field of the user
  339. */
  340. type: Number,
  341. optional: true,
  342. },
  343. 'profile.startDayOfWeek': {
  344. /**
  345. * startDayOfWeek field of the user
  346. */
  347. type: Number,
  348. optional: true,
  349. },
  350. 'profile.starredBoards': {
  351. /**
  352. * list of starred board IDs
  353. */
  354. type: [String],
  355. optional: true,
  356. },
  357. 'profile.icode': {
  358. /**
  359. * icode
  360. */
  361. type: String,
  362. optional: true,
  363. },
  364. 'profile.boardView': {
  365. /**
  366. * boardView field of the user
  367. */
  368. type: String,
  369. optional: true,
  370. allowedValues: [
  371. 'board-view-swimlanes',
  372. 'board-view-lists',
  373. 'board-view-cal',
  374. ],
  375. },
  376. 'profile.listSortBy': {
  377. /**
  378. * default sort list for user
  379. */
  380. type: String,
  381. optional: true,
  382. defaultValue: defaultSortBy,
  383. allowedValues: allowedSortValues,
  384. },
  385. 'profile.templatesBoardId': {
  386. /**
  387. * Reference to the templates board
  388. */
  389. type: String,
  390. defaultValue: '',
  391. },
  392. 'profile.cardTemplatesSwimlaneId': {
  393. /**
  394. * Reference to the card templates swimlane Id
  395. */
  396. type: String,
  397. defaultValue: '',
  398. },
  399. 'profile.listTemplatesSwimlaneId': {
  400. /**
  401. * Reference to the list templates swimlane Id
  402. */
  403. type: String,
  404. defaultValue: '',
  405. },
  406. 'profile.boardTemplatesSwimlaneId': {
  407. /**
  408. * Reference to the board templates swimlane Id
  409. */
  410. type: String,
  411. defaultValue: '',
  412. },
  413. services: {
  414. /**
  415. * services field of the user
  416. */
  417. type: Object,
  418. optional: true,
  419. blackbox: true,
  420. },
  421. heartbeat: {
  422. /**
  423. * last time the user has been seen
  424. */
  425. type: Date,
  426. optional: true,
  427. },
  428. isAdmin: {
  429. /**
  430. * is the user an admin of the board?
  431. */
  432. type: Boolean,
  433. optional: true,
  434. },
  435. createdThroughApi: {
  436. /**
  437. * was the user created through the API?
  438. */
  439. type: Boolean,
  440. optional: true,
  441. },
  442. loginDisabled: {
  443. /**
  444. * loginDisabled field of the user
  445. */
  446. type: Boolean,
  447. optional: true,
  448. },
  449. authenticationMethod: {
  450. /**
  451. * authentication method of the user
  452. */
  453. type: String,
  454. optional: false,
  455. defaultValue: 'password',
  456. },
  457. sessionData: {
  458. /**
  459. * profile settings
  460. */
  461. type: Object,
  462. optional: true,
  463. // eslint-disable-next-line consistent-return
  464. autoValue() {
  465. if (this.isInsert && !this.isSet) {
  466. return {};
  467. }
  468. },
  469. },
  470. 'sessionData.totalHits': {
  471. /**
  472. * Total hits from last searchquery['members.userId'] = Meteor.userId();
  473. * last hit that was returned
  474. */
  475. type: Number,
  476. optional: true,
  477. },
  478. importUsernames: {
  479. /**
  480. * username for imported
  481. */
  482. type: [String],
  483. optional: true,
  484. },
  485. }),
  486. );
  487. Users.allow({
  488. update(userId, doc) {
  489. const user = Users.findOne({
  490. _id: userId,
  491. });
  492. if ((user && user.isAdmin) || (Meteor.user() && Meteor.user().isAdmin))
  493. return true;
  494. if (!user) {
  495. return false;
  496. }
  497. return doc._id === userId;
  498. },
  499. remove(userId, doc) {
  500. const adminsNumber = Users.find({
  501. isAdmin: true,
  502. }).count();
  503. const { isAdmin } = Users.findOne(
  504. {
  505. _id: userId,
  506. },
  507. {
  508. fields: {
  509. isAdmin: 1,
  510. },
  511. },
  512. );
  513. // Prevents remove of the only one administrator
  514. if (adminsNumber === 1 && isAdmin && userId === doc._id) {
  515. return false;
  516. }
  517. // If it's the user or an admin
  518. return userId === doc._id || isAdmin;
  519. },
  520. fetch: [],
  521. });
  522. // Search a user in the complete server database by its name, username or emails adress. This
  523. // is used for instance to add a new user to a board.
  524. const searchInFields = ['username', 'profile.fullname', 'emails.address'];
  525. Users.initEasySearch(searchInFields, {
  526. use: 'mongo-db',
  527. returnFields: [...searchInFields, 'profile.avatarUrl'],
  528. });
  529. Users.safeFields = {
  530. _id: 1,
  531. username: 1,
  532. 'profile.fullname': 1,
  533. 'profile.avatarUrl': 1,
  534. 'profile.initials': 1,
  535. orgs: 1,
  536. teams: 1,
  537. authenticationMethod: 1,
  538. };
  539. if (Meteor.isClient) {
  540. Users.helpers({
  541. isBoardMember() {
  542. const board = Utils.getCurrentBoard();
  543. return board && board.hasMember(this._id);
  544. },
  545. isNotNoComments() {
  546. const board = Utils.getCurrentBoard();
  547. return (
  548. board && board.hasMember(this._id) && !board.hasNoComments(this._id)
  549. );
  550. },
  551. isNoComments() {
  552. const board = Utils.getCurrentBoard();
  553. return board && board.hasNoComments(this._id);
  554. },
  555. isNotCommentOnly() {
  556. const board = Utils.getCurrentBoard();
  557. return (
  558. board && board.hasMember(this._id) && !board.hasCommentOnly(this._id)
  559. );
  560. },
  561. isCommentOnly() {
  562. const board = Utils.getCurrentBoard();
  563. return board && board.hasCommentOnly(this._id);
  564. },
  565. isNotWorker() {
  566. const board = Utils.getCurrentBoard();
  567. return board && board.hasMember(this._id) && !board.hasWorker(this._id);
  568. },
  569. isWorker() {
  570. const board = Utils.getCurrentBoard();
  571. return board && board.hasWorker(this._id);
  572. },
  573. isBoardAdmin(boardId) {
  574. let board;
  575. if (boardId) {
  576. board = Boards.findOne(boardId);
  577. } else {
  578. board = Utils.getCurrentBoard();
  579. }
  580. return board && board.hasAdmin(this._id);
  581. },
  582. });
  583. }
  584. Users.parseImportUsernames = (usernamesString) => {
  585. return usernamesString.trim().split(new RegExp('\\s*[,;]\\s*'));
  586. };
  587. Users.helpers({
  588. importUsernamesString() {
  589. if (this.importUsernames) {
  590. return this.importUsernames.join(', ');
  591. }
  592. return '';
  593. },
  594. teamIds() {
  595. if (this.teams) {
  596. // TODO: Should the Team collection be queried to determine if the team isActive?
  597. return this.teams.map(team => { return team.teamId });
  598. }
  599. return [];
  600. },
  601. orgIds() {
  602. if (this.orgs) {
  603. // TODO: Should the Org collection be queried to determine if the organization isActive?
  604. return this.orgs.map(org => { return org.orgId });
  605. }
  606. return [];
  607. },
  608. orgsUserBelongs() {
  609. if (this.orgs) {
  610. return this.orgs.map(function(org){return org.orgDisplayName}).sort().join(',');
  611. }
  612. return '';
  613. },
  614. orgIdsUserBelongs() {
  615. if (this.orgs) {
  616. return this.orgs.map(function(org){return org.orgId}).join(',');
  617. }
  618. return '';
  619. },
  620. teamsUserBelongs() {
  621. if (this.teams) {
  622. return this.teams.map(function(team){ return team.teamDisplayName}).sort().join(',');
  623. }
  624. return '';
  625. },
  626. teamIdsUserBelongs() {
  627. if (this.teams) {
  628. return this.teams.map(function(team){ return team.teamId}).join(',');
  629. }
  630. return '';
  631. },
  632. boards() {
  633. return Boards.userBoards(this._id, null, {}, { sort: { sort: 1 } })
  634. },
  635. starredBoards() {
  636. const { starredBoards = [] } = this.profile || {};
  637. return Boards.userBoards(
  638. this._id,
  639. false,
  640. { _id: { $in: starredBoards } },
  641. { sort: { sort: 1 } }
  642. );
  643. },
  644. hasStarred(boardId) {
  645. const { starredBoards = [] } = this.profile || {};
  646. return _.contains(starredBoards, boardId);
  647. },
  648. invitedBoards() {
  649. const { invitedBoards = [] } = this.profile || {};
  650. return Boards.userBoards(
  651. this._id,
  652. false,
  653. { _id: { $in: invitedBoards } },
  654. { sort: { sort: 1 } }
  655. );
  656. },
  657. isInvitedTo(boardId) {
  658. const { invitedBoards = [] } = this.profile || {};
  659. return _.contains(invitedBoards, boardId);
  660. },
  661. _getListSortBy() {
  662. const profile = this.profile || {};
  663. const sortBy = profile.listSortBy || defaultSortBy;
  664. const keyPattern = /^(-{0,1})(.*$)/;
  665. const ret = [];
  666. if (keyPattern.exec(sortBy)) {
  667. ret[0] = RegExp.$2;
  668. ret[1] = RegExp.$1 ? -1 : 1;
  669. }
  670. return ret;
  671. },
  672. hasSortBy() {
  673. // if use doesn't have dragHandle, then we can let user to choose sort list by different order
  674. return !this.hasShowDesktopDragHandles();
  675. },
  676. getListSortBy() {
  677. return this._getListSortBy()[0];
  678. },
  679. getListSortTypes() {
  680. return allowedSortValues;
  681. },
  682. getListSortByDirection() {
  683. return this._getListSortBy()[1];
  684. },
  685. /** returns all confirmed move and copy dialog field values
  686. * <li> the board, swimlane and list id is stored for each board
  687. */
  688. getMoveAndCopyDialogOptions() {
  689. let _ret = {}
  690. if (this.profile && this.profile.moveAndCopyDialog) {
  691. _ret = this.profile.moveAndCopyDialog;
  692. }
  693. return _ret;
  694. },
  695. /** returns all confirmed move checklist dialog field values
  696. * <li> the board, swimlane, list and card id is stored for each board
  697. */
  698. getMoveChecklistDialogOptions() {
  699. let _ret = {}
  700. if (this.profile && this.profile.moveChecklistDialog) {
  701. _ret = this.profile.moveChecklistDialog;
  702. }
  703. return _ret;
  704. },
  705. /** returns all confirmed copy checklist dialog field values
  706. * <li> the board, swimlane, list and card id is stored for each board
  707. */
  708. getCopyChecklistDialogOptions() {
  709. let _ret = {}
  710. if (this.profile && this.profile.copyChecklistDialog) {
  711. _ret = this.profile.copyChecklistDialog;
  712. }
  713. return _ret;
  714. },
  715. hasTag(tag) {
  716. const { tags = [] } = this.profile || {};
  717. return _.contains(tags, tag);
  718. },
  719. hasNotification(activityId) {
  720. const { notifications = [] } = this.profile || {};
  721. return _.contains(notifications, activityId);
  722. },
  723. notifications() {
  724. const { notifications = [] } = this.profile || {};
  725. for (const index in notifications) {
  726. if (!notifications.hasOwnProperty(index)) continue;
  727. const notification = notifications[index];
  728. // this preserves their db sort order for editing
  729. notification.dbIndex = index;
  730. notification.activity = Activities.findOne(notification.activity);
  731. }
  732. // this sorts them newest to oldest to match Trello's behavior
  733. notifications.reverse();
  734. return notifications;
  735. },
  736. hasShowDesktopDragHandles() {
  737. const profile = this.profile || {};
  738. return profile.showDesktopDragHandles || false;
  739. },
  740. hasHideCheckedItems() {
  741. const profile = this.profile || {};
  742. return profile.hideCheckedItems || false;
  743. },
  744. hasHiddenSystemMessages() {
  745. const profile = this.profile || {};
  746. return profile.hiddenSystemMessages || false;
  747. },
  748. hasCustomFieldsGrid() {
  749. const profile = this.profile || {};
  750. return profile.customFieldsGrid || false;
  751. },
  752. hasCardMaximized() {
  753. const profile = this.profile || {};
  754. return profile.cardMaximized || false;
  755. },
  756. hasHiddenMinicardLabelText() {
  757. const profile = this.profile || {};
  758. return profile.hiddenMinicardLabelText || false;
  759. },
  760. getEmailBuffer() {
  761. const { emailBuffer = [] } = this.profile || {};
  762. return emailBuffer;
  763. },
  764. getInitials() {
  765. const profile = this.profile || {};
  766. if (profile.initials) return profile.initials;
  767. else if (profile.fullname) {
  768. return profile.fullname
  769. .split(/\s+/)
  770. .reduce((memo, word) => {
  771. return memo + word[0];
  772. }, '')
  773. .toUpperCase();
  774. } else {
  775. return this.username[0].toUpperCase();
  776. }
  777. },
  778. getLimitToShowCardsCount() {
  779. const profile = this.profile || {};
  780. return profile.showCardsCountAt;
  781. },
  782. getName() {
  783. const profile = this.profile || {};
  784. return profile.fullname || this.username;
  785. },
  786. getLanguage() {
  787. const profile = this.profile || {};
  788. return profile.language || 'en';
  789. },
  790. getStartDayOfWeek() {
  791. const profile = this.profile || {};
  792. if (typeof profile.startDayOfWeek === 'undefined') {
  793. // default is 'Monday' (1)
  794. return 1;
  795. }
  796. return profile.startDayOfWeek;
  797. },
  798. getTemplatesBoardId() {
  799. return (this.profile || {}).templatesBoardId;
  800. },
  801. getTemplatesBoardSlug() {
  802. //return (Boards.findOne((this.profile || {}).templatesBoardId) || {}).slug;
  803. return 'templates';
  804. },
  805. remove() {
  806. User.remove({
  807. _id: this._id,
  808. });
  809. },
  810. });
  811. Users.mutations({
  812. /** set the confirmed board id/swimlane id/list id of a board
  813. * @param boardId the current board id
  814. * @param options an object with the confirmed field values
  815. */
  816. setMoveAndCopyDialogOption(boardId, options) {
  817. let currentOptions = this.getMoveAndCopyDialogOptions();
  818. currentOptions[boardId] = options;
  819. return {
  820. $set: {
  821. 'profile.moveAndCopyDialog': currentOptions,
  822. },
  823. };
  824. },
  825. /** set the confirmed board id/swimlane id/list id/card id of a board (move checklist)
  826. * @param boardId the current board id
  827. * @param options an object with the confirmed field values
  828. */
  829. setMoveChecklistDialogOption(boardId, options) {
  830. let currentOptions = this.getMoveChecklistDialogOptions();
  831. currentOptions[boardId] = options;
  832. return {
  833. $set: {
  834. 'profile.moveChecklistDialog': currentOptions,
  835. },
  836. };
  837. },
  838. /** set the confirmed board id/swimlane id/list id/card id of a board (copy checklist)
  839. * @param boardId the current board id
  840. * @param options an object with the confirmed field values
  841. */
  842. setCopyChecklistDialogOption(boardId, options) {
  843. let currentOptions = this.getCopyChecklistDialogOptions();
  844. currentOptions[boardId] = options;
  845. return {
  846. $set: {
  847. 'profile.copyChecklistDialog': currentOptions,
  848. },
  849. };
  850. },
  851. toggleBoardStar(boardId) {
  852. const queryKind = this.hasStarred(boardId) ? '$pull' : '$addToSet';
  853. return {
  854. [queryKind]: {
  855. 'profile.starredBoards': boardId,
  856. },
  857. };
  858. },
  859. addInvite(boardId) {
  860. return {
  861. $addToSet: {
  862. 'profile.invitedBoards': boardId,
  863. },
  864. };
  865. },
  866. removeInvite(boardId) {
  867. return {
  868. $pull: {
  869. 'profile.invitedBoards': boardId,
  870. },
  871. };
  872. },
  873. addTag(tag) {
  874. return {
  875. $addToSet: {
  876. 'profile.tags': tag,
  877. },
  878. };
  879. },
  880. removeTag(tag) {
  881. return {
  882. $pull: {
  883. 'profile.tags': tag,
  884. },
  885. };
  886. },
  887. toggleTag(tag) {
  888. if (this.hasTag(tag)) this.removeTag(tag);
  889. else this.addTag(tag);
  890. },
  891. setListSortBy(value) {
  892. return {
  893. $set: {
  894. 'profile.listSortBy': value,
  895. },
  896. };
  897. },
  898. setName(value) {
  899. return {
  900. $set: {
  901. 'profile.fullname': value,
  902. },
  903. };
  904. },
  905. toggleDesktopHandles(value = false) {
  906. return {
  907. $set: {
  908. 'profile.showDesktopDragHandles': !value,
  909. },
  910. };
  911. },
  912. toggleHideCheckedItems() {
  913. const value = this.hasHideCheckedItems();
  914. return {
  915. $set: {
  916. 'profile.hideCheckedItems': !value,
  917. },
  918. };
  919. },
  920. toggleSystem(value = false) {
  921. return {
  922. $set: {
  923. 'profile.hiddenSystemMessages': !value,
  924. },
  925. };
  926. },
  927. toggleFieldsGrid(value = false) {
  928. return {
  929. $set: {
  930. 'profile.customFieldsGrid': !value,
  931. },
  932. };
  933. },
  934. toggleCardMaximized(value = false) {
  935. return {
  936. $set: {
  937. 'profile.cardMaximized': !value,
  938. },
  939. };
  940. },
  941. toggleLabelText(value = false) {
  942. return {
  943. $set: {
  944. 'profile.hiddenMinicardLabelText': !value,
  945. },
  946. };
  947. },
  948. addNotification(activityId) {
  949. return {
  950. $addToSet: {
  951. 'profile.notifications': {
  952. activity: activityId,
  953. },
  954. },
  955. };
  956. },
  957. removeNotification(activityId) {
  958. return {
  959. $pull: {
  960. 'profile.notifications': {
  961. activity: activityId,
  962. },
  963. },
  964. };
  965. },
  966. addEmailBuffer(text) {
  967. return {
  968. $addToSet: {
  969. 'profile.emailBuffer': text,
  970. },
  971. };
  972. },
  973. clearEmailBuffer() {
  974. return {
  975. $set: {
  976. 'profile.emailBuffer': [],
  977. },
  978. };
  979. },
  980. setAvatarUrl(avatarUrl) {
  981. return {
  982. $set: {
  983. 'profile.avatarUrl': avatarUrl,
  984. },
  985. };
  986. },
  987. setShowCardsCountAt(limit) {
  988. return {
  989. $set: {
  990. 'profile.showCardsCountAt': limit,
  991. },
  992. };
  993. },
  994. setStartDayOfWeek(startDay) {
  995. return {
  996. $set: {
  997. 'profile.startDayOfWeek': startDay,
  998. },
  999. };
  1000. },
  1001. setBoardView(view) {
  1002. return {
  1003. $set: {
  1004. 'profile.boardView': view,
  1005. },
  1006. };
  1007. },
  1008. });
  1009. Meteor.methods({
  1010. setListSortBy(value) {
  1011. check(value, String);
  1012. Meteor.user().setListSortBy(value);
  1013. },
  1014. toggleDesktopDragHandles() {
  1015. const user = Meteor.user();
  1016. user.toggleDesktopHandles(user.hasShowDesktopDragHandles());
  1017. },
  1018. toggleHideCheckedItems() {
  1019. const user = Meteor.user();
  1020. user.toggleHideCheckedItems();
  1021. },
  1022. toggleSystemMessages() {
  1023. const user = Meteor.user();
  1024. user.toggleSystem(user.hasHiddenSystemMessages());
  1025. },
  1026. toggleCustomFieldsGrid() {
  1027. const user = Meteor.user();
  1028. user.toggleFieldsGrid(user.hasCustomFieldsGrid());
  1029. },
  1030. toggleCardMaximized() {
  1031. const user = Meteor.user();
  1032. user.toggleCardMaximized(user.hasCardMaximized());
  1033. },
  1034. toggleMinicardLabelText() {
  1035. const user = Meteor.user();
  1036. user.toggleLabelText(user.hasHiddenMinicardLabelText());
  1037. },
  1038. changeLimitToShowCardsCount(limit) {
  1039. check(limit, Number);
  1040. Meteor.user().setShowCardsCountAt(limit);
  1041. },
  1042. changeStartDayOfWeek(startDay) {
  1043. check(startDay, Number);
  1044. Meteor.user().setStartDayOfWeek(startDay);
  1045. },
  1046. });
  1047. if (Meteor.isServer) {
  1048. Meteor.methods({
  1049. setAllUsersHideSystemMessages() {
  1050. if (Meteor.user() && Meteor.user().isAdmin) {
  1051. // If setting is missing, add it
  1052. Users.update(
  1053. {
  1054. 'profile.hiddenSystemMessages': {
  1055. $exists: false,
  1056. },
  1057. },
  1058. {
  1059. $set: {
  1060. 'profile.hiddenSystemMessages': true,
  1061. },
  1062. },
  1063. {
  1064. multi: true,
  1065. },
  1066. );
  1067. // If setting is false, set it to true
  1068. Users.update(
  1069. {
  1070. 'profile.hiddenSystemMessages': false,
  1071. },
  1072. {
  1073. $set: {
  1074. 'profile.hiddenSystemMessages': true,
  1075. },
  1076. },
  1077. {
  1078. multi: true,
  1079. },
  1080. );
  1081. return true;
  1082. } else {
  1083. return false;
  1084. }
  1085. },
  1086. setCreateUser(
  1087. fullname,
  1088. username,
  1089. initials,
  1090. password,
  1091. isAdmin,
  1092. isActive,
  1093. email,
  1094. importUsernames,
  1095. userOrgsArray,
  1096. userTeamsArray,
  1097. ) {
  1098. check(fullname, String);
  1099. check(username, String);
  1100. check(initials, String);
  1101. check(password, String);
  1102. check(isAdmin, String);
  1103. check(isActive, String);
  1104. check(email, String);
  1105. check(importUsernames, Array);
  1106. check(userOrgsArray, Array);
  1107. check(userTeamsArray, Array);
  1108. if (Meteor.user() && Meteor.user().isAdmin) {
  1109. const nUsersWithUsername = Users.find({
  1110. username,
  1111. }).count();
  1112. const nUsersWithEmail = Users.find({
  1113. email,
  1114. }).count();
  1115. if (nUsersWithUsername > 0) {
  1116. throw new Meteor.Error('username-already-taken');
  1117. } else if (nUsersWithEmail > 0) {
  1118. throw new Meteor.Error('email-already-taken');
  1119. } else {
  1120. Accounts.createUser({
  1121. username,
  1122. password,
  1123. isAdmin,
  1124. isActive,
  1125. email: email.toLowerCase(),
  1126. from: 'admin',
  1127. });
  1128. const user =
  1129. Users.findOne(username) ||
  1130. Users.findOne({
  1131. username,
  1132. });
  1133. if (user) {
  1134. Users.update(user._id, {
  1135. $set: {
  1136. 'profile.fullname': fullname,
  1137. importUsernames,
  1138. 'profile.initials': initials,
  1139. orgs: userOrgsArray,
  1140. teams: userTeamsArray,
  1141. },
  1142. });
  1143. }
  1144. }
  1145. }
  1146. },
  1147. setUsername(username, userId) {
  1148. check(username, String);
  1149. check(userId, String);
  1150. if (Meteor.user() && Meteor.user().isAdmin) {
  1151. const nUsersWithUsername = Users.find({
  1152. username,
  1153. }).count();
  1154. if (nUsersWithUsername > 0) {
  1155. throw new Meteor.Error('username-already-taken');
  1156. } else {
  1157. Users.update(userId, {
  1158. $set: {
  1159. username,
  1160. },
  1161. });
  1162. }
  1163. }
  1164. },
  1165. setEmail(email, userId) {
  1166. check(email, String);
  1167. check(username, String);
  1168. if (Meteor.user() && Meteor.user().isAdmin) {
  1169. if (Array.isArray(email)) {
  1170. email = email.shift();
  1171. }
  1172. const existingUser = Users.findOne(
  1173. {
  1174. 'emails.address': email,
  1175. },
  1176. {
  1177. fields: {
  1178. _id: 1,
  1179. },
  1180. },
  1181. );
  1182. if (existingUser) {
  1183. throw new Meteor.Error('email-already-taken');
  1184. } else {
  1185. Users.update(userId, {
  1186. $set: {
  1187. emails: [
  1188. {
  1189. address: email,
  1190. verified: false,
  1191. },
  1192. ],
  1193. },
  1194. });
  1195. }
  1196. }
  1197. },
  1198. setUsernameAndEmail(username, email, userId) {
  1199. check(username, String);
  1200. check(email, String);
  1201. check(userId, String);
  1202. if (Meteor.user() && Meteor.user().isAdmin) {
  1203. if (Array.isArray(email)) {
  1204. email = email.shift();
  1205. }
  1206. Meteor.call('setUsername', username, userId);
  1207. Meteor.call('setEmail', email, userId);
  1208. }
  1209. },
  1210. setPassword(newPassword, userId) {
  1211. check(userId, String);
  1212. check(newPassword, String);
  1213. if (Meteor.user() && Meteor.user().isAdmin) {
  1214. if (Meteor.user().isAdmin) {
  1215. Accounts.setPassword(userId, newPassword);
  1216. }
  1217. }
  1218. },
  1219. setEmailVerified(email, verified, userId) {
  1220. check(email, String);
  1221. check(verified, Boolean);
  1222. check(userId, String);
  1223. if (Meteor.user() && Meteor.user().isAdmin) {
  1224. Users.update(userId, {
  1225. $set: {
  1226. emails: [
  1227. {
  1228. address: email,
  1229. verified,
  1230. },
  1231. ],
  1232. },
  1233. });
  1234. }
  1235. },
  1236. setInitials(initials, userId) {
  1237. check(initials, String);
  1238. check(userId, String);
  1239. if (Meteor.user() && Meteor.user().isAdmin) {
  1240. Users.update(userId, {
  1241. $set: {
  1242. 'profile.initials': initials,
  1243. },
  1244. });
  1245. }
  1246. },
  1247. // we accept userId, username, email
  1248. inviteUserToBoard(username, boardId) {
  1249. check(username, String);
  1250. check(boardId, String);
  1251. const inviter = Meteor.user();
  1252. const board = Boards.findOne(boardId);
  1253. const allowInvite =
  1254. inviter &&
  1255. board &&
  1256. board.members &&
  1257. _.contains(_.pluck(board.members, 'userId'), inviter._id) &&
  1258. _.where(board.members, {
  1259. userId: inviter._id,
  1260. })[0].isActive;
  1261. // GitHub issue 2060
  1262. //_.where(board.members, { userId: inviter._id })[0].isAdmin;
  1263. if (!allowInvite) throw new Meteor.Error('error-board-notAMember');
  1264. this.unblock();
  1265. const posAt = username.indexOf('@');
  1266. let user = null;
  1267. if (posAt >= 0) {
  1268. user = Users.findOne({
  1269. emails: {
  1270. $elemMatch: {
  1271. address: username,
  1272. },
  1273. },
  1274. });
  1275. } else {
  1276. user =
  1277. Users.findOne(username) ||
  1278. Users.findOne({
  1279. username,
  1280. });
  1281. }
  1282. if (user) {
  1283. if (user._id === inviter._id)
  1284. throw new Meteor.Error('error-user-notAllowSelf');
  1285. } else {
  1286. if (posAt <= 0) throw new Meteor.Error('error-user-doesNotExist');
  1287. if (
  1288. Settings.findOne({
  1289. disableRegistration: true,
  1290. })
  1291. ) {
  1292. throw new Meteor.Error('error-user-notCreated');
  1293. }
  1294. // Set in lowercase email before creating account
  1295. const email = username.toLowerCase();
  1296. username = email.substring(0, posAt);
  1297. const newUserId = Accounts.createUser({
  1298. username,
  1299. email,
  1300. });
  1301. if (!newUserId) throw new Meteor.Error('error-user-notCreated');
  1302. // assume new user speak same language with inviter
  1303. if (inviter.profile && inviter.profile.language) {
  1304. Users.update(newUserId, {
  1305. $set: {
  1306. 'profile.language': inviter.profile.language,
  1307. },
  1308. });
  1309. }
  1310. Accounts.sendEnrollmentEmail(newUserId);
  1311. user = Users.findOne(newUserId);
  1312. }
  1313. board.addMember(user._id);
  1314. user.addInvite(boardId);
  1315. //Check if there is a subtasks board
  1316. if (board.subtasksDefaultBoardId) {
  1317. const subBoard = Boards.findOne(board.subtasksDefaultBoardId);
  1318. //If there is, also add user to that board
  1319. if (subBoard) {
  1320. subBoard.addMember(user._id);
  1321. user.addInvite(subBoard._id);
  1322. }
  1323. }
  1324. try {
  1325. const fullName = inviter.profile !== undefined && inviter.profile.fullname !== undefined ? inviter.profile.fullname : "";
  1326. const userFullName = user.profile !== undefined && user.profile.fullname !== undefined ? user.profile.fullname : "";
  1327. const params = {
  1328. user: userFullName != "" ? userFullName + " (" + user.username + " )" : user.username,
  1329. inviter: fullName != "" ? fullName + " (" + inviter.username + " )" : inviter.username,
  1330. board: board.title,
  1331. url: board.absoluteUrl(),
  1332. };
  1333. const lang = user.getLanguage();
  1334. /*
  1335. if (process.env.MAIL_SERVICE !== '') {
  1336. let transporter = nodemailer.createTransport({
  1337. service: process.env.MAIL_SERVICE,
  1338. auth: {
  1339. user: process.env.MAIL_SERVICE_USER,
  1340. pass: process.env.MAIL_SERVICE_PASSWORD
  1341. },
  1342. })
  1343. let info = transporter.sendMail({
  1344. to: user.emails[0].address.toLowerCase(),
  1345. from: Accounts.emailTemplates.from,
  1346. subject: TAPi18n.__('email-invite-subject', params, lang),
  1347. text: TAPi18n.__('email-invite-text', params, lang),
  1348. })
  1349. } else {
  1350. Email.send({
  1351. to: user.emails[0].address.toLowerCase(),
  1352. from: Accounts.emailTemplates.from,
  1353. subject: TAPi18n.__('email-invite-subject', params, lang),
  1354. text: TAPi18n.__('email-invite-text', params, lang),
  1355. });
  1356. }
  1357. */
  1358. Email.send({
  1359. to: user.emails[0].address.toLowerCase(),
  1360. from: Accounts.emailTemplates.from,
  1361. subject: TAPi18n.__('email-invite-subject', params, lang),
  1362. text: TAPi18n.__('email-invite-text', params, lang),
  1363. });
  1364. } catch (e) {
  1365. throw new Meteor.Error('email-fail', e.message);
  1366. }
  1367. return {
  1368. username: user.username,
  1369. email: user.emails[0].address,
  1370. };
  1371. },
  1372. impersonate(userId) {
  1373. check(userId, String);
  1374. if (!Meteor.users.findOne(userId))
  1375. throw new Meteor.Error(404, 'User not found');
  1376. if (!Meteor.user().isAdmin)
  1377. throw new Meteor.Error(403, 'Permission denied');
  1378. ImpersonatedUsers.insert({ adminId: Meteor.user()._id, userId: userId, reason: 'clickedImpersonate' });
  1379. this.setUserId(userId);
  1380. },
  1381. isImpersonated(userId) {
  1382. check(userId, String);
  1383. const isImpersonated = ImpersonatedUsers.findOne({
  1384. userId: userId,
  1385. });
  1386. return isImpersonated;
  1387. },
  1388. setUsersTeamsTeamDisplayName(teamId, teamDisplayName) {
  1389. check(teamId, String);
  1390. check(teamDisplayName, String);
  1391. if (Meteor.user() && Meteor.user().isAdmin) {
  1392. Users.find({
  1393. teams: {
  1394. $elemMatch: {teamId: teamId}
  1395. }
  1396. }).forEach(user => {
  1397. Users.update({
  1398. _id: user._id,
  1399. teams: {
  1400. $elemMatch: {teamId: teamId}
  1401. }
  1402. }, {
  1403. $set: {
  1404. 'teams.$.teamDisplayName': teamDisplayName
  1405. }
  1406. });
  1407. });
  1408. }
  1409. },
  1410. setUsersOrgsOrgDisplayName(orgId, orgDisplayName) {
  1411. check(orgId, String);
  1412. check(orgDisplayName, String);
  1413. if (Meteor.user() && Meteor.user().isAdmin) {
  1414. Users.find({
  1415. orgs: {
  1416. $elemMatch: {orgId: orgId}
  1417. }
  1418. }).forEach(user => {
  1419. Users.update({
  1420. _id: user._id,
  1421. orgs: {
  1422. $elemMatch: {orgId: orgId}
  1423. }
  1424. }, {
  1425. $set: {
  1426. 'orgs.$.orgDisplayName': orgDisplayName
  1427. }
  1428. });
  1429. });
  1430. }
  1431. },
  1432. });
  1433. Accounts.onCreateUser((options, user) => {
  1434. const userCount = Users.find().count();
  1435. if (userCount === 0) {
  1436. user.isAdmin = true;
  1437. }
  1438. if (user.services.oidc) {
  1439. let email = user.services.oidc.email;
  1440. if (Array.isArray(email)) {
  1441. email = email.shift();
  1442. }
  1443. email = email.toLowerCase();
  1444. user.username = user.services.oidc.username;
  1445. user.emails = [
  1446. {
  1447. address: email,
  1448. verified: true,
  1449. },
  1450. ];
  1451. const initials = user.services.oidc.fullname
  1452. .split(/\s+/)
  1453. .reduce((memo, word) => {
  1454. return memo + word[0];
  1455. }, '')
  1456. .toUpperCase();
  1457. user.profile = {
  1458. initials,
  1459. fullname: user.services.oidc.fullname,
  1460. boardView: 'board-view-swimlanes',
  1461. };
  1462. user.authenticationMethod = 'oauth2';
  1463. // see if any existing user has this email address or username, otherwise create new
  1464. const existingUser = Meteor.users.findOne({
  1465. $or: [
  1466. {
  1467. 'emails.address': email,
  1468. },
  1469. {
  1470. username: user.username,
  1471. },
  1472. ],
  1473. });
  1474. if (!existingUser) return user;
  1475. // copy across new service info
  1476. const service = _.keys(user.services)[0];
  1477. existingUser.services[service] = user.services[service];
  1478. existingUser.emails = user.emails;
  1479. existingUser.username = user.username;
  1480. existingUser.profile = user.profile;
  1481. existingUser.authenticationMethod = user.authenticationMethod;
  1482. Meteor.users.remove({
  1483. _id: user._id,
  1484. });
  1485. Meteor.users.remove({
  1486. _id: existingUser._id,
  1487. }); // is going to be created again
  1488. return existingUser;
  1489. }
  1490. if (options.from === 'admin') {
  1491. user.createdThroughApi = true;
  1492. return user;
  1493. }
  1494. const disableRegistration = Settings.findOne().disableRegistration;
  1495. // If this is the first Authentication by the ldap and self registration disabled
  1496. if (disableRegistration && options && options.ldap) {
  1497. user.authenticationMethod = 'ldap';
  1498. return user;
  1499. }
  1500. // If self registration enabled
  1501. if (!disableRegistration) {
  1502. return user;
  1503. }
  1504. if (!options || !options.profile) {
  1505. throw new Meteor.Error(
  1506. 'error-invitation-code-blank',
  1507. 'The invitation code is required',
  1508. );
  1509. }
  1510. const invitationCode = InvitationCodes.findOne({
  1511. code: options.profile.invitationcode,
  1512. email: options.email,
  1513. valid: true,
  1514. });
  1515. if (!invitationCode) {
  1516. throw new Meteor.Error(
  1517. 'error-invitation-code-not-exist',
  1518. // eslint-disable-next-line quotes
  1519. "The invitation code doesn't exist",
  1520. );
  1521. } else {
  1522. user.profile = {
  1523. icode: options.profile.invitationcode,
  1524. };
  1525. user.profile.boardView = 'board-view-swimlanes';
  1526. // Deletes the invitation code after the user was created successfully.
  1527. setTimeout(
  1528. Meteor.bindEnvironment(() => {
  1529. InvitationCodes.remove({
  1530. _id: invitationCode._id,
  1531. });
  1532. }),
  1533. 200,
  1534. );
  1535. return user;
  1536. }
  1537. });
  1538. }
  1539. const addCronJob = _.debounce(
  1540. Meteor.bindEnvironment(function notificationCleanupDebounced() {
  1541. // passed in the removeAge has to be a number standing for the number of days after a notification is read before we remove it
  1542. const envRemoveAge =
  1543. process.env.NOTIFICATION_TRAY_AFTER_READ_DAYS_BEFORE_REMOVE;
  1544. // default notifications will be removed 2 days after they are read
  1545. const defaultRemoveAge = 2;
  1546. const removeAge = parseInt(envRemoveAge, 10) || defaultRemoveAge;
  1547. SyncedCron.add({
  1548. name: 'notification_cleanup',
  1549. schedule: (parser) => parser.text('every 1 days'),
  1550. job: () => {
  1551. for (const user of Users.find()) {
  1552. if (!user.profile || !user.profile.notifications) continue;
  1553. for (const notification of user.profile.notifications) {
  1554. if (notification.read) {
  1555. const removeDate = new Date(notification.read);
  1556. removeDate.setDate(removeDate.getDate() + removeAge);
  1557. if (removeDate <= new Date()) {
  1558. user.removeNotification(notification.activity);
  1559. }
  1560. }
  1561. }
  1562. }
  1563. },
  1564. });
  1565. SyncedCron.start();
  1566. }),
  1567. 500,
  1568. );
  1569. if (Meteor.isServer) {
  1570. // Let mongoDB ensure username unicity
  1571. Meteor.startup(() => {
  1572. allowedSortValues.forEach((value) => {
  1573. Lists._collection._ensureIndex(value);
  1574. });
  1575. Users._collection._ensureIndex({
  1576. modifiedAt: -1,
  1577. });
  1578. Users._collection._ensureIndex(
  1579. {
  1580. username: 1,
  1581. },
  1582. {
  1583. unique: true,
  1584. },
  1585. );
  1586. Meteor.defer(() => {
  1587. addCronJob();
  1588. });
  1589. });
  1590. // OLD WAY THIS CODE DID WORK: When user is last admin of board,
  1591. // if admin is removed, board is removed.
  1592. // NOW THIS IS COMMENTED OUT, because other board users still need to be able
  1593. // to use that board, and not have board deleted.
  1594. // Someone can be later changed to be admin of board, by making change to database.
  1595. // TODO: Add UI for changing someone as board admin.
  1596. //Users.before.remove((userId, doc) => {
  1597. // Boards
  1598. // .find({members: {$elemMatch: {userId: doc._id, isAdmin: true}}})
  1599. // .forEach((board) => {
  1600. // // If only one admin for the board
  1601. // if (board.members.filter((e) => e.isAdmin).length === 1) {
  1602. // Boards.remove(board._id);
  1603. // }
  1604. // });
  1605. //});
  1606. // Each board document contains the de-normalized number of users that have
  1607. // starred it. If the user star or unstar a board, we need to update this
  1608. // counter.
  1609. // We need to run this code on the server only, otherwise the incrementation
  1610. // will be done twice.
  1611. Users.after.update(function (userId, user, fieldNames) {
  1612. // The `starredBoards` list is hosted on the `profile` field. If this
  1613. // field hasn't been modificated we don't need to run this hook.
  1614. if (!_.contains(fieldNames, 'profile')) return;
  1615. // To calculate a diff of board starred ids, we get both the previous
  1616. // and the newly board ids list
  1617. function getStarredBoardsIds(doc) {
  1618. return doc.profile && doc.profile.starredBoards;
  1619. }
  1620. const oldIds = getStarredBoardsIds(this.previous);
  1621. const newIds = getStarredBoardsIds(user);
  1622. // The _.difference(a, b) method returns the values from a that are not in
  1623. // b. We use it to find deleted and newly inserted ids by using it in one
  1624. // direction and then in the other.
  1625. function incrementBoards(boardsIds, inc) {
  1626. boardsIds.forEach((boardId) => {
  1627. Boards.update(boardId, {
  1628. $inc: {
  1629. stars: inc,
  1630. },
  1631. });
  1632. });
  1633. }
  1634. incrementBoards(_.difference(oldIds, newIds), -1);
  1635. incrementBoards(_.difference(newIds, oldIds), +1);
  1636. });
  1637. // Override getUserId so that we can TODO get the current userId
  1638. const fakeUserId = new Meteor.EnvironmentVariable();
  1639. const getUserId = CollectionHooks.getUserId;
  1640. CollectionHooks.getUserId = () => {
  1641. return fakeUserId.get() || getUserId();
  1642. };
  1643. if (!isSandstorm) {
  1644. Users.after.insert((userId, doc) => {
  1645. const fakeUser = {
  1646. extendAutoValueContext: {
  1647. userId: doc._id,
  1648. },
  1649. };
  1650. fakeUserId.withValue(doc._id, () => {
  1651. /*
  1652. // Insert the Welcome Board
  1653. Boards.insert({
  1654. title: TAPi18n.__('welcome-board'),
  1655. permission: 'private',
  1656. }, fakeUser, (err, boardId) => {
  1657. Swimlanes.insert({
  1658. title: TAPi18n.__('welcome-swimlane'),
  1659. boardId,
  1660. sort: 1,
  1661. }, fakeUser);
  1662. ['welcome-list1', 'welcome-list2'].forEach((title, titleIndex) => {
  1663. Lists.insert({title: TAPi18n.__(title), boardId, sort: titleIndex}, fakeUser);
  1664. });
  1665. });
  1666. */
  1667. // Insert Template Container
  1668. const Future = require('fibers/future');
  1669. const future1 = new Future();
  1670. const future2 = new Future();
  1671. const future3 = new Future();
  1672. Boards.insert(
  1673. {
  1674. title: TAPi18n.__('templates'),
  1675. permission: 'private',
  1676. type: 'template-container',
  1677. },
  1678. fakeUser,
  1679. (err, boardId) => {
  1680. // Insert the reference to our templates board
  1681. Users.update(fakeUserId.get(), {
  1682. $set: {
  1683. 'profile.templatesBoardId': boardId,
  1684. },
  1685. });
  1686. // Insert the card templates swimlane
  1687. Swimlanes.insert(
  1688. {
  1689. title: TAPi18n.__('card-templates-swimlane'),
  1690. boardId,
  1691. sort: 1,
  1692. type: 'template-container',
  1693. },
  1694. fakeUser,
  1695. (err, swimlaneId) => {
  1696. // Insert the reference to out card templates swimlane
  1697. Users.update(fakeUserId.get(), {
  1698. $set: {
  1699. 'profile.cardTemplatesSwimlaneId': swimlaneId,
  1700. },
  1701. });
  1702. future1.return();
  1703. },
  1704. );
  1705. // Insert the list templates swimlane
  1706. Swimlanes.insert(
  1707. {
  1708. title: TAPi18n.__('list-templates-swimlane'),
  1709. boardId,
  1710. sort: 2,
  1711. type: 'template-container',
  1712. },
  1713. fakeUser,
  1714. (err, swimlaneId) => {
  1715. // Insert the reference to out list templates swimlane
  1716. Users.update(fakeUserId.get(), {
  1717. $set: {
  1718. 'profile.listTemplatesSwimlaneId': swimlaneId,
  1719. },
  1720. });
  1721. future2.return();
  1722. },
  1723. );
  1724. // Insert the board templates swimlane
  1725. Swimlanes.insert(
  1726. {
  1727. title: TAPi18n.__('board-templates-swimlane'),
  1728. boardId,
  1729. sort: 3,
  1730. type: 'template-container',
  1731. },
  1732. fakeUser,
  1733. (err, swimlaneId) => {
  1734. // Insert the reference to out board templates swimlane
  1735. Users.update(fakeUserId.get(), {
  1736. $set: {
  1737. 'profile.boardTemplatesSwimlaneId': swimlaneId,
  1738. },
  1739. });
  1740. future3.return();
  1741. },
  1742. );
  1743. },
  1744. );
  1745. // HACK
  1746. future1.wait();
  1747. future2.wait();
  1748. future3.wait();
  1749. // End of Insert Template Container
  1750. });
  1751. });
  1752. }
  1753. Users.after.insert((userId, doc) => {
  1754. // HACK
  1755. doc = Users.findOne({
  1756. _id: doc._id,
  1757. });
  1758. if (doc.createdThroughApi) {
  1759. // The admin user should be able to create a user despite disabling registration because
  1760. // it is two different things (registration and creation).
  1761. // So, when a new user is created via the api (only admin user can do that) one must avoid
  1762. // the disableRegistration check.
  1763. // Issue : https://github.com/wekan/wekan/issues/1232
  1764. // PR : https://github.com/wekan/wekan/pull/1251
  1765. Users.update(doc._id, {
  1766. $set: {
  1767. createdThroughApi: '',
  1768. },
  1769. });
  1770. return;
  1771. }
  1772. //invite user to corresponding boards
  1773. const disableRegistration = Settings.findOne().disableRegistration;
  1774. // If ldap, bypass the inviation code if the self registration isn't allowed.
  1775. // TODO : pay attention if ldap field in the user model change to another content ex : ldap field to connection_type
  1776. if (doc.authenticationMethod !== 'ldap' && disableRegistration) {
  1777. let invitationCode = null;
  1778. if(doc.authenticationMethod.toLowerCase() == 'oauth2')
  1779. { // OIDC authentication mode
  1780. invitationCode = InvitationCodes.findOne({
  1781. email: doc.emails[0].address.toLowerCase(),
  1782. valid: true,
  1783. });
  1784. }
  1785. else{
  1786. invitationCode = InvitationCodes.findOne({
  1787. code: doc.profile.icode,
  1788. valid: true,
  1789. });
  1790. }
  1791. if (!invitationCode) {
  1792. throw new Meteor.Error('error-invitation-code-not-exist');
  1793. } else {
  1794. invitationCode.boardsToBeInvited.forEach((boardId) => {
  1795. const board = Boards.findOne(boardId);
  1796. board.addMember(doc._id);
  1797. });
  1798. if (!doc.profile) {
  1799. doc.profile = {};
  1800. }
  1801. doc.profile.invitedBoards = invitationCode.boardsToBeInvited;
  1802. Users.update(doc._id, {
  1803. $set: {
  1804. profile: doc.profile,
  1805. },
  1806. });
  1807. InvitationCodes.update(invitationCode._id, {
  1808. $set: {
  1809. valid: false,
  1810. },
  1811. });
  1812. }
  1813. }
  1814. });
  1815. }
  1816. // USERS REST API
  1817. if (Meteor.isServer) {
  1818. // Middleware which checks that API is enabled.
  1819. JsonRoutes.Middleware.use(function (req, res, next) {
  1820. const api = req.url.startsWith('/api');
  1821. if ((api === true && process.env.WITH_API === 'true') || api === false) {
  1822. return next();
  1823. } else {
  1824. res.writeHead(301, {
  1825. Location: '/',
  1826. });
  1827. return res.end();
  1828. }
  1829. });
  1830. /**
  1831. * @operation get_current_user
  1832. *
  1833. * @summary returns the current user
  1834. * @return_type Users
  1835. */
  1836. JsonRoutes.add('GET', '/api/user', function (req, res) {
  1837. try {
  1838. Authentication.checkLoggedIn(req.userId);
  1839. const data = Meteor.users.findOne({
  1840. _id: req.userId,
  1841. });
  1842. delete data.services;
  1843. // get all boards where the user is member of
  1844. let boards = Boards.find(
  1845. {
  1846. type: 'board',
  1847. 'members.userId': req.userId,
  1848. },
  1849. {
  1850. fields: {
  1851. _id: 1,
  1852. members: 1,
  1853. },
  1854. },
  1855. );
  1856. boards = boards.map((b) => {
  1857. const u = b.members.find((m) => m.userId === req.userId);
  1858. delete u.userId;
  1859. u.boardId = b._id;
  1860. return u;
  1861. });
  1862. data.boards = boards;
  1863. JsonRoutes.sendResult(res, {
  1864. code: 200,
  1865. data,
  1866. });
  1867. } catch (error) {
  1868. JsonRoutes.sendResult(res, {
  1869. code: 200,
  1870. data: error,
  1871. });
  1872. }
  1873. });
  1874. /**
  1875. * @operation get_all_users
  1876. *
  1877. * @summary return all the users
  1878. *
  1879. * @description Only the admin user (the first user) can call the REST API.
  1880. * @return_type [{ _id: string,
  1881. * username: string}]
  1882. */
  1883. JsonRoutes.add('GET', '/api/users', function (req, res) {
  1884. try {
  1885. Authentication.checkUserId(req.userId);
  1886. JsonRoutes.sendResult(res, {
  1887. code: 200,
  1888. data: Meteor.users.find({}).map(function (doc) {
  1889. return {
  1890. _id: doc._id,
  1891. username: doc.username,
  1892. };
  1893. }),
  1894. });
  1895. } catch (error) {
  1896. JsonRoutes.sendResult(res, {
  1897. code: 200,
  1898. data: error,
  1899. });
  1900. }
  1901. });
  1902. /**
  1903. * @operation get_user
  1904. *
  1905. * @summary get a given user
  1906. *
  1907. * @description Only the admin user (the first user) can call the REST API.
  1908. *
  1909. * @param {string} userId the user ID or username
  1910. * @return_type Users
  1911. */
  1912. JsonRoutes.add('GET', '/api/users/:userId', function (req, res) {
  1913. try {
  1914. Authentication.checkUserId(req.userId);
  1915. let id = req.params.userId;
  1916. let user = Meteor.users.findOne({
  1917. _id: id,
  1918. });
  1919. if (!user) {
  1920. user = Meteor.users.findOne({
  1921. username: id,
  1922. });
  1923. id = user._id;
  1924. }
  1925. // get all boards where the user is member of
  1926. let boards = Boards.find(
  1927. {
  1928. type: 'board',
  1929. 'members.userId': id,
  1930. },
  1931. {
  1932. fields: {
  1933. _id: 1,
  1934. members: 1,
  1935. },
  1936. },
  1937. );
  1938. boards = boards.map((b) => {
  1939. const u = b.members.find((m) => m.userId === id);
  1940. delete u.userId;
  1941. u.boardId = b._id;
  1942. return u;
  1943. });
  1944. user.boards = boards;
  1945. JsonRoutes.sendResult(res, {
  1946. code: 200,
  1947. data: user,
  1948. });
  1949. } catch (error) {
  1950. JsonRoutes.sendResult(res, {
  1951. code: 200,
  1952. data: error,
  1953. });
  1954. }
  1955. });
  1956. /**
  1957. * @operation edit_user
  1958. *
  1959. * @summary edit a given user
  1960. *
  1961. * @description Only the admin user (the first user) can call the REST API.
  1962. *
  1963. * Possible values for *action*:
  1964. * - `takeOwnership`: The admin takes the ownership of ALL boards of the user (archived and not archived) where the user is admin on.
  1965. * - `disableLogin`: Disable a user (the user is not allowed to login and his login tokens are purged)
  1966. * - `enableLogin`: Enable a user
  1967. *
  1968. * @param {string} userId the user ID
  1969. * @param {string} action the action
  1970. * @return_type {_id: string,
  1971. * title: string}
  1972. */
  1973. JsonRoutes.add('PUT', '/api/users/:userId', function (req, res) {
  1974. try {
  1975. Authentication.checkUserId(req.userId);
  1976. const id = req.params.userId;
  1977. const action = req.body.action;
  1978. let data = Meteor.users.findOne({
  1979. _id: id,
  1980. });
  1981. if (data !== undefined) {
  1982. if (action === 'takeOwnership') {
  1983. data = Boards.find(
  1984. {
  1985. 'members.userId': id,
  1986. 'members.isAdmin': true,
  1987. },
  1988. {
  1989. sort: {
  1990. sort: 1 /* boards default sorting */,
  1991. },
  1992. },
  1993. ).map(function (board) {
  1994. if (board.hasMember(req.userId)) {
  1995. board.removeMember(req.userId);
  1996. }
  1997. board.changeOwnership(id, req.userId);
  1998. return {
  1999. _id: board._id,
  2000. title: board.title,
  2001. };
  2002. });
  2003. } else {
  2004. if (action === 'disableLogin' && id !== req.userId) {
  2005. Users.update(
  2006. {
  2007. _id: id,
  2008. },
  2009. {
  2010. $set: {
  2011. loginDisabled: true,
  2012. 'services.resume.loginTokens': '',
  2013. },
  2014. },
  2015. );
  2016. } else if (action === 'enableLogin') {
  2017. Users.update(
  2018. {
  2019. _id: id,
  2020. },
  2021. {
  2022. $set: {
  2023. loginDisabled: '',
  2024. },
  2025. },
  2026. );
  2027. }
  2028. data = Meteor.users.findOne({
  2029. _id: id,
  2030. });
  2031. }
  2032. }
  2033. JsonRoutes.sendResult(res, {
  2034. code: 200,
  2035. data,
  2036. });
  2037. } catch (error) {
  2038. JsonRoutes.sendResult(res, {
  2039. code: 200,
  2040. data: error,
  2041. });
  2042. }
  2043. });
  2044. /**
  2045. * @operation add_board_member
  2046. * @tag Boards
  2047. *
  2048. * @summary Add New Board Member with Role
  2049. *
  2050. * @description Only the admin user (the first user) can call the REST API.
  2051. *
  2052. * **Note**: see [Boards.set_board_member_permission](#set_board_member_permission)
  2053. * to later change the permissions.
  2054. *
  2055. * @param {string} boardId the board ID
  2056. * @param {string} userId the user ID
  2057. * @param {boolean} isAdmin is the user an admin of the board
  2058. * @param {boolean} isNoComments disable comments
  2059. * @param {boolean} isCommentOnly only enable comments
  2060. * @return_type {_id: string,
  2061. * title: string}
  2062. */
  2063. JsonRoutes.add(
  2064. 'POST',
  2065. '/api/boards/:boardId/members/:userId/add',
  2066. function (req, res) {
  2067. try {
  2068. Authentication.checkUserId(req.userId);
  2069. const userId = req.params.userId;
  2070. const boardId = req.params.boardId;
  2071. const action = req.body.action;
  2072. const { isAdmin, isNoComments, isCommentOnly } = req.body;
  2073. let data = Meteor.users.findOne({
  2074. _id: userId,
  2075. });
  2076. if (data !== undefined) {
  2077. if (action === 'add') {
  2078. data = Boards.find({
  2079. _id: boardId,
  2080. }).map(function (board) {
  2081. if (!board.hasMember(userId)) {
  2082. board.addMember(userId);
  2083. function isTrue(data) {
  2084. return data.toLowerCase() === 'true';
  2085. }
  2086. board.setMemberPermission(
  2087. userId,
  2088. isTrue(isAdmin),
  2089. isTrue(isNoComments),
  2090. isTrue(isCommentOnly),
  2091. userId,
  2092. );
  2093. }
  2094. return {
  2095. _id: board._id,
  2096. title: board.title,
  2097. };
  2098. });
  2099. }
  2100. }
  2101. JsonRoutes.sendResult(res, {
  2102. code: 200,
  2103. data: query,
  2104. });
  2105. } catch (error) {
  2106. JsonRoutes.sendResult(res, {
  2107. code: 200,
  2108. data: error,
  2109. });
  2110. }
  2111. },
  2112. );
  2113. /**
  2114. * @operation remove_board_member
  2115. * @tag Boards
  2116. *
  2117. * @summary Remove Member from Board
  2118. *
  2119. * @description Only the admin user (the first user) can call the REST API.
  2120. *
  2121. * @param {string} boardId the board ID
  2122. * @param {string} userId the user ID
  2123. * @param {string} action the action (needs to be `remove`)
  2124. * @return_type {_id: string,
  2125. * title: string}
  2126. */
  2127. JsonRoutes.add(
  2128. 'POST',
  2129. '/api/boards/:boardId/members/:userId/remove',
  2130. function (req, res) {
  2131. try {
  2132. Authentication.checkUserId(req.userId);
  2133. const userId = req.params.userId;
  2134. const boardId = req.params.boardId;
  2135. const action = req.body.action;
  2136. let data = Meteor.users.findOne({
  2137. _id: userId,
  2138. });
  2139. if (data !== undefined) {
  2140. if (action === 'remove') {
  2141. data = Boards.find({
  2142. _id: boardId,
  2143. }).map(function (board) {
  2144. if (board.hasMember(userId)) {
  2145. board.removeMember(userId);
  2146. }
  2147. return {
  2148. _id: board._id,
  2149. title: board.title,
  2150. };
  2151. });
  2152. }
  2153. }
  2154. JsonRoutes.sendResult(res, {
  2155. code: 200,
  2156. data: query,
  2157. });
  2158. } catch (error) {
  2159. JsonRoutes.sendResult(res, {
  2160. code: 200,
  2161. data: error,
  2162. });
  2163. }
  2164. },
  2165. );
  2166. /**
  2167. * @operation new_user
  2168. *
  2169. * @summary Create a new user
  2170. *
  2171. * @description Only the admin user (the first user) can call the REST API.
  2172. *
  2173. * @param {string} username the new username
  2174. * @param {string} email the email of the new user
  2175. * @param {string} password the password of the new user
  2176. * @return_type {_id: string}
  2177. */
  2178. JsonRoutes.add('POST', '/api/users/', function (req, res) {
  2179. try {
  2180. Authentication.checkUserId(req.userId);
  2181. const id = Accounts.createUser({
  2182. username: req.body.username,
  2183. email: req.body.email,
  2184. password: req.body.password,
  2185. from: 'admin',
  2186. });
  2187. JsonRoutes.sendResult(res, {
  2188. code: 200,
  2189. data: {
  2190. _id: id,
  2191. },
  2192. });
  2193. } catch (error) {
  2194. JsonRoutes.sendResult(res, {
  2195. code: 200,
  2196. data: error,
  2197. });
  2198. }
  2199. });
  2200. /**
  2201. * @operation delete_user
  2202. *
  2203. * @summary Delete a user
  2204. *
  2205. * @description Only the admin user (the first user) can call the REST API.
  2206. *
  2207. * @param {string} userId the ID of the user to delete
  2208. * @return_type {_id: string}
  2209. */
  2210. JsonRoutes.add('DELETE', '/api/users/:userId', function (req, res) {
  2211. try {
  2212. Authentication.checkUserId(req.userId);
  2213. const id = req.params.userId;
  2214. // Delete user is enabled, but is still has bug of leaving empty user avatars
  2215. // to boards: boards members, card members and assignees have
  2216. // empty users. So it would be better to delete user from all boards before
  2217. // deleting user.
  2218. // See:
  2219. // - wekan/client/components/settings/peopleBody.jade deleteButton
  2220. // - wekan/client/components/settings/peopleBody.js deleteButton
  2221. // - wekan/client/components/sidebar/sidebar.js Popup.afterConfirm('removeMember'
  2222. // that does now remove member from board, card members and assignees correctly,
  2223. // but that should be used to remove user from all boards similarly
  2224. // - wekan/models/users.js Delete is not enabled
  2225. Meteor.users.remove({ _id: id });
  2226. JsonRoutes.sendResult(res, {
  2227. code: 200,
  2228. data: {
  2229. _id: id,
  2230. },
  2231. });
  2232. } catch (error) {
  2233. JsonRoutes.sendResult(res, {
  2234. code: 200,
  2235. data: error,
  2236. });
  2237. }
  2238. });
  2239. /**
  2240. * @operation create_user_token
  2241. *
  2242. * @summary Create a user token
  2243. *
  2244. * @description Only the admin user (the first user) can call the REST API.
  2245. *
  2246. * @param {string} userId the ID of the user to create token for.
  2247. * @return_type {_id: string}
  2248. */
  2249. JsonRoutes.add('POST', '/api/createtoken/:userId', function (req, res) {
  2250. try {
  2251. Authentication.checkUserId(req.userId);
  2252. const id = req.params.userId;
  2253. const token = Accounts._generateStampedLoginToken();
  2254. Accounts._insertLoginToken(id, token);
  2255. JsonRoutes.sendResult(res, {
  2256. code: 200,
  2257. data: {
  2258. _id: id,
  2259. authToken: token.token,
  2260. },
  2261. });
  2262. } catch (error) {
  2263. JsonRoutes.sendResult(res, {
  2264. code: 200,
  2265. data: error,
  2266. });
  2267. }
  2268. });
  2269. }
  2270. export default Users;