user.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.UserModel = Em.Object.extend({
  21. userName:null,
  22. id:0
  23. });
  24. App.User = DS.Model.extend({
  25. userName:DS.attr('string'),
  26. roles:DS.attr('string'),
  27. isLdap:DS.attr('boolean'),
  28. type: function(){
  29. if(this.get('isLdap')){
  30. return 'LDAP';
  31. }
  32. return 'Local';
  33. }.property('isLdap'),
  34. auditItems:DS.hasMany('App.ServiceAudit'),
  35. admin: DS.attr('boolean')
  36. });
  37. App.EditUserForm = App.Form.extend({
  38. className:App.User,
  39. object:function () {
  40. return App.router.get('mainAdminUserEditController.content');
  41. }.property('App.router.mainAdminUserEditController.content'),
  42. fieldsOptions:[
  43. { name:"userName", displayName:"Username" },
  44. { name:"old_password", displayName:"Old Password", displayType:"password", isRequired: function(){ return this.get('form.isObjectNew'); }.property('form.isObjectNew') },
  45. { name:"new_password", displayName:"New Password", displayType:"password", isRequired: false },
  46. { name:"admin", displayName:"Admin", displayType:"checkbox", isRequired:false },
  47. { name:"roles", displayName:"Role", isRequired:false, isHidden:true },
  48. { name:"isLdap", displayName:"Type", isRequired:false, isHidden:true }
  49. ],
  50. fields:[],
  51. disableUsername:function () {
  52. var field = this.getField("userName");
  53. if (field) field.set("disabled", this.get('isObjectNew') ? false : "disabled");
  54. }.observes('isObjectNew'),
  55. disableAdminCheckbox:function () {
  56. if (!this.get('isObjectNew')) {
  57. var object = this.get('object');
  58. var field = this.getField("admin");
  59. if (field) {
  60. field.set("disabled", (object.get('userName') == App.get('router').getLoginName()) ? "disabled" : false);
  61. }
  62. }
  63. }.observes('isObjectNew'),
  64. getValues:function () {
  65. var values = this._super();
  66. values.type = ['local'];
  67. return values;
  68. },
  69. isValid:function () {
  70. var isValid = this._super();
  71. thisForm = this;
  72. var passField = this.get('field.password');
  73. var passRetype = this.get('field.passwordRetype');
  74. if (!validator.empty(passField.get('value'))) {
  75. if (passField.get('value') != passRetype.get('value')) {
  76. passRetype.set('errorMessage', "Passwords are different");
  77. isValid = false;
  78. }
  79. }
  80. if (isValid && this.get('isObjectNew')) {
  81. var users = App.User.find();
  82. var userNameField = this.getField('userName');
  83. var userName = userNameField.get('value');
  84. users.forEach(function (user) {
  85. if (userName == user.get('userName')) {
  86. userNameField.set('errorMessage', 'User with the same name is already exists');
  87. return isValid = false;
  88. }
  89. });
  90. }
  91. return isValid;
  92. }
  93. });
  94. App.CreateUserForm = App.Form.extend({
  95. className:App.User,
  96. object:function () {
  97. return App.router.get('mainAdminUserCreateController.content');
  98. }.property('App.router.mainAdminUserCreateController.content'),
  99. fieldsOptions:[
  100. { name:"userName", displayName:"Username" },
  101. { name:"password", displayName:"Password", displayType:"password", isRequired: function(){ return this.get('form.isObjectNew'); }.property('form.isObjectNew') },
  102. { name:"passwordRetype", displayName:"Retype Password", displayType:"password", validator:"passwordRetype", isRequired: false },
  103. { name:"admin", displayName:"Admin", displayType:"checkbox", isRequired:false },
  104. { name:"roles", displayName:"Role", isRequired:false, isHidden:true }
  105. ],
  106. fields:[],
  107. disableUsername:function () {
  108. var field = this.getField("userName");
  109. if (field) field.set("disabled", this.get('isObjectNew') ? false : "disabled");
  110. }.observes('isObjectNew'),
  111. disableAdminCheckbox:function () {
  112. if (!this.get('isObjectNew')) {
  113. var object = this.get('object');
  114. var field = this.getField("admin");
  115. if (field) {
  116. field.set("disabled", (object.get('userName') == App.get('router').getLoginName()) ? "disabled" : false);
  117. }
  118. }
  119. }.observes('isObjectNew'),
  120. getValues:function () {
  121. var values = this._super();
  122. values.type = ['local'];
  123. values.id = values.userName;
  124. return values;
  125. },
  126. isValid:function () {
  127. var isValid = this._super();
  128. thisForm = this;
  129. var passField = this.get('field.password');
  130. var passRetype = this.get('field.passwordRetype');
  131. if (!validator.empty(passField.get('value'))) {
  132. if (passField.get('value') != passRetype.get('value')) {
  133. passRetype.set('errorMessage', "Passwords are different");
  134. isValid = false;
  135. }
  136. }
  137. if (isValid && this.get('isObjectNew')) {
  138. var users = App.User.find();
  139. var userNameField = this.getField('userName');
  140. var userName = userNameField.get('value');
  141. users.forEach(function (user) {
  142. if (userName == user.get('userName')) {
  143. userNameField.set('errorMessage', 'User with the same name is already exists');
  144. return isValid = false;
  145. }
  146. });
  147. }
  148. return isValid;
  149. }
  150. });
  151. App.User.FIXTURES = [];