You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

264 regels
7.4 KiB

  1. wn.pages['user-properties'].onload = function(wrapper) {
  2. wn.ui.make_app_page({
  3. parent: wrapper,
  4. title: 'User Permission Restrictions',
  5. single_column: true
  6. });
  7. $(wrapper).find(".layout-main").html("<div class='user-settings' style='min-height: 200px;'></div>\
  8. <table class='table table-bordered' style='background-color: #f9f9f9;'>\
  9. <tr><td>\
  10. <h4><i class='icon-question-sign'></i> "+wn._("Quick Help for Permission Restrictions")+":</h4>\
  11. <ol>\
  12. <li>"+wn._("Apart from the existing Permission Rules, you can apply addition restriction based on Type.")+"</li>\
  13. <li>"+wn._("These restrictions will apply for all transactions linked to the restricted record.")
  14. +wn._("For example, if user X is restricted to company C, user X will not be able to see any transaction that has company C as a linked value.")+"</li>\
  15. <li>"+wn._("These will also be set as default values for those links.")+"</li>\
  16. <li>"+wn._("A user can be restricted to multiple records of the same type.")+"</li>\
  17. </ol>\
  18. </tr></td>\
  19. </table>");
  20. wrapper.user_properties = new wn.UserProperties(wrapper);
  21. }
  22. wn.pages['user-properties'].refresh = function(wrapper) {
  23. wrapper.user_properties.set_from_route();
  24. }
  25. wn.UserProperties = Class.extend({
  26. init: function(wrapper) {
  27. this.wrapper = wrapper;
  28. this.body = $(this.wrapper).find(".user-settings");
  29. this.filters = {};
  30. this.make();
  31. this.refresh();
  32. },
  33. make: function() {
  34. var me = this;
  35. return wn.call({
  36. module:"core",
  37. page:"user_properties",
  38. method: "get_users_and_links",
  39. callback: function(r) {
  40. me.options = r.message;
  41. me.filters.user = me.wrapper.appframe.add_field({
  42. fieldname: "user",
  43. label: wn._("User"),
  44. fieldtype: "Select",
  45. options: (["Select User..."].concat(r.message.users)).join("\n")
  46. });
  47. me.filters.property = me.wrapper.appframe.add_field({
  48. fieldname: "property",
  49. label: wn._("Property"),
  50. fieldtype: "Select",
  51. options: (["Select Property..."].concat(me.get_link_names())).join("\n")
  52. });
  53. me.filters.restriction = me.wrapper.appframe.add_field({
  54. fieldname: "restriction",
  55. label: wn._("Restriction"),
  56. fieldtype: "Link",
  57. options: "[Select]"
  58. });
  59. // bind change event
  60. $.each(me.filters, function(k, f) {
  61. f.$input.on("change", function() {
  62. me.refresh();
  63. });
  64. });
  65. // change options in restriction link
  66. me.filters.property.$input.on("change", function() {
  67. me.filters.restriction.df.options = $(this).val();
  68. });
  69. me.set_from_route();
  70. }
  71. });
  72. },
  73. get_link_names: function() {
  74. return $.map(this.options.link_fields, function(l) { return l[0]; });
  75. },
  76. set_from_route: function() {
  77. var me = this;
  78. if(wn.route_options && this.filters) {
  79. $.each(wn.route_options, function(key, value) {
  80. me.set_filter(key, value);
  81. });
  82. wn.route_options = null;
  83. }
  84. this.refresh();
  85. },
  86. set_filter: function(key, value) {
  87. this.filters[key].$input.val(value);
  88. },
  89. get_user: function() {
  90. var user = this.filters.user.$input.val();
  91. return user=="Select User..." ? null : user;
  92. },
  93. get_property: function() {
  94. var property = this.filters.property.$input.val();
  95. return property=="Select Property..." ? null : property;
  96. },
  97. get_restriction: function() {
  98. return this.filters.restriction.$input.val();
  99. },
  100. render: function(prop_list) {
  101. this.body.empty();
  102. this.prop_list = prop_list;
  103. if(!prop_list || !prop_list.length) {
  104. this.body.html("<div class='alert alert-info'>"+wn._("No User Properties found.")+"</div>");
  105. } else {
  106. this.show_property_table();
  107. }
  108. this.show_add_property();
  109. },
  110. refresh: function() {
  111. var me = this;
  112. if(!me.filters.user) {
  113. this.body.html("<div class='alert alert-info'>"+wn._("Loading")+"...</div>");
  114. return;
  115. }
  116. if(!me.get_user() && !me.get_property()) {
  117. this.body.html("<div class='alert alert-warning'>"+wn._("Select User or Property to start.")+"</div>");
  118. return;
  119. }
  120. // get permissions
  121. return wn.call({
  122. module: "core",
  123. page: "user_properties",
  124. method: "get_properties",
  125. args: {
  126. parent: me.get_user(),
  127. defkey: me.get_property(),
  128. defvalue: me.get_restriction()
  129. },
  130. callback: function(r) {
  131. me.render(r.message);
  132. }
  133. });
  134. },
  135. show_property_table: function() {
  136. var me = this;
  137. this.table = $("<table class='table table-bordered'>\
  138. <thead><tr></tr></thead>\
  139. <tbody></tbody>\
  140. </table>").appendTo(this.body);
  141. $.each([[wn._("User"), 150], [wn._("Type"), 150], [wn._("Restricted To"),150], ["", 50]],
  142. function(i, col) {
  143. $("<th>").html(col[0]).css("width", col[1]+"px")
  144. .appendTo(me.table.find("thead tr"));
  145. });
  146. $.each(this.prop_list, function(i, d) {
  147. var row = $("<tr>").appendTo(me.table.find("tbody"));
  148. $("<td>").html('<a href="#Form/Profile/'+d.parent+'">'
  149. +d.parent+'</a>').appendTo(row);
  150. $("<td>").html(d.defkey).appendTo(row);
  151. $("<td>").html(d.defvalue).appendTo(row);
  152. me.add_delete_button(row, d);
  153. });
  154. },
  155. add_delete_button: function(row, d) {
  156. var me = this;
  157. $("<button class='btn btn-small btn-default'><i class='icon-remove'></i></button>")
  158. .appendTo($("<td>").appendTo(row))
  159. .attr("data-name", d.name)
  160. .attr("data-user", d.parent)
  161. .attr("data-defkey", d.defkey)
  162. .attr("data-defvalue", d.defvalue)
  163. .click(function() {
  164. return wn.call({
  165. module: "core",
  166. page: "user_properties",
  167. method: "remove",
  168. args: {
  169. name: $(this).attr("data-name"),
  170. user: $(this).attr("data-user"),
  171. defkey: $(this).attr("data-defkey"),
  172. defvalue: $(this).attr("data-defvalue")
  173. },
  174. callback: function(r) {
  175. if(r.exc) {
  176. msgprint("Did not remove.");
  177. } else {
  178. me.refresh();
  179. }
  180. }
  181. })
  182. });
  183. },
  184. show_add_property: function() {
  185. var me = this;
  186. $("<button class='btn btn-info'>"+wn._("Add A Restriction")+"</button>")
  187. .appendTo($("<p>").appendTo(this.body))
  188. .click(function() {
  189. var d = new wn.ui.Dialog({
  190. title: "Add New Property",
  191. fields: [
  192. {fieldtype:"Select", label:wn._("User"),
  193. options:me.options.users, reqd:1, fieldname:"parent"},
  194. {fieldtype:"Select", label: wn._("Property"), fieldname:"defkey",
  195. options:me.get_link_names(), reqd:1},
  196. {fieldtype:"Link", label:wn._("Value"), fieldname:"defvalue",
  197. options:'[Select]', reqd:1},
  198. {fieldtype:"Button", label: wn._("Add"), fieldname:"add"},
  199. ]
  200. });
  201. if(me.get_user()) {
  202. d.set_value("parent", me.get_user());
  203. d.get_input("parent").prop("disabled", true);
  204. }
  205. if(me.get_property()) {
  206. d.set_value("defkey", me.get_property());
  207. d.get_input("defkey").prop("disabled", true);
  208. }
  209. if(me.get_restriction()) {
  210. d.set_value("defvalue", me.get_restriction());
  211. d.get_input("defvalue").prop("disabled", true);
  212. }
  213. d.fields_dict["defvalue"].get_query = function(txt) {
  214. var key = d.get_value("defkey");
  215. var doctype = $.map(me.options.link_fields, function(l) {
  216. if(l[0]==key) return l[1];
  217. })[0];
  218. return {
  219. doctype: doctype
  220. }
  221. };
  222. d.get_input("add").click(function() {
  223. var args = d.get_values();
  224. if(!args) {
  225. return;
  226. }
  227. wn.call({
  228. module: "core",
  229. page: "user_properties",
  230. method: "add",
  231. args: args,
  232. callback: function(r) {
  233. if(r.exc) {
  234. msgprint("Did not add.");
  235. } else {
  236. me.refresh();
  237. }
  238. }
  239. })
  240. d.hide();
  241. });
  242. d.show();
  243. });
  244. }
  245. })