couchdb-emily-tools
Advanced tools
Comparing version 0.0.2dev to 0.0.333dev
@@ -6,4 +6,5 @@ /* | ||
*/ | ||
define("CouchDBSecurity",["CouchDBStore"],function(e){function d(){var b="_security";this.setName=function(c){return c?(b=c,true):false};this.getName=function(){return b};this.load=function(c){return this.sync(c,b)}}return function(){d.prototype=new e;return new d}}); | ||
define("CouchDBUser",["CouchDBStore"],function(e){function d(){var b="_users",c="org.couchdb.user:";this.getUserDB=function(){return b};this.setUserDB=function(a){return a?(b=a,true):false};this.getIdPrefix=function(){return c};this.setIdPrefix=function(a){return a?(c=a,true):false};this.setId=function(a){return a?(this.set("_id",c+a),true):false};this.getId=function(){return this.get("_id")};this.load=function(a){return a?this.sync(b,c+a):false}}return function(){d.prototype=new e;return new d}}); | ||
define("CouchDBUsers",["Promise"],function(e){return function(){var d=null;this.setTransport=function(b){return b instanceof Object?(d=b,true):false};this.getTransport=function(){return d};this.login=function(b,c){var a=new e;typeof b=="string"&&typeof c=="string"?d.request("CouchDB",{method:"GET",path:"/_users/org.couchdb.user:"+b,auth:b+":"+c},a.resolve,a):a.reject({error:"name & password must be strings"});return a}}}); | ||
define("CouchDBSecurity",["CouchDBStore"],function(e){function d(){var a="_security";this.setName=function(c){return c?(a=c,true):false};this.getName=function(){return a};this.load=function(c){return this.sync(c,a)}}return function(){d.prototype=new e;return new d}}); | ||
define("CouchDBUser",["CouchDBStore","Promise"],function(e,d){function a(){var c="_users",a="org.couchdb.user:";this.getUserDB=function(){return c};this.setUserDB=function(b){return b?(c=b,true):false};this.getIdPrefix=function(){return a};this.setIdPrefix=function(b){return b?(a=b,true):false};this.setId=function(b){return b?(this.set("_id",a+b),true):false};this.getId=function(){return this.get("_id")};this.load=function(b){return this.sync(c,a+b)};this.login=function(){var b=new d,a=this.get("name"), | ||
c=this.get("password");a&&typeof a=="string"&&typeof c=="string"?this.getTransport().request("CouchDB",{method:"GET",path:"/_users/org.couchdb.user:"+a,auth:a+":"+c},b.resolve,b):b.reject({error:"name & password must be strings"});return b};this.create=function(){var b=new d;this.get("type")||this.set("type","user");this.get("roles")||this.set("roles",[]);this.load(this.get("name")).then(function(){b.reject({error:"Failed to create user. The user already exists"})},function(){this.upload().then(function(a){b.resolve(a)}, | ||
function(a){b.reject(a)})},this);return b}}return function(){a.prototype=new e;return new a}}); |
@@ -80,3 +80,3 @@ /** | ||
["CouchDBStore"], | ||
["CouchDBStore", "Promise"], | ||
@@ -88,3 +88,3 @@ /** | ||
*/ | ||
function CouchDBUser(CouchDBStore) { | ||
function CouchDBUser(CouchDBStore, Promise) { | ||
@@ -181,76 +181,25 @@ /** | ||
this.load = function load(id) { | ||
if (id) { | ||
return this.sync(_userDB, _idPrefix + id); | ||
} else { | ||
return false; | ||
} | ||
return this.sync(_userDB, _idPrefix + id); | ||
}; | ||
}; | ||
return function CouchDBUserFactory() { | ||
CouchDBUserConstructor.prototype = new CouchDBStore; | ||
return new CouchDBUserConstructor; | ||
}; | ||
});/** | ||
* https://github.com/flams/CouchDB-emily-tools | ||
* The MIT License (MIT) | ||
* Copyright (c) 2012 Olivier Scherrer <pode.fr@gmail.com> | ||
*/ | ||
define("CouchDBUsers", | ||
["Promise"], | ||
/** | ||
* @class | ||
* CouchDBUsers is a library with utility tools to manage users. | ||
* It helps with the creation of users and login | ||
*/ | ||
function CouchDBUsers(Promise) { | ||
return function CouchDBUsersConstructor() { | ||
var _transport = null; | ||
/** | ||
* Set the transport to be used, must be an Emily Transport | ||
* @param transport | ||
* @returns true if Transport is from Emily | ||
*/ | ||
this.setTransport = function setTransport(transport) { | ||
if (transport instanceof Object) { | ||
_transport = transport; | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
/** | ||
* Get the currently set transport | ||
* For debugging only | ||
* @private | ||
* @returns {Transport} | ||
*/ | ||
this.getTransport = function getTransport() { | ||
return _transport; | ||
}; | ||
/** | ||
* User login. It fetches the user document | ||
* the corresponds to the given name. | ||
* It will only work if the credentials are correct. | ||
* @param {String} name the name of the user | ||
* @param {String} password the password of the user | ||
* Gets the user profile in couchDB by using its own credentials. | ||
* name and password must be set prior to calling login, or the promise will be rejected. | ||
* If the login is successful, the promise is fulfilled with the user information like: | ||
* { _id: 'org.couchdb.user:couchdb', | ||
* _rev: '1-8995e8ff247dae75048ab2dc800136d7', | ||
* name: 'couchdb', | ||
* password: null, | ||
* roles: [], | ||
* type: 'user' } | ||
* | ||
* @returns {Promise} | ||
*/ | ||
this.login = function login(name, password) { | ||
this.login = function login() { | ||
var promise = new Promise, | ||
name = this.get("name"), | ||
password = this.get("password"); | ||
var promise = new Promise; | ||
if (typeof name == "string" && typeof password == "string") { | ||
_transport.request("CouchDB", { | ||
if (name && typeof name == "string" && typeof password == "string") { | ||
this.getTransport().request("CouchDB", { | ||
method: "GET", | ||
@@ -269,9 +218,50 @@ path: "/_users/org.couchdb.user:"+name, | ||
return promise; | ||
}; | ||
/** | ||
* Adds a user to the database | ||
* The following fields must be set prior to calling create: | ||
* name: the name of the user | ||
* password: its desired password, NOT encrypted | ||
* | ||
* If not specified, the following fields have default values: | ||
* type: "user" | ||
* roles: [] | ||
* | ||
* The function itself will not warn you for incorrect fields | ||
* but the promise that is returned will fulfilled with couchdb's reply. | ||
* @returns {Promise} | ||
*/ | ||
this.create = function create() { | ||
var promise = new Promise; | ||
if (!this.get("type")) { | ||
this.set("type", "user"); | ||
} | ||
if (!this.get("roles")) { | ||
this.set("roles", []); | ||
} | ||
this.load(this.get("name")).then(function () { | ||
promise.reject({error: "Failed to create user. The user already exists"}); | ||
}, function () { | ||
this.upload().then(function (success) { | ||
promise.resolve(success); | ||
}, function (error) { | ||
promise.reject(error); | ||
}); | ||
}, this); | ||
return promise; | ||
}; | ||
}; | ||
return function CouchDBUserFactory() { | ||
CouchDBUserConstructor.prototype = new CouchDBStore; | ||
return new CouchDBUserConstructor; | ||
}; | ||
}); |
{ | ||
"name": "couchdb-emily-tools", | ||
"description": "WIP - CouchDB Tools for Emily&Olives applications", | ||
"version": "0.0.2dev", | ||
"version": "0.0.333dev", | ||
"homepage": "https://github.com/flams/CouchDB-emily-tools", | ||
@@ -6,0 +6,0 @@ "licenses": [{ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16323
246