session.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Provides session methods.
  3. */
  4. elgg.provide('elgg.session');
  5. /**
  6. * Helper function for setting cookies
  7. * @param {string} name
  8. * @param {string} value
  9. * @param {Object} options
  10. *
  11. * {number|Date} options[expires]
  12. * {string} options[path]
  13. * {string} options[domain]
  14. * {boolean} options[secure]
  15. *
  16. * @return {string|undefined} The value of the cookie, if only name is specified. Undefined if no value set
  17. */
  18. elgg.session.cookie = function(name, value, options) {
  19. var cookies = [], cookie = [], i = 0, date, valid = true;
  20. //elgg.session.cookie()
  21. if (elgg.isUndefined(name)) {
  22. return document.cookie;
  23. }
  24. //elgg.session.cookie(name)
  25. if (elgg.isUndefined(value)) {
  26. if (document.cookie && document.cookie !== '') {
  27. cookies = document.cookie.split(';');
  28. for (i = 0; i < cookies.length; i += 1) {
  29. cookie = jQuery.trim(cookies[i]).split('=');
  30. if (cookie[0] === name) {
  31. return decodeURIComponent(cookie[1]);
  32. }
  33. }
  34. }
  35. return undefined;
  36. }
  37. // elgg.session.cookie(name, value[, opts])
  38. options = options || {};
  39. if (elgg.isNull(value)) {
  40. value = '';
  41. options.expires = -1;
  42. }
  43. cookies.push(name + '=' + value);
  44. if (options.expires) {
  45. if (elgg.isNumber(options.expires)) {
  46. date = new Date();
  47. date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
  48. } else if (options.expires.toUTCString) {
  49. date = options.expires;
  50. }
  51. if (date) {
  52. cookies.push('expires=' + date.toUTCString());
  53. }
  54. }
  55. // CAUTION: Needed to parenthesize options.path and options.domain
  56. // in the following expressions, otherwise they evaluate to undefined
  57. // in the packed version for some reason.
  58. if (options.path) {
  59. cookies.push('path=' + (options.path));
  60. }
  61. if (options.domain) {
  62. cookies.push('domain=' + (options.domain));
  63. }
  64. if (options.secure) {
  65. cookies.push('secure');
  66. }
  67. document.cookie = cookies.join('; ');
  68. };
  69. /**
  70. * Returns the object of the user logged in.
  71. *
  72. * @return {ElggUser} The logged in user
  73. */
  74. elgg.get_logged_in_user_entity = function() {
  75. return elgg.session.user;
  76. };
  77. /**
  78. * Returns the GUID of the logged in user or 0.
  79. *
  80. * @return {number} The GUID of the logged in user
  81. */
  82. elgg.get_logged_in_user_guid = function() {
  83. var user = elgg.get_logged_in_user_entity();
  84. return user ? user.guid : 0;
  85. };
  86. /**
  87. * Returns if a user is logged in.
  88. *
  89. * @return {boolean} Whether there is a user logged in
  90. */
  91. elgg.is_logged_in = function() {
  92. return (elgg.get_logged_in_user_entity() instanceof elgg.ElggUser);
  93. };
  94. /**
  95. * Returns if the currently logged in user is an admin.
  96. *
  97. * @return {boolean} Whether there is an admin logged in
  98. */
  99. elgg.is_admin_logged_in = function() {
  100. var user = elgg.get_logged_in_user_entity();
  101. return (user instanceof elgg.ElggUser) && user.isAdmin();
  102. };
  103. /**
  104. * @deprecated Use elgg.session.cookie instead
  105. */
  106. jQuery.cookie = elgg.session.cookie;
  107. // This just has to happen after ElggUser is defined, however it's probably
  108. // better to have this procedural code here than in ElggUser.js
  109. if (elgg.session.user) {
  110. elgg.session.user = new elgg.ElggUser(elgg.session.user);
  111. }