|
|
@@ -183,6 +183,45 @@ module.exports = function(crowi, app) {
|
|
|
}
|
|
|
|
|
|
actions.externalAccounts.associateLdap = function(req, res) {
|
|
|
+ const passport = require('passport');
|
|
|
+ const passportService = crowi.passportService;
|
|
|
+
|
|
|
+ const redirectWithFlash = (type, msg) => {
|
|
|
+ req.flash(type, msg);
|
|
|
+ return res.redirect('/me/external-accounts');
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!passportService.isLdapStrategySetup) {
|
|
|
+ debug('LdapStrategy has not been set up');
|
|
|
+ return redirectWithFlash('warning', 'LdapStrategy has not been set up');
|
|
|
+ }
|
|
|
+
|
|
|
+ const loginForm = req.body.loginForm;
|
|
|
+
|
|
|
+ passport.authenticate('ldapauth', (err, user, info) => {
|
|
|
+ if (err) { // DB Error
|
|
|
+ console.log('LDAP Server Error: ', err);
|
|
|
+ return redirectWithFlash('warningMessage', 'LDAP Server Error occured.');
|
|
|
+ }
|
|
|
+ if (info && info.message) {
|
|
|
+ return redirectWithFlash('warningMessage', info.message);
|
|
|
+ }
|
|
|
+ if (user) {
|
|
|
+ // create ExternalAccount
|
|
|
+ const ldapAccountId = passportService.getLdapAccountIdFromReq(req);
|
|
|
+ const user = req.user;
|
|
|
+
|
|
|
+ ExternalAccount.create({ providerType: 'ldap', accountId: ldapAccountId, user: user._id })
|
|
|
+ .then(() => {
|
|
|
+ return redirectWithFlash('successMessage', 'Successfully added.');
|
|
|
+ })
|
|
|
+ .catch((err) => {
|
|
|
+ return redirectWithFlash('errorMessage', err.message);
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+ })(req, res, () => {});
|
|
|
+
|
|
|
|
|
|
}
|
|
|
|