Просмотр исходного кода

refs 124384: remove crowi dependency

Futa Arai 2 лет назад
Родитель
Сommit
5041b65637

+ 2 - 2
apps/app/src/interfaces/external-user-group.ts

@@ -34,8 +34,8 @@ export interface LdapGroupSyncSettings {
 export type ExternalUserInfo = {
   id: string, // external user id
   username: string,
-  name: string,
-  email?: string,
+  name?: string,
+  email: string,
 }
 
 // Data structure to express the tree structure of external groups, before converting to ExternalUserGroup model

+ 5 - 3
apps/app/src/server/crowi/index.js

@@ -26,12 +26,13 @@ import { aclService as aclServiceSingletonInstance } from '../service/acl';
 import AppService from '../service/app';
 import AttachmentService from '../service/attachment';
 import { configManager as configManagerSingletonInstance } from '../service/config-manager';
-import ExternalAccountService from '../service/external-account';
+import { instanciate as instanciateExternalAccountService, externalAccountService } from '../service/external-account';
 import { G2GTransferPusherService, G2GTransferReceiverService } from '../service/g2g-transfer';
 import { InstallerService } from '../service/installer';
 import PageService from '../service/page';
 import PageGrantService from '../service/page-grant';
 import PageOperationService from '../service/page-operation';
+import PassportService from '../service/passport';
 import SearchService from '../service/search';
 import { SlackIntegrationService } from '../service/slack-integration';
 import { UserNotificationService } from '../service/user-notification';
@@ -362,7 +363,6 @@ Crowi.prototype.setupPassport = async function() {
   logger.debug('Passport is enabled');
 
   // initialize service
-  const PassportService = require('../service/passport');
   if (this.passportService == null) {
     this.passportService = new PassportService(this);
   }
@@ -783,9 +783,11 @@ Crowi.prototype.setupG2GTransferService = async function() {
   }
 };
 
+// execute after setupPassport
 Crowi.prototype.setupExternalAccountService = function() {
   if (this.externalAccountService == null) {
-    this.externalAccountService = new ExternalAccountService(this);
+    instanciateExternalAccountService(this.passportService);
+    this.externalAccountService = externalAccountService;
   }
 };
 

+ 1 - 1
apps/app/src/server/routes/apiv3/external-user-group.ts

@@ -81,7 +81,7 @@ module.exports = (crowi: Crowi): Router => {
 
   router.put('/ldap/sync', loginRequiredStrictly, adminRequired, async(req: AuthorizedRequest, res: ApiV3Response) => {
     try {
-      const ldapUserGroupSyncService = new LdapUserGroupSyncService(crowi, req.user.name, req.body.password);
+      const ldapUserGroupSyncService = new LdapUserGroupSyncService(crowi.passportService, req.user.name, req.body.password);
       await ldapUserGroupSyncService.syncExternalUserGroups();
     }
     catch (err) {

+ 18 - 9
apps/app/src/server/service/external-account.js → apps/app/src/server/service/external-account.ts

@@ -1,24 +1,31 @@
 import { ErrorV3 } from '^/../../packages/core/dist';
+import mongoose from 'mongoose';
 
 import { LoginErrorCode } from '~/interfaces/errors/login-error';
+import { IExternalAccount } from '~/interfaces/external-account';
 import loggerFactory from '~/utils/logger';
 
 import { NullUsernameToBeRegisteredError } from '../models/errors';
 
+import PassportService from './passport';
+
 const logger = loggerFactory('growi:service:external-account-service');
 
 class ExternalAccountService {
 
-  constructor(crowi) {
-    this.crowi = crowi;
+  passportService: PassportService;
+
+  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
+  constructor(passportService: PassportService) {
+    this.passportService = passportService;
   }
 
-  async getOrCreateUser(userInfo, providerId) {
+  async getOrCreateUser(userInfo: {id: string, username: string, name?: string, email: string}, providerId: string): Promise<IExternalAccount | undefined> {
     // get option
-    const isSameUsernameTreatedAsIdenticalUser = this.crowi.passportService.isSameUsernameTreatedAsIdenticalUser(providerId);
-    const isSameEmailTreatedAsIdenticalUser = this.crowi.passportService.isSameEmailTreatedAsIdenticalUser(providerId);
+    const isSameUsernameTreatedAsIdenticalUser = this.passportService.isSameUsernameTreatedAsIdenticalUser(providerId);
+    const isSameEmailTreatedAsIdenticalUser = this.passportService.isSameEmailTreatedAsIdenticalUser(providerId);
 
-    const ExternalAccount = this.crowi.model('ExternalAccount');
+    const ExternalAccount = mongoose.model('ExternalAccount') as any;
 
     try {
       // find or register(create) user
@@ -34,7 +41,6 @@ class ExternalAccountService {
       return externalAccount;
     }
     catch (err) {
-      /* eslint-disable no-else-return */
       if (err instanceof NullUsernameToBeRegisteredError) {
         logger.error(err.message);
         throw new ErrorV3(err.message);
@@ -54,10 +60,13 @@ class ExternalAccountService {
         logger.error(err.message);
         throw new ErrorV3(err.message);
       }
-      /* eslint-enable no-else-return */
     }
   }
 
 }
 
-export default ExternalAccountService;
+// eslint-disable-next-line import/no-mutable-exports
+export let externalAccountService: ExternalAccountService | undefined; // singleton instance
+export function instanciate(passportService: PassportService): void {
+  externalAccountService = new ExternalAccountService(passportService);
+}

+ 10 - 8
apps/app/src/server/service/external-group/external-user-group-sync-service.ts

@@ -1,3 +1,5 @@
+import mongoose from 'mongoose';
+
 import {
   ExternalGroupProviderType, ExternalUserGroupTreeNode, ExternalUserInfo, IExternalUserGroupHasId,
 } from '~/interfaces/external-user-group';
@@ -7,6 +9,7 @@ import ExternalUserGroupRelation from '~/server/models/external-user-group-relat
 import { excludeTestIdsFromTargetIds } from '~/server/util/compare-objectId';
 
 import { configManager } from '../config-manager';
+import { externalAccountService } from '../external-account';
 
 abstract class ExternalUserGroupSyncService {
 
@@ -14,13 +17,10 @@ abstract class ExternalUserGroupSyncService {
 
   authProviderType: string; // auth provider type (e.g: ldap, oidc)
 
-  crowi: any;
-
   // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
-  constructor(crowi: any, groupProviderType: ExternalGroupProviderType, authProviderType: string) {
+  constructor(groupProviderType: ExternalGroupProviderType, authProviderType: string) {
     this.groupProviderType = groupProviderType;
     this.authProviderType = authProviderType;
-    this.crowi = crowi;
   }
 
   /** External user group tree sync method
@@ -92,13 +92,15 @@ abstract class ExternalUserGroupSyncService {
    */
   async getMemberUser(userInfo: ExternalUserInfo): Promise<IUserHasId | null> {
     const autoGenerateUserOnGroupSync = configManager?.getConfig('crowi', `external-user-group:${this.groupProviderType}:autoGenerateUserOnGroupSync`);
+    const ExternalAccount = mongoose.model('ExternalAccount');
 
     const getExternalAccount = async() => {
-      if (autoGenerateUserOnGroupSync) {
-        return this.crowi.externalAccountService.getOrCreateUser(userInfo, this.authProviderType);
+      if (autoGenerateUserOnGroupSync && externalAccountService != null) {
+        return externalAccountService.getOrCreateUser({
+          id: userInfo.id, username: userInfo.username, name: userInfo.name, email: userInfo.email,
+        }, this.authProviderType);
       }
-      return this.crowi.models.ExternalAccount
-        .findOne({ providerType: this.groupProviderType, accountId: userInfo.id });
+      return ExternalAccount.findOne({ providerType: this.groupProviderType, accountId: userInfo.id });
     };
 
     const externalAccount = await getExternalAccount();

+ 12 - 8
apps/app/src/server/service/external-group/ldap-user-group-sync-service.ts

@@ -4,16 +4,20 @@ import {
 
 import { configManager } from '../config-manager';
 import LdapService, { SearchResultEntry } from '../ldap';
+import PassportService from '../passport';
 
 import ExternalUserGroupSyncService from './external-user-group-sync-service';
 
 class LdapUserGroupSyncService extends ExternalUserGroupSyncService {
 
+  passportService: PassportService;
+
   ldapService: LdapService;
 
   // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
-  constructor(crowi: any, userBindUsername?: string, userBindPassword?: string) {
-    super(crowi, ExternalGroupProviderType.ldap, 'ldap');
+  constructor(passportService, userBindUsername?: string, userBindPassword?: string) {
+    super(ExternalGroupProviderType.ldap, 'ldap');
+    this.passportService = passportService;
     this.ldapService = new LdapService(userBindUsername, userBindPassword);
   }
 
@@ -86,9 +90,9 @@ class LdapUserGroupSyncService extends ExternalUserGroupSyncService {
 
   private async getUserInfo(userId: string): Promise<ExternalUserInfo | null> {
     const groupMembershipAttributeType = configManager?.getConfig('crowi', 'external-user-group:ldap:groupMembershipAttributeType');
-    const attrMapUsername = this.crowi.passportService.getLdapAttrNameMappedToUsername();
-    const attrMapName = this.crowi.passportService.getLdapAttrNameMappedToName();
-    const attrMapMail = this.crowi.passportService.getLdapAttrNameMappedToMail();
+    const attrMapUsername = this.passportService.getLdapAttrNameMappedToUsername();
+    const attrMapName = this.passportService.getLdapAttrNameMappedToName();
+    const attrMapMail = this.passportService.getLdapAttrNameMappedToMail();
 
     // get full user info from LDAP server using externalUserInfo (DN or UID)
     const getUserEntries = async() => {
@@ -116,10 +120,10 @@ class LdapUserGroupSyncService extends ExternalUserGroupSyncService {
         const nameToBeRegistered = this.ldapService.getStringValFromSearchResultEntry(userEntry, attrMapName);
         const mailToBeRegistered = this.ldapService.getStringValFromSearchResultEntry(userEntry, attrMapMail);
 
-        return usernameToBeRegistered != null ? {
+        return usernameToBeRegistered != null && mailToBeRegistered != null ? {
           id: uid,
-          username: usernameToBeRegistered || '',
-          name: nameToBeRegistered || '',
+          username: usernameToBeRegistered,
+          name: nameToBeRegistered,
           email: mailToBeRegistered,
         } : null;
       }

+ 1 - 1
apps/app/src/server/service/passport.ts

@@ -984,4 +984,4 @@ class PassportService implements S2sMessageHandlable {
 
 }
 
-module.exports = PassportService;
+export default PassportService;