couchdb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From robertkowalski <...@git.apache.org>
Subject [GitHub] couchdb-fauxton pull request: CORS configuration
Date Tue, 27 Jan 2015 12:40:56 GMT
Github user robertkowalski commented on a diff in the pull request:

    https://github.com/apache/couchdb-fauxton/pull/250#discussion_r23604702
  
    --- Diff: app/addons/cors/views.js ---
    @@ -0,0 +1,359 @@
    +// Licensed under the Apache License, Version 2.0 (the "License"); you may not
    +// use this file except in compliance with the License. You may obtain a copy of
    +// the License at
    +//
    +//   http://www.apache.org/licenses/LICENSE-2.0
    +//
    +// Unless required by applicable law or agreed to in writing, software
    +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    +// License for the specific language governing permissions and limitations under
    +// the License.
    +
    +define([
    +  "app",
    +  "api",
    +  "addons/cors/resources",
    +  "addons/cors/components.react",
    +  "addons/cors/actions"
    +],
    +
    +
    +function (app, FauxtonAPI, CORS, Components, Actions) {
    +  var Views= {};
    +
    +  Views.CORSWrapper = FauxtonAPI.View.extend({
    +    className: 'list',
    +    initialize: function (options) {
    +      this.options = options;
    +    },
    +
    +    afterRender: function () {
    +      Actions.editCors({
    +        cors: this.options.cors,
    +        httpd: this.options.httpd
    +      });
    +      Components.renderCORS(this.el);
    +    },
    +
    +    cleanup: function () {
    +      Components.removeCORS(this.el);
    +    }
    +
    +  });
    +
    +  Views.CORSMain = FauxtonAPI.View.extend({
    +    className: 'cors-page',
    +    template: 'addons/cors/templates/cors',
    +    events: {
    +      'submit form#corsForm': 'submit',
    +      'click .js-enable-cors': 'corsClick',
    +      'click .js-restrict-origin-domains': 'restrictOrigins',
    +      'click .js-all-origin-domains': 'allOrigins'
    +    },
    +
    +    initialize: function () {
    +      this.originDomainTable = this.setView('.origin-domains', new Views.OriginDomainTable({
    +        model: this.model
    +      }));
    +    },
    +
    +    serialize: function () {
    +      return {
    +        enableCors: this.model.get('credentials')
    +      };
    +    },
    +
    +    establish: function(){
    +      return [this.model.fetch()];
    +    },
    +
    +    afterRender: function () {
    +      var corsEnabled = this.$('.js-enable-cors').is(':checked');
    +      this.$('#collapsing-container').toggle(corsEnabled);
    +      this.setupOrigins();
    +    },
    +
    +    corsClick: function (e) {
    +      var isChecked = this.$(e.target).prop('checked');
    +      this.$('#collapsing-container').toggle(isChecked);
    +      this.setupOrigins();
    +    },
    +
    +
    +    setupOrigins: function() {
    +      var storedOrigins = this.model.get('origins');
    +      if (storedOrigins && storedOrigins != '*') {
    +        this.restrictOrigins();
    +      } else {
    +        this.allOrigins();
    +      }
    +    },
    +
    +    allOrigins: function() {
    +      this.$('.js-all-origin-domains').prop('checked', true);
    +      this.$('.js-restrict-origin-domains').prop('checked', false);
    +      this.$('#origin-domains-container').hide();
    +    },
    +
    +    restrictOrigins: function() {
    +      this.$('.js-restrict-origin-domains').prop('checked', true);
    +      this.$('.js-all-origin-domains').prop('checked', false);
    +      this.$('#origin-domains-container').show();
    +    },
    +
    +    formToJSON: function(formSelector){
    +      var formObject = $(formSelector).serializeArray(),
    +        formJSON={};
    +      _.map(formObject, function(field){
    +        formJSON[field.name]=field.value;
    +      });
    +      return formJSON;
    +    },
    +
    +    submit: function(e){
    +      e.preventDefault();
    +      var data = this.formToJSON(e.currentTarget);
    +
    +      if (data.enable_cors === 'on') {
    +
    +        // CORS checked, save data
    +        if (data.restrict_origin_domains === 'on') {
    +          var storedOrigins = this.model.get('origins').split(',');
    +          var newDomain = $.trim(data.new_origin_domain);
    +
    +          // if a new domain has been entered, check it's valid
    +          if (!_.isEmpty(newDomain) && !CORS.validateCORSDomain(newDomain)) {
    +            FauxtonAPI.addNotification({
    +              msg: 'Please enter a valid domain, starting with http/https and only containing
the domain (not a subfolder).',
    +              type: 'error',
    +              clear: true
    +            });
    +            return;
    +          }
    +
    +          // check that the user has entered at least one new origin domain
    +          if (storedOrigins && storedOrigins.length > 0 && storedOrigins
!== '*') {
    +            this.originData = storedOrigins.concat(newDomain).toString();
    +          } else {
    +            if (_.isEmpty(newDomain)) {
    +              FauxtonAPI.addNotification({
    +                msg: 'Please enter a new origin domain.',
    +                type: 'error',
    +                clear: true
    +              });
    +              this.$('.new-origin-domain').focus();
    +              return;
    +            }
    +            this.originData = data.new_origin_domain;
    +          }
    +
    +        } else {
    +          this.originData = "*";
    +        }
    +
    +
    +        var enableOption = new CORS.ConfigModel({
    +          section: 'httpd',
    +          attribute: 'enable_cors',
    +          value: 'true'
    +        });
    +
    +        var enableCreds = new CORS.ConfigModel({
    +          section: 'cors',
    +          attribute: 'credentials',
    +          value: 'true'
    +        });
    +
    +        var allowOrigins = new CORS.ConfigModel({
    +          section: 'cors',
    +          attribute: 'origins',
    +          value: this.originData
    +        });
    +
    +        enableOption.save().then(function (response) {
    +          var notification = FauxtonAPI.addNotification({
    +            msg: 'Your settings have been saved.',
    +            type: 'success',
    +            clear: true
    +          });
    +        },
    +        function (response, errorCode, errorMsg) {
    +          var notification = FauxtonAPI.addNotification({
    +            msg: 'Sorry! There was an error. Code ' + errorCode  + '.',
    +            type: 'error',
    +            clear: true
    +          });
    +        });
    +
    +        enableCreds.save();
    +        allowOrigins.save();
    +        this.$('.new-origin-domain').val('');
    +
    +      } else {
    +
    +        // Disable CORS
    +        var disableOption = new CORS.ConfigModel({
    +          section: 'httpd',
    +          attribute: 'enable_cors',
    +          value: 'false'
    +        });
    +
    +        var disableCreds = new CORS.ConfigModel({
    +          section: 'cors',
    +          attribute: 'credentials',
    +          value: 'false'
    +        });
    +
    +        var disableOrigins = new CORS.ConfigModel({
    +          section: 'cors',
    +          attribute: 'origins',
    +          value: ''
    +        });
    +
    +        disableOption.save().then(function (response) {
    +          var notification = FauxtonAPI.addNotification({
    +            msg: 'Your settings have been saved.',
    +            type: 'success',
    +            clear: true
    +          });
    +        },
    +        function (response, errorCode, errorMsg) {
    +          var notification = FauxtonAPI.addNotification({
    +            msg: 'Sorry! There was an error. Code ' + errorCode  + '.',
    +            type: 'error',
    +            clear: true
    +          });
    +        });
    +
    +        disableCreds.save();
    +        disableOrigins.save();
    +      }
    +    }
    +  });
    +
    +  Views.OriginDomainTable = FauxtonAPI.View.extend({
    --- End diff --
    
    i think we can remove that object


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message