user.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { db, enabledOAuth2 } from './util.js';
  2. /**
  3. * Let a user change settings
  4. * @param {import('http').ServerResponse} res - The server response
  5. * @param {import('cheerio').CheerioAPI} $ - The response body
  6. * @param {import('./util.js').User} user - The current user
  7. * @param {import('./i18n.js').default} dashboardLang - The user language
  8. */
  9. function dashboard_user(res, $, user, dashboardLang) {
  10. db.query( 'SELECT site, token FROM oauthusers WHERE userid = $1', [user.id] ).then( ({rows}) => {
  11. $('<p>').text(dashboardLang.get('oauth.desc')).appendTo('#text .description');
  12. $('<form id="wb-settings" method="post" enctype="application/x-www-form-urlencoded">').append(
  13. $('<h2>').text(dashboardLang.get('oauth.form.default')),
  14. ...enabledOAuth2.map( oauthSite => {
  15. let row = rows.find( row => row.site === oauthSite.id );
  16. let buttons = $('<div>');
  17. if ( row ) {
  18. if ( row.token === null ) buttons.append(
  19. $('<span>').append(
  20. $('<input type="submit">').addClass('wb-oauth-enabled').attr('name', 'oauth_enable_' + oauthSite.id).val(dashboardLang.get('oauth.form.enable'))
  21. ),
  22. $('<span>').append(
  23. $('<input type="submit">').addClass('wb-oauth-connected').attr('name', 'oauth_connect_' + oauthSite.id).val(dashboardLang.get('oauth.form.connect'))
  24. )
  25. );
  26. else buttons.append(
  27. $('<span>').append(
  28. $('<input type="submit">').addClass('wb-oauth-disabled').attr('name', 'oauth_disable_' + oauthSite.id).val(dashboardLang.get('oauth.form.disable'))
  29. ),
  30. $('<span>').append(
  31. $('<input type="submit">').addClass('wb-oauth-unconnected').attr('name', 'oauth_disconnect_' + oauthSite.id).val(dashboardLang.get('oauth.form.disconnect'))
  32. )
  33. );
  34. }
  35. else buttons.append(
  36. $('<span>').append(
  37. $('<input type="submit">').addClass('wb-oauth-disabled').attr('name', 'oauth_disable_' + oauthSite.id).val(dashboardLang.get('oauth.form.disable'))
  38. ),
  39. $('<span>').append(
  40. $('<input type="submit">').addClass('wb-oauth-connected').attr('name', 'oauth_connect_' + oauthSite.id).val(dashboardLang.get('oauth.form.connect'))
  41. )
  42. );
  43. return $('<div>').addClass('wb-oauth-site').attr('id', 'oauth-' + oauthSite.id).append(
  44. $('<fieldset>').append(
  45. $('<legend>').append(
  46. $('<a target="_blank">').attr('href', oauthSite.url).text(oauthSite.name)
  47. ),
  48. $('<div>').append(
  49. $('<span>').text(dashboardLang.get('oauth.form.current')),
  50. ( row ? ( row.token === null ?
  51. $('<span>').addClass('wb-oauth-disabled').text(dashboardLang.get('oauth.form.disabled'))
  52. :
  53. $('<span>').addClass('wb-oauth-connected').text(dashboardLang.get('oauth.form.connected'))
  54. ) :
  55. $('<span>').addClass('wb-oauth-unconnected').text(dashboardLang.get('oauth.form.unconnected'))
  56. )
  57. ),
  58. buttons
  59. )
  60. )
  61. } )
  62. ).attr('action', '/user').appendTo('#text');
  63. }, dberror => {
  64. console.log( '- Dashboard: Error while getting the OAuth2 info: ' + dberror );
  65. createNotice($, 'error', dashboardLang);
  66. $('<p>').text(dashboardLang.get('oauth.failed')).appendTo('#text .description');
  67. } ).then( () => {
  68. let body = $.html();
  69. res.writeHead(200, {'Content-Length': Buffer.byteLength(body)});
  70. res.write( body );
  71. return res.end();
  72. } );
  73. }
  74. /**
  75. * Change settings
  76. * @param {Function} res - The server response
  77. * @param {String} user_id - The current user
  78. * @param {String} type - The setting to change
  79. * @param {String} oauth_id - The OAuth2 site to change
  80. */
  81. function update_user(res, user_id, type, oauth_id) {
  82. if ( !['connect', 'disconnect', 'disable', 'enable'].includes( type ) || !enabledOAuth2.some( oauthSite => oauthSite.id === oauth_id ) ) {
  83. return res('/user', 'savefail');
  84. }
  85. if ( type === 'disconnect' || type === 'enable' ) return db.query( 'DELETE FROM oauthusers WHERE userid = $1 AND site = $2', [user_id, oauth_id] ).then( () => {
  86. if ( type === 'disconnect' ) console.log( '- Dashboard: Successfully disconnected ' + user_id + ' from ' + oauth_id + '.' );
  87. else console.log( '- Dashboard: Successfully enabled ' + oauth_id + ' for ' + user_id + '.' );
  88. return res('/user', 'save');
  89. }, dberror => {
  90. if ( type === 'disconnect' ) console.log( '- Dashboard: Error while disconnecting ' + user_id + ' from ' + oauth_id + ': ' + dberror );
  91. else console.log( '- Dashboard: Error while enabling ' + oauth_id + ' for ' + user_id + ': ' + dberror );
  92. return res('/user', 'savefail');
  93. } );
  94. return db.query( 'SELECT FROM oauthusers WHERE userid = $1 AND site = $2', [user_id, oauth_id] ).then( ({rows:[row]}) => {
  95. if ( type === 'disable' ) {
  96. let sql = 'INSERT INTO oauthusers(userid, site, token) VALUES($1, $2, $3)';
  97. if ( row ) sql = 'UPDATE oauthusers SET token = $3 WHERE userid = $1 AND site = $2';
  98. return db.query( sql, [user_id, oauth_id, null] ).then( () => {
  99. console.log( '- Dashboard: Successfully disabled ' + oauth_id + ' for ' + user_id + '.' );
  100. return res('/user', 'save');
  101. }, dberror => {
  102. console.log( '- Dashboard: Error while disabling ' + oauth_id + ' for ' + user_id + ': ' + dberror );
  103. return res('/user', 'savefail');
  104. } );
  105. }
  106. if ( type !== 'connect' ) return res('/user', 'savefail');
  107. var oauthSite = enabledOAuth2.find( oauthSite => oauthSite.id === oauth_id );
  108. if ( row ) db.query( 'DELETE FROM oauthusers WHERE userid = $1 AND site = $2', [user_id, oauth_id] ).then( () => {
  109. console.log( '- Dashboard: Successfully disconnected ' + user_id + ' from ' + oauth_id + ' for reconnection.' );
  110. }, dberror => {
  111. console.log( '- Dashboard: Error while disconnecting ' + user_id + ' from ' + oauth_id + ' for reconnection: ' + dberror );
  112. } );
  113. let oauthURL = oauthSite.url + 'rest.php/oauth2/authorize?' + new URLSearchParams({
  114. response_type: 'code', redirect_uri: new URL('/oauth/mw', process.env.dashboard).href,
  115. client_id: process.env['oauth_' + oauthSite.id], state: oauthSite.id
  116. }).toString();
  117. return res(oauthURL, 'REDIRECT');
  118. }, dberror => {
  119. console.log( '- Dashboard: Error while getting the OAuth2 info on ' + oauth_id + ' for ' + user_id + ': ' + dberror );
  120. return res('/user', 'savefail');
  121. } );
  122. }
  123. export {
  124. dashboard_user as get,
  125. update_user as post
  126. };