loginHandler.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // creates Object if not present in collection
  2. // initArr = [displayName, shortName, website, isActive]
  3. // objString = ["Org","Team"] for method mapping
  4. function createObject(initArr, objString)
  5. {
  6. functionName = objString === "Org" ? 'setCreateOrgFromOidc' : 'setCreateTeamFromOidc';
  7. creationString = 'setCreate'+ objString + 'FromOidc';
  8. return Meteor.call(functionName,
  9. initArr[0],//displayName
  10. initArr[1],//desc
  11. initArr[2],//shortName
  12. initArr[3],//website
  13. initArr[4]//xxxisActive
  14. );
  15. }
  16. function updateObject(initArr, objString)
  17. {
  18. functionName = objString === "Org" ? 'setOrgAllFieldsFromOidc' : 'setTeamAllFieldsFromOidc';
  19. return Meteor.call(functionName,
  20. initArr[0],//team || org Object
  21. initArr[1],//displayName
  22. initArr[2],//desc
  23. initArr[3],//shortName
  24. initArr[4],//website
  25. initArr[5]//xxxisActive
  26. );
  27. }
  28. //checks whether obj is in collection of userObjs
  29. //params
  30. //e.g. userObjs = user.teams
  31. //e.g. obj = Team.findOne...
  32. //e.g. collection = "team"
  33. function contains(userObjs, obj, collection)
  34. {
  35. id = collection+'Id';
  36. if(typeof userObjs == "undefined" || !userObjs.length)
  37. {
  38. return false;
  39. }
  40. for (const [count, hash] of Object.entries(userObjs))
  41. {
  42. if (hash[id] === obj._id)
  43. {
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49. module.exports = {
  50. // This function adds groups as organizations or teams to users and
  51. // creates them if not already existing
  52. // DEFAULT after creation orgIsActive & teamIsActive: true
  53. // PODC provider needs to send group data within "wekanGroup" scope
  54. // PARAMS to be set for groups within your Oidc provider:
  55. // isAdmin: [true, false] -> admin group becomes admin in wekan
  56. // isOrganization: [true, false] -> creates org and adds to user
  57. // displayName: "string"
  58. addGroupsWithAttributes: function (user, groups){
  59. teamArray=[];
  60. orgArray=[];
  61. isAdmin = [];
  62. teams = user.teams;
  63. orgs = user.orgs;
  64. for (group of groups)
  65. {
  66. initAttributes = [
  67. group.displayName,
  68. group.desc || group.displayName,
  69. group.shortName ||group.displayName,
  70. group.website || group.displayName, group.isActive || false];
  71. isOrg = group.isOrganisation || false;
  72. forceCreate = group.forceCreate|| false;
  73. isAdmin.push(group.isAdmin || false);
  74. if (isOrg)
  75. {
  76. org = Org.findOne({"orgDisplayName": group.displayName});
  77. if(org)
  78. {
  79. if(contains(orgs, org, "org"))
  80. {
  81. initAttributes.unshift(org);
  82. updateObject(initAttributes, "Org");
  83. continue;
  84. }
  85. }
  86. else if(forceCreate)
  87. {
  88. createObject(initAttributes, "Org");
  89. org = Org.findOne({'orgDisplayName': group.displayName});
  90. }
  91. else
  92. {
  93. continue;
  94. }
  95. orgHash = {'orgId': org._id, 'orgDisplayName': group.displayName};
  96. orgArray.push(orgHash);
  97. }
  98. else
  99. {
  100. //start team routine
  101. team = Team.findOne({"teamDisplayName": group.displayName});
  102. if (team)
  103. {
  104. if(contains(teams, team, "team"))
  105. {
  106. initAttributes.unshift(team);
  107. updateObject(initAttributes, "Team");
  108. continue;
  109. }
  110. }
  111. else if(forceCreate)
  112. {
  113. createObject(initAttributes, "Team");
  114. team = Team.findOne({'teamDisplayName': group.displayName});
  115. }
  116. else
  117. {
  118. continue;
  119. }
  120. teamHash = {'teamId': team._id, 'teamDisplayName': group.displayName};
  121. teamArray.push(teamHash);
  122. }
  123. }
  124. // user is assigned to team/org which has set isAdmin: true in oidc data
  125. // hence user will get admin privileges in wekan
  126. // E.g. Admin rights will be withdrawn if no group in oidc provider has isAdmin set to true
  127. users.update({ _id: user._id }, { $set: {isAdmin: isAdmin.some(i => (i === true))}});
  128. teams = {'teams': {'$each': teamArray}};
  129. orgs = {'orgs': {'$each': orgArray}};
  130. users.update({ _id: user._id }, { $push: teams});
  131. users.update({ _id: user._id }, { $push: orgs});
  132. // remove temporary oidc data from user collection
  133. users.update({ _id: user._id }, { $unset: {"services.oidc.groups": []}});
  134. return;
  135. },
  136. changeUsername: function(user, name)
  137. {
  138. username = {'username': name};
  139. if (user.username != username) users.update({ _id: user._id }, { $set: username});
  140. },
  141. changeFullname: function(user, name)
  142. {
  143. username = {'profile.fullname': name};
  144. if (user.username != username) users.update({ _id: user._id }, { $set: username});
  145. },
  146. addEmail: function(user, email)
  147. {
  148. user_email = user.emails || [];
  149. var contained = false;
  150. position = 0;
  151. for (const [count, mail_hash] of Object.entries(user_email))
  152. {
  153. if (mail_hash['address'] === email)
  154. {
  155. contained = true;
  156. position = count;
  157. break;
  158. }
  159. }
  160. if(contained && position != 0)
  161. {
  162. user_email.splice(position,1);
  163. contained = false;
  164. }
  165. if(!contained)
  166. {
  167. user_email.unshift({'address': email, 'verified': true});
  168. user_email = {'emails': user_email};
  169. users.update({ _id: user._id }, { $set: user_email});
  170. }
  171. }
  172. }