GithubHelp home page GithubHelp logo

Comments (6)

colinskow avatar colinskow commented on July 4, 2024 2

I've been giving some thought to this and we definitely need a user database manager. This is not only to keep the design docs up to date when they change, but also to add and remove user roles and change permissions.

from superlogin.

colinskow avatar colinskow commented on July 4, 2024

When you create a user or add a new database to an existing user, SuperLogin automatically seeds the design docs that you specify in your config. If you update the design docs later you will need to manually reseed them.

Maintaining DBs seeded with the correct design docs as they update is complex and outside the scope of SuperLogin. I would love to see an external plugin to help with the tedious tasks of managing userDBs.

SL uses pouchdb-seed-design behind the scenes.

from superlogin.

SukantGujar avatar SukantGujar commented on July 4, 2024

So, while trying to solve this for my app, I went through the code of addUserDB and pouch-seed-design. Apparently, this flow is already taking care of -

  1. Ignoring existing DBs.
  2. Seeding only changed docs in the said DB.
    For me, simply invoking superlogin.addUserDB in the login event should work, as it will not recreate the DB but still update only changed/new design docs.
    However, I think with this minimal logic, Superlogin can internally take care of this need without much complication.
    Please provide your thoughts. I can request a PR.

This feature is a certain requirement for my application, one way or the other. Its a given thing that as new features are added, user DBs will have new designs and modifications. I'd hope you could allocate some of your time to reevaluate this for Superlogin.

from superlogin.

peteruithoven avatar peteruithoven commented on July 4, 2024

We also have the need for this, if only to more easily develop a validate_doc_update function.
So I've started on a script we can run as util. A pitty is that most of it's logic is almost a direct copy of user.js's addUserDBs and dbauth/index.js's addUserDB functions. It currently only creates & updates shared databases and it doesn't update existing sessions. (See my question here: #152 (comment))

const SuperLogin = require('superlogin');
const superloginConfig = require('../superlogin.config.js');
const DBAuth = require('superlogin/lib/dbauth');
const PouchDB = require('pouchdb');
const seed = require('pouchdb-seed-design');
const util = require('superlogin/lib/util');

const superlogin = new SuperLogin(superloginConfig);
const { config } = superlogin;
const dbAuth = new DBAuth(config/* , userDB, couchAuthDB*/);

if (!config.getItem('userDBs.defaultDBs')) {
  console.log('no userDBs.defaultDBs defined');
  process.exit();
}

const cloudant = config.getItem('dbServer.cloudant');
let adapter;
if (cloudant) {
  adapter = require('superlogin/lib/dbauth/cloudant');
} else {
  throw new Error('CouchDB not supported');
}

let allDBs = [];
function addDBs(dbs, type) {
  if (!Array.isArray(dbs)) return;
  dbs.forEach(dbName => {
    const dbConfig = dbAuth.getDBConfig(dbName);
    dbConfig.type = type; // override type
    if (type !== 'shared') return; // private not supported yet
    allDBs.push(dbConfig);
  });
}
addDBs(config.getItem('userDBs.defaultDBs.private'), 'private');
addDBs(config.getItem('userDBs.defaultDBs.shared'), 'shared');

function updateDBs(dbs) {
  console.log('updateDBs: ', dbs);
  return asyncIterator(dbs, dbConfig => {
    console.log('dbConfig: ', dbConfig);
    const { name, adminRoles, memberRoles, designDocs } = dbConfig;
    console.log('updating: ', name);
    let newDB;
    return dbAuth.createDB(name)
      .then(response => {
        if (response !== false) console.log('Created db: ', name);
        newDB = new PouchDB(util.getDBURL(config.getItem('dbServer')) + '/' + name);
        return adapter.initSecurity(newDB, adminRoles, memberRoles);
      })
      .then(() => {
        // Seed the design docs
        if (designDocs && designDocs instanceof Array) {
          // designDocs.forEach(ddName => {
          console.log('designDocs: ', designDocs);
          return asyncIterator(designDocs, ddName => {
            console.log('ddName: ', ddName);
            const dDoc = dbAuth.getDesignDoc(ddName);
            console.log('dDoc: ', dDoc);
            if (dDoc) {
              return seed(newDB, dDoc);
            } else {
              console.warn('Failed to locate design doc: ' + ddName);
            }
          });
        }
      });
  });
}

updateDBs(allDBs).then(() => {
  process.exit();
});

function asyncIterator(args, callback) {
  return new Promise((resolve, reject) => {
    const results = [];
    if (args.length === 0) resolve(results);
    const clone = [...args];
    function loop(arg) {
      callback(arg).then(function (result) {
        results.push(result);
        if (clone.length > 0) {
          loop(clone.shift());
        } else {
          resolve(results);
        }
      });
    }
    loop(clone.shift());
  });
}

from superlogin.

peteruithoven avatar peteruithoven commented on July 4, 2024

I think I've found a way to update current session API keys in databases and re-use the existing dbauth/index.js's addUserDB function. The following function should update all databases according to configuration. Because everything happens in series (not parallel) this should also work for bigger projects.
https://gist.github.com/peteruithoven/b8fd1f60ae526c849479e50a38709590#file-updatedbs-js

Curious what you guys think.

from superlogin.

peteruithoven avatar peteruithoven commented on July 4, 2024

Would it be interesting to add this to the general API, just like removeExpiredKeys?

from superlogin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.