Advertisement
Guest User

Untitled

a guest
Apr 18th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. var lib = require('./../modules/lib');
  2. var bcrypt = require('bcrypt-nodejs');
  3. var pg = require('pg');
  4.  
  5. /*
  6. * Object Constructor
  7. */
  8. function User() {
  9. this.UserId = -1;
  10. this.EmailAddress = '';
  11. this.Password = '';
  12. this.Created = '';
  13. this.FirstName = '';
  14. this.OtherName = '';
  15. this.LastName = '';
  16. this.Title = '';
  17. this.DateOfBirth = '';
  18. this.Role = '';
  19. this.PostNominal = '';
  20. this.Username = '';
  21. }
  22.  
  23. /*
  24. * Hash password in pass propery of object {o}
  25. * @param o Object
  26. */
  27. User.prototype.hash = function (o) {
  28. var salt = bcrypt.genSaltSync(8);
  29. return bcrypt.hashSync(o.pass, salt)
  30. };
  31.  
  32. /*
  33. * validate pass property of object {o}
  34. * against user password property
  35. * @param o Object
  36. * @param callback function(err, valid)
  37. */
  38. User.prototype.validate = function (o, callback) {
  39. callback(bcrypt.compareSync(o.pass, User.Password));
  40. };
  41.  
  42. User.prototype.findAll = function () {
  43.  
  44. };
  45. User.prototype.findOne = function (o) {
  46.  
  47. };
  48.  
  49. /*
  50. * Find user in database with object from form {object}
  51. * populate object with user data for later interrogation
  52. * @param o Object
  53. * @param callback function(err, user)
  54. */
  55. User.prototype.findUser = function (o, callback) {
  56. var error;
  57. var results = [];
  58. var query = {};
  59.  
  60. if (o.user.contains('.ac.uk') || o.user.contains('.co.uk') || o.user.contains('.com') && o.user.contains('@')) {
  61. query = {
  62. text: 'SELECT * from "User" WHERE "EmailAddress"=$1',
  63. values: [o.user.toLowerCase()]
  64. }
  65. } else {
  66. query = {
  67. text: 'SELECT * from "User" WHERE "Username"=$1 LIMIT 1',
  68. values: [o.user]
  69. };
  70. }
  71.  
  72. pg.connect(process.env.DATABASE_URL, function (err, client, done) {
  73. /* if Connection Callback Error */
  74. if (err) {
  75. console.log(err);
  76. }
  77. /* Client runs query */
  78. var q = client.query(query, function (err, result) {
  79. /* Client Q has error */
  80. if (err) throw err;
  81. else return result;
  82. });
  83. /* Client Q has row */
  84. q.on('row', function (row, result) {
  85. results.push(row);
  86. result.addRow(row);
  87. });
  88. /* Client Q has finished */
  89. q.on('end', function (result) {
  90. done();
  91. var found = false;
  92. if (result.rows[0] != undefined) {
  93. setResults(result);
  94. found = true;
  95. }
  96. else found = false;
  97. callback(error, found, result.rows[0]);
  98. });
  99. });
  100. };
  101.  
  102. /*
  103. * populate User object with query data {results}
  104. * @param results Object
  105. */
  106. setResults = function (results) {
  107. set("UserId", results.rows[0].UserId);
  108. set("EmailAddress", results.rows[0].EmailAddress);
  109. set("Password", results.rows[0].Password);
  110. set("Created", results.rows[0].Created);
  111. set("OtherNames", results.rows[0].OtherName);
  112. set("LastName", results.rows[0].LastName);
  113. set("Title", results.rows[0].Title);
  114. set("DateOfBirth", results.rows[0].DateOfBirth);
  115. set("Role", results.rows[0].Role);
  116. set("Username", results.rows[0].Username);
  117. set("FirstName", results.rows[0].FirstName);
  118. // if (results.rows[0].PostNominal!='undefined')
  119. set("PostNominal", results.rows[0].PostNominal);
  120. };
  121.  
  122. /*
  123. * Returns the data in
  124. * current instance of user
  125. */
  126. User.prototype.getResults = function () {
  127. return User;
  128. };
  129. /*
  130. * find by Id function
  131. * TODO FINISH
  132. * @param o Object
  133. */
  134. User.prototype.findbyId = function (id) {
  135. query = {
  136. text: 'SELECT * FROM "User" WHERE "UserId"=$1',
  137. values: [id]
  138. };
  139. };
  140. /*
  141. * find by {what} function
  142. * TODO FINISH
  143. * @param o Object
  144. */
  145. User.prototype.find = function (where, op, what) {
  146. query = {
  147. text: 'SELECT * FROM "User" WHERE $1 $2 $3',
  148. values: [where, op, what]
  149. };
  150. };
  151.  
  152. /*
  153. * sets {property} of user
  154. * to {values} provided
  155. * @param prop String
  156. * @param val Type
  157. */
  158. function set(prop, val) {
  159. User[prop] = val;
  160. }
  161.  
  162. /*
  163. * gets {property} of user
  164. * to {values} provided
  165. * @param prop String
  166. */
  167. function get(prop) {
  168. return User[prop];
  169. }
  170.  
  171. /*
  172. * get {user} and populate object with user
  173. * for rendering of their profile
  174. * @param o Object
  175. */
  176. User.prototype.getUser = function (o) {
  177. var results = [];
  178. var query = {
  179. text: 'SELECT * from "User" WHERE "Username"=$1 LIMIT 1',
  180. values: [o.user]
  181. };
  182.  
  183. pg.connect(process.env.DATABASE_URL, function (err, client, done) {
  184. /* if Connection Callback Error */
  185. if (err) {
  186. console.log(err);
  187. }
  188. /* CLient runs query */
  189. var q = client.query(query, function (err, result) {
  190. /* Client Q has error */
  191. if (err) throw err;
  192. else return result;
  193. });
  194. /* Client Q has row */
  195. q.on('row', function (row, result) {
  196. results.push(row);
  197. result.addRow(row);
  198. });
  199. /* Client Q has finished */
  200. q.on('end', function (result) {
  201. done();
  202. var found = false;
  203. if (result.rows[0] != undefined) {
  204. setResults(result);
  205. found = true;
  206. }
  207. else found = false;
  208. callback(error, found, result.rows[0]);
  209. });
  210. });
  211. };
  212.  
  213. module.exports = User;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement