user.js 5.8 KB

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