2 aclTable: L.cbi.AbstractValue.extend({
3 strGlob: function(pattern, match) {
4 var re = new RegExp('^' + (pattern
5 .replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1')
6 .replace(/\\\*/g, '.*?')) + '$');
11 aclMatch: function(list, group) {
12 for (var i = 0; i < list.length; i++)
14 var x = list[i].replace(/^\s*!\s*/, '');
18 if (this.strGlob(x, group))
22 for (var i = 0; i < list.length; i++)
24 var x = list[i].replace(/^\s*!\s*/, '');
28 if (this.strGlob(x, group))
35 aclTest: function(list, group) {
36 for (var i = 0; i < list.length; i++)
43 aclEqual: function(list1, list2) {
44 if (list1.length != list2.length)
47 for (var i = 0; i < list1.length; i++)
48 if (list1[i] != list2[i])
54 aclFromUCI: function(value) {
56 if (typeof(value) == 'string')
57 list = value.split(/\s+/);
58 else if ($.isArray(value))
65 for (var i = 0; i < this.choices.length; i++)
66 if (this.aclMatch(list, this.choices[i][0]))
67 rv.push(this.choices[i][0]);
72 aclToUCI: function(list) {
73 if (list.length < (this.choices.length / 2))
77 for (var i = 0; i < list.length; i++)
81 for (var i = 0; i < this.choices.length; i++)
82 if (!set[this.choices[i][0]])
83 rv.push('!' + this.choices[i][0]);
88 setAll: function(ev) {
89 $(this).parents('table')
90 .find('input[value=%d]'.format(ev.data.level))
91 .prop('checked', true);
96 var t = $('<table />')
97 .addClass('table table-condensed table-hover')
98 .attr('id', this.id(sid))
101 .text(L.tr('ACL Group')))
103 .text(L.trc('No access', 'N'))
104 .attr('title', L.tr('Set all to no access'))
105 .css('cursor', 'pointer')
106 .click({ level: 0 }, this.setAll))
108 .text(L.trc('Read only access', 'R'))
109 .attr('title', L.tr('Set all to read only access'))
110 .css('cursor', 'pointer')
111 .click({ level: 1 }, this.setAll))
113 .text(L.trc('Full access', 'F'))
114 .attr('title', L.tr('Set all to full access'))
115 .css('cursor', 'pointer')
116 .click({ level: 2 }, this.setAll)));
118 var acl_r = this.aclFromUCI(this.map.get('rpcd', sid, 'read'));
119 var acl_w = this.aclFromUCI(this.map.get('rpcd', sid, 'write'));
122 for (var i = 0; i < this.choices.length; i++)
124 var r = t.get(0).insertRow(-1);
125 var is_r = this.aclTest(acl_r, this.choices[i][0]);
126 var is_w = this.aclTest(acl_w, this.choices[i][0]);
129 .text(this.choices[i][1]);
131 for (var j = 0; j < 3; j++)
134 .append($('<input />')
135 .addClass('form-control')
136 .attr('type', 'radio')
137 .attr('name', '%s_%s'.format(this.id(sid), this.choices[i][0]))
139 .prop('checked', (j == 0 && !is_r && !is_w) ||
140 (j == 1 && is_r && !is_w) ||
148 textvalue: function(sid)
150 var acl_r = this.aclFromUCI(this.map.get('rpcd', sid, 'read'));
151 var acl_w = this.aclFromUCI(this.map.get('rpcd', sid, 'write'));
153 var htmlid = this.id(sid);
154 var radios = $('#' + htmlid + ' input');
158 for (var i = 0; i < this.choices.length; i++)
160 switch (radios.filter('[name=%s_%s]:checked'.format(htmlid, this.choices[i][0])).val())
163 acls.push('%s: %s'.format(this.choices[i][0], L.trc('Full access', 'F')));
167 acls.push('%s: %s'.format(this.choices[i][0], L.trc('Read only access', 'R')));
171 acls.push('%s: %s'.format(this.choices[i][0], L.trc('No access', 'N')));
176 return acls.join(', ');
179 value: function(k, v)
184 this.choices.push([k, v || k]);
190 var acl_r = this.aclFromUCI(this.map.get('rpcd', sid, 'read'));
191 var acl_w = this.aclFromUCI(this.map.get('rpcd', sid, 'write'));
196 var htmlid = this.id(sid);
197 var radios = $('#' + htmlid + ' input');
199 for (var i = 0; i < this.choices.length; i++)
201 switch (radios.filter('[name=%s_%s]:checked'.format(htmlid, this.choices[i][0])).val())
204 acl_r_new.push(this.choices[i][0]);
205 acl_w_new.push(this.choices[i][0]);
209 acl_r_new.push(this.choices[i][0]);
214 if (!this.aclEqual(acl_r, acl_r_new))
215 this.map.set('rpcd', sid, 'read', this.aclToUCI(acl_r_new));
217 if (!this.aclEqual(acl_w, acl_w_new))
218 this.map.set('rpcd', sid, 'write', this.aclToUCI(acl_w_new));
222 execute: function() {
224 return L.ui.listAvailableACLs().then(function(acls) {
225 var m = new L.cbi.Map('rpcd', {
226 caption: L.tr('Guest Logins'),
227 description: L.tr('Manage user accounts and permissions for accessing the LuCI ui.'),
228 readonly: !self.options.acls.users
231 var s = m.section(L.cbi.TypedSection, 'login', {
232 caption: L.tr('Accounts'),
235 add_caption: L.tr('Add account …'),
236 teasers: [ 'username', '__shadow', '__acls' ]
239 s.option(L.cbi.InputValue, 'username', {
240 caption: L.tr('Username'),
241 description: L.tr('Specifies the login name for the guest account'),
246 var shadow = s.option(L.cbi.CheckboxValue, '__shadow', {
247 caption: L.tr('Use system account'),
248 description: L.tr('Use password from the Linux user database')
251 shadow.ucivalue = function(sid) {
252 var pw = this.map.get('rpcd', sid, 'password');
253 return (pw && pw.indexOf('$p$') == 0);
257 var password = s.option(L.cbi.PasswordValue, 'password', {
258 caption: L.tr('Password'),
259 description: L.tr('Specifies the password for the guest account. If you enter a plaintext password here, it will get replaced with a crypted password hash on save.'),
263 password.depends('__shadow', false);
265 password.toggle = function(sid) {
266 var id = '#' + this.id(sid);
267 var pw = this.map.get('rpcd', sid, 'password');
268 var sh = this.section.fields.__shadow.formvalue(sid);
270 if (!sh && pw && pw.indexOf('$p$') == 0)
273 this.callSuper('toggle', sid);
276 shadow.save = password.save = function(sid) {
277 var sh = this.section.fields.__shadow.formvalue(sid);
278 var pw = this.section.fields.password.formvalue(sid);
284 pw = '$p$' + this.section.fields.username.formvalue(sid);
286 if (pw.match(/^\$[0-9p][a-z]?\$/))
288 if (pw != this.map.get('rpcd', sid, 'password'))
289 this.map.set('rpcd', sid, 'password', pw);
294 return L.ui.cryptPassword(pw).then(function(crypt) {
295 map.set('rpcd', sid, 'password', crypt);
300 var o = s.option(self.aclTable, '__acls', {
301 caption: L.tr('User ACLs'),
302 description: L.tr('Specifies the access levels of this account. The "N" column means no access, "R" stands for read only access and "F" for full access.')
306 for (var group_name in acls)
307 groups.push(group_name);
311 for (var i = 0; i < groups.length; i++)
312 o.value(groups[i], acls[groups[i]].description);
314 return m.insertInto('#map');