user.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. var validator = require('utils/validator');
  20. App.User = DS.Model.extend({
  21. userName:DS.attr('string'),
  22. id: Em.computed.alias('userName'),
  23. userType: DS.attr('string'),
  24. auditItems:DS.hasMany('App.ServiceAudit'),
  25. admin: DS.attr('boolean'),
  26. operator: DS.attr('boolean'),
  27. clusterUser: DS.attr('boolean'),
  28. /**
  29. * List of permissions assigned to user
  30. * Available permissions:
  31. * AMBARI.ADMINISTRATOR
  32. * CLUSTER.USER
  33. * CLUSTER.ADMINISTRATOR
  34. * VIEW.USER
  35. * @property {Array} permissions
  36. **/
  37. permissions: DS.attr('array'),
  38. /**
  39. * @type {Boolean}
  40. */
  41. isLdap: Em.computed.equal('userType', 'LDAP')
  42. });
  43. App.CreateUserForm = App.Form.extend({
  44. className:App.User,
  45. object: Em.computed.alias('App.router.mainAdminUserCreateController.content'),
  46. fieldsOptions:[
  47. { name:"userName", displayName:"Username", toLowerCase: function(){var v = this.get('value'); this.set('value', v.toLowerCase())}.observes('value') },
  48. { name:"password", displayName:"Password", displayType:"password", isRequired: true },
  49. { name:"passwordRetype", displayName:"Retype Password", displayType:"password", validator:"passwordRetype", isRequired: true },
  50. { name:"admin", displayName:"Admin", displayType:"checkbox", isRequired:false, defaultValue: true}
  51. ],
  52. fields:[],
  53. isValid:function () {
  54. var isValid = this._super();
  55. var passField = this.get('field.password');
  56. var passRetype = this.get('field.passwordRetype');
  57. if (!validator.empty(passField.get('value'))) {
  58. if (passField.get('value') != passRetype.get('value')) {
  59. passRetype.set('errorMessage', this.t('admin.users.createError.passwordValidation'));
  60. isValid = false;
  61. }
  62. }
  63. if (isValid) {
  64. var users = App.User.find();
  65. var userNameField = this.getField('userName');
  66. var userName = userNameField.get('value');
  67. if (users.mapProperty('userName').contains(userName)) {
  68. userNameField.set('errorMessage', this.t('admin.users.createError.userNameExists'));
  69. return isValid = false;
  70. }
  71. }
  72. return isValid;
  73. },
  74. isWarn: function() {
  75. var isWarn = false;
  76. var userNameField = this.getField('userName');
  77. userNameField.set('warnMessage', '');
  78. var userName = userNameField.get('value');
  79. if (this.isValid() && !validator.isValidUserName(userName)) {
  80. userNameField.set('warnMessage', this.t('users.userName.validationFail'));
  81. isWarn = true;
  82. }
  83. return isWarn;
  84. },
  85. save: function () {
  86. var object = this.get('object');
  87. var formValues = {};
  88. $.each(this.get('fields'), function () {
  89. formValues[Ember.String.decamelize(this.get('name'))] = this.get('value');
  90. });
  91. if (this.get('className')) {
  92. App.store.load(this.get('className'), App.dateTime(), formValues);
  93. }
  94. this.set('result', 1);
  95. return true;
  96. }
  97. });
  98. App.User.FIXTURES = [];