| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430 | //var nodemailer = require('nodemailer');import { SyncedCron } from 'meteor/percolate:synced-cron';import { TAPi18n } from '/imports/i18n';import ImpersonatedUsers from './impersonatedUsers';import { Index, MongoDBEngine } from 'meteor/easy:search'// Sandstorm context is detected using the METEOR_SETTINGS environment variable// in the package definition.const isSandstorm =  Meteor.settings && Meteor.settings.public && Meteor.settings.public.sandstorm;Users = Meteor.users;const allowedSortValues = [  '-modifiedAt',  'modifiedAt',  '-title',  'title',  '-sort',  'sort',];const defaultSortBy = allowedSortValues[0];/** * A User in wekan */Users.attachSchema(  new SimpleSchema({    username: {      /**       * the username of the user       */      type: String,      optional: true,      // eslint-disable-next-line consistent-return      autoValue() {        if (this.isInsert && !this.isSet) {          const name = this.field('profile.fullname');          if (name.isSet) {            return name.value.toLowerCase().replace(/\s/g, '');          }        }      },    },    orgs: {      /**       * the list of organizations that a user belongs to       */       type: [Object],       optional: true,    },    'orgs.$.orgId':{      /**       * The uniq ID of the organization       */       type: String,    },    'orgs.$.orgDisplayName':{      /**       * The display name of the organization       */       type: String,    },    teams: {      /**       * the list of teams that a user belongs to       */       type: [Object],       optional: true,    },    'teams.$.teamId':{      /**       * The uniq ID of the team       */       type: String,    },    'teams.$.teamDisplayName':{      /**       * The display name of the team       */       type: String,    },    emails: {      /**       * the list of emails attached to a user       */      type: [Object],      optional: true,    },    'emails.$.address': {      /**       * The email address       */      type: String,      regEx: SimpleSchema.RegEx.Email,    },    'emails.$.verified': {      /**       * Has the email been verified       */      type: Boolean,    },    createdAt: {      /**       * creation date of the user       */      type: Date,      // eslint-disable-next-line consistent-return      autoValue() {        if (this.isInsert) {          return new Date();        } else if (this.isUpsert) {          return {            $setOnInsert: new Date(),          };        } else {          this.unset();        }      },    },    modifiedAt: {      type: Date,      denyUpdate: false,      // eslint-disable-next-line consistent-return      autoValue() {        if (this.isInsert || this.isUpsert || this.isUpdate) {          return new Date();        } else {          this.unset();        }      },    },    profile: {      /**       * profile settings       */      type: Object,      optional: true,      // eslint-disable-next-line consistent-return      autoValue() {        if (this.isInsert && !this.isSet) {          return {            boardView: 'board-view-swimlanes',          };        }      },    },    'profile.avatarUrl': {      /**       * URL of the avatar of the user       */      type: String,      optional: true,    },    'profile.emailBuffer': {      /**       * list of email buffers of the user       */      type: [String],      optional: true,    },    'profile.fullname': {      /**       * full name of the user       */      type: String,      optional: true,    },    'profile.showDesktopDragHandles': {      /**       * does the user want to show desktop drag handles?       */      type: Boolean,      optional: true,    },    'profile.hideCheckedItems': {      /**       * does the user want to hide checked checklist items?       */      type: Boolean,      optional: true,    },    'profile.cardMaximized': {      /**       * has user clicked maximize card?       */      type: Boolean,      optional: true,    },    'profile.customFieldsGrid': {      /**       * has user at card Custom Fields have Grid (false) or one per row (true) layout?       */      type: Boolean,      optional: true,    },    'profile.hiddenSystemMessages': {      /**       * does the user want to hide system messages?       */      type: Boolean,      optional: true,    },    'profile.hiddenMinicardLabelText': {      /**       * does the user want to hide minicard label texts?       */      type: Boolean,      optional: true,    },    'profile.initials': {      /**       * initials of the user       */      type: String,      optional: true,    },    'profile.invitedBoards': {      /**       * board IDs the user has been invited to       */      type: [String],      optional: true,    },    'profile.language': {      /**       * language of the user       */      type: String,      optional: true,    },    'profile.moveAndCopyDialog' : {      /**       * move and copy card dialog       */      type: Object,      optional: true,      blackbox: true,    },    'profile.moveAndCopyDialog.$.boardId': {      /**       * last selected board id       */      type: String,    },    'profile.moveAndCopyDialog.$.swimlaneId': {      /**       * last selected swimlane id       */      type: String,    },    'profile.moveAndCopyDialog.$.listId': {      /**       * last selected list id       */      type: String,    },    'profile.moveChecklistDialog' : {      /**       * move checklist dialog       */      type: Object,      optional: true,      blackbox: true,    },    'profile.moveChecklistDialog.$.boardId': {      /**       * last selected board id       */      type: String,    },    'profile.moveChecklistDialog.$.swimlaneId': {      /**       * last selected swimlane id       */      type: String,    },    'profile.moveChecklistDialog.$.listId': {      /**       * last selected list id       */      type: String,    },    'profile.moveChecklistDialog.$.cardId': {      /**       * last selected card id       */      type: String,    },    'profile.copyChecklistDialog' : {      /**       * copy checklist dialog       */      type: Object,      optional: true,      blackbox: true,    },    'profile.copyChecklistDialog.$.boardId': {      /**       * last selected board id       */      type: String,    },    'profile.copyChecklistDialog.$.swimlaneId': {      /**       * last selected swimlane id       */      type: String,    },    'profile.copyChecklistDialog.$.listId': {      /**       * last selected list id       */      type: String,    },    'profile.copyChecklistDialog.$.cardId': {      /**       * last selected card id       */      type: String,    },    'profile.notifications': {      /**       * enabled notifications for the user       */      type: [Object],      optional: true,    },    'profile.notifications.$.activity': {      /**       * The id of the activity this notification references       */      type: String,    },    'profile.notifications.$.read': {      /**       * the date on which this notification was read       */      type: Date,      optional: true,    },    'profile.rescueCardDescription': {      /**       * show dialog for saving card description on unintentional card closing       */      type: Boolean,      optional: true,    },    'profile.showCardsCountAt': {      /**       * showCardCountAt field of the user       */      type: Number,      optional: true,    },    'profile.startDayOfWeek': {      /**       * startDayOfWeek field of the user       */      type: Number,      optional: true,    },    'profile.starredBoards': {      /**       * list of starred board IDs       */      type: [String],      optional: true,    },    'profile.icode': {      /**       * icode       */      type: String,      optional: true,    },    'profile.boardView': {      /**       * boardView field of the user       */      type: String,      optional: true,      allowedValues: [        'board-view-swimlanes',        'board-view-lists',        'board-view-cal',      ],    },    'profile.listSortBy': {      /**       * default sort list for user       */      type: String,      optional: true,      defaultValue: defaultSortBy,      allowedValues: allowedSortValues,    },    'profile.templatesBoardId': {      /**       * Reference to the templates board       */      type: String,      defaultValue: '',    },    'profile.cardTemplatesSwimlaneId': {      /**       * Reference to the card templates swimlane Id       */      type: String,      defaultValue: '',    },    'profile.listTemplatesSwimlaneId': {      /**       * Reference to the list templates swimlane Id       */      type: String,      defaultValue: '',    },    'profile.boardTemplatesSwimlaneId': {      /**       * Reference to the board templates swimlane Id       */      type: String,      defaultValue: '',    },    services: {      /**       * services field of the user       */      type: Object,      optional: true,      blackbox: true,    },    heartbeat: {      /**       * last time the user has been seen       */      type: Date,      optional: true,    },    isAdmin: {      /**       * is the user an admin of the board?       */      type: Boolean,      optional: true,    },    createdThroughApi: {      /**       * was the user created through the API?       */      type: Boolean,      optional: true,    },    loginDisabled: {      /**       * loginDisabled field of the user       */      type: Boolean,      optional: true,    },    authenticationMethod: {      /**       * authentication method of the user       */      type: String,      optional: false,      defaultValue: 'password',    },    sessionData: {      /**       * profile settings       */      type: Object,      optional: true,      // eslint-disable-next-line consistent-return      autoValue() {        if (this.isInsert && !this.isSet) {          return {};        }      },    },    'sessionData.totalHits': {      /**       * Total hits from last searchquery['members.userId'] = Meteor.userId();       * last hit that was returned       */      type: Number,      optional: true,    },    importUsernames: {      /**       * username for imported       */      type: [String],      optional: true,    },  }),);Users.allow({  update(userId, doc) {    const user = Users.findOne({      _id: userId,    });    if ((user && user.isAdmin) || (Meteor.user() && Meteor.user().isAdmin))      return true;    if (!user) {      return false;    }    return doc._id === userId;  },  remove(userId, doc) {    const adminsNumber = Users.find({      isAdmin: true,    }).count();    const { isAdmin } = Users.findOne(      {        _id: userId,      },      {        fields: {          isAdmin: 1,        },      },    );    // Prevents remove of the only one administrator    if (adminsNumber === 1 && isAdmin && userId === doc._id) {      return false;    }    // If it's the user or an admin    return userId === doc._id || isAdmin;  },  fetch: [],});// Search a user in the complete server database by its name, username or emails adress. This// is used for instance to add a new user to a board.UserSearchIndex = new Index({  collection: Users,  fields: ['username', 'profile.fullname', 'profile.avatarUrl'],  allowedFields: ['username', 'profile.fullname', 'profile.avatarUrl'],  engine: new MongoDBEngine({    fields: function(searchObject, options) {      return {        'username': 1,        'profile.fullname': 1,        'profile.avatarUrl': 1      };    }  }),});Users.safeFields = {  _id: 1,  username: 1,  'profile.fullname': 1,  'profile.avatarUrl': 1,  'profile.initials': 1,  orgs: 1,  teams: 1,  authenticationMethod: 1,};if (Meteor.isClient) {  Users.helpers({    isBoardMember() {      const board = Utils.getCurrentBoard();      return board && board.hasMember(this._id);    },    isNotNoComments() {      const board = Utils.getCurrentBoard();      return (        board && board.hasMember(this._id) && !board.hasNoComments(this._id)      );    },    isNoComments() {      const board = Utils.getCurrentBoard();      return board && board.hasNoComments(this._id);    },    isNotCommentOnly() {      const board = Utils.getCurrentBoard();      return (        board && board.hasMember(this._id) && !board.hasCommentOnly(this._id)      );    },    isCommentOnly() {      const board = Utils.getCurrentBoard();      return board && board.hasCommentOnly(this._id);    },    isNotWorker() {      const board = Utils.getCurrentBoard();      return board && board.hasMember(this._id) && !board.hasWorker(this._id);    },    isWorker() {      const board = Utils.getCurrentBoard();      return board && board.hasWorker(this._id);    },    isBoardAdmin(boardId) {      let board;      if (boardId) {        board = Boards.findOne(boardId);      } else {        board = Utils.getCurrentBoard();      }      return board && board.hasAdmin(this._id);    },  });}Users.parseImportUsernames = (usernamesString) => {  return usernamesString.trim().split(new RegExp('\\s*[,;]\\s*'));};Users.helpers({  importUsernamesString() {    if (this.importUsernames) {      return this.importUsernames.join(', ');    }    return '';  },  teamIds() {    if (this.teams) {      // TODO: Should the Team collection be queried to determine if the team isActive?      return this.teams.map(team => { return team.teamId });    }    return [];  },  orgIds() {    if (this.orgs) {      // TODO: Should the Org collection be queried to determine if the organization isActive?      return this.orgs.map(org => { return org.orgId });    }    return [];  },  orgsUserBelongs() {    if (this.orgs) {      return this.orgs.map(function(org){return org.orgDisplayName}).sort().join(',');    }    return '';  },  orgIdsUserBelongs() {    if (this.orgs) {      return this.orgs.map(function(org){return org.orgId}).join(',');    }    return '';  },  teamsUserBelongs() {    if (this.teams) {      return this.teams.map(function(team){ return team.teamDisplayName}).sort().join(',');    }    return '';  },  teamIdsUserBelongs() {    if (this.teams) {      return this.teams.map(function(team){ return team.teamId}).join(',');    }    return '';  },  boards() {    return Boards.userBoards(this._id, null, {}, { sort: { sort: 1 } })  },  starredBoards() {    const { starredBoards = [] } = this.profile || {};    return Boards.userBoards(      this._id,      false,      { _id: { $in: starredBoards } },      { sort: { sort: 1 } }    );  },  hasStarred(boardId) {    const { starredBoards = [] } = this.profile || {};    return _.contains(starredBoards, boardId);  },  invitedBoards() {    const { invitedBoards = [] } = this.profile || {};    return Boards.userBoards(      this._id,      false,      { _id: { $in: invitedBoards } },      { sort: { sort: 1 } }    );  },  isInvitedTo(boardId) {    const { invitedBoards = [] } = this.profile || {};    return _.contains(invitedBoards, boardId);  },  _getListSortBy() {    const profile = this.profile || {};    const sortBy = profile.listSortBy || defaultSortBy;    const keyPattern = /^(-{0,1})(.*$)/;    const ret = [];    if (keyPattern.exec(sortBy)) {      ret[0] = RegExp.$2;      ret[1] = RegExp.$1 ? -1 : 1;    }    return ret;  },  hasSortBy() {    // if use doesn't have dragHandle, then we can let user to choose sort list by different order    return !this.hasShowDesktopDragHandles();  },  getListSortBy() {    return this._getListSortBy()[0];  },  getListSortTypes() {    return allowedSortValues;  },  getListSortByDirection() {    return this._getListSortBy()[1];  },  /** returns all confirmed move and copy dialog field values   * <li> the board, swimlane and list id is stored for each board   */  getMoveAndCopyDialogOptions() {    let _ret = {}    if (this.profile && this.profile.moveAndCopyDialog) {      _ret = this.profile.moveAndCopyDialog;    }    return _ret;  },  /** returns all confirmed move checklist dialog field values   * <li> the board, swimlane, list and card id is stored for each board   */  getMoveChecklistDialogOptions() {    let _ret = {}    if (this.profile && this.profile.moveChecklistDialog) {      _ret = this.profile.moveChecklistDialog;    }    return _ret;  },  /** returns all confirmed copy checklist dialog field values   * <li> the board, swimlane, list and card id is stored for each board   */  getCopyChecklistDialogOptions() {    let _ret = {}    if (this.profile && this.profile.copyChecklistDialog) {      _ret = this.profile.copyChecklistDialog;    }    return _ret;  },  hasTag(tag) {    const { tags = [] } = this.profile || {};    return _.contains(tags, tag);  },  hasNotification(activityId) {    const { notifications = [] } = this.profile || {};    return _.contains(notifications, activityId);  },  notifications() {    const { notifications = [] } = this.profile || {};    for (const index in notifications) {      if (!notifications.hasOwnProperty(index)) continue;      const notification = notifications[index];      // this preserves their db sort order for editing      notification.dbIndex = index;      notification.activity = Activities.findOne(notification.activity);    }    // this sorts them newest to oldest to match Trello's behavior    notifications.reverse();    return notifications;  },  hasShowDesktopDragHandles() {    const profile = this.profile || {};    return profile.showDesktopDragHandles || false;  },  hasHideCheckedItems() {    const profile = this.profile || {};    return profile.hideCheckedItems || false;  },  hasHiddenSystemMessages() {    const profile = this.profile || {};    return profile.hiddenSystemMessages || false;  },  hasCustomFieldsGrid() {    const profile = this.profile || {};    return profile.customFieldsGrid || false;  },  hasCardMaximized() {    const profile = this.profile || {};    return profile.cardMaximized || false;  },  hasHiddenMinicardLabelText() {    const profile = this.profile || {};    return profile.hiddenMinicardLabelText || false;  },  hasRescuedCardDescription(){    const profile = this.profile || {};    return profile.rescueCardDescription || false;  },  getEmailBuffer() {    const { emailBuffer = [] } = this.profile || {};    return emailBuffer;  },  getInitials() {    const profile = this.profile || {};    if (profile.initials) return profile.initials;    else if (profile.fullname) {      return profile.fullname        .split(/\s+/)        .reduce((memo, word) => {          return memo + word[0];        }, '')        .toUpperCase();    } else {      return this.username[0].toUpperCase();    }  },  getLimitToShowCardsCount() {    const profile = this.profile || {};    return profile.showCardsCountAt;  },  getName() {    const profile = this.profile || {};    return profile.fullname || this.username;  },  getLanguage() {    const profile = this.profile || {};    return profile.language || 'en';  },  getStartDayOfWeek() {    const profile = this.profile || {};    if (typeof profile.startDayOfWeek === 'undefined') {      // default is 'Monday' (1)      return 1;    }    return profile.startDayOfWeek;  },  getTemplatesBoardId() {    return (this.profile || {}).templatesBoardId;  },  getTemplatesBoardSlug() {    //return (Boards.findOne((this.profile || {}).templatesBoardId) || {}).slug;    return 'templates';  },  remove() {    User.remove({      _id: this._id,    });  },});Users.mutations({  /** set the confirmed board id/swimlane id/list id of a board   * @param boardId the current board id   * @param options an object with the confirmed field values   */  setMoveAndCopyDialogOption(boardId, options) {    let currentOptions = this.getMoveAndCopyDialogOptions();    currentOptions[boardId] = options;    return {      $set: {        'profile.moveAndCopyDialog': currentOptions,      },    };  },  /** set the confirmed board id/swimlane id/list id/card id of a board (move checklist)   * @param boardId the current board id   * @param options an object with the confirmed field values   */  setMoveChecklistDialogOption(boardId, options) {    let currentOptions = this.getMoveChecklistDialogOptions();    currentOptions[boardId] = options;    return {      $set: {        'profile.moveChecklistDialog': currentOptions,      },    };  },  /** set the confirmed board id/swimlane id/list id/card id of a board (copy checklist)   * @param boardId the current board id   * @param options an object with the confirmed field values   */  setCopyChecklistDialogOption(boardId, options) {    let currentOptions = this.getCopyChecklistDialogOptions();    currentOptions[boardId] = options;    return {      $set: {        'profile.copyChecklistDialog': currentOptions,      },    };  },  toggleBoardStar(boardId) {    const queryKind = this.hasStarred(boardId) ? '$pull' : '$addToSet';    return {      [queryKind]: {        'profile.starredBoards': boardId,      },    };  },  addInvite(boardId) {    return {      $addToSet: {        'profile.invitedBoards': boardId,      },    };  },  removeInvite(boardId) {    return {      $pull: {        'profile.invitedBoards': boardId,      },    };  },  addTag(tag) {    return {      $addToSet: {        'profile.tags': tag,      },    };  },  removeTag(tag) {    return {      $pull: {        'profile.tags': tag,      },    };  },  toggleTag(tag) {    if (this.hasTag(tag)) this.removeTag(tag);    else this.addTag(tag);  },  setListSortBy(value) {    return {      $set: {        'profile.listSortBy': value,      },    };  },  setName(value) {    return {      $set: {        'profile.fullname': value,      },    };  },  toggleDesktopHandles(value = false) {    return {      $set: {        'profile.showDesktopDragHandles': !value,      },    };  },  toggleHideCheckedItems() {    const value = this.hasHideCheckedItems();    return {      $set: {        'profile.hideCheckedItems': !value,      },    };  },  toggleSystem(value = false) {    return {      $set: {        'profile.hiddenSystemMessages': !value,      },    };  },  toggleFieldsGrid(value = false) {    return {      $set: {        'profile.customFieldsGrid': !value,      },    };  },  toggleCardMaximized(value = false) {    return {      $set: {        'profile.cardMaximized': !value,      },    };  },  toggleLabelText(value = false) {    return {      $set: {        'profile.hiddenMinicardLabelText': !value,      },    };  },  toggleRescueCardDescription(value = false) {    return {      $set: {        'profile.rescueCardDescription': !value,      },    };  },  addNotification(activityId) {    return {      $addToSet: {        'profile.notifications': {          activity: activityId,        },      },    };  },  removeNotification(activityId) {    return {      $pull: {        'profile.notifications': {          activity: activityId,        },      },    };  },  addEmailBuffer(text) {    return {      $addToSet: {        'profile.emailBuffer': text,      },    };  },  clearEmailBuffer() {    return {      $set: {        'profile.emailBuffer': [],      },    };  },  setAvatarUrl(avatarUrl) {    return {      $set: {        'profile.avatarUrl': avatarUrl,      },    };  },  setShowCardsCountAt(limit) {    return {      $set: {        'profile.showCardsCountAt': limit,      },    };  },  setStartDayOfWeek(startDay) {    return {      $set: {        'profile.startDayOfWeek': startDay,      },    };  },  setBoardView(view) {    return {      $set: {        'profile.boardView': view,      },    };  },});Meteor.methods({  setListSortBy(value) {    check(value, String);    Meteor.user().setListSortBy(value);  },  toggleDesktopDragHandles() {    const user = Meteor.user();    user.toggleDesktopHandles(user.hasShowDesktopDragHandles());  },  toggleHideCheckedItems() {    const user = Meteor.user();    user.toggleHideCheckedItems();  },  toggleSystemMessages() {    const user = Meteor.user();    user.toggleSystem(user.hasHiddenSystemMessages());  },  toggleCustomFieldsGrid() {    const user = Meteor.user();    user.toggleFieldsGrid(user.hasCustomFieldsGrid());  },  toggleCardMaximized() {    const user = Meteor.user();    user.toggleCardMaximized(user.hasCardMaximized());  },  toggleMinicardLabelText() {    const user = Meteor.user();    user.toggleLabelText(user.hasHiddenMinicardLabelText());  },  toggleRescueCardDescription() {    const user = Meteor.user();    user.toggleRescueCardDescription(user.hasRescuedCardDescription());  },  changeLimitToShowCardsCount(limit) {    check(limit, Number);    Meteor.user().setShowCardsCountAt(limit);  },  changeStartDayOfWeek(startDay) {    check(startDay, Number);    Meteor.user().setStartDayOfWeek(startDay);  },});if (Meteor.isServer) {  Meteor.methods({    setAllUsersHideSystemMessages() {      if (Meteor.user() && Meteor.user().isAdmin) {        // If setting is missing, add it        Users.update(          {            'profile.hiddenSystemMessages': {              $exists: false,            },          },          {            $set: {              'profile.hiddenSystemMessages': true,            },          },          {            multi: true,          },        );        // If setting is false, set it to true        Users.update(          {            'profile.hiddenSystemMessages': false,          },          {            $set: {              'profile.hiddenSystemMessages': true,            },          },          {            multi: true,          },        );        return true;      } else {        return false;      }    },    setCreateUser(      fullname,      username,      initials,      password,      isAdmin,      isActive,      email,      importUsernames,      userOrgsArray,      userTeamsArray,    ) {      check(fullname, String);      check(username, String);      check(initials, String);      check(password, String);      check(isAdmin, String);      check(isActive, String);      check(email, String);      check(importUsernames, Array);      check(userOrgsArray, Array);      check(userTeamsArray, Array);      if (Meteor.user() && Meteor.user().isAdmin) {        const nUsersWithUsername = Users.find({          username,        }).count();        const nUsersWithEmail = Users.find({          email,        }).count();        if (nUsersWithUsername > 0) {          throw new Meteor.Error('username-already-taken');        } else if (nUsersWithEmail > 0) {          throw new Meteor.Error('email-already-taken');        } else {          Accounts.createUser({            username,            password,            isAdmin,            isActive,            email: email.toLowerCase(),            from: 'admin',          });          const user =            Users.findOne(username) ||            Users.findOne({              username,            });          if (user) {            Users.update(user._id, {              $set: {                'profile.fullname': fullname,                importUsernames,                'profile.initials': initials,                orgs: userOrgsArray,                teams: userTeamsArray,              },            });          }        }      }    },    setUsername(username, userId) {      check(username, String);      check(userId, String);      if (Meteor.user() && Meteor.user().isAdmin) {        const nUsersWithUsername = Users.find({          username,        }).count();        if (nUsersWithUsername > 0) {          throw new Meteor.Error('username-already-taken');        } else {          Users.update(userId, {            $set: {              username,            },          });        }      }    },    setEmail(email, userId) {      check(email, String);      check(username, String);      if (Meteor.user() && Meteor.user().isAdmin) {        if (Array.isArray(email)) {          email = email.shift();        }        const existingUser = Users.findOne(          {            'emails.address': email,          },          {            fields: {              _id: 1,            },          },        );        if (existingUser) {          throw new Meteor.Error('email-already-taken');        } else {          Users.update(userId, {            $set: {              emails: [                {                  address: email,                  verified: false,                },              ],            },          });        }      }    },    setUsernameAndEmail(username, email, userId) {      check(username, String);      check(email, String);      check(userId, String);      if (Meteor.user() && Meteor.user().isAdmin) {        if (Array.isArray(email)) {          email = email.shift();        }        Meteor.call('setUsername', username, userId);        Meteor.call('setEmail', email, userId);      }    },    setPassword(newPassword, userId) {      check(userId, String);      check(newPassword, String);      if (Meteor.user() && Meteor.user().isAdmin) {        if (Meteor.user().isAdmin) {          Accounts.setPassword(userId, newPassword);        }      }    },    setEmailVerified(email, verified, userId) {      check(email, String);      check(verified, Boolean);      check(userId, String);      if (Meteor.user() && Meteor.user().isAdmin) {        Users.update(userId, {          $set: {            emails: [              {                address: email,                verified,              },            ],          },        });      }    },    setInitials(initials, userId) {      check(initials, String);      check(userId, String);      if (Meteor.user() && Meteor.user().isAdmin) {        Users.update(userId, {          $set: {            'profile.initials': initials,          },        });      }    },    // we accept userId, username, email    inviteUserToBoard(username, boardId) {      check(username, String);      check(boardId, String);      const inviter = Meteor.user();      const board = Boards.findOne(boardId);      const allowInvite =        inviter &&        board &&        board.members &&        _.contains(_.pluck(board.members, 'userId'), inviter._id) &&        _.where(board.members, {          userId: inviter._id,        })[0].isActive;      // GitHub issue 2060      //_.where(board.members, { userId: inviter._id })[0].isAdmin;      if (!allowInvite) throw new Meteor.Error('error-board-notAMember');      this.unblock();      const posAt = username.indexOf('@');      let user = null;      if (posAt >= 0) {        user = Users.findOne({          emails: {            $elemMatch: {              address: username,            },          },        });      } else {        user =          Users.findOne(username) ||          Users.findOne({            username,          });      }      if (user) {        if (user._id === inviter._id)          throw new Meteor.Error('error-user-notAllowSelf');      } else {        if (posAt <= 0) throw new Meteor.Error('error-user-doesNotExist');        if (          Settings.findOne({            disableRegistration: true,          })        ) {          throw new Meteor.Error('error-user-notCreated');        }        // Set in lowercase email before creating account        const email = username.toLowerCase();        username = email.substring(0, posAt);        const newUserId = Accounts.createUser({          username,          email,        });        if (!newUserId) throw new Meteor.Error('error-user-notCreated');        // assume new user speak same language with inviter        if (inviter.profile && inviter.profile.language) {          Users.update(newUserId, {            $set: {              'profile.language': inviter.profile.language,            },          });        }        Accounts.sendEnrollmentEmail(newUserId);        user = Users.findOne(newUserId);      }      board.addMember(user._id);      user.addInvite(boardId);      //Check if there is a subtasks board      if (board.subtasksDefaultBoardId) {        const subBoard = Boards.findOne(board.subtasksDefaultBoardId);        //If there is, also add user to that board        if (subBoard) {          subBoard.addMember(user._id);          user.addInvite(subBoard._id);        }      }      try {        const fullName = inviter.profile !== undefined && inviter.profile.fullname !== undefined ?  inviter.profile.fullname : "";        const userFullName = user.profile !== undefined && user.profile.fullname !== undefined ?  user.profile.fullname : "";        const params = {          user: userFullName != "" ? userFullName + " (" + user.username + " )" : user.username,          inviter: fullName != "" ? fullName + " (" + inviter.username + " )" : inviter.username,          board: board.title,          url: board.absoluteUrl(),        };        const lang = user.getLanguage();/*        if (process.env.MAIL_SERVICE !== '') {          let transporter = nodemailer.createTransport({            service: process.env.MAIL_SERVICE,            auth: {              user: process.env.MAIL_SERVICE_USER,              pass: process.env.MAIL_SERVICE_PASSWORD            },          })          let info = transporter.sendMail({            to: user.emails[0].address.toLowerCase(),            from: Accounts.emailTemplates.from,            subject: TAPi18n.__('email-invite-subject', params, lang),            text: TAPi18n.__('email-invite-text', params, lang),          })        } else {          Email.send({            to: user.emails[0].address.toLowerCase(),            from: Accounts.emailTemplates.from,            subject: TAPi18n.__('email-invite-subject', params, lang),            text: TAPi18n.__('email-invite-text', params, lang),          });        }*/        Email.send({          to: user.emails[0].address.toLowerCase(),          from: Accounts.emailTemplates.from,          subject: TAPi18n.__('email-invite-subject', params, lang),          text: TAPi18n.__('email-invite-text', params, lang),        });      } catch (e) {        throw new Meteor.Error('email-fail', e.message);      }      return {        username: user.username,        email: user.emails[0].address,      };    },    impersonate(userId) {      check(userId, String);      if (!Meteor.users.findOne(userId))        throw new Meteor.Error(404, 'User not found');      if (!Meteor.user().isAdmin)        throw new Meteor.Error(403, 'Permission denied');      ImpersonatedUsers.insert({ adminId: Meteor.user()._id, userId: userId, reason: 'clickedImpersonate' });      this.setUserId(userId);    },    isImpersonated(userId) {      check(userId, String);      const isImpersonated = ImpersonatedUsers.findOne({        userId: userId,      });      return isImpersonated;    },    setUsersTeamsTeamDisplayName(teamId, teamDisplayName) {      check(teamId, String);      check(teamDisplayName, String);      if (Meteor.user() && Meteor.user().isAdmin) {        Users.find({          teams: {              $elemMatch: {teamId: teamId}          }        }).forEach(user => {          Users.update({            _id: user._id,            teams: {              $elemMatch: {teamId: teamId}            }          }, {            $set: {              'teams.$.teamDisplayName': teamDisplayName            }          });        });      }    },    setUsersOrgsOrgDisplayName(orgId, orgDisplayName) {      check(orgId, String);      check(orgDisplayName, String);      if (Meteor.user() && Meteor.user().isAdmin) {        Users.find({          orgs: {              $elemMatch: {orgId: orgId}          }        }).forEach(user => {          Users.update({            _id: user._id,            orgs: {              $elemMatch: {orgId: orgId}            }          }, {            $set: {              'orgs.$.orgDisplayName': orgDisplayName            }          });        });      }    },  });  Accounts.onCreateUser((options, user) => {    const userCount = Users.find().count();    if (userCount === 0) {      user.isAdmin = true;    }    if (user.services.oidc) {      let email = user.services.oidc.email;      if (Array.isArray(email)) {        email = email.shift();      }      email = email.toLowerCase();      user.username = user.services.oidc.username;      user.emails = [        {          address: email,          verified: true,        },      ];      const initials = user.services.oidc.fullname        .split(/\s+/)        .reduce((memo, word) => {          return memo + word[0];        }, '')        .toUpperCase();      user.profile = {        initials,        fullname: user.services.oidc.fullname,        boardView: 'board-view-swimlanes',      };      user.authenticationMethod = 'oauth2';      // see if any existing user has this email address or username, otherwise create new      const existingUser = Meteor.users.findOne({        $or: [          {            'emails.address': email,          },          {            username: user.username,          },        ],      });      if (!existingUser) return user;      // copy across new service info      const service = _.keys(user.services)[0];      existingUser.services[service] = user.services[service];      existingUser.emails = user.emails;      existingUser.username = user.username;      existingUser.profile = user.profile;      existingUser.authenticationMethod = user.authenticationMethod;      Meteor.users.remove({        _id: user._id,      });      Meteor.users.remove({        _id: existingUser._id,      }); // is going to be created again      return existingUser;    }    if (options.from === 'admin') {      user.createdThroughApi = true;      return user;    }    const disableRegistration = Settings.findOne().disableRegistration;    // If this is the first Authentication by the ldap and self registration disabled    if (disableRegistration && options && options.ldap) {      user.authenticationMethod = 'ldap';      return user;    }    // If self registration enabled    if (!disableRegistration) {      return user;    }    if (!options || !options.profile) {      throw new Meteor.Error(        'error-invitation-code-blank',        'The invitation code is required',      );    }    const invitationCode = InvitationCodes.findOne({      code: options.profile.invitationcode,      email: options.email,      valid: true,    });    if (!invitationCode) {      throw new Meteor.Error(        'error-invitation-code-not-exist',        // eslint-disable-next-line quotes        "The invitation code doesn't exist",      );    } else {      user.profile = {        icode: options.profile.invitationcode,      };      user.profile.boardView = 'board-view-swimlanes';      // Deletes the invitation code after the user was created successfully.      setTimeout(        Meteor.bindEnvironment(() => {          InvitationCodes.remove({            _id: invitationCode._id,          });        }),        200,      );      return user;    }  });}const addCronJob = _.debounce(  Meteor.bindEnvironment(function notificationCleanupDebounced() {    // passed in the removeAge has to be a number standing for the number of days after a notification is read before we remove it    const envRemoveAge =      process.env.NOTIFICATION_TRAY_AFTER_READ_DAYS_BEFORE_REMOVE;    // default notifications will be removed 2 days after they are read    const defaultRemoveAge = 2;    const removeAge = parseInt(envRemoveAge, 10) || defaultRemoveAge;    SyncedCron.add({      name: 'notification_cleanup',      schedule: (parser) => parser.text('every 1 days'),      job: () => {        for (const user of Users.find()) {          if (!user.profile || !user.profile.notifications) continue;          for (const notification of user.profile.notifications) {            if (notification.read) {              const removeDate = new Date(notification.read);              removeDate.setDate(removeDate.getDate() + removeAge);              if (removeDate <= new Date()) {                user.removeNotification(notification.activity);              }            }          }        }      },    });    SyncedCron.start();  }),  500,);if (Meteor.isServer) {  // Let mongoDB ensure username unicity  Meteor.startup(() => {    allowedSortValues.forEach((value) => {      Lists._collection.createIndex(value);    });    Users._collection.createIndex({      modifiedAt: -1,    });/* Commented out extra index because of IndexOptionsConflict.    Users._collection.createIndex(      {        username: 1,      },      {        unique: true,      },    );*/    Meteor.defer(() => {      addCronJob();    });  });  // OLD WAY THIS CODE DID WORK: When user is last admin of board,  // if admin is removed, board is removed.  // NOW THIS IS COMMENTED OUT, because other board users still need to be able  // to use that board, and not have board deleted.  // Someone can be later changed to be admin of board, by making change to database.  // TODO: Add UI for changing someone as board admin.  //Users.before.remove((userId, doc) => {  //  Boards  //    .find({members: {$elemMatch: {userId: doc._id, isAdmin: true}}})  //    .forEach((board) => {  //      // If only one admin for the board  //      if (board.members.filter((e) => e.isAdmin).length === 1) {  //        Boards.remove(board._id);  //      }  //    });  //});  // Each board document contains the de-normalized number of users that have  // starred it. If the user star or unstar a board, we need to update this  // counter.  // We need to run this code on the server only, otherwise the incrementation  // will be done twice.  Users.after.update(function (userId, user, fieldNames) {    // The `starredBoards` list is hosted on the `profile` field. If this    // field hasn't been modificated we don't need to run this hook.    if (!_.contains(fieldNames, 'profile')) return;    // To calculate a diff of board starred ids, we get both the previous    // and the newly board ids list    function getStarredBoardsIds(doc) {      return doc.profile && doc.profile.starredBoards;    }    const oldIds = getStarredBoardsIds(this.previous);    const newIds = getStarredBoardsIds(user);    // The _.difference(a, b) method returns the values from a that are not in    // b. We use it to find deleted and newly inserted ids by using it in one    // direction and then in the other.    function incrementBoards(boardsIds, inc) {      boardsIds.forEach((boardId) => {        Boards.update(boardId, {          $inc: {            stars: inc,          },        });      });    }    incrementBoards(_.difference(oldIds, newIds), -1);    incrementBoards(_.difference(newIds, oldIds), +1);  });  // Override getUserId so that we can TODO get the current userId  const fakeUserId = new Meteor.EnvironmentVariable();  const getUserId = CollectionHooks.getUserId;  CollectionHooks.getUserId = () => {    return fakeUserId.get() || getUserId();  };  if (!isSandstorm) {    Users.after.insert((userId, doc) => {      const fakeUser = {        extendAutoValueContext: {          userId: doc._id,        },      };      fakeUserId.withValue(doc._id, () => {        /*        // Insert the Welcome Board        Boards.insert({          title: TAPi18n.__('welcome-board'),          permission: 'private',        }, fakeUser, (err, boardId) => {          Swimlanes.insert({            title: TAPi18n.__('welcome-swimlane'),            boardId,            sort: 1,          }, fakeUser);          ['welcome-list1', 'welcome-list2'].forEach((title, titleIndex) => {            Lists.insert({title: TAPi18n.__(title), boardId, sort: titleIndex}, fakeUser);          });        });        */        // Insert Template Container        const Future = require('fibers/future');        const future1 = new Future();        const future2 = new Future();        const future3 = new Future();        Boards.insert(          {            title: TAPi18n.__('templates'),            permission: 'private',            type: 'template-container',          },          fakeUser,          (err, boardId) => {            // Insert the reference to our templates board            Users.update(fakeUserId.get(), {              $set: {                'profile.templatesBoardId': boardId,              },            });            // Insert the card templates swimlane            Swimlanes.insert(              {                title: TAPi18n.__('card-templates-swimlane'),                boardId,                sort: 1,                type: 'template-container',              },              fakeUser,              (err, swimlaneId) => {                // Insert the reference to out card templates swimlane                Users.update(fakeUserId.get(), {                  $set: {                    'profile.cardTemplatesSwimlaneId': swimlaneId,                  },                });                future1.return();              },            );            // Insert the list templates swimlane            Swimlanes.insert(              {                title: TAPi18n.__('list-templates-swimlane'),                boardId,                sort: 2,                type: 'template-container',              },              fakeUser,              (err, swimlaneId) => {                // Insert the reference to out list templates swimlane                Users.update(fakeUserId.get(), {                  $set: {                    'profile.listTemplatesSwimlaneId': swimlaneId,                  },                });                future2.return();              },            );            // Insert the board templates swimlane            Swimlanes.insert(              {                title: TAPi18n.__('board-templates-swimlane'),                boardId,                sort: 3,                type: 'template-container',              },              fakeUser,              (err, swimlaneId) => {                // Insert the reference to out board templates swimlane                Users.update(fakeUserId.get(), {                  $set: {                    'profile.boardTemplatesSwimlaneId': swimlaneId,                  },                });                future3.return();              },            );          },        );        // HACK        future1.wait();        future2.wait();        future3.wait();        // End of Insert Template Container      });    });  }  Users.after.insert((userId, doc) => {    // HACK    doc = Users.findOne({      _id: doc._id,    });    if (doc.createdThroughApi) {      // The admin user should be able to create a user despite disabling registration because      // it is two different things (registration and creation).      // So, when a new user is created via the api (only admin user can do that) one must avoid      // the disableRegistration check.      // Issue : https://github.com/wekan/wekan/issues/1232      // PR    : https://github.com/wekan/wekan/pull/1251      Users.update(doc._id, {        $set: {          createdThroughApi: '',        },      });      return;    }    //invite user to corresponding boards    const disableRegistration = Settings.findOne().disableRegistration;    // If ldap, bypass the inviation code if the self registration isn't allowed.    // TODO : pay attention if ldap field in the user model change to another content ex : ldap field to connection_type    if (doc.authenticationMethod !== 'ldap' && disableRegistration) {      let invitationCode = null;      if(doc.authenticationMethod.toLowerCase() == 'oauth2')      { // OIDC authentication mode        invitationCode = InvitationCodes.findOne({          email: doc.emails[0].address.toLowerCase(),          valid: true,        });      }      else{        invitationCode = InvitationCodes.findOne({          code: doc.profile.icode,          valid: true,        });      }      if (!invitationCode) {        throw new Meteor.Error('error-invitation-code-not-exist');      } else {        invitationCode.boardsToBeInvited.forEach((boardId) => {          const board = Boards.findOne(boardId);          board.addMember(doc._id);        });        if (!doc.profile) {          doc.profile = {};        }        doc.profile.invitedBoards = invitationCode.boardsToBeInvited;        Users.update(doc._id, {          $set: {            profile: doc.profile,          },        });        InvitationCodes.update(invitationCode._id, {          $set: {            valid: false,          },        });      }    }  });}// USERS REST APIif (Meteor.isServer) {  // Middleware which checks that API is enabled.  JsonRoutes.Middleware.use(function (req, res, next) {    const api = req.url.startsWith('/api');    if ((api === true && process.env.WITH_API === 'true') || api === false) {      return next();    } else {      res.writeHead(301, {        Location: '/',      });      return res.end();    }  });  /**   * @operation get_current_user   *   * @summary returns the current user   * @return_type Users   */  JsonRoutes.add('GET', '/api/user', function (req, res) {    try {      Authentication.checkLoggedIn(req.userId);      const data = Meteor.users.findOne({        _id: req.userId,      });      delete data.services;      // get all boards where the user is member of      let boards = Boards.find(        {          type: 'board',          'members.userId': req.userId,        },        {          fields: {            _id: 1,            members: 1,          },        },      );      boards = boards.map((b) => {        const u = b.members.find((m) => m.userId === req.userId);        delete u.userId;        u.boardId = b._id;        return u;      });      data.boards = boards;      JsonRoutes.sendResult(res, {        code: 200,        data,      });    } catch (error) {      JsonRoutes.sendResult(res, {        code: 200,        data: error,      });    }  });  /**   * @operation get_all_users   *   * @summary return all the users   *   * @description Only the admin user (the first user) can call the REST API.   * @return_type [{ _id: string,   *                 username: string}]   */  JsonRoutes.add('GET', '/api/users', function (req, res) {    try {      Authentication.checkUserId(req.userId);      JsonRoutes.sendResult(res, {        code: 200,        data: Meteor.users.find({}).map(function (doc) {          return {            _id: doc._id,            username: doc.username,          };        }),      });    } catch (error) {      JsonRoutes.sendResult(res, {        code: 200,        data: error,      });    }  });  /**   * @operation get_user   *   * @summary get a given user   *   * @description Only the admin user (the first user) can call the REST API.   *   * @param {string} userId the user ID or username   * @return_type Users   */  JsonRoutes.add('GET', '/api/users/:userId', function (req, res) {    try {      Authentication.checkUserId(req.userId);      let id = req.params.userId;      let user = Meteor.users.findOne({        _id: id,      });      if (!user) {        user = Meteor.users.findOne({          username: id,        });        id = user._id;      }      // get all boards where the user is member of      let boards = Boards.find(        {          type: 'board',          'members.userId': id,        },        {          fields: {            _id: 1,            members: 1,          },        },      );      boards = boards.map((b) => {        const u = b.members.find((m) => m.userId === id);        delete u.userId;        u.boardId = b._id;        return u;      });      user.boards = boards;      JsonRoutes.sendResult(res, {        code: 200,        data: user,      });    } catch (error) {      JsonRoutes.sendResult(res, {        code: 200,        data: error,      });    }  });  /**   * @operation edit_user   *   * @summary edit a given user   *   * @description Only the admin user (the first user) can call the REST API.   *   * Possible values for *action*:   * - `takeOwnership`: The admin takes the ownership of ALL boards of the user (archived and not archived) where the user is admin on.   * - `disableLogin`: Disable a user (the user is not allowed to login and his login tokens are purged)   * - `enableLogin`: Enable a user   *   * @param {string} userId the user ID   * @param {string} action the action   * @return_type {_id: string,   *               title: string}   */  JsonRoutes.add('PUT', '/api/users/:userId', function (req, res) {    try {      Authentication.checkUserId(req.userId);      const id = req.params.userId;      const action = req.body.action;      let data = Meteor.users.findOne({        _id: id,      });      if (data !== undefined) {        if (action === 'takeOwnership') {          data = Boards.find(            {              'members.userId': id,              'members.isAdmin': true,            },            {              sort: {                sort: 1 /* boards default sorting */,              },            },          ).map(function (board) {            if (board.hasMember(req.userId)) {              board.removeMember(req.userId);            }            board.changeOwnership(id, req.userId);            return {              _id: board._id,              title: board.title,            };          });        } else {          if (action === 'disableLogin' && id !== req.userId) {            Users.update(              {                _id: id,              },              {                $set: {                  loginDisabled: true,                  'services.resume.loginTokens': '',                },              },            );          } else if (action === 'enableLogin') {            Users.update(              {                _id: id,              },              {                $set: {                  loginDisabled: '',                },              },            );          }          data = Meteor.users.findOne({            _id: id,          });        }      }      JsonRoutes.sendResult(res, {        code: 200,        data,      });    } catch (error) {      JsonRoutes.sendResult(res, {        code: 200,        data: error,      });    }  });  /**   * @operation add_board_member   * @tag Boards   *   * @summary Add New Board Member with Role   *   * @description Only the admin user (the first user) can call the REST API.   *   * **Note**: see [Boards.set_board_member_permission](#set_board_member_permission)   * to later change the permissions.   *   * @param {string} boardId the board ID   * @param {string} userId the user ID   * @param {boolean} isAdmin is the user an admin of the board   * @param {boolean} isNoComments disable comments   * @param {boolean} isCommentOnly only enable comments   * @return_type {_id: string,   *               title: string}   */  JsonRoutes.add(    'POST',    '/api/boards/:boardId/members/:userId/add',    function (req, res) {      try {        Authentication.checkUserId(req.userId);        const userId = req.params.userId;        const boardId = req.params.boardId;        const action = req.body.action;        const { isAdmin, isNoComments, isCommentOnly } = req.body;        let data = Meteor.users.findOne({          _id: userId,        });        if (data !== undefined) {          if (action === 'add') {            data = Boards.find({              _id: boardId,            }).map(function (board) {              if (!board.hasMember(userId)) {                board.addMember(userId);                function isTrue(data) {                  return data.toLowerCase() === 'true';                }                board.setMemberPermission(                  userId,                  isTrue(isAdmin),                  isTrue(isNoComments),                  isTrue(isCommentOnly),                  userId,                );              }              return {                _id: board._id,                title: board.title,              };            });          }        }        JsonRoutes.sendResult(res, {          code: 200,          data: query,        });      } catch (error) {        JsonRoutes.sendResult(res, {          code: 200,          data: error,        });      }    },  );  /**   * @operation remove_board_member   * @tag Boards   *   * @summary Remove Member from Board   *   * @description Only the admin user (the first user) can call the REST API.   *   * @param {string} boardId the board ID   * @param {string} userId the user ID   * @param {string} action the action (needs to be `remove`)   * @return_type {_id: string,   *               title: string}   */  JsonRoutes.add(    'POST',    '/api/boards/:boardId/members/:userId/remove',    function (req, res) {      try {        Authentication.checkUserId(req.userId);        const userId = req.params.userId;        const boardId = req.params.boardId;        const action = req.body.action;        let data = Meteor.users.findOne({          _id: userId,        });        if (data !== undefined) {          if (action === 'remove') {            data = Boards.find({              _id: boardId,            }).map(function (board) {              if (board.hasMember(userId)) {                board.removeMember(userId);              }              return {                _id: board._id,                title: board.title,              };            });          }        }        JsonRoutes.sendResult(res, {          code: 200,          data: query,        });      } catch (error) {        JsonRoutes.sendResult(res, {          code: 200,          data: error,        });      }    },  );  /**   * @operation new_user   *   * @summary Create a new user   *   * @description Only the admin user (the first user) can call the REST API.   *   * @param {string} username the new username   * @param {string} email the email of the new user   * @param {string} password the password of the new user   * @return_type {_id: string}   */  JsonRoutes.add('POST', '/api/users/', function (req, res) {    try {      Authentication.checkUserId(req.userId);      const id = Accounts.createUser({        username: req.body.username,        email: req.body.email,        password: req.body.password,        from: 'admin',      });      JsonRoutes.sendResult(res, {        code: 200,        data: {          _id: id,        },      });    } catch (error) {      JsonRoutes.sendResult(res, {        code: 200,        data: error,      });    }  });  /**   * @operation delete_user   *   * @summary Delete a user   *   * @description Only the admin user (the first user) can call the REST API.   *   * @param {string} userId the ID of the user to delete   * @return_type {_id: string}   */  JsonRoutes.add('DELETE', '/api/users/:userId', function (req, res) {    try {      Authentication.checkUserId(req.userId);      const id = req.params.userId;      // Delete user is enabled, but is still has bug of leaving empty user avatars      // to boards: boards members, card members and assignees have      // empty users. So it would be better to delete user from all boards before      // deleting user.      // See:      // - wekan/client/components/settings/peopleBody.jade deleteButton      // - wekan/client/components/settings/peopleBody.js deleteButton      // - wekan/client/components/sidebar/sidebar.js Popup.afterConfirm('removeMember'      //   that does now remove member from board, card members and assignees correctly,      //   but that should be used to remove user from all boards similarly      // - wekan/models/users.js Delete is not enabled      Meteor.users.remove({ _id: id });      JsonRoutes.sendResult(res, {        code: 200,        data: {          _id: id,        },      });    } catch (error) {      JsonRoutes.sendResult(res, {        code: 200,        data: error,      });    }  });  /**   * @operation create_user_token   *   * @summary Create a user token   *   * @description Only the admin user (the first user) can call the REST API.   *   * @param {string} userId the ID of the user to create token for.   * @return_type {_id: string}   */  JsonRoutes.add('POST', '/api/createtoken/:userId', function (req, res) {    try {      Authentication.checkUserId(req.userId);      const id = req.params.userId;      const token = Accounts._generateStampedLoginToken();      Accounts._insertLoginToken(id, token);      JsonRoutes.sendResult(res, {        code: 200,        data: {          _id: id,          authToken: token.token,        },      });    } catch (error) {      JsonRoutes.sendResult(res, {        code: 200,        data: error,      });    }  });}export default Users;
 |