loginHandler.js 5.0 KB

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