Skip to content

Instantly share code, notes, and snippets.

@MoreOutput
Last active March 21, 2016 23:10
Show Gist options
  • Save MoreOutput/c36bc858342458a66d8b to your computer and use it in GitHub Desktop.
Save MoreOutput/c36bc858342458a66d8b to your computer and use it in GitHub Desktop.

Revisions

  1. MoreOutput revised this gist Mar 21, 2016. 1 changed file with 5 additions and 7 deletions.
    12 changes: 5 additions & 7 deletions userHelper.ssjs
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    /*
    * Helper object for working with users within xpages
    * Rocky Bevins 2009 ([email protected])
    * Rocky Bevins 2009 ([email protected])
    */
    var User = function() {

    @@ -24,11 +24,9 @@ User.prototype.create = function(fn) {
    User.prototype.isInGroup = function(options, fn) {
    var db = (function() {
    if (options.database === undefined || options.database === null || options.database === 'site') {
    return session.getDatabase('domino-173.nashacademy.com/NASH', 'Names/NashDir.nsf');
    } else if (options.database === 'school') {
    return session.getDatabase('domino-173.nashacademy.com/NASH', 'names.nsf');
    return session.getDatabase('', '');
    } else {
    return session.getDatabase('domino-173.nashacademy.com/NASH', options.database);
    return session.getDatabase('', options.database);
    }
    }()),
    i = 0,
    @@ -57,9 +55,9 @@ User.prototype.isInGroup = function(options, fn) {
    User.prototype.addToGroup = function(options, fn) {
    var udb = (function() {
    if (options.database === undefined) {
    return session.getDatabase('domino-173.nashacademy.com/NASH', 'Names/NashDir.nsf');
    return session.getDatabase('', '');
    } else {
    return session.getDatabase('domino-173.nashacademy.com/NASH', options.database);
    return session.getDatabase('', options.database);
    }
    }()),
    gDoc = udb.getView('($VIMGroups)').getDocumentByKey(options.group),
  2. MoreOutput created this gist Feb 17, 2016.
    84 changes: 84 additions & 0 deletions userHelper.ssjs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    /*
    * Helper object for working with users within xpages
    * Rocky Bevins 2009 ([email protected])
    */
    var User = function() {

    }

    User.prototype.create = function(fn) {
    var userDoc = database.createDocument();
    userDoc.appendItemValue('Form', 'Register');
    userDoc.appendItemValue('FirstName', params.getParameter('FirstName'));
    userDoc.appendItemValue('LastName', params.getParameter('LastName'));
    userDoc.appendItemValue('InternetAddress', params.getParameter('InternetAddress'));
    userDoc.appendItemValue('HTTPPassword', params.getParameter('HTTPPassword'));

    return fn(userDoc.save());
    }

    /*
    * Shorthand for checking the myriad of different possible groups one could be in.
    * Main use is for routing based on group membership.
    */
    User.prototype.isInGroup = function(options, fn) {
    var db = (function() {
    if (options.database === undefined || options.database === null || options.database === 'site') {
    return session.getDatabase('domino-173.nashacademy.com/NASH', 'Names/NashDir.nsf');
    } else if (options.database === 'school') {
    return session.getDatabase('domino-173.nashacademy.com/NASH', 'names.nsf');
    } else {
    return session.getDatabase('domino-173.nashacademy.com/NASH', options.database);
    }
    }()),
    i = 0,
    j = 0,
    gDoc,
    members;

    for (i; i < options.groups.length; i += 1) {
    gDoc = db.getView('($VIMGroups)').getDocumentByKey(options.groups[i]);
    if (gDoc !== null) {
    members = new Array(gDoc.getItemValue('Members'));
    for (j; j <members.length; j += 1) {

    if (members[j] === @UserName() && sessionScope.access !== 'full') {
    if (typeof fn !== 'function') {
    return true;
    } else {
    return fn(true);
    }
    }
    }
    }
    }
    }

    User.prototype.addToGroup = function(options, fn) {
    var udb = (function() {
    if (options.database === undefined) {
    return session.getDatabase('domino-173.nashacademy.com/NASH', 'Names/NashDir.nsf');
    } else {
    return session.getDatabase('domino-173.nashacademy.com/NASH', options.database);
    }
    }()),
    gDoc = udb.getView('($VIMGroups)').getDocumentByKey(options.group),
    members = new Array(gDoc.getItemValue('Members'));

    members.push(options.username);

    gDoc.replaceItemValue('Members', members);

    if (typeof fn === 'function') {
    if (gDoc.save()) {
    fn(true);
    } else {
    fn(false);
    }
    } else {
    gDoc.save();
    }
    }


    var user = new User();