org.js 6.3 KB

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