Пример #1
0
var deleteAdmin = function(username, callback) {
    db.request({
        type: 'DELETE',
        url: '/_config/admins/' + encodeURIComponent(username),
        contentType: 'application/json'
    }, callback);
};
Пример #2
0
exports.get = function (id, callback) {
    var req = {
        url: '/_replicator/' + db.encode(id),
        cache: false /* Work around IE7 issue */
    };
    db.request(req, callback);
};
Пример #3
0
exports.login = function (username, password, callback) {
    if (!isBrowser()) {
        throw new Error('login cannot be called server-side');
    }
    db.request({
        type: "POST",
        url: "/_session",
        data: {name: username, password: password}
    },
    function (err, resp) {
        if (resp && resp.ok) {
            // TODO: for some reason resp.name is set to null in the response
            // even though the roles are correct for the user! Look into this
            // and see if its a bug in couchdb, for now, just using the username
            // given to the login function instead, since we know the login
            // request was accepted.
            exports.userCtx = {name: username, roles: resp.roles};
            exports.session = {userCtx: exports.userCtx};
            exports.emit('change', exports.userCtx);
        }
        if (callback) {
            callback(err, resp);
        }
    });
};
Пример #4
0
var getAdmin = function(username, callback) {
    db.request({
        type: 'GET',
        url: '/_config/admins/' + username,
        contentType: 'application/json'
    }, callback);
};
exports.info = function (callback) {
    if (!isBrowser()) {
        throw new Error('info cannot be called server-side');
    }
    db.request({
        type: "GET",
        url: "/_session"
    },
    function (err, resp) {
        console.log("asking DB for session");
                console.log(resp);
                console.log(exports.userCtx);
        
        log("server session")
        var oldUserCtx = exports.userCtx;
                console.log("change0")
        exports.session = resp;
        exports.userCtx = (resp && resp.userCtx) || {name: null, roles: []};
        // TODO: should this check for differences in more than just name?
        //YES!!
        if (!oldUserCtx || oldUserCtx.name !== exports.userCtx.name) {
        }
        console.log("change!")
            exports.emit('change', exports.userCtx);
        //Test with force change 
        
        if (callback) {
            callback(err, resp);
        }
    });
};
Пример #6
0
var deleteUser = function(authdb, id, user, callback) {
    db.request({
        type: 'DELETE',
        url: '/' + db.encode(authdb) + '/' + db.encode(id) +
             '?rev=' + db.encode(user._rev),
        contentType: 'application/json'
    }, callback);
};
Пример #7
0
exports._replicate = function (options, callback) {
    var req = {
        type: 'POST',
        url: '/_replicate',
        contentType: 'application/json',
        data: JSON.stringify(options)
    };
    db.request(req, callback);
};
Пример #8
0
var createAdmin = function(username, password, callback) {
    var url = '/_config/admins/' + encodeURIComponent(username);
    var req = {
        type: 'PUT',
        url: url,
        data: JSON.stringify(password),
        processData: false,
        contentType: 'application/json'
    };
    db.request(req, callback);
};
Пример #9
0
var saveUser = function(authdb, doc, callback) {
    var url = '/' + authdb + '/' + doc._id;
    var req = {
        type: 'PUT',
        url: url,
        data: JSON.stringify(doc),
        processData: false,
        contentType: 'application/json'
    };
    db.request(req, callback);
};
Пример #10
0
var authdb = function(callback) {
    db.request({
        type: "GET",
        url: "/_session"
    },
    function (err, resp) {
        if (err) {
            return callback(err, null);
        }
        callback(null, resp.info.authentication_db);
    });
};
Пример #11
0
 authdb(function (err, authdb) {
     if (err) {
         return callback(err);
     }
     db.request({
         type: 'GET',
         url: '/' + db.encode(authdb) + '/' + db.encode(id),
         contentType: 'application/json'
     },
     function (err, user) {
         callback(err, user, {authdb: authdb, id: id});
     });
 });
Пример #12
0
exports.start = function (options, callback) {
    if (!options.source) {
        throw new Error('source parameter must be provided');
    }
    if (!options.target) {
        throw new Error('target parameter must be provided');
    }
    var req = {
        type: 'POST',
        url: '/_replicator',
        data: JSON.stringify(options),
        contentType: 'application/json'
    };
    db.request(req, callback);
};
Пример #13
0
 authdb(function (err, authdb) {
     if (err) {
         callback(err, null);
     }
     var req = {
         type: 'GET',
         url: '/' + db.encode(authdb) + '/_all_docs',
         data: db.stringifyQuery(q),
         expect_json: true
     };
     db.request(req, function(err, result) {
         if (err) {
             return callback(err, null);
         }
         var users = _(result.rows).select(function(row) {
             return row.id.match(/^org\.couchdb\.user/);
         });
         callback(null, users);
     });
 });
Пример #14
0
exports.info = function (callback) {
    if (!isBrowser()) {
        throw new Error('info cannot be called server-side');
    }
    db.request({
        type: "GET",
        url: "/_session"
    },
    function (err, resp) {
        var oldUserCtx = exports.userCtx;
        exports.session = resp;
        exports.userCtx = (resp && resp.userCtx) || {name: null, roles: []};
        // TODO: should this check for differences in more than just name?
        if (!oldUserCtx || oldUserCtx.name !== exports.userCtx.name) {
            exports.emit('change', exports.userCtx);
        }
        if (callback) {
            callback(err, resp);
        }
    });
};
Пример #15
0
exports.logout = function (callback) {
    if (!isBrowser()) {
        throw new Error('logout cannot be called server-side');
    }
    db.request({
        type: "DELETE",
        url: "/_session", // don't need baseURL, /_session always available
        username: "******",
        password : "******"
    },
    function (err, resp) {
        if (resp && resp.ok) {
            exports.userCtx = {name: null, roles: []};
            exports.session = {userCtx: exports.userCtx};
            exports.emit('change', exports.userCtx);
        }
        if (callback) {
            callback(err, resp);
        }
    });
};
Пример #16
0
exports.stop = function (doc, callback, /*optional*/options) {
    if (options === undefined) {
        options = {};
    }
    if (options.limit === undefined || options.limit === null) {
        options.limit = 3; /* times */
    }
    if (options.delay === undefined || options.delay === null) {
        options.delay = 500; /* ms */
    }

    var req = {
        type: 'DELETE',
        url: '/_replicator/' +
          db.encode(doc._id) +
          '?rev=' + db.encode(doc._rev)
    };

    db.request(req, function (err, rv) {

        if (err && err.status === 409) {  /* Document update conflict */

            /* Race condition:
                The CouchDB replication finished (or was updated) between
                the caller's replication.get and now. Subject to restrictions
                in options, call replication.get and then try again. */

            /* Termination condition for recursion... */
            if (options.limit > 0) {

                /* ...with well-defined progress toward it */
                options.limit -= 1;

                return exports.get(doc._id, function (e, d) {
                    if (e) {
                        throw new Error(
                          'The specified replication document changed ' +
                          'since last read, and we failed to re-request it'
                        );
                    }
                    /* Go around */
                    setTimeout(function () {
                        return exports.stop(d, callback, options);
                    }, options.delay);
                });
            }

        } else {

            /* Normal case:
                Replication document was not changed since the last
                read; go ahead and invoke the callback and return. */

            return callback(err, rv);
        }

        /* Not reached */
        return false;

    });

};