edit.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. App.MainAdminUserEditView = Em.View.extend({
  20. templateName: require('templates/main/admin/user/edit'),
  21. /**
  22. * @type {bool}
  23. */
  24. userId: false,
  25. /**
  26. * Form to edit existing user
  27. * @type {App.EditUserForm}
  28. */
  29. userForm: App.EditUserForm.create({}),
  30. /**
  31. * Edit existing user
  32. * @method edit
  33. * @return {Boolean}
  34. */
  35. edit: function () {
  36. var form = this.get("userForm");
  37. if (!form.isValid()) return false;
  38. var Users = {
  39. roles: this.identifyRoles(form)
  40. };
  41. this.setPassword(Users, form);
  42. return !!App.ajax.send({
  43. name: 'admin.user.edit',
  44. sender: this,
  45. data: {
  46. form: form,
  47. user: form.getField("userName").get('value'),
  48. data: JSON.stringify({
  49. Users: Users
  50. })
  51. },
  52. success: 'editUserSuccessCallback',
  53. error: 'editUserErrorCallback'
  54. });
  55. },
  56. /**
  57. * set password to query data if it's not empty string
  58. * @param Users
  59. * @param form
  60. * @return {Boolean}
  61. */
  62. setPassword: function (Users, form) {
  63. if (form.getField("new_password").get('value') != "" && form.getField("old_password").get('value') != "") {
  64. Users.password = form.getField("new_password").get('value');
  65. Users.old_password = form.getField("old_password").get('value');
  66. return true;
  67. }
  68. return false;
  69. },
  70. /**
  71. * identify roles of user by admin checkbox
  72. * @param form
  73. * @return {String}
  74. */
  75. identifyRoles: function (form) {
  76. var roles = (form.getField("admin").get('value') === true) ? 'admin,user' : 'user';
  77. form.getField("roles").set("value", roles);
  78. return roles;
  79. },
  80. /**
  81. * Success callback for edit user request
  82. * @param {object} data
  83. * @param {object} opt
  84. * @param {object} params
  85. * @method editUserSuccessCallback
  86. */
  87. editUserSuccessCallback: function (data, opt, params) {
  88. params.form.save();
  89. App.router.transitionTo("allUsers");
  90. },
  91. /**
  92. * Error callback for edit user request
  93. * @param {object} request
  94. * @method editUserErrorCallback
  95. */
  96. editUserErrorCallback: function (request) {
  97. App.ModalPopup.show({
  98. header: Em.I18n.t('admin.users.editButton'),
  99. body: this.parseErrorMessage(request),
  100. secondary: null
  101. });
  102. },
  103. /**
  104. * derive only valuable info from response,
  105. * return content after last ":"
  106. * @param request
  107. * @return {String}
  108. */
  109. parseErrorMessage: function (request) {
  110. var message = JSON.parse(request.responseText).message;
  111. return message.substr(message.lastIndexOf(':') + 1);
  112. },
  113. /**
  114. * Submit form by Enter-click
  115. * @param {object} event
  116. * @returns {bool}
  117. * @method keyPress
  118. */
  119. keyPress: function (event) {
  120. if (event.keyCode === 13) {
  121. this.edit();
  122. return false;
  123. }
  124. return true;
  125. },
  126. didInsertElement: function () {
  127. var form = this.get('userForm');
  128. var isLdapValue = form.getField("isLdap").get("value");
  129. form.getField("old_password").set("disabled", isLdapValue);
  130. form.getField("new_password").set("disabled", isLdapValue);
  131. form.getField("new_passwordRetype").set("disabled", isLdapValue);
  132. form.propertyDidChange('object');
  133. }
  134. });