[auth] moved logic for auths collections into AuthsCollection
This commit is contained in:
111
dist/swagger-ui.js
vendored
111
dist/swagger-ui.js
vendored
@@ -35,11 +35,9 @@ this["Handlebars"]["templates"]["auth_button"] = Handlebars.template({"1":functi
|
|||||||
},"useData":true});
|
},"useData":true});
|
||||||
this["Handlebars"]["templates"]["auth_view"] = Handlebars.template({"1":function(depth0,helpers,partials,data) {
|
this["Handlebars"]["templates"]["auth_view"] = Handlebars.template({"1":function(depth0,helpers,partials,data) {
|
||||||
return " <input type=\"button\" class=\"auth_logout__button\" value=\"logout\">\n";
|
return " <input type=\"button\" class=\"auth_logout__button\" value=\"logout\">\n";
|
||||||
},"3":function(depth0,helpers,partials,data) {
|
|
||||||
return " <div class=\"auth_submit\"><a class=\"auth_submit_button\" href=\"#\" data-sw-translate>apply</a></div>\n";
|
|
||||||
},"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
|
},"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
|
||||||
var stack1, buffer = "<div class=\"auth_container\">\n\n <div class=\"auth_inner\"></div>\n\n";
|
var stack1, buffer = "<div class=\"auth_container\">\n\n <div class=\"auth_inner\"></div>\n <div class=\"auth_submit\"><a class=\"auth_submit_button\" href=\"#\" data-sw-translate>apply</a></div>\n";
|
||||||
stack1 = helpers['if'].call(depth0, (depth0 != null ? depth0.isLogout : depth0), {"name":"if","hash":{},"fn":this.program(1, data),"inverse":this.program(3, data),"data":data});
|
stack1 = helpers['if'].call(depth0, (depth0 != null ? depth0.isLogout : depth0), {"name":"if","hash":{},"fn":this.program(1, data),"inverse":this.noop,"data":data});
|
||||||
if (stack1 != null) { buffer += stack1; }
|
if (stack1 != null) { buffer += stack1; }
|
||||||
return buffer + "</div>\n";
|
return buffer + "</div>\n";
|
||||||
},"useData":true});
|
},"useData":true});
|
||||||
@@ -457,15 +455,8 @@ this["Handlebars"]["templates"]["main"] = Handlebars.template({"1":function(dept
|
|||||||
if (stack1 != null) { buffer += stack1; }
|
if (stack1 != null) { buffer += stack1; }
|
||||||
return buffer + " </h4>\n </div>\n</div>\n";
|
return buffer + " </h4>\n </div>\n</div>\n";
|
||||||
},"useData":true});
|
},"useData":true});
|
||||||
//'use strict';
|
|
||||||
//
|
|
||||||
//SwaggerUi.Models.Oauth2Model = Backbone.Model.extend({
|
|
||||||
// validate: function () {
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
//});
|
|
||||||
this["Handlebars"]["templates"]["oauth2"] = Handlebars.template({"1":function(depth0,helpers,partials,data) {
|
this["Handlebars"]["templates"]["oauth2"] = Handlebars.template({"1":function(depth0,helpers,partials,data) {
|
||||||
var stack1, helper, functionType="function", helperMissing=helpers.helperMissing, escapeExpression=this.escapeExpression, buffer = " <li>\n <input class=\"oauth-scope\" type=\"checkbox\" scope=\""
|
var stack1, helper, functionType="function", helperMissing=helpers.helperMissing, escapeExpression=this.escapeExpression, buffer = " <li>\n <input class=\"oauth-scope\" type=\"checkbox\" data-scope=\""
|
||||||
+ escapeExpression(((helper = (helper = helpers.scope || (depth0 != null ? depth0.scope : depth0)) != null ? helper : helperMissing),(typeof helper === functionType ? helper.call(depth0, {"name":"scope","hash":{},"data":data}) : helper)))
|
+ escapeExpression(((helper = (helper = helpers.scope || (depth0 != null ? depth0.scope : depth0)) != null ? helper : helperMissing),(typeof helper === functionType ? helper.call(depth0, {"name":"scope","hash":{},"data":data}) : helper)))
|
||||||
+ "\" oauthtype=\""
|
+ "\" oauthtype=\""
|
||||||
+ escapeExpression(((helper = (helper = helpers.OAuthSchemeKey || (depth0 != null ? depth0.OAuthSchemeKey : depth0)) != null ? helper : helperMissing),(typeof helper === functionType ? helper.call(depth0, {"name":"OAuthSchemeKey","hash":{},"data":data}) : helper)))
|
+ escapeExpression(((helper = (helper = helpers.OAuthSchemeKey || (depth0 != null ? depth0.OAuthSchemeKey : depth0)) != null ? helper : helperMissing),(typeof helper === functionType ? helper.call(depth0, {"name":"OAuthSchemeKey","hash":{},"data":data}) : helper)))
|
||||||
@@ -25060,6 +25051,7 @@ window.SwaggerUi = Backbone.Router.extend({
|
|||||||
|
|
||||||
window.SwaggerUi.Views = {};
|
window.SwaggerUi.Views = {};
|
||||||
window.SwaggerUi.Models = {};
|
window.SwaggerUi.Models = {};
|
||||||
|
window.SwaggerUi.Collections = {};
|
||||||
window.SwaggerUi.partials = {};
|
window.SwaggerUi.partials = {};
|
||||||
window.SwaggerUi.utils = {};
|
window.SwaggerUi.utils = {};
|
||||||
|
|
||||||
@@ -25265,6 +25257,47 @@ SwaggerUi.Views.AuthButtonView = Backbone.View.extend({
|
|||||||
|
|
||||||
'use strict';
|
'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;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
'use strict';
|
||||||
|
|
||||||
SwaggerUi.Views.AuthView = Backbone.View.extend({
|
SwaggerUi.Views.AuthView = Backbone.View.extend({
|
||||||
events: {
|
events: {
|
||||||
'click .auth_submit_button': 'authorizeClick',
|
'click .auth_submit_button': 'authorizeClick',
|
||||||
@@ -25284,10 +25317,10 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
|||||||
opts.data = opts.data || {};
|
opts.data = opts.data || {};
|
||||||
this.router = this.options.router;
|
this.router = this.options.router;
|
||||||
|
|
||||||
this.collection = new Backbone.Collection();
|
this.collection = new SwaggerUi.Collections.AuthsCollection();
|
||||||
this.collection.add(this.parseData(opts.data));
|
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);
|
this.$innerEl = this.$(this.selectors.innerEl);
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -25304,7 +25337,7 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
|||||||
authorizeClick: function (e) {
|
authorizeClick: function (e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
if (this.isValidCollection()) {
|
if (this.collection.isValid()) {
|
||||||
this.authorize();
|
this.authorize();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -25349,10 +25382,6 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
|||||||
this.$innerEl.append(authEl);
|
this.$innerEl.append(authEl);
|
||||||
},
|
},
|
||||||
|
|
||||||
isValidCollection: function () {
|
|
||||||
return this.collection.length === this.collection.where({ valid: true }).length;
|
|
||||||
},
|
|
||||||
|
|
||||||
authorize: function () {
|
authorize: function () {
|
||||||
this.collection.forEach(function (auth) {
|
this.collection.forEach(function (auth) {
|
||||||
var keyAuth, basicAuth;
|
var keyAuth, basicAuth;
|
||||||
@@ -25369,16 +25398,14 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
|||||||
} else if (type === 'basic') {
|
} else if (type === 'basic') {
|
||||||
basicAuth = new SwaggerClient.PasswordAuthorization(auth.get('username'), auth.get('password'));
|
basicAuth = new SwaggerClient.PasswordAuthorization(auth.get('username'), auth.get('password'));
|
||||||
this.router.api.clientAuthorizations.add(auth.get('type'), basicAuth);
|
this.router.api.clientAuthorizations.add(auth.get('type'), basicAuth);
|
||||||
|
} else if (type === 'oauth2') {
|
||||||
|
//todo add handling login of oauth2
|
||||||
}
|
}
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
this.router.load();
|
this.router.load();
|
||||||
},
|
},
|
||||||
|
|
||||||
isAuthorizedCollection: function () {
|
|
||||||
return this.collection.length === this.collection.where({ isLogout: true }).length;
|
|
||||||
},
|
|
||||||
|
|
||||||
logoutClick: function (e) {
|
logoutClick: function (e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
@@ -25634,6 +25661,37 @@ SwaggerUi.Views.MainView = Backbone.View.extend({
|
|||||||
|
|
||||||
'use strict';
|
'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;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
'use strict';
|
||||||
|
|
||||||
SwaggerUi.Views.Oauth2View = Backbone.View.extend({
|
SwaggerUi.Views.Oauth2View = Backbone.View.extend({
|
||||||
events: {
|
events: {
|
||||||
'change .oauth-scope': 'scopeChange'
|
'change .oauth-scope': 'scopeChange'
|
||||||
@@ -25642,13 +25700,16 @@ SwaggerUi.Views.Oauth2View = Backbone.View.extend({
|
|||||||
template: Handlebars.templates.oauth2,
|
template: Handlebars.templates.oauth2,
|
||||||
|
|
||||||
render: function () {
|
render: function () {
|
||||||
$(this.el).html(this.template(this.model.toJSON()));
|
this.$el.html(this.template(this.model.toJSON()));
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
scopeChange: function () {
|
scopeChange: function (e) {
|
||||||
|
var val = $(e.target).prop('checked');
|
||||||
|
var scope = $(e.target).data('scope');
|
||||||
|
|
||||||
|
this.model.setScopes(scope, val);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|||||||
8
dist/swagger-ui.min.js
vendored
8
dist/swagger-ui.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -247,6 +247,7 @@ window.SwaggerUi = Backbone.Router.extend({
|
|||||||
|
|
||||||
window.SwaggerUi.Views = {};
|
window.SwaggerUi.Views = {};
|
||||||
window.SwaggerUi.Models = {};
|
window.SwaggerUi.Models = {};
|
||||||
|
window.SwaggerUi.Collections = {};
|
||||||
window.SwaggerUi.partials = {};
|
window.SwaggerUi.partials = {};
|
||||||
window.SwaggerUi.utils = {};
|
window.SwaggerUi.utils = {};
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
//'use strict';
|
|
||||||
//
|
|
||||||
//SwaggerUi.Models.Oauth2Model = Backbone.Model.extend({
|
|
||||||
// validate: function () {
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
//});
|
|
||||||
@@ -19,10 +19,10 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
|||||||
opts.data = opts.data || {};
|
opts.data = opts.data || {};
|
||||||
this.router = this.options.router;
|
this.router = this.options.router;
|
||||||
|
|
||||||
this.collection = new Backbone.Collection();
|
this.collection = new SwaggerUi.Collections.AuthsCollection();
|
||||||
this.collection.add(this.parseData(opts.data));
|
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);
|
this.$innerEl = this.$(this.selectors.innerEl);
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -39,7 +39,7 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
|||||||
authorizeClick: function (e) {
|
authorizeClick: function (e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
if (this.isValidCollection()) {
|
if (this.collection.isValid()) {
|
||||||
this.authorize();
|
this.authorize();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -84,10 +84,6 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
|||||||
this.$innerEl.append(authEl);
|
this.$innerEl.append(authEl);
|
||||||
},
|
},
|
||||||
|
|
||||||
isValidCollection: function () {
|
|
||||||
return this.collection.length === this.collection.where({ valid: true }).length;
|
|
||||||
},
|
|
||||||
|
|
||||||
authorize: function () {
|
authorize: function () {
|
||||||
this.collection.forEach(function (auth) {
|
this.collection.forEach(function (auth) {
|
||||||
var keyAuth, basicAuth;
|
var keyAuth, basicAuth;
|
||||||
@@ -104,16 +100,14 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
|||||||
} else if (type === 'basic') {
|
} else if (type === 'basic') {
|
||||||
basicAuth = new SwaggerClient.PasswordAuthorization(auth.get('username'), auth.get('password'));
|
basicAuth = new SwaggerClient.PasswordAuthorization(auth.get('username'), auth.get('password'));
|
||||||
this.router.api.clientAuthorizations.add(auth.get('type'), basicAuth);
|
this.router.api.clientAuthorizations.add(auth.get('type'), basicAuth);
|
||||||
|
} else if (type === 'oauth2') {
|
||||||
|
//todo add handling login of oauth2
|
||||||
}
|
}
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
this.router.load();
|
this.router.load();
|
||||||
},
|
},
|
||||||
|
|
||||||
isAuthorizedCollection: function () {
|
|
||||||
return this.collection.length === this.collection.where({ isLogout: true }).length;
|
|
||||||
},
|
|
||||||
|
|
||||||
logoutClick: function (e) {
|
logoutClick: function (e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
|
|||||||
41
src/main/javascript/view/AuthsCollection.js
Normal file
41
src/main/javascript/view/AuthsCollection.js
Normal 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;
|
||||||
|
}
|
||||||
|
});
|
||||||
31
src/main/javascript/view/Oauth2Model.js
Normal file
31
src/main/javascript/view/Oauth2Model.js
Normal 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;
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -8,12 +8,15 @@ SwaggerUi.Views.Oauth2View = Backbone.View.extend({
|
|||||||
template: Handlebars.templates.oauth2,
|
template: Handlebars.templates.oauth2,
|
||||||
|
|
||||||
render: function () {
|
render: function () {
|
||||||
$(this.el).html(this.template(this.model.toJSON()));
|
this.$el.html(this.template(this.model.toJSON()));
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
scopeChange: function () {
|
scopeChange: function (e) {
|
||||||
|
var val = $(e.target).prop('checked');
|
||||||
|
var scope = $(e.target).data('scope');
|
||||||
|
|
||||||
|
this.model.setScopes(scope, val);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -1,10 +1,8 @@
|
|||||||
<div class="auth_container">
|
<div class="auth_container">
|
||||||
|
|
||||||
<div class="auth_inner"></div>
|
<div class="auth_inner"></div>
|
||||||
|
<div class="auth_submit"><a class="auth_submit_button" href="#" data-sw-translate>apply</a></div>
|
||||||
{{#if isLogout}}
|
{{#if isLogout}}
|
||||||
<input type="button" class="auth_logout__button" value="logout">
|
<input type="button" class="auth_logout__button" value="logout">
|
||||||
{{else}}
|
|
||||||
<div class="auth_submit"><a class="auth_submit_button" href="#" data-sw-translate>apply</a></div>
|
|
||||||
{{/if}}
|
{{/if}}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
<ul class="api-popup-scopes">
|
<ul class="api-popup-scopes">
|
||||||
{{#each scopes}}
|
{{#each scopes}}
|
||||||
<li>
|
<li>
|
||||||
<input class="oauth-scope" type="checkbox" scope="{{scope}}" oauthtype="{{OAuthSchemeKey}}"/>
|
<input class="oauth-scope" type="checkbox" data-scope="{{scope}}" oauthtype="{{OAuthSchemeKey}}"/>
|
||||||
<label>{{scope}}</label><br/>
|
<label>{{scope}}</label><br/>
|
||||||
<span class="api-scope-desc">{{description}}
|
<span class="api-scope-desc">{{description}}
|
||||||
{{#if OAuthSchemeKey}}
|
{{#if OAuthSchemeKey}}
|
||||||
|
|||||||
Reference in New Issue
Block a user