Skip to content

Instantly share code, notes, and snippets.

@Plypy

Plypy/READEME.md Secret

Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Plypy/c8c44307995910ed4bbc to your computer and use it in GitHub Desktop.
Save Plypy/c8c44307995910ed4bbc to your computer and use it in GitHub Desktop.
Script for Accouts Injection for OpenMRS TALK

As I've added a async hook, which sends login requests to Discourse in order to trigger its sync logic, onto the Dashboard User model. So all we need to do is to perform a save action, and it's exactly what this simple script do.

Let's walk through the procedure step by step.

###Prerequisites

  • openmrs-contrib-id-sso module with ID-105 feature.
  • Shutdown dashboard
  • Disable db-admin module temporarily.

Edit conf.js to achieve this.

###Inject this script Relative to Dashboard's root directory, put this script under app/scripts/talk.js. And append this line at the bottom of app/app.js

require('./scripts/talk');

To ease server's pressure, the script will send out requests by batch. In the top of the script, you can see two paramter, payload and period, which means we'll send at most payload requests every period milliseconds. You need to set this by your own judgement.

###Start the magic Just start the dashboard by node app/app.js and the script will be performed

However, as the sync process is fully async, and we can't know when it will end. The ending message finished sending requests, wait.......... only suggests that we've sent out all the requests. So after that you need to determine the ending time by yourself, usually just wait some 10s of seconds will be fine with respect to your setting.

###Then The logs are also stored in app/scripts/talk.log. Once it's done, check the log file and search error log. Item that looks like this, [ERROR] discourse-sso - Failed to sync user 'username', records those users that have failed to sync. You have to deal with this manually, it's also simple, just login to formage and find that user and save it. Then the sync hook will be triggered once again.

###Clean up Remove the file we created and get the source clean.

rm app/scripts -rf
git checkout -- app/app.js

Don't forget to restore your conf.js.

var payload = 10; // the amount of sync operation we do per batch
var period = 3000; // batch interval
var async = require('async');
require('../new-db');
var User = require('../model/user');
var log4js = require('log4js');
var log = log4js.getLogger('discourse-sso');
var path = require('path');
var file = log4js.appenders.file(path.join(__dirname, 'talk.log'));
log4js.addAppender(file, 'discourse-sso');
var cargo = async.cargo(function (users, callback) {
for (var i in users) {
var user = users[i];
if ('end' === user) {
log.info('finished sending requests, wait..........');
continue;
}
log.info('resaving user', user.username);
user.skipLDAP = true;
user.save();
}
setTimeout(callback, period);
}, payload);
var stream = User.find().stream();
stream.on('data', function (user) {
cargo.push(user);
}).on('close', function () {
cargo.push('end');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment