dc6aa3605fe085a51ee5511f1e097722aabe32b5
[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                                                         Gilbert.api.auth.get_passwd_form(function(formspec) {
29                                                                 var change_password_window = application.createWindow({
30                                                                         layout: 'fit',
31                                                                         resizable: false,
32                                                                         title: 'Change password',
33                                                                         iconCls: 'key--pencil',
34                                                                         width: 266,
35                                                                         height: 170,
36                                                                         items: change_password_form = new Ext.FormPanel(Ext.applyIf({
37                                                                                 frame: true,
38                                                                                 bodyStyle: 'padding: 5px 5px 0',
39                                                                                 buttons: [{
40                                                                                         text: 'Change password',
41                                                                                         iconCls: 'key--pencil',
42                                                                                         handler: function(button, event) {
43                                                                                                 change_password_form.getForm().submit({
44                                                                                                         success: function(form, action) {
45                                                                                                                 Ext.MessageBox.alert('Password changed', 'Your password has been changed.');
46                                                                                                         },
47                                                                                                 });
48                                                                                         },
49                                                                                 }],
50                                                                                 api: {
51                                                                                         submit: Gilbert.api.auth.submit_passwd_form,
52                                                                                 },
53                                                                         }, formspec))
54                                                                 });
55                                                                 change_password_window.show();
56                                                         });
57                                                         
58                                                 },
59                                         },{
60                                                 text: 'Log out',
61                                                 iconCls: 'door-open-out',
62                                                 handler: function(button, event) {
63                                                         Gilbert.api.auth.logout(function(result) {
64                                                                 if (result) {
65                                                                         document.location.reload();
66                                                                 } else {
67                                                                         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.');
68                                                                 }
69                                                         })
70                                                 }
71                                         }],
72                                 });
73                                 application.doLayout();
74                         });
75                 },
76                 showLoginWindow: function(application) {
77                         application.mainmenu.hide();
78                         application.doLayout();
79                         var login_window = application.createWindow({
80                                 header: false,
81                                 closable: false,
82                                 resizable: false,
83                                 draggable: false,
84                                 width: 266,
85                                 height: 130,
86                                 layout: 'fit',
87                                 items: login_form = new Ext.FormPanel({
88                                         frame: true,
89                                         bodyStyle: 'padding: 5px 5px 0',
90                                         items: [
91                                                 {
92                                                         fieldLabel: 'Username',
93                                                         name: 'username',
94                                                         xtype: 'textfield',
95                                                 },
96                                                 {
97                                                         fieldLabel: 'Password',
98                                                         name: 'password',
99                                                         xtype: 'textfield',
100                                                         inputType: 'password',
101                                                 }
102                                         ],
103                                         buttons: [
104                                                 {
105                                                         text: 'Log in',
106                                                         iconCls: 'door-open-in',
107                                                         handler: function(button, event) {
108                                                                 var the_form = login_form.getForm().el.dom;
109                                                                 var username = the_form[0].value;
110                                                                 var password = the_form[1].value;
111                                                                 Gilbert.api.auth.login(username, password, function(result) {
112                                                                         if (result) {
113                                                                                 document.location.reload();
114                                                                         } else {
115                                                                                 Ext.MessageBox.alert('Log in failed', 'Unable to authenticate using the credentials provided. Please try again.', function() {
116                                                                                         login_form.getForm().reset();
117                                                                                 });
118                                                                         }
119                                                                 });
120                                                         }
121                                                 }
122                                         ],
123                                 }),
124                         });
125                         login_window.show();
126                 },
127         }
128 })());