PageRenderTime 62ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/webapp/bower_components/swagger-ui/src/main/javascript/view/AuthsCollection.js

https://gitlab.com/atomfrede/jhipster-ci-example-gradle
JavaScript | 91 lines | 72 code | 19 blank | 0 comment | 10 complexity | 7db4d30de465b496fa02a07e35a9431c MD5 | raw file
  1. 'use strict';
  2. SwaggerUi.Collections.AuthsCollection = Backbone.Collection.extend({
  3. constructor: function() {
  4. var args = Array.prototype.slice.call(arguments);
  5. args[0] = this.parse(args[0]);
  6. Backbone.Collection.apply(this, args);
  7. },
  8. add: function (model) {
  9. var args = Array.prototype.slice.call(arguments);
  10. if (Array.isArray(model)) {
  11. args[0] = _.map(model, function(val) {
  12. return this.handleOne(val);
  13. }, this);
  14. } else {
  15. args[0] = this.handleOne(model);
  16. }
  17. Backbone.Collection.prototype.add.apply(this, args);
  18. },
  19. handleOne: function (model) {
  20. var result = model;
  21. if (! (model instanceof Backbone.Model) ) {
  22. switch (model.type) {
  23. case 'oauth2':
  24. result = new SwaggerUi.Models.Oauth2Model(model);
  25. break;
  26. case 'basic':
  27. result = new SwaggerUi.Models.BasicAuthModel(model);
  28. break;
  29. case 'apiKey':
  30. result = new SwaggerUi.Models.ApiKeyAuthModel(model);
  31. break;
  32. default:
  33. result = new Backbone.Model(model);
  34. }
  35. }
  36. return result;
  37. },
  38. isValid: function () {
  39. var valid = true;
  40. this.models.forEach(function(model) {
  41. if (!model.validate()) {
  42. valid = false;
  43. }
  44. });
  45. return valid;
  46. },
  47. isAuthorized: function () {
  48. return this.length === this.where({ isLogout: true }).length;
  49. },
  50. isPartiallyAuthorized: function () {
  51. return this.where({ isLogout: true }).length > 0;
  52. },
  53. parse: function (data) {
  54. var authz = Object.assign({}, window.swaggerUi.api.clientAuthorizations.authz);
  55. return _.map(data, function (auth, name) {
  56. var isBasic = authz[name] && auth.type === 'basic' && authz[name].username && authz[name].password;
  57. _.extend(auth, {
  58. title: name
  59. });
  60. if (authz[name] || isBasic) {
  61. _.extend(auth, {
  62. isLogout: true,
  63. value: isBasic ? undefined : authz[name].value,
  64. username: isBasic ? authz[name].username : undefined,
  65. password: isBasic ? authz[name].password : undefined,
  66. valid: true
  67. });
  68. }
  69. return auth;
  70. });
  71. }
  72. });