Created
February 14, 2012 17:00
-
-
Save boxxxie/1828125 to your computer and use it in GitHub Desktop.
_users couchdb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//http://blog.couchbase.com/what%E2%80%99s-new-couchdb-10-%E2%80%94-part-4-security%E2%80%99n-stuff-users-authentication-authorisation-and-permissions | |
//i wasn't able to do the curl stuff from the above site, but i was able to do stuff with $.couch in futon | |
//editing an existing user (need to be logged in as admin to do this) | |
$.couch.userDb(function(db){ | |
db.openDoc("org.couchdb.user:10", //user name is "10" | |
{success:function(user){ | |
user.roles.push("hello"); | |
db.saveDoc(user); | |
}})}) | |
//signing up a new user (need to be logged on as admin to set roles) | |
//user only needs a name and the userDoc _id == "org.couchdb.user:" + .name | |
$.couch.signup({name:"user name", roles:["pos_sales"]}, | |
"password", | |
{success:function(){ | |
console.log(arguments);}}) | |
//logging in as admin, or other. returns response.roles[] | |
$.couch.login({name:"paul",password:"12345", | |
success:function(response){ | |
console.log(response) | |
}}) | |
function autoLog(){console.log(arguments);} | |
function autoSuccess(){return {success:autoLog};} | |
//getting session information | |
$.couch.session(autoSuccess()) | |
/* response session obj | |
//unauthenticated requests to CouchDB will not see values for userCtx.name or userCtx.roles. | |
{info: { | |
authenticated: "cookie", | |
authentication_db: "_users", | |
authentication_handlers: ["oauth","cookie","default"] | |
}, | |
ok: true, | |
userCtx:{ | |
name: "paul", | |
roles: ["_admin"] //notice "_admin" | |
} | |
} | |
*/ | |
//creating a user doc with a new password (doesn't save to the db, just makes a doc with a proper password) | |
$.couch.prepareUserDoc({name:"special",roles:["myrole"]},'12345') | |
//XHR finished loading: "http://localhost:5984/_uuids?count=1". | |
Object = { | |
_id: "org.couchdb.user:special", | |
name: "special", | |
password_sha: "ebd098d5a41bd11756ab8a8f3a759ba7b2d123bf", | |
roles: Array[1], | |
salt: "129d294032a8aafd9c188bca3b00a706", | |
type: "user", | |
__proto__: Object | |
} | |
//this should change the user password (doc validation must allow for this to happen) | |
function changeUserPassword(userName,newPassword){ | |
$.couch.userDb | |
(function(db){ | |
db.openDoc("org.couchdb.user:"+userName, | |
{success:function(user){ | |
//$.couch.prepareUserDoc(user,newPassword) | |
$.couch.signup(user,newPassword); | |
}})}) | |
} | |
function printSession(){ | |
$.couch.session({success:function(){console.log(arguments);}}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment