summaryrefslogtreecommitdiff
path: root/luci2/htdocs/luci2/session.js
blob: 9e5b435da6b78388ec92b7d7752fec6e5af7dd9e (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
Class.extend({
	login: L.rpc.declare({
		object: 'session',
		method: 'login',
		params: [ 'username', 'password' ],
		expect: { '': { } }
	}),

	access: L.rpc.declare({
		object: 'session',
		method: 'access',
		params: [ 'scope', 'object', 'function' ],
		expect: { access: false }
	}),

	isAlive: function()
	{
		return L.session.access('ubus', 'session', 'access');
	},

	startHeartbeat: function()
	{
		this._hearbeatInterval = window.setInterval(function() {
			L.session.isAlive().then(function(alive) {
				if (!alive)
				{
					L.session.stopHeartbeat();
					L.ui.login(true);
				}

			});
		}, L.globals.timeout * 2);
	},

	stopHeartbeat: function()
	{
		if (typeof(this._hearbeatInterval) != 'undefined')
		{
			window.clearInterval(this._hearbeatInterval);
			delete this._hearbeatInterval;
		}
	},


	aclCache: { },

	callAccess: L.rpc.declare({
		object: 'session',
		method: 'access',
		expect: { '': { } }
	}),

	callAccessCallback: function(acls)
	{
		L.session.aclCache = acls;
	},

	updateACLs: function()
	{
		return L.session.callAccess()
			.then(L.session.callAccessCallback);
	},

	hasACL: function(scope, object, func)
	{
		var acls = L.session.aclCache;

		if (typeof(func) == 'undefined')
			return (acls && acls[scope] && acls[scope][object]);

		if (acls && acls[scope] && acls[scope][object])
			for (var i = 0; i < acls[scope][object].length; i++)
				if (acls[scope][object][i] == func)
					return true;

		return false;
	}
});