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.
 
 
 
 
 
 

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