user.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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:function(){
  23. return this.get('userName');
  24. }.property('userName'),
  25. isLdap:DS.attr('boolean'),
  26. type: function(){
  27. if(this.get('isLdap')){
  28. return 'LDAP';
  29. }
  30. return 'Local';
  31. }.property('isLdap'),
  32. auditItems:DS.hasMany('App.ServiceAudit'),
  33. admin: DS.attr('boolean'),
  34. operator: DS.attr('boolean'),
  35. /**
  36. * List of permissions assigned to user
  37. * Available permissions:
  38. * AMBARI.ADMIN
  39. * CLUSTER.READ
  40. * CLUSTER.OPERATE
  41. * VIEW.USE
  42. * @property {Array} permissions
  43. **/
  44. permissions: DS.attr('array')
  45. });
  46. App.EditUserForm = App.Form.extend({
  47. className:App.User,
  48. object:function () {
  49. return App.router.get('mainAdminUserEditController.content');
  50. }.property('App.router.mainAdminUserEditController.content'),
  51. fieldsOptions:[
  52. { name:"userName", displayName:"Username" },
  53. { name:"old_password", displayName:"Current Password", displayType:"password", isRequired: false },
  54. { name:"new_password", displayName:"New Password", displayType:"password", isRequired: false },
  55. { name:"new_passwordRetype", displayName:"Retype New Password", displayType:"password", isRequired: false },
  56. { name:"admin", displayName:"Admin", displayType:"checkbox", isRequired:false },
  57. { name:"isLdap", displayName:"Type", isRequired:false, isHidden:true }
  58. ],
  59. fields:[],
  60. disableUsername:function () {
  61. this.getField("userName").set("disabled", "disabled");
  62. }.observes('object'),
  63. disableAdminCheckbox:function () {
  64. var object = this.get('object');
  65. if (object) {
  66. if (object.get('userName') == App.get('router').getLoginName()) {
  67. this.getField("admin").set("disabled", true);
  68. } else {
  69. this.getField("admin").set("disabled", false);
  70. }
  71. }
  72. }.observes('object'),
  73. isValid:function () {
  74. var isValid = this._super();
  75. var thisForm = this;
  76. var newPass = this.get('field.new_password');
  77. var oldPass = this.get('field.old_password');
  78. var passRetype = this.get('field.new_passwordRetype');
  79. if (!validator.empty(newPass.get('value'))) {
  80. if(validator.empty(oldPass.get('value'))){
  81. oldPass.set('errorMessage', this.t('admin.users.editError.requiredField'));
  82. isValid = false;
  83. }
  84. if (newPass.get('value') != passRetype.get('value')) {
  85. passRetype.set('errorMessage', this.t('admin.users.createError.passwordValidation'));
  86. isValid = false;
  87. }
  88. }
  89. return isValid;
  90. },
  91. save: function () {
  92. var object = this.get('object');
  93. var formValues = {};
  94. $.each(this.get('fields'), function () {
  95. formValues[this.get('name')] = this.get('value');
  96. });
  97. $.each(formValues, function (k, v) {
  98. object.set(k, v);
  99. });
  100. //App.store.commit();
  101. this.set('result', 1);
  102. return true;
  103. }
  104. });
  105. App.CreateUserForm = App.Form.extend({
  106. className:App.User,
  107. object:function () {
  108. return App.router.get('mainAdminUserCreateController.content');
  109. }.property('App.router.mainAdminUserCreateController.content'),
  110. fieldsOptions:[
  111. { name:"userName", displayName:"Username", toLowerCase: function(){var v = this.get('value'); this.set('value', v.toLowerCase())}.observes('value') },
  112. { name:"password", displayName:"Password", displayType:"password", isRequired: true },
  113. { name:"passwordRetype", displayName:"Retype Password", displayType:"password", validator:"passwordRetype", isRequired: true },
  114. { name:"admin", displayName:"Admin", displayType:"checkbox", isRequired:false, defaultValue: true}
  115. ],
  116. fields:[],
  117. isValid:function () {
  118. var isValid = this._super();
  119. var passField = this.get('field.password');
  120. var passRetype = this.get('field.passwordRetype');
  121. if (!validator.empty(passField.get('value'))) {
  122. if (passField.get('value') != passRetype.get('value')) {
  123. passRetype.set('errorMessage', this.t('admin.users.createError.passwordValidation'));
  124. isValid = false;
  125. }
  126. }
  127. if (isValid) {
  128. var users = App.User.find();
  129. var userNameField = this.getField('userName');
  130. var userName = userNameField.get('value');
  131. if (users.mapProperty('userName').contains(userName)) {
  132. userNameField.set('errorMessage', this.t('admin.users.createError.userNameExists'));
  133. return isValid = false;
  134. }
  135. }
  136. return isValid;
  137. },
  138. isWarn: function() {
  139. var isWarn = false;
  140. var userNameField = this.getField('userName');
  141. userNameField.set('warnMessage', '');
  142. var userName = userNameField.get('value');
  143. if (this.isValid() && !validator.isValidUserName(userName)) {
  144. userNameField.set('warnMessage', this.t('users.userName.validationFail'));
  145. isWarn = true;
  146. }
  147. return isWarn;
  148. },
  149. save: function () {
  150. var object = this.get('object');
  151. var formValues = {};
  152. $.each(this.get('fields'), function () {
  153. formValues[Ember.String.decamelize(this.get('name'))] = this.get('value');
  154. });
  155. if (this.get('className')) {
  156. App.store.load(this.get('className'), App.dateTime(), formValues);
  157. }
  158. else {
  159. console.log("Please define class name for your form " + this.constructor);
  160. }
  161. this.set('result', 1);
  162. return true;
  163. }
  164. });
  165. App.User.FIXTURES = [];