1 | var mongoose = require('mongoose');
|
2 |
|
3 | bcrypt = require('./crypt');
|
4 |
|
5 | exports.bcrypt = bcrypt;
|
6 |
|
7 |
|
8 | function MongooseAdminUser() {
|
9 | this.fields = {};
|
10 |
|
11 | var AdminUserData = new mongoose.Schema({
|
12 | username:{type:String, required:true, unique:true},
|
13 | passwordHash:{type:String, editable:false},
|
14 | is_superuser :{type:Boolean,'default':false},
|
15 | permissions:[{type:mongoose.Schema.ObjectId, ref:'_MongooseAdminPermission'}]
|
16 | },{strict:true});
|
17 | mongoose.model('_MongooseAdminUser', AdminUserData);
|
18 | };
|
19 |
|
20 | MongooseAdminUser.prototype.toSessionStore = function() {
|
21 | var serialized = {};
|
22 | for (var i in this) {
|
23 | if (typeof i !== 'function' || typeof i !== 'object') {
|
24 | serialized[i] = this[i];
|
25 | }
|
26 | }
|
27 |
|
28 | return JSON.stringify(serialized);
|
29 | };
|
30 |
|
31 | MongooseAdminUser.fromSessionStore = function(sessionStore) {
|
32 | var sessionObject = JSON.parse(sessionStore);
|
33 | var adminUser = new MongooseAdminUser();
|
34 | for (var i in sessionObject) {
|
35 | if (sessionObject.hasOwnProperty(i)) {
|
36 | adminUser[i] = sessionObject[i];
|
37 | }
|
38 | }
|
39 |
|
40 | return adminUser;
|
41 | };
|
42 |
|
43 | MongooseAdminUser.ensureExists = function(username, password, onReady) {
|
44 | var adminUser = new MongooseAdminUser();
|
45 | var adminUserModel = mongoose.model('_MongooseAdminUser');
|
46 |
|
47 | adminUserModel.findOne({'username': username}, function(err, adminUserData) {
|
48 | if (err) {
|
49 | console.log('Unable to check if admin user exists because: ' + err);
|
50 | oReady('Unable to check if user exist', null);
|
51 | } else {
|
52 | if (adminUserData) {
|
53 | var salt = bcrypt.gen_salt_sync(10);
|
54 | adminUserData.passwordHash = bcrypt.encrypt_sync(password, salt);
|
55 | } else {
|
56 | adminUserData = new adminUserModel();
|
57 | adminUserData.username = username;
|
58 | var salt = bcrypt.gen_salt_sync(10);
|
59 | adminUserData.passwordHash = bcrypt.encrypt_sync(password, salt);
|
60 | }
|
61 | adminUserData.is_superuser = true;
|
62 | adminUserData.save(function(err) {
|
63 | if (err) {
|
64 | console.log('Unable to create or update admin user because: ' + err);
|
65 | onReady('Unable to create or update admin user', null);
|
66 | } else {
|
67 | adminUser.fields = adminUserData;
|
68 | onReady(null, adminUser);
|
69 | }
|
70 | });
|
71 | }
|
72 | });
|
73 | };
|
74 |
|
75 | MongooseAdminUser.getByUsernamePassword = function(username, password, onReady) {
|
76 | var adminUser = new MongooseAdminUser();
|
77 | var adminUserModel = mongoose.model('_MongooseAdminUser');
|
78 |
|
79 | adminUserModel.findOne({'username': username}, function(err, adminUserData) {
|
80 | if (err) {
|
81 | console.log('Unable to get admin user because: ' + err);
|
82 | onReady('Unable to get admin user', null);
|
83 | } else {
|
84 | if (adminUserData) {
|
85 | if (bcrypt.compare_sync(password, adminUserData.passwordHash)) {
|
86 | adminUser.fields = adminUserData;
|
87 | onReady(null, adminUser);
|
88 | } else {
|
89 | onReady(null, null);
|
90 | }
|
91 | } else {
|
92 | onReady(null, null);
|
93 | }
|
94 | }
|
95 | });
|
96 | };
|
97 |
|
98 | exports.MongooseAdminUser = MongooseAdminUser;
|