您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

224 行
6.0 KiB

  1. // Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. //
  3. // MIT License (MIT)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a
  6. // copy of this software and associated documentation files (the "Software"),
  7. // to deal in the Software without restriction, including without limitation
  8. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. // and/or sell copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. //
  22. wn.provide("wn.perm");
  23. var READ = 0, WRITE = 1, CREATE = 2;
  24. var SUBMIT = 3, CANCEL = 4, AMEND = 5;
  25. $.extend(wn.perm, {
  26. doctype_perm: {},
  27. has_perm: function(doctype, level, type) {
  28. if(!level) level = 0;
  29. var perms = wn.perm.doctype_perm;
  30. if(!perms[doctype])
  31. perms[doctype] = wn.perm.get_perm(doctype);
  32. if(!perms[doctype])
  33. return false;
  34. if(!perms[doctype][level])
  35. return false;
  36. return perms[doctype][level][type];
  37. },
  38. get_perm: function(doctype, dn) {
  39. var perm = [[0,0],];
  40. if(in_list(user_roles, 'Administrator'))
  41. perm[0][READ] = 1;
  42. if(locals["DocType"][doctype] && locals["DocType"][doctype].istable) {
  43. parent_df = wn.model.get("DocField", {fieldtype:"Table", options:doctype});
  44. if(parent_df.length) {
  45. doctype = parent_df[0].parent;
  46. }
  47. }
  48. $.each(wn.model.get("DocPerm", {parent:doctype}), function(i, p) {
  49. var pl = cint(p.permlevel?p.permlevel:0);
  50. // if user role
  51. if(in_list(user_roles, p.role)) {
  52. // if field match
  53. if(wn.perm.check_match(p, doctype, dn)) { // new style
  54. if(!perm[pl])
  55. perm[pl] = [];
  56. if(!perm[pl][READ]) {
  57. if(cint(p.read)) perm[pl][READ]=1; else perm[pl][READ]=0;
  58. }
  59. if(!perm[pl][WRITE]) {
  60. if(cint(p.write)) { perm[pl][WRITE]=1; perm[pl][READ]=1; }
  61. else perm[pl][WRITE]=0;
  62. }
  63. if(!perm[pl][CREATE]) {
  64. if(cint(p.create))perm[pl][CREATE]=1; else perm[pl][CREATE]=0;
  65. }
  66. if(!perm[pl][SUBMIT]) {
  67. if(cint(p.submit))perm[pl][SUBMIT]=1; else perm[pl][SUBMIT]=0;
  68. }
  69. if(!perm[pl][CANCEL]) {
  70. if(cint(p.cancel))perm[pl][CANCEL]=1; else perm[pl][CANCEL]=0;
  71. }
  72. if(!perm[pl][AMEND]) {
  73. if(cint(p.amend)) perm[pl][AMEND]=1; else perm[pl][AMEND]=0;
  74. }
  75. }
  76. }
  77. });
  78. return perm;
  79. },
  80. get_match_rule: function(doctype) {
  81. var match_rules = {};
  82. var match = true;
  83. $.each(wn.model.get("DocPerm", {parent:doctype}), function(i, p) {
  84. if(p.permlevel==0 && in_list(user_roles, p.role)) {
  85. if(p.match) {
  86. match_keys = wn.perm.get_match_keys(p.match);
  87. match_rules[match_keys[0]] = wn.defaults.get_user_defaults(match_keys[1]);
  88. } else {
  89. match = false;
  90. }
  91. }
  92. });
  93. return match ? match_rules : {};
  94. },
  95. get_match_keys: function(match) {
  96. if(match.indexOf(":")!=-1) {
  97. key_list = match.split(":");
  98. } else {
  99. key_list = [match, match];
  100. }
  101. return key_list;
  102. },
  103. check_match: function(p, doctype, name) {
  104. if(!name) return true;
  105. var out =false;
  106. if(p.match) {
  107. var key_list = wn.perm.get_match_keys(p.match);
  108. var document_key = key_list[0];
  109. var default_key = key_list[1];
  110. var match_values = wn.defaults.get_user_defaults(default_key);
  111. if(match_values) {
  112. for(var i=0 ; i<match_values.length;i++) {
  113. // user must have match field in defaults
  114. if(match_values[i]==locals[doctype][name][document_key]) {
  115. // must match document
  116. return true;
  117. }
  118. }
  119. return false;
  120. } else if(!locals[doctype][name][document_key]) { // blanks are true
  121. return true;
  122. } else {
  123. return false;
  124. }
  125. } else {
  126. return true;
  127. }
  128. },
  129. get_field_display_status: function(df, doc, perm, explain) {
  130. if(!doc) return "Write"
  131. if(!df.permlevel) df.permlevel = 0;
  132. perm = perm || wn.perm.get_perm(doc.doctype, doc.name);
  133. var p = perm[df.permlevel],
  134. ret = null;
  135. // permission level
  136. if(p && p[WRITE] && !df.disabled)
  137. ret='Write';
  138. else if(p && p[READ])
  139. ret='Read';
  140. else
  141. ret='None';
  142. if(explain) console.log("By Permission:" + ret)
  143. // hidden
  144. if(cint(df.hidden)) {
  145. ret = 'None';
  146. }
  147. if(explain) console.log("By Hidden:" + ret)
  148. // for submit
  149. if(ret=='Write' && cint(doc.docstatus) > 0) {
  150. ret = 'Read';
  151. }
  152. if(explain) console.log("By Submit:" + ret)
  153. // allow on submit
  154. var allow_on_submit = df.fieldtype!= "Table" ?
  155. cint(df.allow_on_submit) :
  156. 0;
  157. // if(allow_on_submit && doc.parent) {
  158. // parent_df = wn.model.get("DocField", {
  159. // "parent": doc.parenttype,
  160. // "fieldname": doc.parentfield
  161. // });
  162. // allow_on_submit = parent_df ?
  163. // parent_df[0].allow_on_submit :
  164. // 0;
  165. // }
  166. if(explain) console.log("Allow on Submit:" + allow_on_submit)
  167. if(ret=="Read" && allow_on_submit && cint(doc.docstatus)==1 &&
  168. perm[df.permlevel][WRITE]) {
  169. ret='Write';
  170. }
  171. if(explain) console.log("By Allow on Submt:" + ret)
  172. // workflow state
  173. if(ret=="Write" && cur_frm && cur_frm.state_fieldname) {
  174. if(cint(cur_frm.read_only)) {
  175. ret = 'Read';
  176. }
  177. // fields updated by workflow must be read-only
  178. if(in_list(cur_frm.states.update_fields, df.fieldname) ||
  179. df.fieldname==cur_frm.state_fieldname) {
  180. ret = 'Read';
  181. }
  182. }
  183. if(explain) console.log("By Workflow:" + ret)
  184. // make a field read_only if read_only
  185. // is checked (disregards write permission)
  186. if(ret=="Write" && cint(df.read_only)) {
  187. ret = "Read";
  188. }
  189. if(explain) console.log("By Read Only:" + ret)
  190. return ret;
  191. }
  192. });