使用Meteor帐户禁止系统?

时间:2014-03-28 01:02:36

标签: javascript twitter meteor blocking account

我正在使用使用accounts-ui和accounts-twitter的meteor的聊天应用程序。我希望能够禁止人们滥用网站,但我不确定如何做到这一点,或者甚至可能。有没有办法做到这一点?这是我用来运行聊天应用程序部分的代码:

ui.js:

// render all of our messages in the ui
Template.chatBox.helpers({
  "messages": function() {
    return chatCollection.find();
  }
});

// get the value for handlerbar helper user
Template.chatMessage.helpers({
  "user": function() {
    if(this.userId == 'me') {
      return this.userId;
    } else if(this.userId) {
      getUsername(this.userId);
      return Session.get('user-' + this.userId);
    } else {
      return 'anonymous-' + this.subscriptionId;
    }
  }
});

// when Send Chat clicked at the message to the collection
Template.chatBox.events({
    "click #send": function() {
            if (Meteor.user() == null) {
              alert("You must login to post");
            return;
          }
            $('#messages').animate({"scrollTop": $('#messages')[0].scrollHeight}, "fast");
            var message = $('#chat-message').val();

            // check to see if the message has any characters in it
            if (message.length < 1) {
              alert("You must enter a message to post.");
            return;
          }

            if (message.length > 200) {
              alert("Your message is too long... they can't read that fast!");
            return;
          }

            chatCollection.insert({
                userId: 'me',
                message: message
            });
            $('#chat-message').val('');

            //Validation
            var bot =Check_bots();

            if(bot==false)
            {    
            //add the message to the stream
            chatStream.emit('chat', message);
       }
        else
        {
            alert("Slow down! No need to post that fast.");
            return false;
        }
    },

    "keypress #chat-message": function(e) {
        if (Meteor.user() == null) {
            alert("You must login to post");
            return;
        }
        if (e.which == 13) {

          //Validation
       var bot =Check_bots();

        if(bot==false)
        {
            $('#messages').animate({"scrollTop": $('#messages')[0].scrollHeight}, "fast");
            console.log("you pressed enter");
            e.preventDefault();
            //repeat function from #send click event here
            var message = $('#chat-message').val();

            // check to see if the message has any characters in it
            if (message.length < 1) {
              alert("You must enter a message to post.");
            return;
          }

          if (message.length > 200) {
              alert("Your message is too long... they can't read that fast!");
            return;
          }

            chatCollection.insert({
                userId: 'me',
                message: message
            });
            $('#chat-message').val('');

            //add the message to the stream
            chatStream.emit('chat', message);
        }
        else
        {
            alert("Slow down! No need to post that fast.");
            return false;
        }
    }
  }
});

chatStream.on('chat', function(message) {
  chatCollection.insert({
    userId: this.userId,
    subscriptionId: this.subscriptionId,
    message: message
  });
});

var lastintime=0;
var defference=0;
var msg_count=0;

function Check_bots()
{
    var seconds = new Date().getTime() / 1000;
    seconds=parseInt(seconds);

    if(lastintime < seconds)
    {
        defference = seconds -lastintime;
        lastintime=seconds;

        if(defference<=5 && msg_count>=3)
        {
            return true;
        }
        else
        {
             return false;
        }
    }
}

users.js:

Accounts.ui.config({
  passwordSignupFields: "USERNAME_ONLY"
});

getUsername = function(id) {
  Meteor.subscribe('user-info', id);
  Deps.autorun(function() {
    var user = Meteor.users.findOne(id);
    if(user) {
      Session.set('user-' + id, user.username);
    }
  });
}

有人知道这样做的方法吗?

1 个答案:

答案 0 :(得分:8)

您可以使用 Accounts.validateLoginAttempt

服务器端代码

Accounts.validateLoginAttempt(function(info) {
    var user = info.user;

    if(user.isBanned) throw new Meteor.Error(403, 'You are banned');

});

使用上面的代码:如果您向MongoDB数据库中的任何用户添加了isBanned: true,该用户将无法登录。