forked from rinkp/custommailcow-ldap
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
88 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,88 @@ | ||
import { | ||
DataSource, | ||
Repository, | ||
} from 'typeorm'; | ||
import { | ||
Defaults, | ||
SOGoMailIdentity, | ||
} from './types'; | ||
import { SogoUserProfile } from './entities/SogoUserProfile'; | ||
import { Users } from './entities/User'; | ||
import { getActiveDirectoryDisplayName } from './index'; | ||
import axios from 'axios'; | ||
import { containerConfig } from './index'; | ||
|
||
// Connection options for the DB | ||
let dataSource : DataSource; | ||
let SogoUserProfileRepository: Repository<SogoUserProfile>; | ||
|
||
/** | ||
* Initialize database connection. Setup database if it does not yet exist | ||
*/ | ||
export async function initializeMailcowDatabase(): Promise<void> { | ||
dataSource = new DataSource({ | ||
type: 'mariadb', | ||
host: '172.22.1.251', | ||
port: 3306, | ||
username: 'mailcow', | ||
password: containerConfig.DB_PASSWORD, | ||
database: 'mailcow', | ||
entities: [ | ||
SogoUserProfile, | ||
], | ||
}); | ||
|
||
await dataSource.initialize() | ||
.catch((error) => { | ||
console.log(error); | ||
}); | ||
|
||
SogoUserProfileRepository = dataSource.getRepository(SogoUserProfile); | ||
} | ||
|
||
/** | ||
* Edit the signatures of a user | ||
* @param user - user to edit the signatures of | ||
* @param SOBs - all SOB for which the user should get signatures | ||
*/ | ||
export async function editUserSignatures(user: Users, SOBs: string[]): Promise<void> { | ||
console.log(`Changing signatures for ${user.email}`); | ||
|
||
let userProfile: SogoUserProfile = await SogoUserProfileRepository.findOneOrFail({ | ||
where: { | ||
c_uid: user.email, | ||
}, | ||
}); | ||
|
||
let defaultSettings: Defaults = JSON.parse(userProfile.c_defaults); | ||
let newIdentities: SOGoMailIdentity[] = []; | ||
|
||
for (let identity of defaultSettings.SOGoMailIdentities) { | ||
if (identity.signature.indexOf('class="autogenerated"') === -1) { | ||
newIdentities.push(identity); | ||
} | ||
} | ||
|
||
for (let identityMail of SOBs) { | ||
const committeeDisplayName: string = await getActiveDirectoryDisplayName(identityMail); | ||
let signature: string = (await axios.get(`https://signature.gewis.nl/${identityMail}`)).data; | ||
signature = signature.replace('{{displayName}}', user.displayName); | ||
signature = signature.replaceAll('{{committeeDisplayName}}', committeeDisplayName); | ||
signature = signature.replaceAll('{{identityMail}}', identityMail); | ||
|
||
signature.replaceAll('', ''); | ||
|
||
let newIdentity : SOGoMailIdentity = { | ||
email: identityMail, | ||
fullName: `${user.displayName} | ${committeeDisplayName}`, | ||
replyTo: user.email, | ||
signature: `${signature}`, | ||
}; | ||
newIdentities.push(newIdentity); | ||
} | ||
|
||
defaultSettings.SOGoMailIdentities = newIdentities; | ||
userProfile.c_defaults = JSON.stringify(defaultSettings); | ||
|
||
await SogoUserProfileRepository.update(user.email, userProfile); | ||
import { | ||
DataSource, | ||
Repository, | ||
} from 'typeorm'; | ||
import { | ||
Defaults, | ||
SOGoMailIdentity, | ||
} from './types'; | ||
import { SogoUserProfile } from './entities/SogoUserProfile'; | ||
import { Users } from './entities/User'; | ||
import { getActiveDirectoryDisplayName } from './index'; | ||
import axios from 'axios'; | ||
import { containerConfig } from './index'; | ||
|
||
// Connection options for the DB | ||
let dataSource : DataSource; | ||
let SogoUserProfileRepository: Repository<SogoUserProfile>; | ||
|
||
/** | ||
* Initialize database connection. Setup database if it does not yet exist | ||
*/ | ||
export async function initializeMailcowDatabase(): Promise<void> { | ||
dataSource = new DataSource({ | ||
type: 'mariadb', | ||
host: '172.22.1.251', | ||
port: 3306, | ||
username: 'mailcow', | ||
password: containerConfig.DB_PASSWORD, | ||
database: 'mailcow', | ||
entities: [ | ||
SogoUserProfile, | ||
], | ||
}); | ||
|
||
await dataSource.initialize() | ||
.catch((error) => { | ||
console.log(error); | ||
}); | ||
|
||
SogoUserProfileRepository = dataSource.getRepository(SogoUserProfile); | ||
} | ||
|
||
/** | ||
* Edit the signatures of a user | ||
* @param user - user to edit the signatures of | ||
* @param SOBs - all SOB for which the user should get signatures | ||
*/ | ||
export async function editUserSignatures(user: Users, SOBs: string[]): Promise<void> { | ||
console.log(`Changing signatures for ${user.email}`); | ||
|
||
let userProfile: SogoUserProfile = await SogoUserProfileRepository.findOneOrFail({ | ||
where: { | ||
c_uid: user.email, | ||
}, | ||
}); | ||
|
||
let defaultSettings: Defaults = JSON.parse(userProfile.c_defaults); | ||
let newIdentities: SOGoMailIdentity[] = []; | ||
|
||
for (let identity of defaultSettings.SOGoMailIdentities) { | ||
if (identity.signature.indexOf('class="autogenerated"') === -1) { | ||
newIdentities.push(identity); | ||
} | ||
} | ||
|
||
for (let identityMail of SOBs) { | ||
const committeeDisplayName: string = await getActiveDirectoryDisplayName(identityMail); | ||
let signature: string = (await axios.get(`https://signature.gewis.nl/${identityMail}`)).data; | ||
signature = signature.replace('{{displayName}}', user.displayName); | ||
signature = signature.replaceAll('{{committeeDisplayName}}', committeeDisplayName); | ||
signature = signature.replaceAll('{{identityMail}}', identityMail); | ||
|
||
signature.replaceAll('', ''); | ||
|
||
let newIdentity : SOGoMailIdentity = { | ||
email: identityMail, | ||
fullName: `${user.displayName} | ${committeeDisplayName}`, | ||
replyTo: user.email, | ||
signature: `${signature}`, | ||
}; | ||
newIdentities.push(newIdentity); | ||
} | ||
|
||
defaultSettings.SOGoMailIdentities = newIdentities; | ||
userProfile.c_defaults = JSON.stringify(defaultSettings); | ||
|
||
await SogoUserProfileRepository.update(user.email, userProfile); | ||
} |