[auth] moved logic for auths collections into AuthsCollection

This commit is contained in:
bodnia
2016-03-09 03:18:26 +02:00
parent e7d5ad6d2e
commit c3d4bcca3d
10 changed files with 175 additions and 53 deletions

View File

@@ -247,6 +247,7 @@ window.SwaggerUi = Backbone.Router.extend({
window.SwaggerUi.Views = {};
window.SwaggerUi.Models = {};
window.SwaggerUi.Collections = {};
window.SwaggerUi.partials = {};
window.SwaggerUi.utils = {};

View File

@@ -1,7 +0,0 @@
//'use strict';
//
//SwaggerUi.Models.Oauth2Model = Backbone.Model.extend({
// validate: function () {
//
// }
//});

View File

@@ -19,10 +19,10 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
opts.data = opts.data || {};
this.router = this.options.router;
this.collection = new Backbone.Collection();
this.collection = new SwaggerUi.Collections.AuthsCollection();
this.collection.add(this.parseData(opts.data));
this.$el.html(this.tpls.main({isLogout: this.isAuthorizedCollection()}));
this.$el.html(this.tpls.main({isLogout: this.collection.isAuthorized()}));
this.$innerEl = this.$(this.selectors.innerEl);
},
@@ -39,7 +39,7 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
authorizeClick: function (e) {
e.preventDefault();
if (this.isValidCollection()) {
if (this.collection.isValid()) {
this.authorize();
}
},
@@ -84,10 +84,6 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
this.$innerEl.append(authEl);
},
isValidCollection: function () {
return this.collection.length === this.collection.where({ valid: true }).length;
},
authorize: function () {
this.collection.forEach(function (auth) {
var keyAuth, basicAuth;
@@ -104,16 +100,14 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
} else if (type === 'basic') {
basicAuth = new SwaggerClient.PasswordAuthorization(auth.get('username'), auth.get('password'));
this.router.api.clientAuthorizations.add(auth.get('type'), basicAuth);
} else if (type === 'oauth2') {
//todo add handling login of oauth2
}
}, this);
this.router.load();
},
isAuthorizedCollection: function () {
return this.collection.length === this.collection.where({ isLogout: true }).length;
},
logoutClick: function (e) {
e.preventDefault();

View File

@@ -0,0 +1,41 @@
'use strict';
SwaggerUi.Collections.AuthsCollection = Backbone.Collection.extend({
add: function (model) {
var args = Array.prototype.slice.call(arguments);
if (Array.isArray(model)) {
args[0] = _.map(model, function(val) {
return this.handleOne(val);
}, this);
} else {
args[0] = this.handleOne(model);
}
Backbone.Collection.prototype.add.apply(this, args);
},
handleOne: function (model) {
var result = model;
if (! (model instanceof Backbone.Model) ) {
switch (model.type) {
case 'oauth2':
result = new SwaggerUi.Models.Oauth2Model(model);
break;
default:
result = new Backbone.Model(model);
}
}
return result;
},
isValid: function () {
return this.length === this.where({ valid: true }).length;
},
isAuthorized: function () {
return this.length === this.where({ isLogout: true }).length;
}
});

View File

@@ -0,0 +1,31 @@
'use strict';
SwaggerUi.Models.Oauth2Model = Backbone.Model.extend({
defaults: {
scopes: {}
},
initialize: function () {
this.on('change', this.validate);
},
setScopes: function (name, val) {
var auth = _.extend({}, this.attributes);
var index = _.findIndex(auth.scopes, function(o) {
return o.scope === name;
});
auth.scopes[index].checked = val;
this.set(auth);
},
validate: function () {
var valid = _.findIndex(this.get('scopes'), function (o) {
return o.checked === true;
}) > -1;
this.set('valid', valid);
return valid;
}
});

View File

@@ -8,12 +8,15 @@ SwaggerUi.Views.Oauth2View = Backbone.View.extend({
template: Handlebars.templates.oauth2,
render: function () {
$(this.el).html(this.template(this.model.toJSON()));
this.$el.html(this.template(this.model.toJSON()));
return this;
},
scopeChange: function () {
scopeChange: function (e) {
var val = $(e.target).prop('checked');
var scope = $(e.target).data('scope');
this.model.setScopes(scope, val);
}
});