Cleanup of Gilbert plugins API and JavaScript.
[philo.git] / contrib / gilbert / media / gilbert / Gilbert.api.auth.js
1 GILBERT_PLUGINS.push(new (function() {
2         return {
3                 init: function(application) {
4                         if (GILBERT_LOGGED_IN) {
5                                 application.on('ready', this.addUserMenu, this, {
6                                         single: true,
7                                 });
8                         } else {
9                                 application.on('ready', this.showLoginWindow, this, {
10                                         single: true,
11                                 });
12                         }
13                 },
14                 addUserMenu: function(application) {
15                         Gilbert.api.auth.whoami(function(result) {
16                                 application.mainmenu.add({
17                                         xtype: 'tbfill',
18                                 },{
19                                         xtype: 'tbseparator',
20                                 },{
21                                         xtype: 'button',
22                                         iconCls: 'user-silhouette',
23                                         text: '<span style="font-weight: bolder;">' + result + '</span>',
24                                         menu: [{
25                                                 text: 'Change password',
26                                                 iconCls: 'key--pencil',
27                                                 handler: function(button, event) {
28                                                         var change_password_window = application.createWindow({
29                                                                 layout: 'fit',
30                                                                 resizable: false,
31                                                                 title: 'Change password',
32                                                                 iconCls: 'key--pencil',
33                                                                 width: 266,
34                                                                 height: 170,
35                                                                 items: change_password_form = new Ext.FormPanel({
36                                                                         frame: true,
37                                                                         bodyStyle: 'padding: 5px 5px 0',
38                                                                         items: [{
39                                                                                 fieldLabel: 'Current password',
40                                                                                 name: 'current_password',
41                                                                                 xtype: 'textfield',
42                                                                                 inputType: 'password',
43                                                                         },{
44                                                                                 fieldLabel: 'New password',
45                                                                                 name: 'new_password',
46                                                                                 xtype: 'textfield',
47                                                                                 inputType: 'password',
48                                                                         },{
49                                                                                 fieldLabel: 'New password (confirm)',
50                                                                                 name: 'new_password_confirm',
51                                                                                 xtype: 'textfield',
52                                                                                 inputType: 'password',
53                                                                         }],
54                                                                         buttons: [{
55                                                                                 text: 'Change password',
56                                                                                 iconCls: 'key--pencil',
57                                                                                 handler: function(button, event) {
58                                                                                         var the_form = change_password_form.getForm().el.dom;
59                                                                                         var current_password = the_form[0].value;
60                                                                                         var new_password = the_form[1].value;
61                                                                                         var new_password_confirm = the_form[2].value;
62                                                                                         Gilbert.api.auth.passwd(current_password, new_password, new_password_confirm, function(result) {
63                                                                                                 if (result) {
64                                                                                                         Ext.MessageBox.alert('Password changed', 'Your password has been changed.');
65                                                                                                 } else {
66                                                                                                         Ext.MessageBox.alert('Password unchanged', 'Unable to change your password.', function() {
67                                                                                                                 change_password_form.getForm().reset();
68                                                                                                         });
69                                                                                                 }
70                                                                                         });
71                                                                                 },
72                                                                         }],
73                                                                 })
74                                                         });
75                                                         change_password_window.show();
76                                                 },
77                                         },{
78                                                 text: 'Log out',
79                                                 iconCls: 'door-open-out',
80                                                 handler: function(button, event) {
81                                                         Gilbert.api.auth.logout(function(result) {
82                                                                 if (result) {
83                                                                         document.location.reload();
84                                                                 } else {
85                                                                         Ext.MessageBox.alert('Log out failed', 'You have <strong>not</strong> been logged out. This could mean that your connection with the server has been severed. Please try again.');
86                                                                 }
87                                                         })
88                                                 }
89                                         }],
90                                 });
91                                 application.doLayout();
92                         });
93                 },
94                 showLoginWindow: function(application) {
95                         application.mainmenu.hide();
96                         application.doLayout();
97                         var login_window = application.createWindow({
98                                 header: false,
99                                 closable: false,
100                                 resizable: false,
101                                 draggable: false,
102                                 width: 266,
103                                 height: 130,
104                                 layout: 'fit',
105                                 items: login_form = new Ext.FormPanel({
106                                         frame: true,
107                                         bodyStyle: 'padding: 5px 5px 0',
108                                         items: [
109                                                 {
110                                                         fieldLabel: 'Username',
111                                                         name: 'username',
112                                                         xtype: 'textfield',
113                                                 },
114                                                 {
115                                                         fieldLabel: 'Password',
116                                                         name: 'password',
117                                                         xtype: 'textfield',
118                                                         inputType: 'password',
119                                                 }
120                                         ],
121                                         buttons: [
122                                                 {
123                                                         text: 'Log in',
124                                                         iconCls: 'door-open-in',
125                                                         handler: function(button, event) {
126                                                                 var the_form = login_form.getForm().el.dom;
127                                                                 var username = the_form[0].value;
128                                                                 var password = the_form[1].value;
129                                                                 Gilbert.api.auth.login(username, password, function(result) {
130                                                                         if (result) {
131                                                                                 document.location.reload();
132                                                                         } else {
133                                                                                 Ext.MessageBox.alert('Log in failed', 'Unable to authenticate using the credentials provided. Please try again.', function() {
134                                                                                         login_form.getForm().reset();
135                                                                                 });
136                                                                         }
137                                                                 });
138                                                         }
139                                                 }
140                                         ],
141                                 }),
142                         });
143                         login_window.show();
144                 },
145         }
146 })());