accounts-base - accounts-google - accounts-password - / accounts-ui - alanning:roles - aldeed:autoform - aldeed:collection2 - email - iron:router - mizzao:bootboxjs - random -
// INVITE_CREATED = 0; // Email - INVITE_EMAILED = 1; // INVITE_COMPLETED = 2;
// , if (Meteor.isClient) { // . , Template.registerHelper('userCompany', function () { var company = Company.findOne({userId: Meteor.userId()}); if (company == undefined) { if (Meteor.userId() != null) { var user = Meteor.users.findOne({_id: Meteor.userId()}, {fields: {'companyId': 1}}); company = Company.findOne({_id: user.companyId}); } } return company; }); // Email Template.registerHelper('validateEmail', function (email) { var re = /\S+@\S+\.\S+/; return re.test(email); }); }
Company = new Mongo.Collection('company'); // if (Meteor.isServer) { Meteor.methods({ // . registerAdminUser: function(companyId, userId) { check(companyId, String); check(userId, String); Roles.addUsersToRoles(Meteor.userId(), ["CompanyAdmin"]); } }); } //SimpleSchema.debug = true; // Company.attachSchema(new SimpleSchema({ // title: { type: String, label: "", min: 3, max: 200 }, // description: { type: String, label: " ", min: 20, max: 1000, autoform: { rows: 5 } }, //id . . userId: { type: String, autoValue: function() { if (this.isInsert) { return Meteor.userId(); } else { this.unset(); } }, label: "", //denyInsert: true, denyUpdate: true, optional: true }, // . createdAt: { type: Date, autoValue: function() { if (this.isInsert) { return new Date; } else if (this.isUpsert) { return {$setOnInsert: new Date}; } else { this.unset(); } }, denyUpdate: true, optional: true }, // . updatedAt: { type: Date, autoValue: function() { if (this.isUpdate) { return new Date(); } }, denyInsert: true, optional: true }, }));
Invite = new Mongo.Collection('invite'); //SimpleSchema.debug = true; // if (Meteor.isServer) { Meteor.methods({ // email invationSender: function (email) { check(this.userId, String); check(email, String); // . Email var token = Random.hexString(10); // . var company = Company.findOne({userId: this.userId}); var companyName = company.title; // // - , var inviteId = Invite.insert({email:email,token:token,status:INVITE_CREATED}); // , this.unblock(); // // , Email.send({ to: email, from: 'info@forsk.ru', subject: ' '+companyName+' Kellot.ru', html: '! Kellot.Ru '+Meteor.user().profile.name+' ' + ' "'+companyName+'". ' + '<br/><br/> : '+token+ '<br/><br/> , ' + '<a href="http://p.kellot.ru/company/invite/'+token+'">http://p.kellot.ru/company/invite/'+token+'</a> ' + ' . .'+ '<br/><br/> , Kellot.Ru' }); // "" "" Invite.update({_id:inviteId}, {$set: {status: INVITE_EMAILED}}, {}, function(error, count) { console.log('update error', error, count); }); return true; }, // deleteInvite: function(inviteId) { check(inviteId, String); var invite = Invite.findOne({_id: inviteId}); // "" . // , if (invite.status != INVITE_COMPLETED) { Invite.remove({_id: inviteId}); return true; } else { return false; } }, // activateInviteToken: function (activationToken, userId) { check(this.userId, String); check(activationToken, String); check(userId, String); // - , var user = Meteor.users.findOne({_id:userId}); var invite = Invite.findOne({token:activationToken}); var company = Company.findOne({_id:invite.companyId}); // - if (invite.status == INVITE_COMPLETED) { return false; } // Meteor.users.update({_id:userId}, { $set: {companyId: company._id } }); // Invite.update({_id:invite._id}, { $set: {invitedUserId: userId, status: 2 } }); // Roles.addUsersToRoles(Meteor.userId(), ["CompanyMember"]); return true; } }); } // Invite.attachSchema(new SimpleSchema({ //Email , email: { type: String, label: " / Email", min: 3, max: 30 }, // . token: { type: String, label: " ", min: 10, max: 10 }, // status: { type: Number, label: " " }, // invitedUserId: { type: String, label: " ", optional: true }, // ? creator: { type: String, label: "", autoValue: function() { if (this.isInsert) { return Meteor.userId(); } else { this.unset(); } }, denyUpdate: true, optional: true }, // companyId: { type: String, autoValue: function() { if (this.isInsert) { return Company.findOne({userId:Meteor.userId()})._id; } else { this.unset(); } }, label: "", denyUpdate: true, optional: true }, // createdAt: { type: Date, autoValue: function() { if (this.isInsert) { return new Date; } else if (this.isUpsert) { return {$setOnInsert: new Date}; } else { this.unset(); } }, denyUpdate: true, optional: true }, // . // updatedAt: { type: Date, autoValue: function() { if (this.isUpdate) { return new Date(); } }, denyInsert: true, optional: true } }));
Router.map(function () { ... // this.route('activateInviteToCompany', { trackPageView: true, path: '/company/invite/:activationToken', waitOn: function () { // , , Meteor.subscribe("inviteToken", Router.current().params.activationToken); Meteor.subscribe('companyToken', Router.current().params.activationToken); return Meteor.subscribe('userToken', Router.current().params.activationToken); } }); ... Router.onBeforeAction(function (pause) { Alerts.removeSeen(); // if (Meteor.userId() == null) { if (pause.url != '/index' && pause.url != '/' && pause.url != '/reviews' && pause.url != '/company/invite/'+Router.current().params.activationToken) { Router.go('index'); } } // , , // // , // if (Meteor.isClient && Meteor.userId() != null) { // ... if (UI._globalHelpers.userCompany() == undefined && (pause.url != '/firstLogin' && pause.url != '/company/register' )) { //... , ... if (Session.get('activationToken') != undefined) { // var activationToken = Session.get('activationToken'); Session.set('activationToken', undefined); // var invite = Invite.findOne({ token: activationToken }); // Meteor.call('activateInviteToken', activationToken, Meteor.userId(), function (error, result) { // if (error) { // ... console.log(error); bootbox.alert(" . , ! : " + error.reason); } else { // ! Meteor.subscribe('company'); Meteor.subscribe('invite'); bootbox.alert(" !"); } }); } else { // - // ( ) Router.go('firstLoginForm'); } } } this.next(); }); });
// function getCompanyByInviteToken(tokenId) { var invite = Invite.findOne({ token: tokenId }); var company = Company.findOne({ _id: invite.companyId }); //console.log('getCompanyByInviteToken', tokenId, invite.companyId, company._id); return company; } ... Meteor.publish('inviteToken', function (tokenId) { check(tokenId, Match.Any); return Invite.find({ token: tokenId }); }); Meteor.publish('companyToken', function (tokenId) { check(tokenId, Match.Any); var company = getCompanyByInviteToken(tokenId); return Company.find({_id:company._id}); }); Meteor.publish('userToken', function (tokenId) { check(tokenId, Match.Any); var company = getCompanyByInviteToken(tokenId); return Meteor.users.find({ _id: company.userId }, {fields: {'services':0, 'roles':0, createdAt:0}}); });
<template name="inviteList"> <div class="panel panel-success" style="float: left; margin-right: 20px;"> <div class="panel-heading"> <h3 class="panel-title"> !</h3> </div> <div class="panel-body"> {{#if invitedUsers.count}} <div class="list-group"> {{#each invitedUsers}} <div class="list-group-item"> <div class="row-content"> <div class="least-content">{{inviteTextStatus}} {{#if isInRole 'CompanyAdmin'}} {{#if inviteIsComplete}} {{else}} <a class="deleteInviteBtn" data-id="{{_id}}" href="#">x</a> {{/if}} {{/if}} </div> <p class="list-group-item-text">{{email}}</p> </div> </div> <div class="list-group-separator"></div> {{/each}} </div> {{else}} ! {{/if}} </div> {{#if isInRole 'CompanyAdmin'}} <div class="panel-footer"> {{> inviteSend}} </div> {{/if}} </div> </template> <template name="inviteSend"> {{#autoForm collection="Invite" id="inviteSend" type="insert"}} {{> inviteFieldset}} <button id="sendInviteBtn" class="btn btn-primary" style="width:100%"></button> {{/autoForm}} </template> <template name="inviteFieldset"> <fieldset> {{> afQuickField name='email'}} </fieldset> </template> <template name="activateInviteToCompany"> {{#if currentUser}} . . {{ else }} {{#if inviteIsActivated}} , <b>{{userActivationCode}}</b> . . {{ else }} ! <br/><br/> , - <b>{{companyNameByInviteCode}}</b> <b>{{companyUserNameByInviteCode}}</b>. <br/><br/> <b>{{userActivationCode}}</b> !<br/><br/> , " / " , <b>{{companyNameByInviteCode}}</b>! {{/if}} {{/if}} </template>
Template.inviteSend.events({ 'click #sendInviteBtn': function () { // Email var email = $('#inviteSend [name=email]').val(); $('#sendInviteBtn').attr("disabled", true); // Email var existsInvite = Invite.findOne({email:email}); if ( existsInvite == undefined ) { // - Email if (UI._globalHelpers.validateEmail( email )) { // Email - Meteor.call('invationSender', email, function (error, result) { if (error) { //- . $('#inviteSend [name=email]').val(""); $('#sendInviteBtn').removeAttr("disabled"); bootbox.alert(" . ! : " + error.reason); } else { // . , $('#inviteSend [name=email]').val(""); $('#sendInviteBtn').removeAttr("disabled"); Meteor.subscribe('invite', Meteor.userId()); bootbox.alert(" " + email); } }); } else { // Email - $('#inviteSend [name=email]').val(""); $('#sendInviteBtn').removeAttr("disabled"); bootbox.alert("Email email@example.ru!"); } } else { // Email - . $('#inviteSend [name=email]').val(""); $('#sendInviteBtn').removeAttr("disabled"); bootbox.alert(" Email !"); } } }); Template.inviteList.events({ // 'click .deleteInviteBtn': function () { // Meteor.call('deleteInvite', this._id, function (error, result) { if (error) { bootbox.alert(" . ! : " + error.reason); } else { bootbox.alert(" !"); } }); } }); // Template.inviteList.helpers({ // invitedUsers: function () { return Invite.find(); }, // inviteTextStatus: function() { var textStatus = '-'; switch(this.status) { case INVITE_CREATED: textStatus = ''; break; case INVITE_EMAILED: textStatus = ''; break; case INVITE_COMPLETED: textStatus = ''; break; } return textStatus; }, // inviteIsComplete: function () { if (this.status == INVITE_COMPLETED) { return true; } else { return false; } }, // inviteIsEmailed: function () { if (this.status == INVITE_EMAILED) { return true; } else { return false; } }, // inviteIsCreated: function () { if (this.status == INVITE_CREATED) { return true; } else { return false; } } }); if (Meteor.isClient) { // Template.activateInviteToCompany.rendered = function () { Session.set('activationToken', Router.current().params.activationToken); }; // Template.activateInviteToCompany.helpers({ // companyNameByInviteCode: function () { var invite = Invite.findOne({token:Router.current().params.activationToken}); var company = Company.findOne({_id:invite.companyId}); return company.title; }, // companyUserNameByInviteCode: function () { var invite = Invite.findOne({token:Router.current().params.activationToken}); var company = Company.findOne({_id:invite.companyId}); var user = Meteor.users.findOne({_id:company.userId}); return user.profile.name; }, // userActivationCode: function () { return Router.current().params.activationToken; }, // // inviteIsActivated: function () { var userInviteCode = Router.current().params.activationToken; var invite = Invite.findOne({token: userInviteCode}); if (invite.status == INVITE_COMPLETED) { return true; } else { return false; } } }); }
Source: https://habr.com/ru/post/257113/
All Articles