daily pastebin goal
63%
SHARE
TWEET

Untitled

a guest Jan 25th, 2016 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // User database simulation
  2. var users = [];
  3.  
  4. var defaultUser = {
  5.     'rights': 1, /* 0 - 3: 0 is banned, 1 is default, 2 is moderator, 3 is admin */
  6.   'activated': false,
  7.   'createdAt': null,
  8.   'updatedAt': null,
  9.   'username': null,
  10.   'userId': null,
  11.   'email': null,
  12.   'pass': null, /* will be encrypted */
  13.   'profile': {
  14.     'sex': null,
  15.     'age': null,
  16.     'avatar': null,
  17.     'updatedAt': null,
  18.   },
  19.   'messages': {
  20.     'inbox': [],
  21.     'outbox': [],
  22.     'trash': [],
  23.     'drafts': []
  24.   }
  25. };
  26.  
  27. var defaultMessage = {
  28.     'id': null,
  29.     'date': null,
  30.     'from': null,
  31.     'to': null,
  32.     'message': null
  33. };
  34.  
  35. var userManagement = {
  36.     'register': function(username, email, pass){
  37.         var user = $.extend({}, defaultUser);
  38.         user.username = username;
  39.         user.email = email;
  40.         user.pass = pass;
  41.         user.userId = username + '_' + Math.floor(Date.now() / 1000);
  42.         // If everything is valid, register:
  43.         // User database insert simulation
  44.         users.push(user);
  45.         console.log('Registered', user);
  46.   },
  47.     'login': function(username, pass) {
  48.     // User database query simulation
  49.     for(var i = 0, l = users.length; i < l; i++) {
  50.         var user = users[i];
  51.           if(user.username === username) {
  52.         if(user.pass === pass) {
  53.             console.log('Logged in', user);
  54.         } else {
  55.             console.log('Pass incorrect');
  56.         }
  57.       } else {
  58.         console.log('User not found');
  59.       }
  60.     }
  61.   },
  62.   'forgotUsername': function(email) {
  63.     // User database query simulation
  64.     for(var i = 0, l = users.length; i < l; i++) {
  65.         var user = users[i];
  66.       if(user.email === email) {
  67.         console.log('username ['+ user.username +'] send to ' + user.email);
  68.       } else {
  69.         console.log('User not found');
  70.       }
  71.     }  
  72.   },
  73.   'forgotPass': function(username) {
  74.     // User database query simulation
  75.     for(var i = 0, l = users.length; i < l; i++) {
  76.         var user = users[i];
  77.       if(user.username === username) {
  78.         console.log('pass from user ['+ user.username +'] send to ' + user.email);
  79.       } else {
  80.         console.log('User not found');
  81.       }
  82.     }  
  83.   },
  84.   'getUserById': function(userId){
  85.     var key;
  86.     for(var i = 0, l = users.length; i < l; i++) {
  87.         var user = users[i];
  88.       if(user.userId === userId) {
  89.         return user;    
  90.       }
  91.     }
  92.     return null;
  93.   },
  94.   'getUserByUsername': function(username){
  95.     for(var i = 0, l = users.length; i < l; i++) {
  96.         var user = users[i];
  97.       if(user.username === username) {
  98.         return user;    
  99.       }
  100.     }
  101.     return null;
  102.   }
  103.   /* TODO: updateProfile, activate */
  104. }
  105.  
  106. var message = {
  107.     'send': function(fromUserId, toUserId, msg){
  108.     var sender = userManagement.getUserById(fromUserId);
  109.     var receiver = userManagement.getUserById(toUserId);
  110.     var message = $.extend({}, defaultMessage);
  111.     message.id = fromUserId + '_' + Math.floor(Date.now() / 1000);
  112.     message.from = sender.username;
  113.     message.fromUserId = fromUserId;
  114.     message.to = receiver.username
  115.     message.toUserId = toUserId;
  116.     message.message = msg;
  117.     message.date = new Date();
  118.     sender.messages.outbox.push(message);
  119.     receiver.messages.inbox.push(message);
  120.   }
  121.   /* TODO: delete, move etc. */
  122. }
  123.  
  124. userManagement.register('barry', 'barry@test.nl', 'bcf2ibc');
  125. userManagement.register('john', 'john@test.nl', 'bahjscb');
  126.  
  127. userManagement.login('test', 'blabla'); // fail
  128. userManagement.login('barry', 'blabla'); // fail
  129. userManagement.login('barry', 'bcf2ibc'); // success
  130. userManagement.login('John', 'bahjscb'); // success
  131. //userManagement.forgotPass('barry');
  132.  
  133. var barry = userManagement.getUserByUsername('barry');
  134. var john = userManagement.getUserByUsername('John');
  135.  
  136. message.send(barry.userId, john.userId, 'My test message.');
  137. message.send(barry.userId, john.userId, 'You received my previous message?');
  138. message.send(john.userId, barry.userId, 'Yes I did received them.');
  139.  
  140. console.log(users);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top