Last active
March 21, 2016 23:10
-
-
Save MoreOutput/c36bc858342458a66d8b to your computer and use it in GitHub Desktop.
XPage SSJS User helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * 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('', ''); | |
| } else { | |
| return session.getDatabase('', 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('', ''); | |
| } else { | |
| return session.getDatabase('', 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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment