Przeglądaj źródła

Merge pull request #853 from weseek/fix/852-generate-url-with-siteUrl

Fix/852 generate url with site url
Yuki Takei 7 lat temu
rodzic
commit
bb5a0b2124

+ 3 - 3
src/client/js/components/PagePathAutoComplete.jsx

@@ -1,7 +1,7 @@
 import React from 'react';
 import PropTypes from 'prop-types';
 
-import * as pagePathUtils from '@commons/util/page-path-utils';
+import * as pathUtils from '@commons/util/path-utils';
 import SearchTypeahead from './SearchTypeahead';
 
 export default class PagePathAutoComplete extends React.Component {
@@ -35,8 +35,8 @@ export default class PagePathAutoComplete extends React.Component {
 
   getKeywordOnInit(path) {
     return this.props.addSlashToTheEnd
-      ? pagePathUtils.addSlashToTheEnd(path)
-      : pagePathUtils.removeLastSlash(path);
+      ? pathUtils.addSlashToTheEnd(path)
+      : pathUtils.removeLastSlash(path);
   }
 
   render() {

+ 2 - 2
src/client/js/legacy/crowi.js

@@ -7,7 +7,7 @@ import ReactDOM from 'react-dom';
 
 import { debounce } from 'throttle-debounce';
 
-const pagePathUtils = require('@commons/util/page-path-utils');
+const pathUtils = require('@commons/util/path-utils');
 const entities = require('entities');
 const escapeStringRegexp = require('escape-string-regexp');
 require('jquery.cookie');
@@ -343,7 +343,7 @@ $(function() {
     if (name.match(/.+\/$/)) {
       name = name.substr(0, name.length - 1);
     }
-    top.location.href = pagePathUtils.encodePagePath(name) + '#edit';
+    top.location.href = pathUtils.encodePagePath(name) + '#edit';
     return false;
   });
 

+ 0 - 0
src/lib/util/page-path-utils.js → src/lib/util/path-utils.js


+ 8 - 8
src/server/routes/page.js

@@ -3,7 +3,7 @@ module.exports = function(crowi, app) {
 
   const debug = require('debug')('growi:routes:page')
     , logger = require('@alias/logger')('growi:routes:page')
-    , pagePathUtils = require('@commons/util/page-path-utils')
+    , pathUtils = require('@commons/util/path-utils')
     , Page = crowi.model('Page')
     , User = crowi.model('User')
     , Config   = crowi.model('Config')
@@ -153,7 +153,7 @@ module.exports = function(crowi, app) {
       seener_threshold: SEENER_THRESHOLD,
     };
     renderVars.pager = generatePager(result.offset, result.limit, result.totalCount);
-    renderVars.pages = pagePathUtils.encodePagesPath(result.pages);
+    renderVars.pages = pathUtils.encodePagesPath(result.pages);
   }
 
   function replacePlaceholdersOfTemplate(template, req) {
@@ -233,7 +233,7 @@ module.exports = function(crowi, app) {
     }
     else if (page.redirectTo) {
       debug(`Redirect to '${page.redirectTo}'`);
-      return res.redirect(encodeURI(page.redirectTo + '?redirectFrom=' + pagePathUtils.encodePagePath(path)));
+      return res.redirect(encodeURI(page.redirectTo + '?redirectFrom=' + pathUtils.encodePagePath(path)));
     }
 
     logger.debug('Page is found when processing pageShowForGrowiBehavior', page._id, page.path);
@@ -334,7 +334,7 @@ module.exports = function(crowi, app) {
 
       if (hasPortalPage) {
         logger.debug('The portal page is found', portalPagePath);
-        return res.redirect(encodeURI(portalPagePath + '?redirectFrom=' + pagePathUtils.encodePagePath(req.path)));
+        return res.redirect(encodeURI(portalPagePath + '?redirectFrom=' + pathUtils.encodePagePath(req.path)));
       }
     }
 
@@ -446,7 +446,7 @@ module.exports = function(crowi, app) {
     }
 
     renderVars.pager = generatePager(result.offset, result.limit, result.totalCount);
-    renderVars.pages = pagePathUtils.encodePagesPath(result.pages);
+    renderVars.pages = pathUtils.encodePagesPath(result.pages);
     res.render('customlayout-selector/page_list', renderVars);
 
   };
@@ -460,7 +460,7 @@ module.exports = function(crowi, app) {
     const page = await Page.findByIdAndViewer(id, req.user);
 
     if (page != null) {
-      return res.redirect(pagePathUtils.encodePagePath(page.path));
+      return res.redirect(pathUtils.encodePagePath(page.path));
     }
 
     return res.redirect('/');
@@ -510,7 +510,7 @@ module.exports = function(crowi, app) {
         result.pages.pop();
       }
 
-      result.pages = pagePathUtils.encodePagesPath(result.pages);
+      result.pages = pathUtils.encodePagesPath(result.pages);
       return res.json(ApiResponse.success(result));
     }
     catch (err) {
@@ -1086,7 +1086,7 @@ module.exports = function(crowi, app) {
 
     try {
       let result = await Page.findListByCreator(page.creator, req.user, queryOptions);
-      result.pages = pagePathUtils.encodePagesPath(result.pages);
+      result.pages = pathUtils.encodePagesPath(result.pages);
 
       return res.json(ApiResponse.success(result));
     }

+ 4 - 3
src/server/service/config-manager.js

@@ -1,5 +1,6 @@
-const ConfigLoader = require('../service/config-loader')
-  , debug = require('debug')('growi:service:ConfigManager');
+const debug = require('debug')('growi:service:ConfigManager');
+const pathUtils = require('@commons/util/path-utils');
+const ConfigLoader = require('../service/config-loader');
 
 const KEYS_FOR_SAML_USE_ONLY_ENV_OPTION = [
   'security:passport-saml:isEnabled',
@@ -80,7 +81,7 @@ class ConfigManager {
   getSiteUrl() {
     const siteUrl = this.getConfig('crowi', 'app:siteUrl');
     if (siteUrl != null) {
-      return siteUrl;
+      return pathUtils.removeLastSlash(siteUrl);
     }
     else {
       return '[The site URL is not set. Please set it!]';

+ 5 - 4
src/server/service/passport.js

@@ -1,4 +1,5 @@
 const debug = require('debug')('growi:service:PassportService');
+const urljoin = require('url-join');
 const passport = require('passport');
 const LocalStrategy = require('passport-local').Strategy;
 const LdapStrategy = require('passport-ldapauth');
@@ -312,7 +313,7 @@ class PassportService {
       clientId: config.crowi['security:passport-google:clientId'] || process.env.OAUTH_GOOGLE_CLIENT_ID,
       clientSecret: config.crowi['security:passport-google:clientSecret'] || process.env.OAUTH_GOOGLE_CLIENT_SECRET,
       callbackURL: (this.crowi.configManager.getConfig('crowi', 'app:siteUrl') != null)
-        ? `${this.crowi.configManager.getSiteUrl()}/passport/google/callback`                               // auto-generated with v3.2.4 and above
+        ? urljoin(this.crowi.configManager.getSiteUrl(), '/passport/google/callback')                       // auto-generated with v3.2.4 and above
         : config.crowi['security:passport-google:callbackUrl'] || process.env.OAUTH_GOOGLE_CALLBACK_URI,    // DEPRECATED: backward compatible with v3.2.3 and below
       skipUserProfile: false,
     }, function(accessToken, refreshToken, profile, done) {
@@ -359,7 +360,7 @@ class PassportService {
       clientID: config.crowi['security:passport-github:clientId'] || process.env.OAUTH_GITHUB_CLIENT_ID,
       clientSecret: config.crowi['security:passport-github:clientSecret'] || process.env.OAUTH_GITHUB_CLIENT_SECRET,
       callbackURL: (this.crowi.configManager.getConfig('crowi', 'app:siteUrl') != null)
-        ? `${this.crowi.configManager.getSiteUrl()}/passport/github/callback`                               // auto-generated with v3.2.4 and above
+        ? urljoin(this.crowi.configManager.getSiteUrl(), '/passport/github/callback')                       // auto-generated with v3.2.4 and above
         : config.crowi['security:passport-github:callbackUrl'] || process.env.OAUTH_GITHUB_CALLBACK_URI,    // DEPRECATED: backward compatible with v3.2.3 and below
       skipUserProfile: false,
     }, function(accessToken, refreshToken, profile, done) {
@@ -406,7 +407,7 @@ class PassportService {
       consumerKey: config.crowi['security:passport-twitter:consumerKey'] || process.env.OAUTH_TWITTER_CONSUMER_KEY,
       consumerSecret: config.crowi['security:passport-twitter:consumerSecret'] || process.env.OAUTH_TWITTER_CONSUMER_SECRET,
       callbackURL: (this.crowi.configManager.getConfig('crowi', 'app:siteUrl') != null)
-        ? `${this.crowi.configManager.getSiteUrl()}/passport/twitter/callback`                               // auto-generated with v3.2.4 and above
+        ? urljoin(this.crowi.configManager.getSiteUrl(), '/passport/twitter/callback')                       // auto-generated with v3.2.4 and above
         : config.crowi['security:passport-twitter:callbackUrl'] || process.env.OAUTH_TWITTER_CALLBACK_URI,   // DEPRECATED: backward compatible with v3.2.3 and below
       skipUserProfile: false,
     }, function(accessToken, refreshToken, profile, done) {
@@ -452,7 +453,7 @@ class PassportService {
     passport.use(new SamlStrategy({
       entryPoint: configManager.getConfig('crowi', 'security:passport-saml:entryPoint'),
       callbackUrl: (this.crowi.configManager.getConfig('crowi', 'app:siteUrl') != null)
-        ? `${this.crowi.configManager.getSiteUrl()}/passport/saml/callback`          // auto-generated with v3.2.4 and above
+        ? urljoin(this.crowi.configManager.getSiteUrl(), '/passport/saml/callback')  // auto-generated with v3.2.4 and above
         : configManager.getConfig('crowi', 'security:passport-saml:callbackUrl'),    // DEPRECATED: backward compatible with v3.2.3 and below
       issuer: configManager.getConfig('crowi', 'security:passport-saml:issuer'),
       cert: configManager.getConfig('crowi', 'security:passport-saml:cert'),

+ 12 - 10
src/server/util/googleAuth.js

@@ -1,3 +1,7 @@
+const debug = require('debug')('growi:lib:googleAuth');
+const urljoin = require('url-join');
+const { GoogleApis } = require('googleapis');
+
 /**
  * googleAuth utility
  */
@@ -5,9 +9,7 @@
 module.exports = function(crowi) {
   'use strict';
 
-  const { GoogleApis } = require('googleapis');
-  var google = new GoogleApis()
-    , debug = require('debug')('growi:lib:googleAuth')
+  const google = new GoogleApis()
     , config = crowi.getConfig()
     , lib = {}
     ;
@@ -21,11 +23,11 @@ module.exports = function(crowi) {
   }
 
   lib.createAuthUrl = function(req, callback) {
-    var callbackUrl = crowi.configManager.getSiteUrl() + '/google/callback';
-    var oauth2Client = createOauth2Client(callbackUrl);
+    const callbackUrl = urljoin(crowi.configManager.getSiteUrl(), '/google/callback');
+    const oauth2Client = createOauth2Client(callbackUrl);
     google.options({auth: oauth2Client});
 
-    var redirectUrl = oauth2Client.generateAuthUrl({
+    const redirectUrl = oauth2Client.generateAuthUrl({
       access_type: 'offline',
       scope: ['profile', 'email'],
     });
@@ -34,11 +36,11 @@ module.exports = function(crowi) {
   };
 
   lib.handleCallback = function(req, callback) {
-    var callbackUrl = crowi.configManager.getSiteUrl() + '/google/callback';
-    var oauth2Client = createOauth2Client(callbackUrl);
+    const callbackUrl = urljoin(crowi.configManager.getSiteUrl(), '/google/callback');
+    const oauth2Client = createOauth2Client(callbackUrl);
     google.options({auth: oauth2Client});
 
-    var code = req.session.googleAuthCode || null;
+    const code = req.session.googleAuthCode || null;
 
     if (!code) {
       return callback(new Error('No code exists.'), null);
@@ -53,7 +55,7 @@ module.exports = function(crowi) {
 
       oauth2Client.credentials = tokens;
 
-      var oauth2 = google.oauth2('v2');
+      const oauth2 = google.oauth2('v2');
       oauth2.userinfo.get({}, function(err, response) {
         debug('Response of oauth2.userinfo.get', err, response);
         if (err) {

+ 9 - 7
src/server/util/slack.js

@@ -1,3 +1,6 @@
+const debug = require('debug')('growi:util:slack');
+const urljoin = require('url-join');
+
 /**
  * slack
  */
@@ -5,8 +8,7 @@
 module.exports = function(crowi) {
   'use strict';
 
-  const debug = require('debug')('growi:util:slack'),
-    config = crowi.getConfig(),
+  const config = crowi.getConfig(),
     Config = crowi.model('Config'),
     Slack = require('slack-node'),
     slack = {};
@@ -123,10 +125,10 @@ module.exports = function(crowi) {
     const attachment = {
       color: '#263a3c',
       author_name: '@' + user.username,
-      author_link: url + '/user/' + user.username,
+      author_link: urljoin(url, 'user', user.username),
       author_icon: user.image,
       title: page.path,
-      title_link: url + '/' + page._id,
+      title_link: urljoin(url, page._id),
       text: body,
       mrkdwn_in: ['text'],
     };
@@ -151,7 +153,7 @@ module.exports = function(crowi) {
     const attachment = {
       color: '#263a3c',
       author_name: '@' + user.username,
-      author_link: url + '/user/' + user.username,
+      author_link: urljoin(url, 'user', user.username),
       author_icon: user.image,
       text: body,
       mrkdwn_in: ['text'],
@@ -174,7 +176,7 @@ module.exports = function(crowi) {
     let text;
     const url = crowi.configManager.getSiteUrl();
 
-    const pageUrl = `<${url}${path}|${path}>`;
+    const pageUrl = `<${urljoin(url, path)}|${path}>`;
     if (updateType == 'create') {
       text = `:rocket: ${user.username} created a new page! ${pageUrl}`;
     }
@@ -187,7 +189,7 @@ module.exports = function(crowi) {
 
   const getSlackMessageTextForComment = function(path, user) {
     const url = crowi.configManager.getSiteUrl();
-    const pageUrl = `<${url}${path}|${path}>`;
+    const pageUrl = `<${urljoin(url, path)}|${path}>`;
     const text = `:speech_balloon: ${user.username} commented on ${pageUrl}`;
 
     return text;