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.
 
 
 
 
 
 

216 rivejä
6.6 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. from __future__ import unicode_literals
  23. import webnotes, json
  24. class Profile:
  25. """
  26. A profile object is created at the beginning of every request with details of the use.
  27. The global profile object is `webnotes.user`
  28. """
  29. def __init__(self, name=''):
  30. self.name = name or webnotes.session.get('user')
  31. self.roles = []
  32. self.all_read = []
  33. self.can_create = []
  34. self.can_read = []
  35. self.can_write = []
  36. self.can_cancel = []
  37. self.can_search = []
  38. self.can_get_report = []
  39. self.allow_modules = []
  40. # for doctypes with create permission but are not supposed to be created using New
  41. self.in_create = []
  42. def _load_roles(self):
  43. self.roles = webnotes.get_roles()
  44. return self.roles
  45. def get_roles(self):
  46. """get list of roles"""
  47. if self.roles:
  48. return self.roles
  49. return self._load_roles()
  50. def build_doctype_map(self):
  51. """build map of special doctype properties"""
  52. self.doctype_map = {}
  53. for r in webnotes.conn.sql("""select name, in_create, issingle, istable,
  54. read_only, module from tabDocType""", as_dict=1):
  55. r['child_tables'] = []
  56. self.doctype_map[r['name']] = r
  57. for r in webnotes.conn.sql("""select parent, options from tabDocField
  58. where fieldtype="Table"
  59. and parent not like "old_parent:%%"
  60. and ifnull(docstatus,0)=0
  61. """):
  62. if r[0] in self.doctype_map:
  63. self.doctype_map[r[0]]['child_tables'].append(r[1])
  64. def build_perm_map(self):
  65. """build map of permissions at level 0"""
  66. self.perm_map = {}
  67. for r in webnotes.conn.sql("""select parent, `read`, `write`, `create`, `submit`, `cancel`
  68. from tabDocPerm where docstatus=0
  69. and ifnull(permlevel,0)=0
  70. and parent not like "old_parent:%%"
  71. and role in ('%s')""" % "','".join(self.get_roles()), as_dict=1):
  72. dt = r['parent']
  73. if not dt in self.perm_map:
  74. self.perm_map[dt] = {}
  75. for k in ('read', 'write', 'create', 'submit', 'cancel'):
  76. if not self.perm_map[dt].get(k):
  77. self.perm_map[dt][k] = r.get(k)
  78. def build_permissions(self):
  79. """build lists of what the user can read / write / create
  80. quirks:
  81. read_only => Not in Search
  82. in_create => Not in create
  83. """
  84. self.build_doctype_map()
  85. self.build_perm_map()
  86. for dt in self.doctype_map:
  87. dtp = self.doctype_map[dt]
  88. p = self.perm_map.get(dt, {})
  89. if not dtp.get('istable'):
  90. if p.get('create') and not dtp.get('issingle'):
  91. if dtp.get('in_create'):
  92. self.in_create.append(dt)
  93. else:
  94. self.can_create.append(dt)
  95. elif p.get('write'):
  96. self.can_write.append(dt)
  97. elif p.get('read'):
  98. if dtp.get('read_only'):
  99. self.all_read.append(dt)
  100. else:
  101. self.can_read.append(dt)
  102. if p.get('cancel'):
  103. self.can_cancel.append(dt)
  104. if (p.get('read') or p.get('write') or p.get('create')):
  105. self.can_get_report.append(dt)
  106. self.can_get_report += dtp['child_tables']
  107. if not dtp.get('istable'):
  108. if not dtp.get('issingle') and not dtp.get('read_only'):
  109. self.can_search.append(dt)
  110. if not dtp.get('module') in self.allow_modules:
  111. self.allow_modules.append(dtp.get('module'))
  112. self.can_write += self.can_create
  113. self.can_write += self.in_create
  114. self.can_read += self.can_write
  115. self.all_read += self.can_read
  116. def get_defaults(self):
  117. """
  118. Get the user's default values based on user and role profile
  119. """
  120. roles = self.get_roles() + [self.name]
  121. res = webnotes.conn.sql("""select defkey, defvalue
  122. from `tabDefaultValue` where parent in ("%s") order by idx""" % '", "'.join(roles))
  123. self.defaults = {'owner': [self.name], "user": [self.name]}
  124. for rec in res:
  125. if not self.defaults.has_key(rec[0]):
  126. self.defaults[rec[0]] = []
  127. self.defaults[rec[0]].append(rec[1] or '')
  128. return self.defaults
  129. # update recent documents
  130. def update_recent(self, dt, dn):
  131. rdl = webnotes.cache().get_value("recent:" + self.name)
  132. new_rd = [dt, dn]
  133. # clear if exists
  134. for i in range(len(rdl)):
  135. rd = rdl[i]
  136. if rd==new_rd:
  137. del rdl[i]
  138. break
  139. if len(rdl) > 19:
  140. rdl = rdl[:19]
  141. rdl = [new_rd] + rdl
  142. r = webnotes.cache().set_value("recent:" + self.name, rdl)
  143. def load_profile(self):
  144. d = webnotes.conn.sql("""select email, first_name, last_name,
  145. ifnull(email_signature,""), theme, ifnull(background_image,"")
  146. from tabProfile where name = %s""", self.name, as_dict=1)[0]
  147. if not self.can_read:
  148. self.build_permissions()
  149. d.name = self.name
  150. d.recent = json.dumps(webnotes.cache().get_value("recent:" + self.name) or [])
  151. if not d.theme:
  152. d.theme = "Default"
  153. d['roles'] = self.get_roles()
  154. d['defaults'] = self.get_defaults()
  155. d['can_create'] = self.can_create
  156. d['can_write'] = self.can_write
  157. d['can_read'] = list(set(self.can_read))
  158. d['can_cancel'] = list(set(self.can_cancel))
  159. d['can_get_report'] = list(set(self.can_get_report))
  160. d['allow_modules'] = self.allow_modules
  161. d['all_read'] = self.all_read
  162. d['can_search'] = list(set(self.can_search))
  163. d['in_create'] = self.in_create
  164. return d
  165. def get_user_fullname(user):
  166. fullname = webnotes.conn.sql("SELECT CONCAT_WS(' ', first_name, last_name) FROM `tabProfile` WHERE name=%s", user)
  167. return fullname and fullname[0][0] or ''
  168. def get_system_managers():
  169. """returns all system manager's profile details"""
  170. system_managers = webnotes.conn.sql("""select distinct name
  171. from tabProfile p
  172. where docstatus < 2 and enabled = 1
  173. and name not in ("Administrator", "Guest")
  174. and exists (select * from tabUserRole ur
  175. where ur.parent = p.name and ur.role="System Manager")""")
  176. return [p[0] for p in system_managers]