startup.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * The SSO constructor.
  3. *
  4. * Notice that the returned sso instance has asynchronous behavior: its state
  5. * component will only be initialized once the server callback has returned,
  6. * which will almost always be some milliseconds after the instance itself is
  7. * returned: check sso.state.online to ensure the connection attempts is done:
  8. * - undefined -> not yet
  9. * -> false -> failed, values are defaults,
  10. * -> true -> succeeded,valuers are those provided by the server.
  11. *
  12. * @param {string} document_cookies
  13. * @returns {DrupalSSO}
  14. * @constructor
  15. */
  16. DrupalSSO = function (document_cookies) {
  17. // Called without `new`: call with it.
  18. if (!(this instanceof DrupalSSO)) {
  19. return new DrupalSSO(document_cookies);
  20. }
  21. // Work around "this" interpretation in local scope methods.
  22. var that = this;
  23. this.settings = {
  24. client: {}
  25. };
  26. this.state = {
  27. anonymousName: 'anome',
  28. cookieName: 'SESS___4___8__12__16__20__24__28__32',
  29. // Online is only set once the initialization has completed.
  30. online: undefined
  31. };
  32. var user = {
  33. uid: 0,
  34. name: 'undefined name',
  35. roles: ['anonymous user']
  36. };
  37. var userDep = new Tracker.Dependency();
  38. this.getUserId = function () {
  39. userDep.depend();
  40. return user.uid;
  41. };
  42. this.getUserName = function () {
  43. userDep.depend();
  44. return user.name;
  45. };
  46. this.getUserRoles = function () {
  47. userDep.depend();
  48. return user.roles;
  49. };
  50. /**
  51. * Parse a cookie blob for value of the relevant session cookie.
  52. *
  53. * @param {string} cookieBlob
  54. * @returns {undefined}
  55. */
  56. this.getSessionCookie = function (cookieBlob) {
  57. cookieBlob = '; ' + cookieBlob;
  58. var cookieName = that.state.cookieName;
  59. var cookieValue = undefined;
  60. var cookies = cookieBlob.split('; ' + cookieName + "=");
  61. if (cookies.length == 2) {
  62. cookieValue = cookies.pop().split(';').shift();
  63. }
  64. return cookieValue;
  65. }
  66. // Constructor body.
  67. _.extend(that.settings.client, Meteor.settings.public);
  68. Meteor.call('drupal-sso.initState', function (err, res) {
  69. if (err) {
  70. throw new Meteor.Error('init-state', err);
  71. }
  72. _.extend(that.state, res);
  73. });
  74. };