added setPublicStatus, getPublicStatus, setPassword and isPasswordProtected

pull/85/merge
Peter 'Pita' Martischka 2011-08-10 22:33:31 +01:00
parent 292c68a0a5
commit ce1012438e
4 changed files with 96 additions and 11 deletions

View File

@ -289,7 +289,14 @@ exports.setPublicStatus = function(padID, publicStatus, callback)
return;
}
//convert string to boolean
if(typeof publicStatus == "string")
publicStatus = publicStatus == "true" ? true : false;
//set the password
pad.setPublicStatus(publicStatus);
callback();
});
}
@ -312,7 +319,7 @@ exports.getPublicStatus = function(padID, callback)
return;
}
callback(null, {publicStatus: pad.getPublicStatus()});
});
}
@ -335,7 +342,10 @@ exports.setPassword = function(padID, password, callback)
return;
}
//set the password
pad.setPassword(password);
callback();
});
}
@ -358,7 +368,7 @@ exports.isPasswordProtected = function(padID, callback)
return;
}
callback(null, {isPasswordProtected: pad.isPasswordProtected()});
});
}

View File

@ -8,6 +8,7 @@ var db = require("./DB").db;
var async = require("async");
var settings = require('../utils/Settings');
var authorManager = require("./AuthorManager");
var crypto = require("crypto");
/**
* Copied from the Etherpad source code. It converts Windows line breaks to Unix line breaks and convert Tabs to spaces
@ -44,6 +45,17 @@ Class('Pad', {
init: -1
}, // chatHead
publicStatus : {
is: 'rw',
init: false,
getterName : 'getPublicStatus'
}, //publicStatus
passwordHash : {
is: 'rw',
init: null
}, // passwordHash
id : { is : 'r' }
},
@ -82,7 +94,12 @@ Class('Pad', {
}
db.set("pad:"+this.id+":revs:"+newRev, newRevData);
db.set("pad:"+this.id, {atext: this.atext, pool: this.pool.toJsonable(), head: this.head, chatHead: this.chatHead});
db.set("pad:"+this.id, {atext: this.atext,
pool: this.pool.toJsonable(),
head: this.head,
chatHead: this.chatHead,
publicStatus: this.publicStatus,
passwordHash: this.passwordHash});
}, //appendRevision
getRevisionChangeset : function(revNum, callback)
@ -336,10 +353,23 @@ Class('Pad', {
_this.atext = value.atext;
_this.pool = _this.pool.fromJsonable(value.pool);
//ensure we have a local chatHead variable
if(value.chatHead != null)
_this.chatHead = value.chatHead;
else
_this.chatHead = -1;
//ensure we have a local publicStatus variable
if(value.publicStatus != null)
_this.publicStatus = value.publicStatus;
else
_this.publicStatus = false;
//ensure we have a local passwordHash variable
if(value.passwordHash != null)
_this.passwordHash = value.passwordHash;
else
_this.passwordHash = null;
}
//this pad doesn't exist, so create it
else
@ -351,7 +381,52 @@ Class('Pad', {
callback(null);
});
},
//set in db
setPublicStatus: function(publicStatus)
{
this.publicStatus = publicStatus;
db.setSub("pad:"+this.id, ["publicStatus"], this.publicStatus);
},
setPassword: function(password)
{
this.passwordHash = password == null ? null : hash(password, generateSalt());
db.setSub("pad:"+this.id, ["passwordHash"], this.passwordHash);
},
isCorrectPassword: function(password)
{
return compare(this.passwordHash, password)
},
isPasswordProtected: function()
{
return this.passwordHash != null;
}
}, // methods
});
/* Crypto helper methods */
function hash(password, salt)
{
var shasum = crypto.createHash('sha512');
shasum.update(password + salt);
return shasum.digest("hex") + "$" + salt;
}
function generateSalt()
{
var len = 86;
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./";
var randomstring = '';
for (var i = 0; i < len; i++)
{
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum, rnum + 1);
}
return randomstring;
}
function compare(hashStr, password)
{
return hash(password, hashStr.split("$")[1]) === hashStr;
}

View File

@ -52,11 +52,11 @@ var functions = {
"setText" : ["padID", "text"],
"getRevisionsCount" : ["padID"],
// "deletePad" : ["padID"],
"getReadOnlyID" : ["padID"]
// "setPublicStatus" : ["padID", "publicStatus"],
// "getPublicStatus" : ["padID"],
// "setPassword" : ["padID", "password"],
// "isPasswordProtected" : ["padID"]
"getReadOnlyID" : ["padID"],
"setPublicStatus" : ["padID", "publicStatus"],
"getPublicStatus" : ["padID"],
"setPassword" : ["padID", "password"],
"isPasswordProtected" : ["padID"]
};
/**

View File

@ -156,7 +156,7 @@
<div class="define">getRevisionsCount(padID)</div>
<div class="define">deletePad(padID)</div>
<div class="define">getReadOnlyID(padID)</div>
<div class="define">setPublicStatus(padID,publicStatud)</div>
<div class="define">setPublicStatus(padID,publicStatus)</div>
<div class="define">getPublicStatus(padID)</div>
<div class="define">setPassword(padID,password)</div>
<div class="define">isPasswordProtected(padID)</div>