// Cronicle Admin Page -- API Keys Class.add( Page.Admin, { gosub_api_keys: function(args) { // show API Key list app.setWindowTitle( "API Keys" ); this.div.addClass('loading'); app.api.post( 'app/get_api_keys', copy_object(args), this.receive_keys.bind(this) ); }, receive_keys: function(resp) { // receive all API Keys from server, render them sorted this.lastAPIKeysResp = resp; var html = ''; this.div.removeClass('loading'); var size = get_inner_window_size(); var col_width = Math.floor( ((size.width * 0.9) + 200) / 7 ); if (!resp.rows) resp.rows = []; // sort by title ascending this.api_keys = resp.rows.sort( function(a, b) { return a.title.toLowerCase().localeCompare( b.title.toLowerCase() ); } ); html += this.getSidebarTabs( 'api_keys', [ ['activity', "Activity Log"], ['api_keys', "API Keys"], ['categories', "Categories"], ['plugins', "Plugins"], ['servers', "Servers"], ['users', "Users"] ] ); var cols = ['App Title', 'API Key', 'Status', 'Author', 'Created', 'Actions']; html += '
'; html += '
'; html += 'API Keys'; html += '
'; html += '
'; var self = this; html += this.getBasicTable( this.api_keys, cols, 'key', function(item, idx) { var actions = [ 'Edit', 'Delete' ]; return [ '
' + self.getNiceAPIKey(item, true, col_width) + '
', '
' + encode_entities(item.key) + '
', item.active ? ' Active' : ' Suspended', self.getNiceUsername(item.username, true, col_width), ''+get_nice_date(item.created, true)+'', actions.join(' | ') ]; } ); html += '
'; html += '
'; html += ''; html += '
  Add API Key...
'; html += '
'; // padding html += ''; // sidebar tabs this.div.html( html ); }, edit_api_key: function(idx) { // jump to edit sub if (idx > -1) Nav.go( '#Admin?sub=edit_api_key&id=' + this.api_keys[idx].id ); else Nav.go( '#Admin?sub=new_api_key' ); }, delete_api_key: function(idx) { // delete key from search results this.api_key = this.api_keys[idx]; this.show_delete_api_key_dialog(); }, gosub_new_api_key: function(args) { // create new API Key var html = ''; app.setWindowTitle( "New API Key" ); this.div.removeClass('loading'); html += this.getSidebarTabs( 'new_api_key', [ ['activity', "Activity Log"], ['api_keys', "API Keys"], ['new_api_key', "New API Key"], ['categories', "Categories"], ['plugins', "Plugins"], ['servers', "Servers"], ['users', "Users"] ] ); html += '
New API Key
'; html += '
'; html += '
'; this.api_key = { privileges: {}, key: get_unique_id() }; html += this.get_api_key_edit_html(); // buttons at bottom html += ''; html += '
'; html += '
'; html += ''; html += ''; html += ''; html += ''; html += '
Cancel
 
  Create Key
'; html += '
'; html += '
'; // table wrapper div html += ''; // sidebar tabs this.div.html( html ); setTimeout( function() { $('#fe_ak_title').focus(); }, 1 ); }, cancel_api_key_edit: function() { // cancel editing API Key and return to list Nav.go( 'Admin?sub=api_keys' ); }, do_new_api_key: function(force) { // create new API Key app.clearError(); var api_key = this.get_api_key_form_json(); if (!api_key) return; // error if (!api_key.title.length) { return app.badField('#fe_ak_title', "Please enter an app title for the new API Key."); } this.api_key = api_key; app.showProgress( 1.0, "Creating API Key..." ); app.api.post( 'app/create_api_key', api_key, this.new_api_key_finish.bind(this) ); }, new_api_key_finish: function(resp) { // new API Key created successfully app.hideProgress(); Nav.go('Admin?sub=edit_api_key&id=' + resp.id); setTimeout( function() { app.showMessage('success', "The new API Key was created successfully."); }, 150 ); }, gosub_edit_api_key: function(args) { // edit API Key subpage this.div.addClass('loading'); app.api.post( 'app/get_api_key', { id: args.id }, this.receive_key.bind(this) ); }, receive_key: function(resp) { // edit existing API Key var html = ''; this.api_key = resp.api_key; app.setWindowTitle( "Editing API Key \"" + (this.api_key.title) + "\"" ); this.div.removeClass('loading'); html += this.getSidebarTabs( 'edit_api_key', [ ['activity', "Activity Log"], ['api_keys', "API Keys"], ['edit_api_key', "Edit API Key"], ['categories', "Categories"], ['plugins', "Plugins"], ['servers', "Servers"], ['users', "Users"] ] ); html += '
Editing API Key “' + (this.api_key.title) + '”
'; html += '
'; html += '
'; html += ''; html += this.get_api_key_edit_html(); html += ''; html += '
'; html += '
'; html += ''; html += ''; html += ''; html += ''; html += ''; html += ''; html += '
Cancel
 
Delete Key...
 
  Save Changes
'; html += '
'; html += '
'; html += '
'; // table wrapper div html += ''; // sidebar tabs this.div.html( html ); }, do_save_api_key: function() { // save changes to api key app.clearError(); var api_key = this.get_api_key_form_json(); if (!api_key) return; // error this.api_key = api_key; app.showProgress( 1.0, "Saving API Key..." ); app.api.post( 'app/update_api_key', api_key, this.save_api_key_finish.bind(this) ); }, save_api_key_finish: function(resp, tx) { // new API Key saved successfully app.hideProgress(); app.showMessage('success', "The API Key was saved successfully."); window.scrollTo( 0, 0 ); }, show_delete_api_key_dialog: function() { // show dialog confirming api key delete action var self = this; app.confirm( 'Delete API Key', "Are you sure you want to permanently delete the API Key \""+this.api_key.title+"\"? There is no way to undo this action.", 'Delete', function(result) { if (result) { app.showProgress( 1.0, "Deleting API Key..." ); app.api.post( 'app/delete_api_key', self.api_key, self.delete_api_key_finish.bind(self) ); } } ); }, delete_api_key_finish: function(resp, tx) { // finished deleting API Key var self = this; app.hideProgress(); Nav.go('Admin?sub=api_keys', 'force'); setTimeout( function() { app.showMessage('success', "The API Key '"+self.api_key.title+"' was deleted successfully."); }, 150 ); }, get_api_key_edit_html: function() { // get html for editing an API Key (or creating a new one) var html = ''; var api_key = this.api_key; // API Key html += get_form_table_row( 'API Key', ' « Generate Random' ); html += get_form_table_caption( "The API Key string is used to authenticate API calls." ); html += get_form_table_spacer(); // status html += get_form_table_row( 'Status', '' ); html += get_form_table_caption( "'Disabled' means that the API Key remains in the system, but it cannot be used for any API calls." ); html += get_form_table_spacer(); // title html += get_form_table_row( 'App Title', '' ); html += get_form_table_caption( "Enter the title of the application that will be using the API Key."); html += get_form_table_spacer(); // description html += get_form_table_row('App Description', ''); html += get_form_table_caption( "Optionally enter a more detailed description of the application." ); html += get_form_table_spacer(); // privilege list var priv_html = ''; for (var idx = 0, len = config.privilege_list.length; idx < len; idx++) { var priv = config.privilege_list[idx]; if (priv.id != 'admin') { var has_priv = !!api_key.privileges[ priv.id ]; priv_html += '
'; priv_html += ''; priv_html += ''; priv_html += '
'; } } html += get_form_table_row( 'Privileges', priv_html ); html += get_form_table_caption( "Select which privileges the API Key should have." ); html += get_form_table_spacer(); return html; }, get_api_key_form_json: function() { // get api key elements from form, used for new or edit var api_key = this.api_key; api_key.key = $('#fe_ak_key').val(); api_key.active = $('#fe_ak_status').val(); api_key.title = $('#fe_ak_title').val(); api_key.description = $('#fe_ak_desc').val(); if (!api_key.key.length) { return app.badField('#fe_ak_key', "Please enter an API Key string, or generate a random one."); } for (var idx = 0, len = config.privilege_list.length; idx < len; idx++) { var priv = config.privilege_list[idx]; api_key.privileges[ priv.id ] = $('#fe_ak_priv_'+priv.id).is(':checked') ? 1 : 0; } return api_key; }, generate_key: function() { // generate random api key $('#fe_ak_key').val( get_unique_id() ); } });