org.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. Org = new Mongo.Collection('org');
  3. /**
  4. * A Organization in Wekan. A Enterprise in Trello.
  5. */
  6. Org.attachSchema(
  7. new SimpleSchema({
  8. orgDisplayName: {
  9. /**
  10. * the name to display for the organization
  11. */
  12. type: String,
  13. optional: true,
  14. },
  15. orgDesc: {
  16. /**
  17. * the description the organization
  18. */
  19. type: String,
  20. optional: true,
  21. max: 190,
  22. },
  23. orgShortName: {
  24. /**
  25. * short name of the organization
  26. */
  27. type: String,
  28. optional: true,
  29. max: 255,
  30. },
  31. orgAutoAddUsersWithDomainName: {
  32. /**
  33. * automatically add users with domain name
  34. */
  35. type: String,
  36. optional: true,
  37. max: 255,
  38. },
  39. orgWebsite: {
  40. /**
  41. * website of the organization
  42. */
  43. type: String,
  44. optional: true,
  45. max: 255,
  46. },
  47. orgIsActive: {
  48. /**
  49. * status of the organization
  50. */
  51. type: Boolean,
  52. optional: true,
  53. },
  54. createdAt: {
  55. /**
  56. * creation date of the organization
  57. */
  58. type: Date,
  59. denyUpdate: false,
  60. // eslint-disable-next-line consistent-return
  61. autoValue() {
  62. if (this.isInsert) {
  63. return new Date();
  64. } else if (this.isUpsert) {
  65. return { $setOnInsert: new Date() };
  66. } else {
  67. this.unset();
  68. }
  69. },
  70. },
  71. modifiedAt: {
  72. type: Date,
  73. denyUpdate: false,
  74. // eslint-disable-next-line consistent-return
  75. autoValue() {
  76. if (this.isInsert || this.isUpsert || this.isUpdate) {
  77. return new Date();
  78. } else {
  79. this.unset();
  80. }
  81. },
  82. },
  83. }),
  84. );
  85. if (Meteor.isServer) {
  86. Org.allow({
  87. insert(userId, doc) {
  88. const user = ReactiveCache.getUser(userId) || ReactiveCache.getCurrentUser();
  89. if (user?.isAdmin)
  90. return true;
  91. if (!user) {
  92. return false;
  93. }
  94. return doc._id === userId;
  95. },
  96. update(userId, doc) {
  97. const user = ReactiveCache.getUser(userId) || ReactiveCache.getCurrentUser();
  98. if (user?.isAdmin)
  99. return true;
  100. if (!user) {
  101. return false;
  102. }
  103. return doc._id === userId;
  104. },
  105. remove(userId, doc) {
  106. const user = ReactiveCache.getUser(userId) || ReactiveCache.getCurrentUser();
  107. if (user?.isAdmin)
  108. return true;
  109. if (!user) {
  110. return false;
  111. }
  112. return doc._id === userId;
  113. },
  114. fetch: [],
  115. });
  116. Meteor.methods({
  117. setCreateOrg(
  118. orgDisplayName,
  119. orgDesc,
  120. orgShortName,
  121. orgAutoAddUsersWithDomainName,
  122. orgWebsite,
  123. orgIsActive,
  124. ) {
  125. if (ReactiveCache.getCurrentUser()?.isAdmin) {
  126. check(orgDisplayName, String);
  127. check(orgDesc, String);
  128. check(orgShortName, String);
  129. check(orgAutoAddUsersWithDomainName, String);
  130. check(orgWebsite, String);
  131. check(orgIsActive, Boolean);
  132. const nOrgNames = ReactiveCache.getOrgs({ orgShortName }).length;
  133. if (nOrgNames > 0) {
  134. throw new Meteor.Error('orgname-already-taken');
  135. } else {
  136. Org.insert({
  137. orgDisplayName,
  138. orgDesc,
  139. orgShortName,
  140. orgAutoAddUsersWithDomainName,
  141. orgWebsite,
  142. orgIsActive,
  143. });
  144. }
  145. }
  146. },
  147. setCreateOrgFromOidc(
  148. orgDisplayName,
  149. orgDesc,
  150. orgShortName,
  151. orgAutoAddUsersWithDomainName,
  152. orgWebsite,
  153. orgIsActive,
  154. ) {
  155. check(orgDisplayName, String);
  156. check(orgDesc, String);
  157. check(orgShortName, String);
  158. check(orgAutoAddUsersWithDomainName, String);
  159. check(orgWebsite, String);
  160. check(orgIsActive, Boolean);
  161. const nOrgNames = ReactiveCache.getOrgs({ orgShortName }).length;
  162. if (nOrgNames > 0) {
  163. throw new Meteor.Error('orgname-already-taken');
  164. } else {
  165. Org.insert({
  166. orgDisplayName,
  167. orgDesc,
  168. orgShortName,
  169. orgAutoAddUsersWithDomainName,
  170. orgWebsite,
  171. orgIsActive,
  172. });
  173. }
  174. },
  175. setOrgDisplayName(org, orgDisplayName) {
  176. if (ReactiveCache.getCurrentUser()?.isAdmin) {
  177. check(org, Object);
  178. check(orgDisplayName, String);
  179. Org.update(org, {
  180. $set: { orgDisplayName: orgDisplayName },
  181. });
  182. Meteor.call('setUsersOrgsOrgDisplayName', org._id, orgDisplayName);
  183. }
  184. },
  185. setOrgDesc(org, orgDesc) {
  186. if (ReactiveCache.getCurrentUser()?.isAdmin) {
  187. check(org, Object);
  188. check(orgDesc, String);
  189. Org.update(org, {
  190. $set: { orgDesc: orgDesc },
  191. });
  192. }
  193. },
  194. setOrgShortName(org, orgShortName) {
  195. if (ReactiveCache.getCurrentUser()?.isAdmin) {
  196. check(org, Object);
  197. check(orgShortName, String);
  198. Org.update(org, {
  199. $set: { orgShortName: orgShortName },
  200. });
  201. }
  202. },
  203. setAutoAddUsersWithDomainName(org, orgAutoAddUsersWithDomainName) {
  204. if (ReactiveCache.getCurrentUser()?.isAdmin) {
  205. check(org, Object);
  206. check(orgAutoAddUsersWithDomainName, String);
  207. Org.update(org, {
  208. $set: { orgAutoAddUsersWithDomainName: orgAutoAddUsersWithDomainName },
  209. });
  210. }
  211. },
  212. setOrgIsActive(org, orgIsActive) {
  213. if (ReactiveCache.getCurrentUser()?.isAdmin) {
  214. check(org, Object);
  215. check(orgIsActive, Boolean);
  216. Org.update(org, {
  217. $set: { orgIsActive: orgIsActive },
  218. });
  219. }
  220. },
  221. setOrgAllFieldsFromOidc(
  222. org,
  223. orgDisplayName,
  224. orgDesc,
  225. orgShortName,
  226. orgAutoAddUsersWithDomainName,
  227. orgWebsite,
  228. orgIsActive,
  229. ) {
  230. check(org, Object);
  231. check(orgDisplayName, String);
  232. check(orgDesc, String);
  233. check(orgShortName, String);
  234. check(orgAutoAddUsersWithDomainName, String);
  235. check(orgWebsite, String);
  236. check(orgIsActive, Boolean);
  237. Org.update(org, {
  238. $set: {
  239. orgDisplayName: orgDisplayName,
  240. orgDesc: orgDesc,
  241. orgShortName: orgShortName,
  242. orgAutoAddUsersWithDomainName: orgAutoAddUsersWithDomainName,
  243. orgWebsite: orgWebsite,
  244. orgIsActive: orgIsActive,
  245. },
  246. });
  247. Meteor.call('setUsersOrgsOrgDisplayName', org._id, orgDisplayName);
  248. },
  249. setOrgAllFields(
  250. org,
  251. orgDisplayName,
  252. orgDesc,
  253. orgShortName,
  254. orgAutoAddUsersWithDomainName,
  255. orgWebsite,
  256. orgIsActive,
  257. ) {
  258. if (ReactiveCache.getCurrentUser()?.isAdmin) {
  259. check(org, Object);
  260. check(orgDisplayName, String);
  261. check(orgDesc, String);
  262. check(orgShortName, String);
  263. check(orgAutoAddUsersWithDomainName, String);
  264. check(orgWebsite, String);
  265. check(orgIsActive, Boolean);
  266. Org.update(org, {
  267. $set: {
  268. orgDisplayName: orgDisplayName,
  269. orgDesc: orgDesc,
  270. orgShortName: orgShortName,
  271. orgAutoAddUsersWithDomainName: orgAutoAddUsersWithDomainName,
  272. orgWebsite: orgWebsite,
  273. orgIsActive: orgIsActive,
  274. },
  275. });
  276. Meteor.call('setUsersOrgsOrgDisplayName', org._id, orgDisplayName);
  277. }
  278. },
  279. });
  280. }
  281. if (Meteor.isServer) {
  282. // Index for Organization name.
  283. Meteor.startup(() => {
  284. // Org._collection.createIndex({ name: -1 });
  285. Org._collection.createIndex({ orgDisplayName: 1 });
  286. });
  287. }
  288. export default Org;