123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- Settings = new Mongo.Collection('settings');
- Settings.attachSchema(new SimpleSchema({
- disableRegistration: {
- type: Boolean,
- },
- 'mailServer.username': {
- type: String,
- optional: true,
- },
- 'mailServer.password': {
- type: String,
- optional: true,
- },
- 'mailServer.host': {
- type: String,
- optional: true,
- },
- 'mailServer.port': {
- type: String,
- optional: true,
- },
- 'mailServer.from': {
- type: String,
- optional: true,
- defaultValue: 'Wekan',
- },
- createdAt: {
- type: Date,
- denyUpdate: true,
- },
- modifiedAt: {
- type: Date,
- },
- }));
- Settings.helpers({
- mailUrl () {
- const mailUrl = `smtp://${this.mailServer.username}:${this.mailServer.password}@${this.mailServer.host}:${this.mailServer.port}/`;
- return mailUrl;
- },
- });
- Settings.allow({
- update(userId) {
- const user = Users.findOne(userId);
- return user && user.isAdmin;
- },
- });
- Settings.before.update((userId, doc, fieldNames, modifier) => {
- modifier.$set = modifier.$set || {};
- modifier.$set.modifiedAt = new Date();
- });
- if (Meteor.isServer) {
- Meteor.startup(() => {
- const setting = Settings.findOne({});
- if(!setting){
- const now = new Date();
- const defaultSetting = {disableRegistration: false, mailServer: {
- username: '', password:'', host: '', port:'', from: '',
- }, createdAt: now, modifiedAt: now};
- Settings.insert(defaultSetting);
- }
- const newSetting = Settings.findOne();
- process.env.MAIL_URL = newSetting.mailUrl();
- Accounts.emailTemplates.from = newSetting.mailServer.from;
- });
- function getRandomNum (min, max) {
- const range = max - min;
- const rand = Math.random();
- return (min + Math.round(rand * range));
- }
- function sendInvitationEmail (_id){
- const icode = InvitationCodes.findOne(_id);
- const author = Users.findOne(Meteor.userId());
- try {
- const params = {
- email: icode.email,
- inviter: Users.findOne(icode.authorId).username,
- user: icode.email.split('@')[0],
- icode: icode.code,
- url: FlowRouter.url('sign-up'),
- };
- const lang = author.getLanguage();
- Email.send({
- to: icode.email,
- from: Accounts.emailTemplates.from,
- subject: TAPi18n.__('email-invite-register-subject', params, lang),
- text: TAPi18n.__('email-invite-register-text', params, lang),
- });
- } catch (e) {
- throw new Meteor.Error('email-fail', e.message);
- }
- }
- Meteor.methods({
- sendInvitation(emails, boards) {
- check(emails, [String]);
- check(boards, [String]);
- const user = Users.findOne(Meteor.userId());
- if(!user.isAdmin){
- throw new Meteor.Error('not-allowed');
- }
- emails.forEach((email) => {
- if (email && SimpleSchema.RegEx.Email.test(email)) {
- const code = getRandomNum(100000, 999999);
- InvitationCodes.insert({code, email, boardsToBeInvited: boards, createdAt: new Date(), authorId: Meteor.userId()}, function(err, _id){
- if(!err && _id) sendInvitationEmail(_id);
- });
- }
- });
- },
- });
- }
|