Elgg  Version 1.9
session.js
Go to the documentation of this file.
1 
4 elgg.provide('elgg.session');
5 
19 elgg.session.cookie = function(name, value, options) {
20  var cookies = [], cookie = [], i = 0, date, valid = true;
21 
22  //elgg.session.cookie()
23  if (elgg.isUndefined(name)) {
24  return document.cookie;
25  }
26 
27  //elgg.session.cookie(name)
28  if (elgg.isUndefined(value)) {
29  if (document.cookie && document.cookie !== '') {
30  cookies = document.cookie.split(';');
31  for (i = 0; i < cookies.length; i += 1) {
32  cookie = jQuery.trim(cookies[i]).split('=');
33  if (cookie[0] === name) {
34  return decodeURIComponent(cookie[1]);
35  }
36  }
37  }
38  return undefined;
39  }
40 
41  // elgg.session.cookie(name, value[, opts])
42  options = options || {};
43 
44  if (elgg.isNull(value)) {
45  value = '';
46  options.expires = -1;
47  }
48 
49  cookies.push(name + '=' + value);
50 
51  if (options.expires) {
52  if (elgg.isNumber(options.expires)) {
53  date = new Date();
54  date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
55  } else if (options.expires.toUTCString) {
56  date = options.expires;
57  }
58 
59  if (date) {
60  cookies.push('expires=' + date.toUTCString());
61  }
62  }
63 
64  // CAUTION: Needed to parenthesize options.path and options.domain
65  // in the following expressions, otherwise they evaluate to undefined
66  // in the packed version for some reason.
67  if (options.path) {
68  cookies.push('path=' + (options.path));
69  }
70 
71  if (options.domain) {
72  cookies.push('domain=' + (options.domain));
73  }
74 
75  if (options.secure) {
76  cookies.push('secure');
77  }
78 
79  document.cookie = cookies.join('; ');
80 };
81 
87 elgg.get_logged_in_user_entity = function() {
88  return elgg.session.user;
89 };
90 
96 elgg.get_logged_in_user_guid = function() {
97  var user = elgg.get_logged_in_user_entity();
98  return user ? user.guid : 0;
99 };
100 
106 elgg.is_logged_in = function() {
107  return (elgg.get_logged_in_user_entity() instanceof elgg.ElggUser);
108 };
109 
115 elgg.is_admin_logged_in = function() {
116  var user = elgg.get_logged_in_user_entity();
117  return (user instanceof elgg.ElggUser) && user.isAdmin();
118 };
119 
123 jQuery.cookie = elgg.session.cookie;
124 
125 // This just has to happen after ElggUser is defined, however it's probably
126 // better to have this procedural code here than in ElggUser.js
127 if (elgg.session.user) {
128  elgg.session.user = new elgg.ElggUser(elgg.session.user);
129 }
elgg
Definition: install.js:23
i
Definition: admin.php:47
elgg menu user
Definition: admin.php:190
elgg session cookie
Helper function for setting cookies.
Definition: session.js:19
GNU GENERAL PUBLIC LICENSE June Free Software Franklin Fifth MA USA Everyone is permitted to copy and distribute verbatim copies of this license document
Definition: LICENSE.txt:4