aboutsummaryrefslogtreecommitdiff
path: root/engine/js/lib/session.js
blob: 227c607eb948b444f444f0986cfff91e1ada2e4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
 * @todo comment
 */
elgg.provide('elgg.session');

/**
 * Helper function for setting cookies
 * @param {string} name
 * @param {string} value
 * @param {Object} options
 *  {number|Date} options[expires]
 * 	{string} options[path]
 * 	{string} options[domain]
 * 	{boolean} options[secure]
 * 
 * @return {string} The value of the cookie, if only name is specified
 */
elgg.session.cookie = function(name, value, options) {
	//elgg.session.cookie()
	if(typeof name == 'undefined') {
		return document.cookie;
	}
	
	//elgg.session.cookie(name)
	if (typeof value == 'undefined') {
		if (document.cookie && document.cookie != '') {
			var cookies = document.cookie.split(';');
			for (var i = 0; i < cookies.length; i++) {
				var cookie = jQuery.trim(cookies[i]).split('=');
				if (cookie[0] == name) {
					return decodeURIComponent(cookie[1]);
				}
			}
		}
		return undefined;
	}
	
	// elgg.session.cookie(name, value[, opts])
	var cookies = [];

	options = options || {};
	
	if (value === null) {
		value = '';
		options.expires = -1;
	}
	
	cookies.push(name + '=' + value);
	
	if (typeof options.expires == 'number') {
		var date, valid = true;
		
		if (typeof options.expires == 'number') {
			date = new Date();
			date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
		} else if(options.expires.toUTCString) {
			date = options.expires;
		} else {
			valid = false;
		}
		
		valid ? cookies.push('expires=' + date.toUTCString()) : 0;
	}
	
	// CAUTION: Needed to parenthesize options.path and options.domain
	// in the following expressions, otherwise they evaluate to undefined
	// in the packed version for some reason.
	if (options.path) {
		cookies.push('path=' + (options.path));
	}

	if (options.domain) {
		cookies.push('domain=' + (options.domain));
	}
	
	if (options.secure) {
		cookies.push('secure');
	}
	
	document.cookie = cookies.join('; ');
};

/**
 * @return {ElggUser} The logged in user
 */
elgg.get_loggedin_user = function() {
	return elgg.session.user;
};

/**
 * @return {number} The GUID of the logged in user
 */
elgg.get_loggedin_userid = function() {
	var user = elgg.get_loggedin_user();
	return user ? user.guid : 0;
};

/**
 * @return {boolean} Whether there is a user logged in
 */
elgg.isloggedin = function() {
	return (elgg.get_loggedin_user() instanceof elgg.ElggUser);
};

/**
 * @return {boolean} Whether there is an admin logged in
 */
elgg.isadminloggedin = function() {
	var user = elgg.get_loggedin_user();
	return (user instanceof ElggUser) && user.isAdmin();
};

/**
 * @deprecated Use elgg.session.cookie instead
 */
$.cookie = elgg.session.cookie;