luci2: add missing depends to OpenWrt Makefile
[project/luci2/ui.git] / luci2 / htdocs / luci2 / session.js
1 Class.extend({
2         login: L.rpc.declare({
3                 object: 'session',
4                 method: 'login',
5                 params: [ 'username', 'password' ],
6                 expect: { '': { } }
7         }),
8
9         access: L.rpc.declare({
10                 object: 'session',
11                 method: 'access',
12                 params: [ 'scope', 'object', 'function' ],
13                 expect: { access: false }
14         }),
15
16         isAlive: function()
17         {
18                 return L.session.access('ubus', 'session', 'access');
19         },
20
21         startHeartbeat: function()
22         {
23                 this._hearbeatInterval = window.setInterval(function() {
24                         L.session.isAlive().then(function(alive) {
25                                 if (!alive)
26                                 {
27                                         L.session.stopHeartbeat();
28                                         L.ui.login(true);
29                                 }
30
31                         });
32                 }, L.globals.timeout * 2);
33         },
34
35         stopHeartbeat: function()
36         {
37                 if (typeof(this._hearbeatInterval) != 'undefined')
38                 {
39                         window.clearInterval(this._hearbeatInterval);
40                         delete this._hearbeatInterval;
41                 }
42         },
43
44
45         aclCache: { },
46
47         callAccess: L.rpc.declare({
48                 object: 'session',
49                 method: 'access',
50                 expect: { '': { } }
51         }),
52
53         callAccessCallback: function(acls)
54         {
55                 L.session.aclCache = acls;
56         },
57
58         updateACLs: function()
59         {
60                 return L.session.callAccess()
61                         .then(L.session.callAccessCallback);
62         },
63
64         hasACL: function(scope, object, func)
65         {
66                 var acls = L.session.aclCache;
67
68                 if (typeof(func) == 'undefined')
69                         return (acls && acls[scope] && acls[scope][object]);
70
71                 if (acls && acls[scope] && acls[scope][object])
72                         for (var i = 0; i < acls[scope][object].length; i++)
73                                 if (acls[scope][object][i] == func)
74                                         return true;
75
76                 return false;
77         }
78 });