Yuki Takei 8 лет назад
Родитель
Сommit
59799eacfe

+ 1 - 1
lib/models/external-account.js

@@ -14,7 +14,7 @@ const schema = new mongoose.Schema({
   createdAt: { type: Date, default: Date.now, required: true },
   createdAt: { type: Date, default: Date.now, required: true },
 });
 });
 // compound index
 // compound index
-schema.index({ providerType: 1, accountId: 1 });
+schema.index({ providerType: 1, accountId: 1}, { unique: true });
 // apply plugins
 // apply plugins
 schema.plugin(mongoosePaginate);
 schema.plugin(mongoosePaginate);
 schema.plugin(uniqueValidator);
 schema.plugin(uniqueValidator);

+ 39 - 0
lib/routes/me.js

@@ -183,6 +183,45 @@ module.exports = function(crowi, app) {
   }
   }
 
 
   actions.externalAccounts.associateLdap = function(req, res) {
   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, () => {});
+
 
 
   }
   }
 
 

+ 7 - 0
lib/views/admin/external-accounts.html

@@ -12,6 +12,13 @@
 
 
 {% block content_main %}
 {% block content_main %}
 <div class="content-main">
 <div class="content-main">
+  {% set wmessage = req.flash('warningMessage') %}
+  {% if wmessage.length %}
+  <div class="alert alert-warning">
+    {{ wmessage }}
+  </div>
+  {% endif %}
+
   {% set smessage = req.flash('successMessage') %}
   {% set smessage = req.flash('successMessage') %}
   {% if smessage.length %}
   {% if smessage.length %}
   <div class="alert alert-success">
   <div class="alert alert-success">

+ 16 - 2
lib/views/me/external-accounts.html

@@ -26,6 +26,7 @@
   {% if error.length %}
   {% if error.length %}
   {% for e in error %}
   {% for e in error %}
   <div class="alert alert-danger">
   <div class="alert alert-danger">
+    <b>Server Error occured:</b><br>
     {{ e }}
     {{ e }}
   </div>
   </div>
   {% endfor %}
   {% endfor %}
@@ -43,7 +44,7 @@
   {% set message = req.flash('successMessage') %}
   {% set message = req.flash('successMessage') %}
   {% if message.length %}
   {% if message.length %}
   <div class="alert alert-success">
   <div class="alert alert-success">
-    {{ message }}
+    <b>{{ message }}</b>
   </div>
   </div>
   {% endif %}
   {% endif %}
 
 
@@ -154,7 +155,7 @@
                     <div class="form-group">
                     <div class="form-group">
                       <div class="col-xs-12 text-right">
                       <div class="col-xs-12 text-right">
                         <button type="button" class="btn btn-default" onclick="testAssociateLdap()">{{ t('Test') }}</button>
                         <button type="button" class="btn btn-default" onclick="testAssociateLdap()">{{ t('Test') }}</button>
-                        <button type="button" class="btn btn-primary" onclick="associateLdap()">{{ t('Save') }}</button>
+                        <button type="button" class="btn btn-primary" onclick="associateLdap()">{{ t('Add') }}</button>
                       </div>
                       </div>
                     </div>
                     </div>
 
 
@@ -187,6 +188,19 @@
     </div><!-- /.modal-dialog -->
     </div><!-- /.modal-dialog -->
 
 
     <script>
     <script>
+      /**
+       * associate (submit the form)
+       */
+      function associateLdap() {
+        var $form = $('#formLdapAssociation');
+        var $action = '/me/external-accounts/associateLdap';
+        $form.attr('action', $action);
+        $form.submit();
+      }
+
+      /**
+       * test association (ajax)
+       */
       function testAssociateLdap() {
       function testAssociateLdap() {
         function showMessage(formId, msg, status) {
         function showMessage(formId, msg, status) {
           $('#' + formId + ' > .alert').remove();
           $('#' + formId + ' > .alert').remove();