user.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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:"admin", displayName:"Admin", displayType:"checkbox", isRequired:false },
  46. { name:"roles", displayName:"Role", isRequired:false, isHidden:true },
  47. { name:"isLdap", displayName:"Type", isRequired:false, isHidden:true }
  48. ],
  49. fields:[],
  50. disableUsername:function () {
  51. this.getField("userName").set("disabled", "disabled");
  52. }.observes('object'),
  53. disableAdminCheckbox:function () {
  54. var object = this.get('object');
  55. if (object) {
  56. if ((object.get('userName') == App.get('router').getLoginName()) || App.supports.ldapGroupMapping && object.get("isLdap")) {
  57. this.getField("admin").set("disabled", true);
  58. } else {
  59. this.getField("admin").set("disabled", false);
  60. }
  61. }
  62. }.observes('object'),
  63. isValid:function () {
  64. var isValid = this._super();
  65. thisForm = this;
  66. var newPass = this.get('field.new_password');
  67. var oldPass = this.get('field.old_password');
  68. if (!validator.empty(newPass.get('value')) && validator.empty(oldPass.get('value'))) {
  69. oldPass.set('errorMessage', this.t('admin.users.editError.requiredField'));
  70. isValid = false;
  71. }
  72. return isValid;
  73. },
  74. save: function () {
  75. var object = this.get('object');
  76. var formValues = {};
  77. $.each(this.get('fields'), function () {
  78. formValues[this.get('name')] = this.get('value');
  79. });
  80. $.each(formValues, function (k, v) {
  81. object.set(k, v);
  82. });
  83. //App.store.commit();
  84. this.set('result', 1);
  85. return true;
  86. }
  87. });
  88. App.CreateUserForm = App.Form.extend({
  89. className:App.User,
  90. object:function () {
  91. return App.router.get('mainAdminUserCreateController.content');
  92. }.property('App.router.mainAdminUserCreateController.content'),
  93. fieldsOptions:[
  94. { name:"userName", displayName:"Username", toLowerCase: function(){var v = this.get('value'); this.set('value', v.toLowerCase())}.observes('value') },
  95. { name:"password", displayName:"Password", displayType:"password", isRequired: true },
  96. { name:"passwordRetype", displayName:"Retype Password", displayType:"password", validator:"passwordRetype", isRequired: true },
  97. { name:"admin", displayName:"Admin", displayType:"checkbox", isRequired:false },
  98. { name:"roles", displayName:"Role", isRequired:false, isHidden:true }
  99. ],
  100. fields:[],
  101. isValid:function () {
  102. var isValid = this._super();
  103. var passField = this.get('field.password');
  104. var passRetype = this.get('field.passwordRetype');
  105. if (!validator.empty(passField.get('value'))) {
  106. if (passField.get('value') != passRetype.get('value')) {
  107. passRetype.set('errorMessage', this.t('admin.users.createError.passwordValidation'));
  108. isValid = false;
  109. }
  110. }
  111. if (isValid) {
  112. var users = App.User.find();
  113. var userNameField = this.getField('userName');
  114. var userName = userNameField.get('value');
  115. if (!validator.isValidUserName(userName)) {
  116. userNameField.set('errorMessage', this.t('users.userName.validationFail'));
  117. isValid = false;
  118. }
  119. if (users.mapProperty('userName').contains(userName)) {
  120. userNameField.set('errorMessage', this.t('admin.users.createError.userNameExists'));
  121. return isValid = false;
  122. }
  123. }
  124. return isValid;
  125. },
  126. save: function () {
  127. var object = this.get('object');
  128. var formValues = {};
  129. $.each(this.get('fields'), function () {
  130. formValues[Ember.String.decamelize(this.get('name'))] = this.get('value');
  131. });
  132. if (this.get('className')) {
  133. App.store.load(this.get('className'), formValues.user_name, formValues);
  134. }
  135. else {
  136. console.log("Please define class name for your form " + this.constructor);
  137. }
  138. this.set('result', 1);
  139. return true;
  140. }
  141. });
  142. App.User.FIXTURES = [];