org.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. orgWebsite: {
  32. /**
  33. * website of the organization
  34. */
  35. type: String,
  36. optional: true,
  37. max: 255,
  38. },
  39. orgIsActive: {
  40. /**
  41. * status of the organization
  42. */
  43. type: Boolean,
  44. optional: true,
  45. },
  46. createdAt: {
  47. /**
  48. * creation date of the organization
  49. */
  50. type: Date,
  51. denyUpdate: false,
  52. // eslint-disable-next-line consistent-return
  53. autoValue() {
  54. if (this.isInsert) {
  55. return new Date();
  56. } else if (this.isUpsert) {
  57. return { $setOnInsert: new Date() };
  58. } else {
  59. this.unset();
  60. }
  61. },
  62. },
  63. modifiedAt: {
  64. type: Date,
  65. denyUpdate: false,
  66. // eslint-disable-next-line consistent-return
  67. autoValue() {
  68. if (this.isInsert || this.isUpsert || this.isUpdate) {
  69. return new Date();
  70. } else {
  71. this.unset();
  72. }
  73. },
  74. },
  75. }),
  76. );
  77. if (Meteor.isServer) {
  78. Org.allow({
  79. insert(userId, doc) {
  80. const user = ReactiveCache.getUser(userId) || ReactiveCache.getCurrentUser();
  81. if (user?.isAdmin)
  82. return true;
  83. if (!user) {
  84. return false;
  85. }
  86. return doc._id === userId;
  87. },
  88. update(userId, doc) {
  89. const user = ReactiveCache.getUser(userId) || ReactiveCache.getCurrentUser();
  90. if (user?.isAdmin)
  91. return true;
  92. if (!user) {
  93. return false;
  94. }
  95. return doc._id === userId;
  96. },
  97. remove(userId, doc) {
  98. const user = ReactiveCache.getUser(userId) || ReactiveCache.getCurrentUser();
  99. if (user?.isAdmin)
  100. return true;
  101. if (!user) {
  102. return false;
  103. }
  104. return doc._id === userId;
  105. },
  106. fetch: [],
  107. });
  108. Meteor.methods({
  109. setCreateOrg(
  110. orgDisplayName,
  111. orgDesc,
  112. orgShortName,
  113. orgWebsite,
  114. orgIsActive,
  115. ) {
  116. if (ReactiveCache.getCurrentUser()?.isAdmin) {
  117. check(orgDisplayName, String);
  118. check(orgDesc, String);
  119. check(orgShortName, String);
  120. check(orgWebsite, String);
  121. check(orgIsActive, Boolean);
  122. const nOrgNames = Org.find({ orgShortName }).count();
  123. if (nOrgNames > 0) {
  124. throw new Meteor.Error('orgname-already-taken');
  125. } else {
  126. Org.insert({
  127. orgDisplayName,
  128. orgDesc,
  129. orgShortName,
  130. orgWebsite,
  131. orgIsActive,
  132. });
  133. }
  134. }
  135. },
  136. setCreateOrgFromOidc(
  137. orgDisplayName,
  138. orgDesc,
  139. orgShortName,
  140. orgWebsite,
  141. orgIsActive,
  142. ) {
  143. check(orgDisplayName, String);
  144. check(orgDesc, String);
  145. check(orgShortName, String);
  146. check(orgWebsite, String);
  147. check(orgIsActive, Boolean);
  148. const nOrgNames = Org.find({ orgShortName }).count();
  149. if (nOrgNames > 0) {
  150. throw new Meteor.Error('orgname-already-taken');
  151. } else {
  152. Org.insert({
  153. orgDisplayName,
  154. orgDesc,
  155. orgShortName,
  156. orgWebsite,
  157. orgIsActive,
  158. });
  159. }
  160. },
  161. setOrgDisplayName(org, orgDisplayName) {
  162. if (ReactiveCache.getCurrentUser()?.isAdmin) {
  163. check(org, Object);
  164. check(orgDisplayName, String);
  165. Org.update(org, {
  166. $set: { orgDisplayName: orgDisplayName },
  167. });
  168. Meteor.call('setUsersOrgsOrgDisplayName', org._id, orgDisplayName);
  169. }
  170. },
  171. setOrgDesc(org, orgDesc) {
  172. if (ReactiveCache.getCurrentUser()?.isAdmin) {
  173. check(org, Object);
  174. check(orgDesc, String);
  175. Org.update(org, {
  176. $set: { orgDesc: orgDesc },
  177. });
  178. }
  179. },
  180. setOrgShortName(org, orgShortName) {
  181. if (ReactiveCache.getCurrentUser()?.isAdmin) {
  182. check(org, Object);
  183. check(orgShortName, String);
  184. Org.update(org, {
  185. $set: { orgShortName: orgShortName },
  186. });
  187. }
  188. },
  189. setOrgIsActive(org, orgIsActive) {
  190. if (ReactiveCache.getCurrentUser()?.isAdmin) {
  191. check(org, Object);
  192. check(orgIsActive, Boolean);
  193. Org.update(org, {
  194. $set: { orgIsActive: orgIsActive },
  195. });
  196. }
  197. },
  198. setOrgAllFieldsFromOidc(
  199. org,
  200. orgDisplayName,
  201. orgDesc,
  202. orgShortName,
  203. orgWebsite,
  204. orgIsActive,
  205. ) {
  206. check(org, Object);
  207. check(orgDisplayName, String);
  208. check(orgDesc, String);
  209. check(orgShortName, String);
  210. check(orgWebsite, String);
  211. check(orgIsActive, Boolean);
  212. Org.update(org, {
  213. $set: {
  214. orgDisplayName: orgDisplayName,
  215. orgDesc: orgDesc,
  216. orgShortName: orgShortName,
  217. orgWebsite: orgWebsite,
  218. orgIsActive: orgIsActive,
  219. },
  220. });
  221. Meteor.call('setUsersOrgsOrgDisplayName', org._id, orgDisplayName);
  222. },
  223. setOrgAllFields(
  224. org,
  225. orgDisplayName,
  226. orgDesc,
  227. orgShortName,
  228. orgWebsite,
  229. orgIsActive,
  230. ) {
  231. if (ReactiveCache.getCurrentUser()?.isAdmin) {
  232. check(org, Object);
  233. check(orgDisplayName, String);
  234. check(orgDesc, String);
  235. check(orgShortName, String);
  236. check(orgWebsite, String);
  237. check(orgIsActive, Boolean);
  238. Org.update(org, {
  239. $set: {
  240. orgDisplayName: orgDisplayName,
  241. orgDesc: orgDesc,
  242. orgShortName: orgShortName,
  243. orgWebsite: orgWebsite,
  244. orgIsActive: orgIsActive,
  245. },
  246. });
  247. Meteor.call('setUsersOrgsOrgDisplayName', org._id, orgDisplayName);
  248. }
  249. },
  250. });
  251. }
  252. if (Meteor.isServer) {
  253. // Index for Organization name.
  254. Meteor.startup(() => {
  255. // Org._collection.createIndex({ name: -1 });
  256. Org._collection.createIndex({ orgDisplayName: 1 });
  257. });
  258. }
  259. export default Org;