summaryrefslogtreecommitdiffstats
path: root/panes/settingsGlobalUsers.html
blob: 65c7b587c98674dc95baa7f194b10097c6879ca1 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<!-- 

	User list
	Copyright (C) 2010-2015 Ian Moore (imoore76 at yahoo dot com)
	
	$Id: settingsGlobalUsers.html 595 2015-04-17 09:50:36Z imoore76 $

 -->
<div>
<table style='width: auto; margin-left:auto; margin-right:auto;' class='vboxInvisible'>
	<tr>
		<td colspan='2'><span class='translate'>Users</span>:</td>
	</tr>
	<tr style='vertical-align:top'>
		<td style='width: 400px'>
			<ul id='vboxSettingsUserList' class='vboxBordered vboxList vboxHover' style='width: 400px; margin-top: 0px;'>
				<li>user</li>
			</ul>
		</td>
		<td style='width: 10px' id='vboxSettingsGlobalUsersToolbar'></td>
	</tr>
</table>


<!-- Main Div -->
</div>


<script type='text/javascript'>

/* Build Toolbar */

/*
 * Init Users buttons and toolbar
 */

var sButtons = new Array(

	{
		'name' : 'addUser',
		'label' : 'Add User',
		'icon' : 'vm_new',
		'click' : function () {
	
			var l = new vboxLoader();
			l.addFileToDOM('panes/userEdit.html');
			l.onLoad = function(){

				// Set mode
				$('#vboxUserEdit').trigger('setMode','addUser');
				
				var buttons = {};
				buttons[trans('OK','QIMessageBox')] = function() {
					var u = $('#vboxUserEdit').find('input[name=username]').first().val();
					var p1 = $('#vboxUserEdit').find('input[name=npass1]').first().val();
					var p2 = $('#vboxUserEdit').find('input[name=npass2]').first().val();
					var a = $('#vboxUserEdit').find('input[name=admin]').first().prop('checked');
					if(u.length == 0) {
						$(this).remove();
						return;
					}
					if(p1.length == 0 || (p1 != p2)) {
						vboxAlert(trans('The passwords you have entered do not match.','UIUsers'),{'width':'auto'});
						return;
					}
					var dialog = this;
					var chp = new vboxLoader();
					chp.add('addUser',function(d){
						if(d) {
							if(d.success) {
								vboxSettingsGlobalAddUserList({'username':u,'admin':a});
								$(dialog).remove();
							} else {
								vboxAlert(trans('A user with that username already exists.','UIUsers'),{'width':'auto'});
							}
						} else {
							// unknown error
							$(dialog).remove();
						}
					},{'u':u,'p':p1,'a':a});
					chp.run();
				};
				buttons[trans('Cancel','QIMessageBox')] = function(){
					$(this).remove();
				};
				$('#vboxUserEdit').dialog({'closeOnEscape':false,'width':500,'height':220,'buttons':buttons,'modal':true,'autoOpen':true,'dialogClass':'vboxDialogContent','title':'<img src="images/vbox/vm_new_16px.png" class="vboxDialogTitleIcon" /> '+trans('Add User','UIUsers')});

			};
			l.run();
		}
	},

	{
		'name' : 'delUser',
		'label' : 'Remove User',
		'icon' : 'vm_delete',
		'noDisabledIcon' : true,
		'enabled' : function (item) { return (item && $(item).data('username') && $(item).data('username') != $('#vboxPane').data('vboxSession').user); },
		'click' : function () {
			var buttons = {};
			buttons[trans('OK','QIMessageBox')] = function() {
				var user = $('#vboxSettingsUserList').children('li.vboxListItemSelected').data();
				var l = new vboxLoader();
				l.add('delUser',function(d){
					if(d && d.success) {
						$('#vboxSettingsUserList').children('li.vboxListItemSelected').remove();
						$('#vboxSettingsUserList').trigger('select',null);
					}
				},{'u':user.username});
				l.run();
				$(this).remove();
			};
			var user = $('#vboxSettingsUserList').children('li.vboxListItemSelected').data();
			vboxConfirm(trans('Are you sure you want to remove the user <b>%s</b>? This action cannot be undone.','UIUsers').replace('%s',user.username),buttons);
		}
	},
	{
		'name' : 'chpwUser',
		'label' : 'Change Password',
		'icon' : 'register',
		'noDisabledIcon' : true,
		'enabled' : function (item) { return (item && $(item).data('username') && $(item).data('username') == $('#vboxPane').data('vboxSession').user); },
		'click' : function () {
			var l = new vboxLoader();
			l.addFileToDOM('panes/userEdit.html');
			l.onLoad = function(){
				
				// Set mode
				$('#vboxUserEdit').trigger('setMode','changePassword');
				
				var buttons = {};
				buttons[trans('OK','QIMessageBox')] = function() {
					var o = $('#vboxUserEdit').find('input[name=opass]').first().val();
					var n1 = $('#vboxUserEdit').find('input[name=npass1]').first().val();
					var n2 = $('#vboxUserEdit').find('input[name=npass2]').first().val();
					if(o.length == 0) {
						vboxAlert(trans('The password you have entered is invalid.','UIUsers'),{'width':'auto'});
						return;
					}
					if(n1.length == 0 || (n1 != n2)) {
						vboxAlert(trans('The passwords you have entered do not match.','UIUsers'),{'width':'auto'});
						return;
					}
					var dialog = this;
					var chp = new vboxLoader();
					chp.add('changePassword',function(d){
						if(d) {
							if(d.success) {
								vboxAlert(trans('Password changed.','UIUsers'),{'width':'auto'});
								$(dialog).remove();
							} else {
								vboxAlert(trans('The password you have entered is invalid.','UIUsers'),{'width':'auto'});
							}
						} else {
							// unknown error
							$(dialog).remove();
						}
					},{'old':o,'new':n1});
					chp.run();
				};
				buttons[trans('Cancel','QIMessageBox')] = function(){
					$(this).remove();
				};
				$('#vboxUserEdit').dialog({'closeOnEscape':false,'width':400,'height':200,'buttons':buttons,'modal':true,'autoOpen':true,'dialogClass':'vboxDialogContent','title':'<img src="images/vbox/register_16px.png" class="vboxDialogTitleIcon" /> '+trans('Change Password','UIUsers')});
			};
			l.run();			
			
		}
	},
	{
		'name' : 'editUser',
		'label' : 'Edit User',
		'icon' : 'register',
		'noDisabledIcon' : true,
		'enabled' : function (item) { return (item && $(item).data('username') && $(item).data('username') != $('#vboxPane').data('vboxSession').user); },
		'click' : function () {
			
			var l = new vboxLoader();
			l.addFileToDOM('panes/userEdit.html');
			l.onLoad = function(){

				// Set mode
				$('#vboxUserEdit').trigger('setMode','editUser');
				
				// Set values
				var user = $('#vboxSettingsUserList').children('li.vboxListItemSelected').data();
				$('#vboxEditUserUsername').html(user.username);
				$('#vboxUserEdit').find('input[name=admin]').first().prop('checked', (user.admin ? true : false));
				
				var buttons = {};
				buttons[trans('OK','QIMessageBox')] = function() {
					
					var p1 = $('#vboxUserEdit').find('input[name=npass1]').first().val();
					var p2 = $('#vboxUserEdit').find('input[name=npass2]').first().val();
					var a = $('#vboxUserEdit').find('input[name=admin]').first().prop('checked');
					
					if(p1.length > 0 && (p1 != p2)) {
						vboxAlert(trans('The passwords you have entered do not match.','UIUsers'),{'width':'auto'});
						return;
					}
					var dialog = this;
					var chp = new vboxLoader();
					chp.add('editUser',function(d){
						if(d && d.success) {
							$('#vboxSettingsUserList').children('li.vboxListItemSelected').data({'admin':a}).html(user.username + (a ? ' ('+trans('Admin User','UIUsers')+')' : ''));
							$(dialog).remove();
						} else {
							// unknown error
							$(dialog).remove();
						}
					},{'u':user.username,'p':p1,'a':a});
					chp.run();
				};
				buttons[trans('Cancel','QIMessageBox')] = function(){
					$(this).remove();
				};
				$('#vboxUserEdit').dialog({'closeOnEscape':false,'width':500,'height':230,'buttons':buttons,'modal':true,'autoOpen':true,'dialogClass':'vboxDialogContent','title':'<img src="images/vbox/register_16px.png" class="vboxDialogTitleIcon" /> '+trans('Edit User','UIUsers')});

			};
			l.run();
			
		}
	}
);

var userToolbar = new vboxToolbarSmall({buttons: sButtons, language_context: 'UIUsers'});
userToolbar.disabledString = 'dis';
userToolbar.renderTo('vboxSettingsGlobalUsersToolbar');
$('#vboxSettingsUserList').on('select', function(e,el){userToolbar.update(el);});

/*
 *
 * Function to fill users
 * 
 */
function vboxSettingsGlobalAddUserList(n) {

	$('<li />').attr({'class':'vboxListItem'}).html($('<div />').html(n.username + (n.admin ? ' ('+trans('Admin User','UIUsers')+')' : ''))).hoverClass('vboxHover').data(n).click(function(){
		$(this).parent().children('li.vboxListItemSelected').removeClass('vboxListItemSelected').addClass('vboxListItem');
		$(this).addClass('vboxListItemSelected').removeClass('vboxListItem');
		$('#vboxSettingsUserList').trigger('select',this);
	}).appendTo($('#vboxSettingsUserList'));
	
}

/*
 * 
 * Fill network adapters
 *
 */
$('#vboxSettingsUserList').children().remove();
var users = $('#vboxSettingsDialog').data('vboxUsers');
for(var i in users) {
	vboxSettingsGlobalAddUserList(users[i]);
}
$('#vboxSettingsUserList').trigger('select',null);

 
</script>